data frame as X in linear model lm() ?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

data frame as X in linear model lm() ?

ivo welch-2
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.

sincerely,

/ivo welch

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: data frame as X in linear model lm() ?

Gabor Grothendieck
Try using dot notation:

lm(y ~., x.d)


On 3/25/06, ivo welch <[hidden email]> 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.
>
> sincerely,
>
> /ivo welch
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: data frame as X in linear model lm() ?

Brian Ripley
In reply to this post by ivo welch-2
I guess you are looking for

  print(summary(lm(y ~ ., data=x.d)))

since '.' refers to all the columns of 'data' except perhaps the response.

On Sat, 25 Mar 2006, ivo welch 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.
>
> sincerely,
>
> /ivo welch

--
Brian D. Ripley,                  [hidden email]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html