[英]Factor with reference level in R?
In R there are factors and there are ordered factors.在 R 中有因子,也有有序因子。 Is there anything "in between", so to speak?
可以这么说,有什么“介于两者之间”吗? factors are completely unordered, while ordered factors are completely ordered;
因子是完全无序的,而有序因子是完全有序的; I want a factor type that has a reference level.
我想要一个具有参考水平的因子类型。 This would be useful in tagging a control group, for instance, that I always want to consider as a "baseline".
例如,这在标记控制组时很有用,我一直想将其视为“基线”。
I have seen relevel()
in R, but this is not what I want, since there is no way (apparently) to tell the difference between a factor whose reference level is the first level, and a regular factor.我在 R 中看到过
relevel()
,但这不是我想要的,因为没有办法(显然)区分参考水平是第一水平的因素和常规因素之间的区别。
Does anyone know how to tag a reference level in R, or if not, how I can extend the factor class to do so?有谁知道如何在 R 中标记参考水平,或者如果没有,我如何扩展因子类来做到这一点?
You can customize the reference group using contrasts
.您可以使用
contrasts
自定义参考组。 For example, to set the contrast so that group2
is the reference in sleep
, I would do this:例如,要设置对比度以便
group2
是sleep
的参考,我会这样做:
Without the contrast, group1 is the reference group.在没有对比的情况下,group1 是参考组。
> lm(extra~group, sleep)
Call:
lm(formula = extra ~ group, data = sleep)
Coefficients:
(Intercept) group2
0.75 1.58
With the contrast set:使用对比度设置:
> contrasts(sleep$group) <- contr.treatment(nlevels(sleep$group), base=2)
> lm(extra~group, sleep)
Call:
lm(formula = extra ~ group, data = sleep)
Coefficients:
(Intercept) group1
2.33 -1.58
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.