【发布时间】:2016-07-18 19:08:59
【问题描述】:
我想将 caret package 中的函数 confusionMatrix() 合并到函数 shuffle100 中,以从分类树模型生成的主列表的子集(数据帧)中生成混淆矩阵。我的目标是生成混淆矩阵统计信息,例如分类精度、kappa 度量等(下面的所需输出)。很抱歉问这么简单的问题,但我无法弄清楚。如果有人可以提供帮助,请提前非常感谢。
可在此地址找到可重现的虚拟数据:
生成分类树模型预测和混淆矩阵的嵌套列表的代码
library(caret)
library(e1071)
library(rpart)
set.seed(1235)
shuffle100 <-lapply(seq(10), function(n){ #produce 10 different shuffled data-frames
subset <- my_data[sample(nrow(my_data), 80),] #shuffle 80 rows in the data-frame
subset_idx <- sample(1:nrow(subset), replace = FALSE)
subset <- subset[subset_idx, ]
subset_resampled_idx <- createDataPartition(subset_idx, times = 1, p = 0.7, list = FALSE) #partition data-frame into 70 % training and 30 % test subsets
subset_resampled <- subset[subset_resampled_idx, ] #70 % training data
ct_mod<-rpart(Family~., data=subset_resampled, method="class", control=rpart.control(cp=0.005)) #10 ct models
ct_pred<-predict(ct_mod, newdata=subset[,2:13])
confusionMatrix(ct_pred, norm$Family)#10 confusion matrices
})
错误信息
Error in sort.list(y) : 'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?
Called from: sort.list(y)
期望的结果
Confusion Matrix and Statistics
Reference
Prediction G8 V4
G8 42 12
V4 8 18
Accuracy : 0.75
95% CI : (0.6406, 0.8401)
No Information Rate : 0.625
P-Value [Acc > NIR] : 0.01244
Kappa : 0.4521
Mcnemar's Test P-Value : 0.50233
Sensitivity : 0.8400
Specificity : 0.6000
Pos Pred Value : 0.7778
Neg Pred Value : 0.6923
Prevalence : 0.6250
Detection Rate : 0.5250
Detection Prevalence : 0.6750
Balanced Accuracy : 0.7200
'Positive' Class : G8
【问题讨论】:
标签: r list r-caret rpart confusion-matrix