[R] data frame as X in linear model lm() ?
ggrothendieck at gmail.com
Sat Mar 25 16:24:34 CET 2006
Try using dot notation:
lm(y ~., x.d)
On 3/25/06, ivo welch <ivowel at gmail.com> wrote:
> Dear R wizards: This must have an obvious solution, but I am stumped.
> I can run a linear regression giving a matrix as the independent set
> of variables, but if I give a data frame (which I would like to give,
> because it should tell the linear model the names of the variables), R
> does not like it. An example is:
> N=20; y= rnorm(N);
> x.m <- (matrix( nrow=N, ncol=2 ));
> x.m[,1]=rnorm(N); x.m[,2]=rnorm(N);
> print(summary(lm(y ~ x.m))); # works
> x.d <- as.data.frame(x.m);
> names(x.d) <- c("one.rv", "two.rv"); # I would like the summary to
> know my variable names!
> print(summary(lm(y ~ x.d))); # fails
> [please indulge my semi-colons.] how should this be done? help, as
> always, appreciated.
> /ivo welch
> R-help at stat.math.ethz.ch mailing list
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
More information about the R-help