Krishna Kalyan Krishna Kalyan - 2 months ago 18
R Question

Fitting many models in for loop using R

I wanted to create a list of models that will be used to fit my data.

Code Below

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

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


The error I get
Error: could not find function "model_name"

Answer

make a variable that is the function:

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

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