迭代模型而无需使用R对其进行硬编码

时间:2018-07-02 12:37:21

标签: r automation hardcode

我有这个代码

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

这个想法是制作一个公式列表,并将我的代码应用于列表中的每个元素

1 个答案:

答案 0 :(得分:2)

可能不是一个完整的答案,但这应该可以带来一个想法:

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将用于选择所需的项。

想法是从所需的术语中构建公式,这里每次删除一个,按?as.formula文档中的说明粘贴它们并获取该公式,其余代码可直接使用,结果在此示例中,列表将包含表而不是公式。