1

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?

RoyalTS
  • 9,545
  • 12
  • 60
  • 101

1 Answers1

1

I got the same problem recently. I was in a hurry and came to this ugly solution :

res <- as.list(vector(length = ncol(df[,-1])))
for(i in 1:ncol(df[,-1])) {
    res[[i]] <-  model.matrix(~ -1 + ., data = df[,i+1, drop = FALSE])
}
res <- do.call(cbind, res)

Really interested to know if this is possible directly with model.matrix

Gilles San Martin
  • 4,224
  • 1
  • 18
  • 31