以下是一个可重现的示例,基本上我想做的是创建五个估算数据集,然后使用插入符号中的训练函数将 SVM 应用于每个估算数据集,然后使用 caretEnsemble 集成生成的训练模型。最后,我使用集成模型预测每个测试集。
但是,我收到此错误
check_bestpreds_obs(modelLibrary) 中的错误:
每个组件模型的观察值不同。请重新训练具有相同 Y 变量的模型
有什么方法可以帮助我整合不同的训练模型吗?
任何帮助都非常感谢。
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')