在 R 中的插入符号模型上使用 predict() 函数时出错
Error when using predict() function on caret models in R
我目前正在尝试在 caret
中制作几个不同的模型,从逻辑模型到 XGBoost。创建模型很容易,但是当我想使用模型对开始前预留的测试集进行预测时,我收到一条错误消息,内容如下:
Error in UseMethod("predict") :
no applicable method for 'predict' applied to an object of class "data.frame"
和:
Error in predict(logistic_model$finalModel, new_data = pd_test)$.pred_class :
$ operator is invalid for atomic vectors`
这是逻辑模型:
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
我该如何解决这个问题,以便我可以在未修改的测试集上测试我的模型?我尝试了几个 logistic_model$
选项,但都无济于事
您可以使用以下代码
log_class_predictions <- predict(logistic_model, new_data = pd_test)
log_predictions <- predict(logistic_model, new_data = pd_test, type = "prob")
我目前正在尝试在 caret
中制作几个不同的模型,从逻辑模型到 XGBoost。创建模型很容易,但是当我想使用模型对开始前预留的测试集进行预测时,我收到一条错误消息,内容如下:
Error in UseMethod("predict") :
no applicable method for 'predict' applied to an object of class "data.frame"
和:
Error in predict(logistic_model$finalModel, new_data = pd_test)$.pred_class :
$ operator is invalid for atomic vectors`
这是逻辑模型:
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
我该如何解决这个问题,以便我可以在未修改的测试集上测试我的模型?我尝试了几个 logistic_model$
选项,但都无济于事
您可以使用以下代码
log_class_predictions <- predict(logistic_model, new_data = pd_test)
log_predictions <- predict(logistic_model, new_data = pd_test, type = "prob")