Difference in behaviour of model.matrix
Douglas Bates
bates@stat.wisc.edu
Thu, 25 Jun 1998 14:55:46 -0500 (CWT)
This is an obscure R/S incompatibility but it is tripping up some code
for us in the lme library. If you specify the contrasts argument in a
call to model.matrix, that seems to take precedence over the
interpretation of the formula. In S if the formula contains a "- 1",
that will cause the contrasts to be suppressed.
S> foo <- data.frame(bar = factor(rep(1:3, rep(2,3))))
S> foo
bar
1 1
2 1
3 2
4 2
5 3
6 3
S> model.matrix(~bar - 1, model.frame(~ bar - 1, data = foo))
bar1 bar2 bar3
1 1 0 0
2 1 0 0
3 0 1 0
4 0 1 0
5 0 0 1
6 0 0 1
S> model.matrix(~bar - 1, model.frame(~ bar - 1, data = foo),
+ contr = list(bar = contr.treatment(3)))
bar1 bar2 bar3
1 1 0 0
2 1 0 0
3 0 1 0
4 0 1 0
5 0 0 1
6 0 0 1
whereas
R> foo <- data.frame(bar = factor(rep(1:3, rep(2,3))))
R> model.matrix(~bar - 1, model.frame(~ bar - 1, data = foo))
bar1 bar2 bar3
[1,] 1 0 0
[2,] 1 0 0
[3,] 0 1 0
[4,] 0 1 0
[5,] 0 0 1
[6,] 0 0 1
attr(,"assign")
[1] 1 1 1
R> model.matrix(~bar - 1, model.frame(~ bar - 1, data = foo),
+ contr = list(bar=contr.treatment(3)))
bar2 bar3
[1,] 0 0
[2,] 0 0
[3,] 1 0
[4,] 1 0
[5,] 0 1
[6,] 0 1
attr(,"assign")
[1] 1 1
I would be willing to admit that our code should perhaps be redesigned
to avoid this unfortunate construction.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._