简体   繁体   English

如何在R中的顺序逻辑回归模型中重新设置因子?

[英]How to re-level factor in ordinal logistic regression model in R?

I am currently working on an ordinal logistic regression model with R. The output coefficients are using the same reference level. 我目前正在使用R进行序数逻辑回归模型。输出系数使用相同的参考水平。 I am wondering how can I change the reference level? 我想知道如何更改参考水平? Be more specific, see the example below. 具体来说,请参见下面的示例。 I do not want to use real data, so I simulated one. 我不想使用真实数据,因此我模拟了一个。 Both a and T are from 1 to 5 a和T均为1到5

polr(formula = T ~ a, data = d, Hess = TRUE)

Coefficients:
      Value Std. Error  t value
a2  0.18823     0.5734  0.32825
a3  0.14747     0.5287  0.27895
a4 -0.50157     0.5766 -0.86985
a5  0.02843     0.5448  0.05219

The coefficient of "a" use reference level 1, a2, a3, a4, and a5 basically compare level 2,3,4,5 with reference level 1. My question is how can I relevel it so that the output will give a3|2, a4|3, a5|4(ie the beta3-beta2,beta4-beta3) automatically? 系数“ a”使用参考级别1,a2,a3,a4和a5基本上将级别2、3、4、5与参考级别1进行比较。我的问题是如何重新调整级别,以便输出将给出a3 |。 2,a4 | 3,a5 | 4(即beta3-beta2,beta4-beta3)自动生成? I have search around and didn't find similar question. 我到处搜索,没有找到类似的问题。 Thanks a lot. 非常感谢。

I think you want the contr.sdif function from the MASS package: from ?contr.sdif , 我认为您需要MASS软件包中的contr.sdif函数:来自?contr.sdif

The contrast coefficients are chosen so that the coded coefficients in a one-way layout are the differences between the means of the second and first levels, the third and second levels, and so on. 选择对比度系数,使得单向布局中的编码系数是第二级和第一级,第三级和第二级等的均值之间的差。 This makes most sense for ordered factors, but does not assume that the levels are equally spaced. 对于有序因素,这最有意义,但不假定这些级别的距离相等。

So: 所以:

library(MASS)
contrasts(d$a) <- contr.sdif(5) # set contrasts: from @Marius in comments
polr(formula = T ~ a, data = d, Hess = TRUE)

According to the documentation for ?polr , it should be possible to add the argument contrasts=list(a=contr.sdif) instead of setting the contrasts on the variable itself; 根据?polr的文档,应该可以添加自变量“ contrasts=list(a=contr.sdif)而不是在变量本身上设置对比度。 however, this seems to give an error (an analogous setup works fine for lm ). 但是,这似乎会产生错误(类似的设置对lm可以正常工作)。 This looks like a bug in polr to me, but that's somewhat hard to believe since this a venerable and well-tested part of R ... Where possible (maybe not in this case) I prefer the contrasts argument in the formula because it's a bit more explicit/visible when reading the code, but tastes differ. 对我来说,这似乎是一个polr错误,但是很难相信,因为这是R中经过了良好考验的一部分……在可能的情况下(可能不是这种情况),我更喜欢公式中的contrasts参数,因为它是在阅读代码时更加显式/可见,但是口味有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM