如何在使用R进行回归分析时为我的变量设置对比? [英] How to set contrasts for my variable in regression analysis with R?
问题描述
在编码过程中,我需要更改分配给一个因子的虚拟值.但是,以下代码不起作用.有什么建议吗?
During coding, I need to change the dummy value assigned to a factor. However, the following code does not work. Any suggestion?
test_mx= data.frame(a= c(T,T,T,F,F,F), b= c(1,1,1,0,0,0))
test_mx
a b
1 TRUE 1
2 TRUE 1
3 TRUE 1
4 FALSE 0
5 FALSE 0
6 FALSE 0
model= glm(b ~ a, data= test_mx, family= "binomial")
summary(model)
model= glm(a ~ b, data= test_mx, family= "binomial")
summary(model)
在这里,我将得到b的系数为47.现在,如果我交换虚拟值,则它应该为-47.然而,这种情况并非如此.
Here I will get the coef for b is 47. Now if I swap the dummy value, it should be -47 then. However, this is not the case.
test_mx2= test_mx
contrasts(test_mx2$a)
TRUE
FALSE 0
TRUE 1
contrasts(test_mx2$a) = c(1,0)
contrasts(test_mx2$a)
[,1]
FALSE 1
TRUE 0
model= glm(a ~ b, data= test_mx2, family= "binomial")
summary(model)
b的
系数仍然相同.到底是怎么回事?谢谢.
coef for b is still the same. What is going on? Thanks.
推荐答案
关于您的问题,有几处令人困惑的事情.您同时使用了a ~ b
和b ~ a
,那么您到底在看什么?
There are several confusing things regarding your question. You have used both a ~ b
and b ~ a
, so what are you looking at exactly?
- 对比度仅适用于协变量/自变量,因为它与模型矩阵的构建有关;因此,对于
a ~ b
,应将对比度应用于b
,而对于b ~ a
,应将对比度应用于a
; - 对比度仅适用于因子/逻辑变量,不适用于数值变量.因此,除非您将
b
作为因素,否则您将无法与之形成对比.
- Contrasts only applies to covariates / independent variables, as it is related to construction of model matrix; So for
a ~ b
, contrasts should be applied tob
, while forb ~ a
, contrasts should be applied toa
; - Contrasts only works for factor / logical variables, rather than numerical variables. So unless you have
b
as a factor, you can't play contrasts with it.
在不更改数据类型的情况下,很明显只有模型b ~ a
才有资格进行进一步讨论.在下面,我将展示如何为a
设置对比度.
Without changing data type, it is clear that only a model b ~ a
is legitimate for further discussion. In the following, I will show how to set contrasts for a
.
方法1:使用glm
和lm
Method 1: using contrasts
argument of glm
and lm
我们可以通过glm
的contrasts
参数(与lm
相同)来控制对比处理:
We can control contrasts treatment by the contrasts
argument of glm
(the same for lm
):
## dropping the first factor level (default)
coef(glm(b ~ a, data = test_mx, family = binomial(),
contrasts = list(a = contr.treatment(n = 2, base = 1))))
#(Intercept) a2
# -24.56607 49.13214
## dropping the second factor level
coef(glm(b ~ a, data = test_mx, family = binomial(),
contrasts = list(a = contr.treatment(n = 2, base = 2))))
#(Intercept) a1
# 24.56607 -49.13214
在这里,contr.treatment
正在生成对比度矩阵:
Here, contr.treatment
is generating a contrasts matrix:
contr.treatment(n = 2, base = 1)
# 2
#1 0
#2 1
contr.treatment(n = 2, base = 2)
# 1
#1 1
#2 0
并将它们传递给glm
以有效地更改model.matrix.default
的行为.让我们比较两种情况的模型矩阵:
and they are passed to glm
to effectively change the behaviour of model.matrix.default
. Let's compare the model matrix for two cases:
model.matrix.default( ~ a, test_mx, contrasts.arg =
list(a = contr.treatment(n = 2, base = 1)))
# (Intercept) a2
#1 1 1
#2 1 1
#3 1 1
#4 1 0
#5 1 0
#6 1 0
model.matrix.default( ~ a, test_mx, contrasts.arg =
list(a = contr.treatment(n = 2, base = 2)))
# (Intercept) a1
#1 1 0
#2 1 0
#3 1 0
#4 1 1
#5 1 1
#6 1 1
a
的第二列只是0
和1
之间的转换,这是您期望的虚拟变量.
The second column for a
is just a flip between 0
and 1
, which is what you have expected for a dummy variable.
方法2:直接将对比度"属性设置为数据框
我们可以使用C
或contrasts
设置对比度"属性(C
仅用于设置,但contrasts
也可以用于查看):
We can use C
or contrasts
to set "contrasts" attributes (C
is only for setting, but contrasts
can be used for viewing as well):
test_mx2 <- test_mx
contrasts(test_mx2$a) <- contr.treatment(n = 2, base = 1)
str(test_mx2)
#'data.frame': 6 obs. of 2 variables:
# $ a: Factor w/ 2 levels "FALSE","TRUE": 2 2 2 1 1 1
# ..- attr(*, "contrasts")= num [1:2, 1] 0 1
# .. ..- attr(*, "dimnames")=List of 2
# .. .. ..$ : chr "FALSE" "TRUE"
# .. .. ..$ : chr "2"
# $ b: num 1 1 1 0 0 0
test_mx3 <- test_mx
contrasts(test_mx3$a) <- contr.treatment(n = 2, base = 2)
str(test_mx3)
#'data.frame': 6 obs. of 2 variables:
# $ a: Factor w/ 2 levels "FALSE","TRUE": 2 2 2 1 1 1
# ..- attr(*, "contrasts")= num [1:2, 1] 1 0
# .. ..- attr(*, "dimnames")=List of 2
# .. .. ..$ : chr "FALSE" "TRUE"
# .. .. ..$ : chr "1"
# $ b: num 1 1 1 0 0 0
现在我们可以在不使用contrasts
参数的情况下适应glm
:
Now we can fit glm
without using contrasts
argument:
coef(glm(b ~ a, data = test_mx2, family = "binomial"))
#(Intercept) a2
# -24.56607 49.13214
coef(glm(b ~ a, data = test_mx3, family = "binomial"))
#(Intercept) a1
# 24.56607 -49.13214
方法3:为全局更改设置options("contrasts")
Method 3: setting options("contrasts")
for a global change
哈哈哈,@ BenBolker还提到了另一个选项,即设置R的全局选项.对于您的仅涉及两个级别的因子的特定示例,我们可以使用?contr.SAS
.
Hahaha, @BenBolker yet mentions another option, which is by setting the global options of R. For your specific example with a factor involving only two levels, we can makes use of ?contr.SAS
.
## using R default contrasts options
#$contrasts
# unordered ordered
#"contr.treatment" "contr.poly"
coef(glm(b ~ a, data = test_mx, family = "binomial"))
#(Intercept) aTRUE
# -24.56607 49.13214
options(contrasts = c("contr.SAS", "contr.poly"))
coef(glm(b ~ a, data = test_mx, family = "binomial"))
#(Intercept) aFALSE
# 24.56607 -49.13214
但是我相信Ben只是为了说明这一点而已.他不会在现实中采取这种方式,因为更改全局选项不利于获得可复制的R代码.
But I believe Ben is just mention this to complete the picture; He will not take this way in reality, as changing global options is not good for getting reproducible R code.
另一个问题是,contr.SAS
只会将最后一个因子水平作为参考.在您只有2个级别的情况下,这可以有效地进行翻转".
Another issue is that contr.SAS
will just treat the last factor level as reference. In your particular case with only 2 levels, this effectively does the "flipping".
方法4:手动重新编码因子水平
我无意提及这一点,因为它是如此琐碎,但是由于我添加了方法3",因此最好也添加这一点.
I had no intention to mention this as it is so trivial, but since I have added "Method 3", I'd better add this one, too.
test_mx4 <- test_mx
test_mx4$a <- factor(test_mx4$a, levels = c("TRUE", "FALSE"))
coef(glm(b ~ a, data = test_mx4, family = "binomial"))
#(Intercept) aTRUE
# -24.56607 49.13214
test_mx5 <- test_mx
test_mx5$a <- factor(test_mx5$a, levels = c("FALSE", "TRUE"))
coef(glm(b ~ a, data = test_mx5, family = "binomial"))
#(Intercept) aFALSE
# 24.56607 -49.13214
这篇关于如何在使用R进行回归分析时为我的变量设置对比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!