问题
When R performs a regression using a categorical variable, it's effectively dummy coding. That is, one of levels is omitted as base or reference and the regression formula includes dummies for all the other levels. But which one is it, that R picks as reference and how I can influence this choice?
Example data with four levels (from UCLA's IDRE):
hsb2 <- read.csv("http://www.ats.ucla.edu/stat/data/hsb2.csv")
summary(lm(write ~ factor(race), data = hsb2))
# level 1 is the reference level
hsb2.ordered <- hsb2[rev(order(hsb2$race)),]
summary(lm(write ~ factor(race), data = hsb2.ordered))
# level 1 is still the reference level
回答1:
The order of factor levels in R does not depend on the order of the data. Hence, changing the order of the data does not affect the reference level of the factor.
You can obtain the order of the levels with the function levels
:
fac <- factor(hsb2$race)
levels(fac)
# [1] "1" "2" "3" "4"
The order of factor levels is based on the alphabetical order of the data.
You can chage the reference level with the relevel
function:
fac2 <- relevel(fac, ref = "2")
levels(fac2)
# [1] "2" "1" "3" "4"
Now, level "2"
is the reference level. This also affects the regression:
lm(write ~ fac2, data = hsb2)
#
# Call:
# lm(formula = write ~ fac2, data = hsb2)
#
# Coefficients:
# (Intercept) fac21 fac23 fac24
# 58.000 -11.542 -9.800 -3.945
The function factor
allows for creating any ordering of factor levels:
fac3 <- factor(fac, levels = c("3", "4", "2", "1"))
levels(fac3)
# [1] "3" "4" "2" "1"
来源:https://stackoverflow.com/questions/33871122/categorical-variables-in-r-which-one-does-r-pick-as-reference