Chris T.
Chris T.

Reputation: 1801

Outputs standardization in caretEnsemble

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"

Upvotes: 0

Views: 14

Answers (0)

Related Questions