【发布时间】:2017-12-28 00:39:13
【问题描述】:
我知道我可以像这样构造一个模型矩阵:
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)
这给了
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"
请注意,其中一个因子水平被删除,因为如果包含它,设计矩阵将是秩不足的。但是,在我想估计的模型中,这实际上很好,我想保留第四列。有什么办法可以防止model.matrix 掉线吗?
【问题讨论】:
-
确实如此。刚刚投票结束。
标签: r model.matrix