使用R在for循环中拟合许多模型

时间:2016-10-03 10:43:42

标签: r

我想创建一个用于拟合数据的模型列表。

以下代码

models = list("naiveBayes","rf")

for(model_name in models){
  print(model_name)
  formual = V35 ~ .
  model = model_name(formual,data=train)
}

我得到的错误 Error: could not find function "model_name"

1 个答案:

答案 0 :(得分:1)

创建一个函数变量:

models = c("naiveBayes","rf")

for(model_name in models){
  print(model_name)
  formual = V35 ~ .
  m <- match.fun(model_name)
  m(formual,data=train)
}