在R中的插入符号模型上使用predict()函数时出错

时间:2019-10-28 18:23:53

标签: r logistic-regression prediction r-caret caret

我目前正在尝试在caret中创建一些不同的模型,从逻辑模型到XGBoost。创建模型很容易,但是当我想使用模型对开始之前已经搁置的测试集进行预测时,会收到一条错误消息,提示诸如:

  

UseMethod(“ predict”)中的错误:

     

没有适用于“预测”的方法应用于“ data.frame”类的对象

和:

  

预测错误(logistic_model $ finalModel,new_data = pd_test)$。pred_class:

     

$运算符对原子向量无效

这是物流模型:

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$选择,但无济于事

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码 log_class_predictions <- predict(logistic_model, new_data = pd_test)

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