[R] Why can we combine design matrix and data-frame in R?

S Ellison S.Ellison at LGCGroup.com
Sat May 12 13:47:21 CEST 2012


>I am trying to understand why this line works:
>
> lm1x = lm(y~X-1, tmp)

Well, I would not normally define a data frame element as a matrix myself  (though I might well define a list element as one). But specifying a matrix as the terms part of an lm is documented in lm's details:
"If response is a matrix a linear model is fitted separately by least-squares to each column of the matrix"

So _something_ will happen.

Whether the something is useful depends on the intent.

> Here it seems that I was combining the design matrix and the data frame...
Did you inspect tmp after adding the design matrix? Was it an odd looking data frame or a list?
What seems to have been done is that the design matrix has been added to a list. I wouldn't normally do that if tmp is a data frame, and r would not do so unless the lengths all matched.  But a list should be ok. And lm takes a list or environment as its data argument, so a list of things will work even if they are different types. In other words tmp is just a ragbag of things, each of which lm understands.
*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}



More information about the R-help mailing list