我正在使用R插入符号进行分类 . 我在训练时收到以下错误消息:
Error in train.default(train[, predictorsNames], train[, outcomeName], : Class probabilities are needed to score models using the area under the ROC curve. Set classProbs = TRUE in the trainControl() function.
我做了一些关于这个问题的搜索 . 以下两个链接正在讨论类似的问题 . Error when I try to predict class probabilities in R - caret和R caret train Error in evalSummaryFunction: cannnot compute class probabilities for regression根据给出的答案,问题可能是由于未将outcomeName定义为因子或无效的级别名称 . 但是我已经将outcomeName转换为一个因子,尝试了不同的级别名称并设置了 classProbs=TRUE
但它仍然不起作用 .
library(caret)
library(gbm)
我使用的数据集是 dat
,它有6个变量 . 我需要对变量 "FlagD60"
进行分类 .
> dput(droplevels(head(dat,5)))
structure(list(FICO = c(689L, 689L, 689L, 783L, 783L), Line = c(4000.001686,
3700.002962, 3600.001866, 14500.00101, 5262.002105), Balance = c(1686L,
2962L, 1866L, 1014L, 2105L), Payment = c(53L, 79L, 33L, 21L,
15L), Age = c(6L, 81L, 82L, 235L, 57L), FlagD60 = c(0L, 0L, 0L,
0L, 0L)), .Names = c("FICO", "Line", "Balance", "Payment", "Age",
"FlagD60"), row.names = c(NA, 5L), class = "data.frame")
我生成了一个新的因子,其级别为“是”和“否”,用于分类和拆分数据 . 由于我不知道错误是否来自这个准备阶段,我也留下了它供你参考 .
### prepare for classification ###
outcomeName <- 'FlagD60'
predictorsNames <- names(dat)[names(dat) != outcomeName]
dat$FlagD60b=ifelse(dat$FlagD60==1,'yes','no')
dat$FlagD60b=as.factor(dat$FlagD60b)
outcomeName='FlagD60b'
trainIndex=createDataPartition(dat[,outcomeName],p=0.75,list =
FALSE,times=1)
train=dat[ trainIndex,]
test =dat[-trainIndex,]
以下是 levels(train$FlagD60b)
的结果 .
[1] "no" "yes"
然后我建造了这样的模型 .
#### repeated 10-fold CV, grid, gbm ####
ctrl=trainControl(method = "repeatedcv",number = 10,repeats = 10,
summaryFunction = twoClassSummary,
classProbs = TRUE)
set.seed(520)
gbmfit=train(train[,predictorsNames], train[,outcomeName],
method="gbm",
trcontrol=ctrl,
verbose=FALSE,
metric="ROC")
这就像我上面所说的那样给出了错误 . 您的任何想法将非常感激 .
并且 sessionInfo()
的输出也包含在内供您参考 .
> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
locale:
[1] LC_COLLATE=Chinese (Simplified)_China.936 LC_CTYPE=Chinese (Simplified)_China.936
[3] LC_MONETARY=Chinese (Simplified)_China.936 LC_NUMERIC=C
[5] LC_TIME=Chinese (Simplified)_China.936
attached base packages:
[1] parallel splines stats graphics grDevices utils datasets methods base
other attached packages:
[1] plyr_1.8.4 gbm_2.1.3 survival_2.39-4 caret_6.0-73 ggplot2_2.2.1 lattice_0.20-34
loaded via a namespace (and not attached):
[1] Rcpp_0.12.9 magrittr_1.5 MASS_7.3-45 munsell_0.4.3 colorspace_1.3-2
[6] foreach_1.4.3 minqa_1.2.4 stringr_1.2.0 car_2.1-4 tools_3.3.1
[11] nnet_7.3-12 pbkrtest_0.4-7 grid_3.3.1 gtable_0.2.0 nlme_3.1-128
[16] mgcv_1.8-12 quantreg_5.29 MatrixModels_0.4-1 iterators_1.0.8 lme4_1.1-12
[21] lazyeval_0.2.0 assertthat_0.1 tibble_1.2 Matrix_1.2-6 nloptr_1.0.4
[26] reshape2_1.4.2 ModelMetrics_1.1.0 codetools_0.2-14 stringi_1.1.2 scales_0.4.1
[31] stats4_3.3.1 SparseM_1.76