Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在进行二元分类时,如何解决predict()中的错误:R中的下标越界?_R_Statistics - Fatal编程技术网

在进行二元分类时,如何解决predict()中的错误:R中的下标越界?

在进行二元分类时,如何解决predict()中的错误:R中的下标越界?,r,statistics,R,Statistics,我试图在R中使用二进制分类来计算测试度量。我认为我有正确的代码,但是我一直遇到错误 目标是创建一个分类器,通过训练决策树模型,在给定其他变量的情况下帮助检测糖尿病。为正类使用0.50的概率截止值创建分类器 以下是我的代码: # load packages library("mlbench") library("tibble") library("rpart") # set seed set.seed(457) # load dat

我试图在R中使用二进制分类来计算测试度量。我认为我有正确的代码,但是我一直遇到错误

目标是创建一个分类器,通过训练决策树模型,在给定其他变量的情况下帮助检测糖尿病。为正类使用0.50的概率截止值创建分类器

以下是我的代码:

# load packages
library("mlbench")
library("tibble")
library("rpart")

# set seed 
set.seed(457)

# load data, remove NA rows, coerce to tibble
data("PimaIndiansDiabetes2")
diabetes = as_tibble(na.omit(PimaIndiansDiabetes2))

# split data
dbt_trn_idx = sample(nrow(diabetes), size = 0.8 * nrow(diabetes))
dbt_trn = diabetes[dbt_trn_idx, ]
dbt_tst = diabetes[-dbt_trn_idx, ]

# check data
dbt_trn

# fit models
mod_tree = rpart(diabetes ~ ., dbt_trn)

# get predicted probabilities for "positive" class, always use second alphabetically for +
prob_tree = predict(mod_tree, dbt_trn)[,"glucose"]

# create tibble of results for tree
results = tibble(
  actual   = dbt_tst$diabetes,
  prob_tree  = prob_tree,
)

# evaluate knn with various metrics
tree_eval = evaluate(
  data = results,
  target_col = "actual",
  prediction_cols = "prob_tree",
  positive = "diabetes",
  type = "binomial",
  metrics = list("Accuracy" = TRUE)
  cutoff = 0.5)

tree_eval 
我不断地犯错误: “预测中的错误(mod_tree,dbt_trn)[,”“glucose”]: 下标越界“


我不确定如何解决这个问题。任何帮助都会很好

调用的
predict
结果有
pos
neg
列。而且,看起来您打算在测试集上进行预测

问题树
的定义如下:

prob_tree = predict(mod_tree, dbt_tst)[,"pos"]