Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call lm with a model matrix instead of a formula

Tags:

r

lm

I want to fit a linear model in R using lm to obtain coefficient estimates and p-values + p-value for total model fit (ANOVA-like), so basically the output from summary.lm.

The problem is that I want to use my own model matrix, instead of specifying it using a formula when calling lm.

I've tried using the underlying lm.fit function which allows for this, but then I loose the convenience of the summary.lm function, and I don't want to recalculate all the test statistics again myself.

Is there a way to 'trick' lm and give it a model matrix rather than a formula?

Thanks!

like image 991
Misconstruction Avatar asked Feb 18 '16 14:02

Misconstruction


1 Answers

Here is an example using the built in BOD data frame:

# inputs
demand <- BOD$demand
mm <- model.matrix(~ Time, BOD) # model matrix

summary(lm(demand ~ mm + 0))

or

summary(lm(demand ~. + 0, as.data.frame(mm)))
like image 52
G. Grothendieck Avatar answered Sep 24 '22 13:09

G. Grothendieck