【发布时间】: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