简体   繁体   English

R中两个分类变量之间的相互作用

[英]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

Use relevel : 使用relevel

# 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.

相关问题 比较 R 中两个连续变量和一个分类变量之间的三向交互作用的治疗效果 - Compare treatment effects in three way interaction between two continuous variables and one categorical variable in R 可视化 R 中两个连续变量和一个分类变量之间的三向交互 - Visualising a three way interaction between two continuous variables and one categorical variable in R R lm 如何选择与分类变量和连续变量之间的交互作用的对比? - How does R lm choose contrasts with interaction between a categorical and continuous variables? R,如何仅使用加法公式手动添加分类变量之间的交互作用? - In R, how to add manually interaction between categorical variables using only the additive formula? 两个类别变量的R条形图 - R barplot of two categorical variables 绘制两个分类变量 - plot r two categorical variables 具有类别和平方连续变量的R lm交互项 - R lm interaction terms with categorical and squared continuous variables R:“ isat”回归(“ gets”包)中连续和分类变量之间的交互 - R: interaction between continuous and categorical vars in 'isat' regression ('gets' package) R中连续变量和分类变量之间的相互作用:是否可以包含所有类别? - Interaction between continuous and categorical variable in R: is there a way to include all categories? 如何获得R中两个分类变量的百分比 - How to get Percentage in two categorical variables in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM