如何使用循环将模型另存为向量?

时间:2019-03-30 20:51:24

标签: r

因此,我要进行此作业,其中必须创建3个不同的模型(r)。我可以单独解决这些问题。但是,我想更进一步,并创建一个函数来使用for循环训练所有函数。 (我知道我可以创建一个函数来每次训练3个模型。我不是在寻找其他解决问题的方法,我想这样做(或以类似的方式),因为现在我有3个模型,但可以想象是否我想训练20人!

我尝试创建一个列表来存储所有三个模型,但是我一直在发出一些警告。

document.body.onresize=function(){Adjustments();};


function Adjustments(){

 var W=Container.offsetWidth, H=Container.offsetHeight;


 // You've now got the new resolution so go for your life!

 }

所以这是我创建的训练功能,并且可以正常工作,但是现在我想一次训练所有三个!

所以我尝试了这个:


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
 }

这是警告:

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)}

当我建模时,输出是这样的:

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

2 个答案:

答案 0 :(得分:1)

我认为问题出在这一行:

{Models[i] <- TrainingFunction(methods[i],Volume~.,List$trainingSet,5)}

如果要将模型分配到列表的第i个位置,则应使用双括号,例如:

{Models[[i]] <- TrainingFunction(methods[i],Volume~.,List$trainingSet,5)}

另一种替代方法是使用lapply而不是显式循环,因此可以完全避免该问题:

train_from_method <- function(methods) {TrainingFunction(methods,Volume~.,List$trainingSet,5)}
Models <- lapply(species_vector, train_from_method)

答案 1 :(得分:0)

请考虑使用switch来避免使用许多ifelse,尤其是扩展到20个模型时。然后使用lapply来建立一个没有初始化或迭代分配的列表:

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))