[R] lm on matrix data
R. Michael Weylandt
michael.weylandt at gmail.com
Thu Oct 11 00:33:26 CEST 2012
On Wed, Oct 10, 2012 at 3:35 PM, Baoqiang Cao <bqcaomail at gmail.com> wrote:
> Hi,
>
> I have a question about using lm on matrix, have to admit it is very
> trivial but I just couldn't find the answer after searched the mailing
> list and other online tutorial. It would be great if you could help.
>
> I have a matrix "trainx" of 492(rows) by 220(columns) that is my x,
> and trainy is 492 by 1. Also, I have the newdata testx which is 240
> (rows) by 220 (columns). Here is what I got:
>
> py <- predict(lm(trainy ~ trainx ), data.frame(testx))
> Warning message:
> 'newdata' had 240 rows but variable(s) found have 492 rows
>
> The fitting formula I intended is: trainy ~ trainx[,1] + trainx[,2] +
> .. +trainx[,220].
>
I think you want a formula like
trainy ~ .
meaning "trainy" explained by everything else. (Admittedly, I think
any model with 220 regressors is going to be absolutely terrible, but
that's a different email)
What I think is happening here is that lm() looks for "trainx" as a
column name in the data set you provide, can't find it, and then finds
the "trainx" dataset as a whole, which doesn't fit the dimensionality
you need. Take a look at ?formula for more on how to use formula
notation properly.
Cheers,
Michael
More information about the R-help
mailing list