假设我有以下规范:
glm(death ~ age + black + hisp + other + rich + middle, family = binomial("probit"), data=data)是否有一种直接的方式来增加“种族群体”(黑人、拉美裔和其他群体)和“收入群体”(富有、中等)之间的所有双向互动。因此,相互作用将是black_rich,black_middle,hisp*丰富,等等。
发布于 2016-03-22 02:44:12
公式接口允许您使用^-operator轻松地完成这一任务,您可以通过(ethnicity + incgrp)^2从两个因素变量构造所有的双向交互,但这只适用于使用R因子约定的情况。看来,您正试图通过执行SAS样式的虚拟变量创建来规避公式和因素的正确使用。对于你的处境,你可以尝试:
glm(death ~ age + (black + hisp + other)*( rich + middle), family = binomial("probit"), data=data)formula解释使用^和*来构造交互。他们失去了传统的数学意义。请参阅?formula
发布于 2016-03-22 03:39:33
考虑粘贴公式中的所有组合:
vars1 <- c('black', 'hisp', 'other')
vars2 <- c('rich', 'middle')
interactions <- outer(vars1, vars2, function(x,y){paste0(x,'*',y)})
intjoin <- paste(interactions, collapse=" + ")
#[1] "black*rich + hisp*rich + other*rich + black*middle + hisp*middle + other*middle"
model <- glm(paste0('death ~ age + black + hisp + other + rich + middle + ', intjoin),
family = binomial("probit"), data=data) https://stackoverflow.com/questions/36144890
复制相似问题