【问题标题】:Error when using predict() function on caret models in R在 R 中的插入符号模型上使用 predict() 函数时出错
【发布时间】:2020-02-24 01:02:35
【问题描述】:

我目前正在尝试在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$ 选项,但都无济于事

【问题讨论】:

    标签: r logistic-regression prediction r-caret


    【解决方案1】:

    您可以使用以下代码

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

    【讨论】:

      猜你喜欢
      • 2018-08-30
      • 2020-10-06
      • 2019-10-01
      • 2015-07-17
      • 2020-04-13
      • 2016-09-25
      • 2015-05-14
      • 2018-10-15
      • 2014-02-01
      相关资源
      最近更新 更多