[R] Error: invalid type(list) for variable when using lm()

Phil Spector spector at stat.berkeley.edu
Tue Nov 16 20:04:48 CET 2010


Tianchan -
    Your X is not a matrix -- it's a dataframe.  Probably the 
simplest solution is to use

     lm(y~as.matrix(X))

but you should also learn the difference between a data frame
and a matrix.

 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu


On Tue, 16 Nov 2010, Tianchan Niu wrote:

> Dear All,I would like to do multiple regression in R. I used: lm(y~X),
> where y is a n by 1 vector, and X is a n by m matrix. I kept getting the error
> message:Error in model.frame.default(formula = y ~ X,  :  invalid type (list) for variable 'X'. However, when I used: lm(y~X[,1]+X[,2]+X[,3]+…+X[,m]), it
> works well, but this is not the form I prefer, it makes my codes complicated.Please help.Thank you very much,Jane


More information about the R-help mailing list