df <- data.frame(
disease = c(0,1,0,1),
var1 = c(0,1,2,0),
var2 =c(0,1,2,0),
var3 = c(0,1,2,0),
var40 = c(0,1,2,0),
Bi = c(0,1,0,1),
gender = c(1,0,1,0),
P1 = c(-0.040304832,0.006868288,0.002663759,0.020251087),
P2 = c(0.010566526,0.002663759,0.017480721,-0.008685749),
P3 = c(-0.008685749,0.020251087,-0.040304832,0.002663759),
P4 = c(0.017480721,0.024306667,0.002663759,0.010566526),
stringsAsFactors = FALSE)
上述数据框(df)由分类和数字变量组成;疾病,Bi和性别与标签0,1,而var1到var40由标签0,1,2组成,而PC1,PC2,PC3,PC4由连续数字变量组成。一个变量的glm模型代码为:
glm(disease ~ var1*Bi+ gender+P1+P2+P3+P4, family = binomial(link
= 'logit'), data = df)
我需要一些帮助来编写一个循环,自动执行疾病与变种1(var1)到Variant40(var)的多变量回归分析,即具有相同的协变量; Bi,性别,P1,P2,P3,P4。对于所有40种变体,我正在做类似于下面提到的循环,但它不起作用:
for (i in df$var1:df$var40) {glm(DepVar1 ~ i*Bi+gender+P1+P2+P3+P4, data=df,
family=binomial("logit")) }
答案 0 :(得分:1)
动态购买公式可能有点棘手,但有update()
和reformulate()
等功能可以提供帮助。例如
results <- Map(function(i) {
newform <- update(disease ~ Bi+gender+P1+P2+P3+P4, reformulate(c(".", i)))
glm(newform, data=df, family=binomial("logit"))
}, names(subset(df, select=var1:var40)))
这里我们使用Map
而不是for循环,因此更容易保存结果(使用此方法将它们放入列表中)。但我们使用update()
添加基本公式中感兴趣的新变量。例如,
update(disease ~ Bi+gender+P1+P2+P3+P4, ~ . + var1)
# disease ~ Bi + gender + P1 + P2 + P3 + P4 + var1
这会在右侧添加一个变量。我们使用reformulate()
将列的名称作为字符串转换为公式。
您可以使用
从列表中获取所有模型results$var1
results$var40
# etc