[R] Change the order of variables in a linear model

Frank Paetzold Frank.Paetzold at q-das.de
Wed May 16 12:22:32 CEST 2012


Hello,

the following lines

m <- matrix(c(1,1,9,1,2,6,1,3,7,2,1,4,2,2,5,2,3,1,3,1,2,3,2,-1,3,3,-2), 9,
3, byrow = TRUE, dimnames=list(NULL, cbind('A','B','Y')))
md <- as.data.frame(m)
md$A <- as.factor(md$A)
md$B <- as.factor(md$B)

mm <- model.matrix(Y~A+B+A:B, data=md)

produce 

> mm
  (Intercept) A2 A3 B2 B3 A2:B2 A3:B2 A2:B3 A3:B3
1           1  0  0  0  0     0     0     0     0
2           1  0  0  1  0     0     0     0     0
3           1  0  0  0  1     0     0     0     0
4           1  1  0  0  0     0     0     0     0
5           1  1  0  1  0     1     0     0     0
6           1  1  0  0  1     0     0     1     0
7           1  0  1  0  0     0     0     0     0
8           1  0  1  1  0     0     1     0     0
9           1  0  1  0  1     0     0     0     1
attr(,"assign")
[1] 0 1 1 2 2 3 3 3 3
attr(,"contrasts")
attr(,"contrasts")$A
[1] "contr.treatment"

attr(,"contrasts")$B
[1] "contr.treatment"


However, instead of the order 
  (Intercept) A2 A3 B2 B3 A2:B2 A3:B2 A2:B3 A3:B3
                                                          |         |  
                                                      changed order 
i'd like to have                                 |         |
  (Intercept) A2 A3 B2 B3 A2:B2 A2:B3 A3:B2 A3:B3

that is, the order of the A:B interaction variables is changed.
Is there a way to freely position variables in a model?

Thank you,

Frank


--
View this message in context: http://r.789695.n4.nabble.com/Change-the-order-of-variables-in-a-linear-model-tp4630230.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list