Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I save models as a vector with a loop?

Tags:

r

So I have this assignment where I have to create 3 different models (r). I can do them individually without a problem. However I want to take it a step further and to create a function that trains all of them with a for loop. (I know I could create a function that trained the 3 models each time. I am not looking for other solutions to the problem, I want to do it this way (or in a similar fashion) because now I have 3 models but imagine if I wanted to train 20!

I tried creating a list to store all three models, but i keep having some warnings.


library(caret)
library(readr)
library(rstudioapi)
library(e1071)
library(dplyr)
library(rpart)

TrainingFunction <- function(method,formula,data,tune) {
 fitcontrol <-  trainControl(method = "repeatedcv", repeats = 4)

 if(method == "rf") {Model <- train(formula, data = data,method = method, trcontrol = fitcontrol , tunelenght = tune)}
 else if (method == "knn"){    
   preObj <- preProcess(data[, c(13,14,15)], method=c("center", "scale"))
   data <- predict(preObj, data)
   Model <- train(formula, data = data,method = method, trcontrol = fitcontrol , tunelenght = tune)  
 }
 else if (method == "svm"){Model <- svm(formula, data = data,cost=1000 , gamma = 0.001)}
   Model
 }

So this is a training function I created, and it works, but now I want to train all three at once !

So I tried this:

methods <- c("rf","knn","svm") 
Models <- vector(mode = "list" , length = length(methods))
for(i in 1:length(methods))
{Models[i] <- TrainingFunction(methods[i],Volume~.,List$trainingSet,5)}

This are the warnings :

Warning messages:
1: In Models[i] <- TrainingFunction(methods[i], Volume ~ ., List$trainingSet,  :
  number of items to replace is not a multiple of replacement length
2: In Models[i] <- TrainingFunction(methods[i], Volume ~ ., List$trainingSet,  :
  number of items to replace is not a multiple of replacement length
3: In svm.default(x, y, scale = scale, ..., na.action = na.action) :
  Variable(s) ‘ProductType.GameConsole’ constant. Cannot scale data.
4: In Models[i] <- TrainingFunction(methods[i], Volume ~ ., List$trainingSet,  :
  number of items to replace is not a multiple of replacement length

When I do Models the output is this :


[[1]]
[1] "rf"

[[2]]
[1] "knn"

[[3]]
svm(formula = formula, data = data, cost = 1000, gamma = 0.001)

like image 239
José Rodrigues Avatar asked Jun 01 '26 22:06

José Rodrigues


1 Answers

Consider switch to avoid the many if and else especially if extending to 20 models. Then use lapply to build a list without initialization or iterative assignment:

TrainingFunction <- function(method, formula, data, tune) {
 fitcontrol <-  trainControl(method = "repeatedcv", repeats = 4)

 Model <- switch(method,
     "rf" = train(formula, data = data, method = method, 
                  trcontrol = fitcontrol, tunelength = tune)
     "knn" = {    
          preObj <- preProcess(data[,c(13,14,15)], 
                               method=c("center", "scale"))
          data <- predict(preObj, data)
          train(formula, data = data, method = method, 
                trcontrol = fitcontrol, tunelength = tune)  
          }
     "svm" = svm(formula, data = data, cost = 1000, gamma = 0.001)
 )
}

methods <- c("rf","knn","svm") 

Model_list <-lapply(methods, function(m)
    TrainingFunction(m, Volume~., List$trainingSet, 5))
like image 128
Parfait Avatar answered Jun 03 '26 10:06

Parfait



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!