【问题标题】:Obtaining Predictions for New Observations (R Programming Language)获得新观察的预测(R 编程语言)
【发布时间】:2021-11-05 13:21:12
【问题描述】:

我正在使用 R 编程语言。我在 R 中为这个数据集创建了一个决策树(预测“diabetes”列是“pos”还是“neg”):

#load libraries
library(pdp)
library(C50)

#load data
data(pima)

#remove na's
new_data = na.omit(pima)

#format data
new_data$age = as.factor(ifelse(new_data$age >30, "1", "0"))
new_data$pregnant = as.factor(ifelse(new_data$pregnant >2, "1", "0"))

#run model
tree_mod <- C5.0(x = new_data[, 1:8], rules = TRUE, y = new_data$diabetes)

这是我的问题:我正在尝试获取模型为新观察所做的“预测”列。然后我想将此列附加到原始数据集。

通过以下链接,https://cran.r-project.org/web/packages/C50/vignettes/C5.0.html,我使用了“预测”功能:

#pretend this is new data
new = new_data[1:10,]

#run predictions
pred = predict(tree_mod, newdata = new[, 1:8])

但这会产生以下错误:

Error in x[j] : invalid subscript type 'closure'

谁能告诉我怎么做?

我正在尝试创建这样的东西(“prediction_made_by_model”):

   pregnant glucose pressure triceps insulin mass pedigree age diabetes prediction_made_by_model
4         0      89       66      23      94 28.1    0.167   0      neg                      pos
5         0     137       40      35     168 43.1    2.288   1      pos                      neg
7         1      78       50      32      88 31.0    0.248   0      pos                      neg
9         0     197       70      45     543 30.5    0.158   1      pos                      pos
14        0     189       60      23     846 30.1    0.398   1      pos                      neg
15        1     166       72      19     175 25.8    0.587   1      pos                      pos

谢谢!

【问题讨论】:

  • vars 定义在哪里?

标签: r data-manipulation prediction decision-tree


【解决方案1】:

我能够弄清楚。由于某种原因,这在以前不起作用:

pred = predict(tree_mod, newdata = new[, 1:8])

new$prediction_made_by_model = pred

【讨论】:

    猜你喜欢
    • 2014-08-06
    • 1970-01-01
    • 2018-11-28
    • 2016-10-15
    • 1970-01-01
    • 1970-01-01
    • 2016-09-19
    • 2011-04-13
    • 2021-08-16
    相关资源
    最近更新 更多