在使用rpart()
和predict()
命令生成预测模型后,我应该在 R 中使用什么命令来执行混淆矩阵?
# Grow tree
library(rpart)
fit <- rpart(activity ~ ., method="class", data=train.data)
printcp(fit) # display the results
plotcp(fit) # visualize cross-validation results
summary(fit) # detailed summary of splits
# Prune the tree (in my case is exactly the same as the initial model)
pfit <- prune(fit, cp=0.10) # from cptable
pfit <- prune(fit,cp=fit$cptable[which.min(fit$cptable[,"xerror"]),"CP"])
# Predict using the test dataset
pred1 <- predict(fit, test.data, type="class")
# Show re-substitution error
table(train.data$activity, predict(fit, type="class"))
# Accuracy rate
sum(test.data$activity==pred1)/length(pred1)
我想以清晰的方式总结真阳性、假阴性、假阳性和真阴性。在相同的矩阵中具有灵敏度、特异性、阳性预测值和阴性预测值也会很棒。
资料来源:http ://en.wikipedia.org/wiki/Sensitivity_and_specificity