[R] lm/model.matrix confusion (? bug)

Berwin A Turlach berwin at maths.uwa.edu.au
Wed Dec 12 11:36:32 CET 2007


G'day Mark,

On Wed, 12 Dec 2007 02:05:54 -0800 (PST)
Mark Difford <mark_difford at yahoo.co.uk> wrote:

> In order to get the same coefficients as we get from the following
[...] 
> we need to do the following (if we use model.matrix to specify the
> model)

By why would you want to do this?

> ##
> summary ( lm(Gas ~ model.matrix(~ Insul/Temp - 1) - 1, data =
> whiteside) )
> 
> That is, we need to take out "two intercepts."  Is this "correct"?

Yes.
 
> Shouldn't lm check to see if an intercept has been requested as part
> of the model formula?

No, it does not.  In the Details section of lm's help page you will
find the following:

     A formula has an implied intercept term.  To remove this use
     either 'y ~ x - 1' or 'y ~ 0 + x'.  See 'formula' for more details
     of allowed formulae.

Thus, except if you explicitly ask for a constant term not be included,
lm will add a constant term (a column of ones) additionally to what
ever you have specified on the right hand side of the formula.

> If I do
> ##
> summary(lm(as.formula(Gas ~ model.matrix (~ Insul/Temp-1,
> data=whiteside)), data=whiteside))
> 
> we get a strange model.  

Well, you get a model in which not all parameters are identifiable, and
a particular parameter that is not identifiable is estimated by NA.  I
am not sure what is strange about this.

> But the formula part of this model qualifies
> as a valid formula
> ##
> as.formula(Gas ~ model.matrix (~ Insul/Temp-1, data=whiteside))

Debatable, the above command only shows that it can be coerced into a
valid formula. :)

> just as if I were to write: lm(Gas ~ Insul/Temp - 1, data=whiteside)
> 
> But we know that the _correct_ formula is the following
 
> ##
> as.formula(Gas ~ model.matrix (~ Insul/Temp-1, data=whiteside) -1)

Why is this formula any more correct than the other one?  Both specify
exactly the same model.  It is just that one does it in an
overparameterised way.

> (Sorry, this is getting really long) --- So, my question/confusion
> comes down to wanting to know why lm() doesn't check to see if an
> intercept has been specified when the model has been specified using
> model.matrix.

Because lm() is documented not to check this.  If you do not want to
have an intercept in the model you have to specifically ask it for.

Also, comparing the output of 
	summary( lm(Gas ~ Insul/Temp - 1, data = whiteside) )
and
	summary( lm(Gas ~ Insul/Temp, data = whiteside ) )

you can see that lm() does not check whether there is an implicit
intercept in the model.  Compare the (Adjusted) R-squared values
returned; one case is using the formula for models with no intercept
the other one the formula for models with intercept.  Similar story
with the reported F-statistics.  

Cheers,

	Berwin

=========================== Full address =============================
Berwin A Turlach                            Tel.: +65 6515 4416 (secr)
Dept of Statistics and Applied Probability        +65 6515 6650 (self)
Faculty of Science                          FAX : +65 6872 3919       
National University of Singapore     
6 Science Drive 2, Blk S16, Level 7          e-mail: statba at nus.edu.sg
Singapore 117546                    http://www.stat.nus.edu.sg/~statba



More information about the R-help mailing list