最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - Outputs standardization in caretEnsemble - Stack Overflow

programmeradmin12浏览0评论

I'm using R's caretEnsemble with HMDA data to implement "voting" method. After I fitted the base models with caretList, stack them together using caretEnsemble, and predict the trained ensemble against testing data, the predict function returns an error saying that my predictor variables were specified with different types from the fit.

I surmise this is because either the caretList or caretEnsemble did not output some of the models' predictions in factor & saw scores (probabilities) format, but I'm not sure. Is there any way to fix this? Any thoughts would be appreciated.

Below is my current code.

library(AER)  # for HMDA data
library(caret)
library(caretEnsemble)

# Load dataset
data(HMDA)
set.seed(123)
trainIndex <- createDataPartition(HMDA$deny, p = 0.8, list = FALSE)
trainData <- HMDA[trainIndex, ]
testData <- HMDA[-trainIndex, ]

# apply train control across all models
ctrl <- trainControl(method = "cv", number = 5, savePredictions = "final", classProbs = TRUE)

# fit models
models <- caretList(
  deny ~ ., data = trainData , trControl = ctrl,
  methodList = c("glm", "glm", "svmPoly", "nb", "gbm"),
  tuneList = list(
    probit = caretModelSpec(method = "glm", family = binomial(link = "probit")),
    logit = caretModelSpec(method = "glm", family = binomial(link = "logit")),
    svm = caretModelSpec(method = "svmPoly"),
    nb = caretModelSpec(method = "nb"),
    gbm = caretModelSpec(
      method = "gbm",
      verbose = FALSE
    )
  )
)

# stack base models
ensemble_model <- caretStack(models, metric = "Accuracy", trControl = ctrl)

# Prediction
stacked_pred <- predict(ensemble_model, newdata = testData )

# This throws an error message
Error in UseMethod("predict") : 
  no applicable method for 'predict' applied to an object of class "factor"
发布评论

评论列表(0)

  1. 暂无评论