首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在R model.matrix中特别指定对比

在R model.matrix中特别指定对比
EN

Stack Overflow用户
提问于 2018-07-03 09:33:28
回答 2查看 757关注 0票数 0

如果我有一个2级的变量(条件),并且想要创建一个model.matrix,R会自动指定conditionB作为设计矩阵中的术语。

代码语言:javascript
复制
condition <- as.factor( c("A","A","A","B","B","B"))
df <- data.frame(condition)
design <- model.matrix( ~ condition)

> df
  condition
1         A
2         A
3         A
4         B
5         B
6         B


> design
  (Intercept) conditionB
1           1          0
2           1          0
3           1          0
4           1          1
5           1          1
6           1          1
attr(,"assign")
[1] 0 1
attr(,"contrasts")
attr(,"contrasts")$condition
[1] "contr.treatment"

问题:我想知道相对于conditionA的结果。如何在model.matrix()中指定这一点?

(一种解决办法是逆由此产生的FCs)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-03 09:54:18

您可以使用C函数来确定要考虑的基:

以A为基地:

代码语言:javascript
复制
 model.matrix(~C(condition,base=1))
  (Intercept) C(condition, base = 1)2
1           1                       0
2           1                       0
3           1                       0
4           1                       1
5           1                       1
6           1                       1
attr(,"assign")
[1] 0 1
attr(,"contrasts")
attr(,"contrasts")$`C(condition, base = 1)`
  2
A 0
B 1

以B为基数:

代码语言:javascript
复制
model.matrix(~C(condition,base=2))
  (Intercept) C(condition, base = 2)1
1           1                       1
2           1                       1
3           1                       1
4           1                       0
5           1                       0
6           1                       0
attr(,"assign")
[1] 0 1
attr(,"contrasts")
attr(,"contrasts")$`C(condition, base = 2)`
  1
A 1
B 0
票数 0
EN

Stack Overflow用户

发布于 2018-12-17 14:07:36

这是你想要的结果吗?

代码语言:javascript
复制
df <- data.frame(condition)
design <- model.matrix( ~ condition-1)
design
  conditionA conditionB
1          1          0
2          1          0
3          1          0
4          0          1
5          0          1
6          0          1
attr(,"assign")
[1] 1 1
attr(,"contrasts")
attr(,"contrasts")$`condition`
[1] "contr.treatment"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51151169

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档