Reputation: 39
I am trying to use train function for Leave-One-Out (LOO) cross validation (LOOCV).
While
train(y~ x1, data = test, method="lm", trControl = trainControl(method = "LOOCV"))
works well, I need to write it in another format that I can put it in a for loop that I make as many as models that I want. But the following format does not work and give an error:
train(paste("y~", colnames(test[2])), data = test, method="lm", trControl = trainControl(method = "LOOCV"))
Error: Please use column names for x
But the same format in "lm" function works well:
lm(paste("y~", colnames(test[2])), data = test)
Can you please guide me how to fix this issue?
Upvotes: 1
Views: 1332
Reputation: 39
Found the solution!
lm("y~x1", data=test)
works as same as
lm (y~x1, data=test)
But "y~x1" does not work in the train function. Need to add 'as.formula' before it:
train(as.formula("y~x", data=test, method="lm", trControl = trainControl(method = "LOOCV"))
Upvotes: 2