混淆矩阵和插入符号包 - rpart算法

时间:2018-03-28 16:19:11

标签: r r-caret confusion-matrix rpart

我正在使用插入包开发一个简单的rpart模型。

library(caret)
data <- fread(sprintf("%s/%s", dataDir, dataFile), header=T, stringsAsFactors = TRUE)
split <- createDataPartition(y = data$Audit, p = 0.7, list = FALSE)
trainSet <- data[split,]
test <- data[-split,]

fitControl <- trainControl( method = "repeatedcv",  number = 10,repeats = 10)


fitControl <- trainControl( method = "repeatedcv",  number = 10,repeats = 10)

fit <- train(Audit ~ Activity1 + Activity2 + Activity3 + Activity4 + Activity5 + Activity6 + Activity7 + Activity8, data = trainSet, method="rpart", trControl = fitControl)

现在我试图创建一个混淆矩阵:

preds<-predict(fit2, newdata = test)
cm<-confusionMatrix(preds,test$Audit)
print(cm)

当我看到输出时,我看到了:

     [,1] [,2]
[1,]    0    0
[2,]    4    0

这对我来说真的很困惑,而且没有意义。我期待这样的事情成为我的困惑矩阵

preds A B U
    A 0 0 0
    B 0 4 0
    U 2 0 9

这是我的数据的快照:

snapshot of the data

这是我的数据输入:

dput(as.data.frame(data))
structure(list(Activity1 = structure(c(2L, 1L, 2L, 1L, 1L, 1L, 
1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 
2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 
2L, 2L), .Label = c("a", "b"), class = "factor"), Activity2 = 
structure(c(2L, 
1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 
1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 1L, 2L, 2L, 1L, 2L, 2L), .Label = c("a", "b"), class = "factor"), 
Activity3 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L), .Label = c("a", "b"), class = "factor"), Activity4 = structure(c(2L, 
1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("b", 
"e"), class = "factor"), Activity5 = structure(c(2L, 1L, 
2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 
2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("b", "d"), class = "factor"), 
Activity6 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L), .Label = c("d", "e"), class = "factor"), Activity7 = structure(c(1L, 
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 
2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 
1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L), .Label = c("a", 
"c"), class = "factor"), Activity8 = structure(c(2L, 2L, 
4L, 2L, 2L, 2L, 2L, 4L, 2L, 1L, 3L, 1L, 2L, 4L, 1L, 2L, 2L, 
2L, 2L, 2L, 4L, 2L, 3L, 2L, 4L, 2L, 1L, 2L, 3L, 4L, 2L, 2L, 
1L, 2L, 2L, 2L, 3L, 4L, 1L, 2L, 2L, 2L, 3L, 1L, 1L, 2L, 2L, 
3L, 2L, 2L, 2L, 3L, 2L, 2L, 4L, 3L), .Label = c("a", "c", 
"d", "e"), class = "factor"), Audit = structure(c(3L, 1L, 
3L, 2L, 1L, 1L, 1L, 3L, 3L, 2L, 3L, 2L, 2L, 3L, 2L, 1L, 3L, 
3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 3L, 3L, 3L, 1L, 2L, 
2L, 1L, 3L, 2L, 3L, 3L, 2L, 1L, 3L, 3L, 3L, 2L, 2L, 3L, 3L, 
3L, 3L, 3L, 2L, 3L, 3L, 2L, 3L, 3L), .Label = c("A", "B", 
"U"), class = "factor")), .Names = c("Activity1", "Activity2", 
"Activity3", "Activity4", "Activity5", "Activity6", "Activity7", 
"Activity8", "Audit"), row.names = c(NA, -56L), class = "data.frame")

以下是我的测试数据的输入:

> dput(as.data.frame(test))
structure(list(Activity1 = structure(c(1L, 1L, 1L, 2L, 2L, 1L, 
1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L), .Label = c("a", "b"), class = 
"factor"), 
Activity2 = structure(c(1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 
1L, 2L, 2L, 1L, 2L, 2L), .Label = c("a", "b"), class = "factor"), 
Activity3 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L), .Label = c("a", "b"), class = "factor"), 
Activity4 = structure(c(2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L), .Label = c("b", "e"), class = "factor"), 
Activity5 = structure(c(2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L), .Label = c("b", "d"), class = "factor"), 
Activity6 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L), .Label = c("d", "e"), class = "factor"), 
Activity7 = structure(c(2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 
1L, 1L, 1L, 2L, 1L, 1L), .Label = c("a", "c"), class = "factor"), 
Activity8 = structure(c(2L, 2L, 2L, 4L, 2L, 2L, 2L, 3L, 2L, 
2L, 3L, 2L, 2L, 4L, 3L), .Label = c("a", "c", "d", "e"), class = "factor"), 
Audit = structure(c(2L, 3L, 2L, 3L, 3L, 1L, 2L, 3L, 1L, 3L, 
3L, 3L, 2L, 3L, 3L), .Label = c("A", "B", "U"), class = "factor")), .Names = c("Activity1", 
"Activity2", "Activity3", "Activity4", "Activity5", "Activity6", 
"Activity7", "Activity8", "Audit"), class = "data.frame", row.names = c(NA, 
-15L))

有人可以帮我吗?感谢

1 个答案:

答案 0 :(得分:0)

我刚刚发现了为什么我的confusionMatrix是nbot正常工作的原因。这是因为我在代码的开头加载了库(插入符号)以及库(ModelMetrics)。一旦我评论了库(ModelMetrics)并重新启动了我的R,我就能让混淆矩阵完美地工作。