[R] model.matrix behaviour
Axel Urbiz
axel.urbiz at gmail.com
Fri Jan 8 00:34:44 CET 2016
Hello,
I apologize my prior email was sent in html.
It is not very clear to me from the model.matrix documentation, why simply changing the order of terms in the formula may give a different design matrix. Please note I’m purposely not including main effects in the model formulae.
set.seed(1)
x1 <- rnorm(100)
f1 <- factor(sample(letters[1:3], 100, replace = TRUE))
trt <- sample(c(-1,1), 100, replace = TRUE)
df <- data.frame(x1=x1, f1=f1, trt=trt)
head(model.matrix( ~ x1:trt + f1:trt, data = df))
(Intercept) x1:trt trt:f1b trt:f1c
1 1 -0.6264538 0 0
2 1 -0.1836433 0 0
3 1 0.8356286 -1 0
4 1 -1.5952808 0 0
5 1 -0.3295078 0 0
6 1 -0.8204684 1 0
head(model.matrix(~ f1:trt + x1:trt, data = df))
(Intercept) f1a:trt f1b:trt f1c:trt trt:x1
1 1 1 0 0 -0.6264538
2 1 -1 0 0 -0.1836433
3 1 0 -1 0 0.8356286
4 1 -1 0 0 -1.5952808
5 1 -1 0 0 -0.3295078
6 1 0 1 0 -0.8204684
Thanks,
Axel.
More information about the R-help
mailing list