2016-11-23 41 views
0

以下是一个可重复的例子,什么基本上我试图做的,正在创作5点估算的数据集然后应用SVM使用中插入符号火车功能各估算数据集,然后合奏使用caretEnsemble产生训练模型。最后,我使用整体模型预测每个测试集。caretEnsmble与SVM(问题),不同的培训数据集

不过,我得到这个错误

Error in check_bestpreds_obs(modelLibrary) :
Observed values for each component model are not the same. Please re-train the models with the same Y variable

有反正周围,可以帮助我合奏不同的培训模式?

任何帮助是真正的赞赏。

library(mice) 
    library(e1071) 
    library(caret) 
    library("caretEnsemble") 

data <- iris 
#Generate 10% missing values at Random 
iris.mis <- prodNA(iris, noNA = 0.1) 
#remove categorical variables 
iris.mis <- subset(iris.mis, select = -c(Species)) 

# 5 Imputation using mice pmm 

imp <- mice(iris.mis, m=5, maxit = 10, method = 'pmm', seed = 500) 

# save 5 imputed dataset. 
x1 <- complete(imp, action = 1, include = FALSE) 
x2 <- complete(imp, action = 2, include = FALSE) 
x3 <- complete(imp, action = 3, include = FALSE) 
x4 <- complete(imp, action = 4, include = FALSE) 
x5 <- complete(imp, action = 5, include = FALSE) 

## Apply the following method for each imputed set 

form <- iris$Sepal.Width # target column 
n <- nrow(x1) # since all data sample are the same length 
prop <- n%/%fold 
set.seed(7) 
newseq <- rank(runif(n)) 
k <- as.factor((newseq - 1)%/%prop + 1) 
CVfolds <- 10 


CVrepeats <- 3 
    indexPreds <- createMultiFolds(x1[k != i,]$Sepal.Width, CVfolds, CVrepeats) 
    ctrl <- trainControl(method = "repeatedcv", repeats = CVrepeats,number = CVfolds, returnResamp = "all", savePredictions = "all", index = indexPreds) 




fit1 <- train(Sepal.Width ~., data = x1[k !=i, ],method='svmLinear2',trControl = ctrl) 
fit2 <- train(Sepal.Width ~., data = x2[k != i, ],method='svmLinear2',trControl = ctrl) 
fit3 <- train(Sepal.Width ~., data = x3[k != i, ],method='svmLinear2',trControl = ctrl) 
fit4 <- train(Sepal.Width ~., data = x4[k != i, ],method='svmLinear2',trControl = ctrl) 
fit5 <- train(Sepal.Width ~., data = x5[k != i, ],method='svmLinear2',trControl = ctrl) 




#combine the created model to a list 
     svm.fit <- list(svmLinear1 = fit1, svmLinear2 = fit2, svmLinear3 = fit3, svmLinear4 = fit4, svmLinear5 = fit5) 

    # convert the list to cartlist 
    class(svm.fit) <- "caretList" 

    #create the ensemble where the error occur. 
    svm.all <- caretEnsemble(svm.fit,method='svmLinear2') 
+0

我想你忘了在'形式在此处指定光圈< - Sepal.Width'。 –

+0

谢谢你发现这个,但我仍然得到同样的错误。 – user3895291

回答

0

你必须简化你的例子。获取错误不需要太多移动部件和循环。其中一个内部caretEnsemble控件抛出此错误,但该消息没有很好定义。

这就是说, caretList需要有一个指定的trainControl对象,您使用每个火车模型。否则,重采样会为每个模型不同,你会得到错误:

"Component models do not have the same re-sampling strategies"

下一个问题是,你正在使用不同的数据集,每个列车对象。 CaretEnsemble旨在与相同的训练数据集一起使用。即使他们有相同的基础,你的x1到x5也是不同的。这将导致错误:

"Observed values for each component model are not the same. Please re-train the models with the same Y variable"

最后,如果你想从单独训练的模型构建一个model.list只使用c(model1, model2)。看到文档c.train

+0

非常感谢您的回复,我真的很感激,我有简化代码的建议。您能否给我提供一个从单独培训的模型中构建model.list的示例?这是否意味着我不能用与不同训练数据集关联的模型构建插入符号集。 – user3895291