I know I can construct a model matrix like so:
df <- data.frame(outcome = rnorm(4),
group1 = rep(c('A', 'B'), each = 2),
group2 = rep(c('A', 'B'), times = 2))
model.matrix(outcome ~ group1 + group2 - 1, data=df)
That gives
group1A group1B group2B
1 1 0 0
2 1 0 1
3 0 1 0
4 0 1 1
attr(,"assign")
[1] 1 1 2
attr(,"contrasts")
attr(,"contrasts")$group1
[1] "contr.treatment"
attr(,"contrasts")$group2
[1] "contr.treatment"
Note that one of the factor levels is dropped because were it included the design matrix would be rank deficient. However, in a model I want to estimate this is actually fine and I would like to retain the fourth column. Is there any way to keep model.matrix
from dropping it?