Reputation: 1170
In R there are factors and there are ordered factors. 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.
Does anyone know how to tag a reference level in R, or if not, how I can extend the factor class to do so?
Upvotes: 3
Views: 1767
Reputation: 9687
You can customize the reference group using contrasts
. For example, to set the contrast so that group2
is the reference in sleep
, I would do this:
Without the contrast, group1 is the reference group.
> 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
EDIT:
Based on the comment below, the reference level is stored implicitly, but it is easy to retrieve if you know what to look for. The reference group will be in the rows on the contrasts and but not the columns. Try this:
> reference_group <- function(x) do.call(setdiff, dimnames(contrasts(x)))
> reference_group(sleep$group)
[1] "2"
Upvotes: 3