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
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. > > sincerely, > > /ivo welch > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
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, ripley at stats.ox.ac.uk 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