0

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"

2
  • given that model_name is a string and not a function, what made you think that this would work? Also, did you look at the caret package? it's tailor made to help do things like this. Also, rf is ambiguous since there's a stats::rf() but rf is naming shorthand in the caret package. also, model is only ever going to hold the last model since you're not making a list. Commented Oct 3, 2016 at 10:52
  • @hrbrmstr yes. I just wanted to give an example and caret package does not have the models I am looking for. Commented Oct 3, 2016 at 13:52

1 Answer 1

1

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)
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hey guys, this code is pretty handy! Does anyone know how to do predictions based on the output of this code? Also how does this code cater to different model arguments?
This code does not have output, you need to store the model result in a variable, then use this model variable for prediction of new samples. If you want to pass different model arguments to the model function, you can put the arguments in a list object and use it in the loop. So besides the models vector you will have another object params which is a list of lists. Each list consists of the parameters you want to pass to the specific model funcion. For a better answer you could make a new question, with an example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.