1
votes

I am currently trying to make a few different models in caret, ranging from logistic model to XGBoost. Creating the models is easy enough, however when I want to use the models to make predictions on a test set I have set aside before beginning, I get an error messages saying things like:

Error in UseMethod("predict") :

no applicable method for 'predict' applied to an object of class "data.frame"

and:

Error in predict(logistic_model$finalModel, new_data = pd_test)$.pred_class :

$ operator is invalid for atomic vectors`

Here is the logistic model:

set.seed(100)

train_test_split <- initial_split(pd_data, prop = 0.8)
pd_train <- training(train_test_split)
pd_test <- testing(train_test_split)

# caret 
# logistic model
# model creation and VIF
log_control <- trainControl(method = "cv", number = 5, classProbs = TRUE, 
                            summaryFunction = twoClassSummary)

logistic_model <- train(default ~ profit_margin + interest_coverage_ratio + 
                        age_of_company + liquidity_ratio_2 
                        + unpaid_debt_collection
                        + adverse_audit_opinion + amount_unpaid_debt 
                        + payment_reminders, data = pd_train, 
                        trControl = log_control, 
                        method = "glm", family = "binomial", metric = "ROC")

vif(logistic_model$finalModel)

log_class_predictions <- predict(logistic_model$finalModel, new_data = pd_test)$.pred_class
log_predictions <- predict(logistic_model$finalModel$tuneValue, 
                           new_data = pd_test, type = "prob")$.pred_1

How can I fix this so that I can test my models on the untouched test set? I have tried several logistic_model$ choices, but to no avail

1

1 Answers

1
votes

You can use the following code

log_class_predictions <- predict(logistic_model, new_data = pd_test)

log_predictions <- predict(logistic_model, new_data = pd_test, type = "prob")