无需使用 R 硬编码即可迭代模型

Iterate models without hardcoding it with R

我有这个代码

#  [1] "X6"  "X5"  "X7"  "X4"  "X3"  "X8"  "X19" "X9"  "X10" "X16"    
formula = result ~ X3+X4+X5+X6+X7+X8+X9+X10+X16+X19
full_magnetude_model = glm.fit <- glm(formula, data = train)
full_magnetude_predict = predict(full_magnetude_model, newdata=test)

# Comparing results
full_magnetude_results <- ifelse(full_magnetude_predict > 0.5, 1, 0)
true_results = test$result

# results
table(full_magnetude_results,true_results)

它工作正常,但结果因不同的公式而不同,我需要为以下对象做同样的事情:

#  [1] "X6"  "X5"  "X7"  "X4"  "X3"  "X8"  "X19" "X9"  "X10" "X16"    
#  [1] "X6"  "X5"  "X7"  "X4"  "X3"  "X8"  "X19" "X9"  "X10"    
#  [1] "X6"  "X5"  "X7"  "X4"  "X3"  "X8"  "X19" "X9"      
#  [1] "X6"  "X5"  "X7"  "X4"  "X3"  "X8"  "X19"     
#  [1] "X6"  "X5"  "X7"  "X4"  "X3"  "X8"      

等等,我可以手动执行此操作,但有没有聪明的方法来执行此操作?

更新

完整代码: https://github.com/martin-varbanov96/fmi_summer_2018/blob/master/fmi_6ti_sem/Pril_stat/project/main.R

我的想法是制作一个公式列表并为列表的每个元素应用我的代码

可能不是一个完整的答案,但这应该给出一个想法:

terms=c("X3", "X4", "X5", "X6", "X7", "X8", "X9", "X10", "X16", 
        "X19")

lapply(10:6,function(x) {
  formula <- as.formula(paste("result ~ ", paste0(terms[1:x],collapse="+")))
  formula
})

给出:

[[1]]
result ~ X3 + X4 + X5 + X6 + X7 + X8 + X9 + X10 + X16 + X19
<environment: 0x0000000016fbec50>

[[2]]
result ~ X3 + X4 + X5 + X6 + X7 + X8 + X9 + X10 + X16
<environment: 0x0000000016fc29a0>

[[3]]
result ~ X3 + X4 + X5 + X6 + X7 + X8 + X9 + X10
<environment: 0x00000000170f44b0>

[[4]]
result ~ X3 + X4 + X5 + X6 + X7 + X8 + X9
<environment: 0x00000000170f8a98>

[[5]]
result ~ X3 + X4 + X5 + X6 + X7 + X8
<environment: 0x00000000170fd1d0>

Lapply 将迭代传递给匿名函数的范围(10 到 6),此 x 将用于 select 所需的术语。

我们的想法是根据需要的术语构建您的公式,这里每次删除一个,按照 ?as.formula 文档中的显示粘贴它们并获取公式,您的代码的其余部分可以按原样使用,结果列表将包含表格而不是本例中的公式。