[R] model.matrix for a factor effect with no intercept

David Firth d.firth at warwick.ac.uk
Wed Feb 23 15:55:13 CET 2005


I was surprised by this (in R 2.0.1):

 > a <- ordered(-1:1)
 > a
[1] -1 0  1
Levels: -1 < 0 < 1

 > model.matrix(~ a)
   (Intercept)           a.L        a.Q
1           1 -7.071068e-01  0.4082483
2           1 -9.073800e-17 -0.8164966
3           1  7.071068e-01  0.4082483
attr(,"assign")
[1] 0 1 1
attr(,"contrasts")
attr(,"contrasts")$a
[1] "contr.poly"

 > model.matrix(~ -1 + a)
   a-1 a0 a1
1   1  0  0
2   0  1  0
3   0  0  1
attr(,"assign")
[1] 1 1 1
attr(,"contrasts")
attr(,"contrasts")$a
[1] "contr.poly"

Without the intercept, treatment contrasts seem to have been used (this 
despite the "contr.poly" in the "contrasts" attribute).

It's not restricted to ordered factors.  For example, if Helmert 
contrasts are used for nominal factors, the same sort of thing happens.

I suppose it is a deliberate feature (perhaps to protect the user from 
accidentally fitting models that make no sense?  or maybe some better 
reason?) -- is it explained somewhere?

David




More information about the R-help mailing list