[英]Interaction between two categorical variables in R
We are dealing with a regression model that contains two categorical variables age groups and gender. 我们正在处理一个回归模型,该模型包含两个分类变量年龄组和性别。
We want to include an interaction term between the two categorical variables but the resulting model only displays the effects of the interactions between females with all age groups. 我们希望在两个分类变量之间包含一个交互项,但结果模型只显示女性与所有年龄组之间的相互作用的影响。
How can we adjust the code so that it keeps "males" aged "26-30" as a reference level and shows the effect of all other groups in its output? 我们如何调整代码以使“男性”老化为“26-30”作为参考水平并显示其输出中所有其他群体的效果?
Adjustment code 调整代码
count_med_op3 <- glm(Count_OP ~ Gender * age_group + otherfactors,
data = med, family = 'poisson')
Result wanted for: 结果需要:
GenderMale:age_group"0-1"
GenderMale:age_group"2-6"
GenderMale:age_group"7-18"
GenderMale:age_group"19-25"
GenderMale:age_group"31-36"
Genderfemale:age_group"0-1"
Genderfemale:age_group"2-6"
Genderfemale:age_group"7-18"
Genderfemale:age_group"19-25"
Genderfemale:age_group"26-30"
other factors
# simulate some data
df_foo = data_frame(
age = as.factor(sample(seq(10, 90, 10), 100, replace = TRUE)),
y = rnorm(100),
gender = as.factor(sample(c("Male", "Female"), 100, replace = TRUE))
)
# female as omitted level
df_foo %>%
lm(y ~ age*gender, data = .) %>%
summary()
# male as omitted level
df_foo %>%
lm(y ~ age*relevel(gender, ref = "Male"), data = .) %>%
summary()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.