简体   繁体   English

错误:`data` 和 `reference` 应该是具有相同水平的因素。 使用混淆矩阵(插入符号)

[英]Error: `data` and `reference` should be factors with the same levels. Using confusionMatrix (caret)

I am getting an error when using the confusionMatrix() function from the caret package.使用caret包中的confusionMatrix()函数时出现错误。 To reproduce the example, I use the Sonar dataset from the mlbench package.为了重现该示例,我使用了mlbench包中的Sonar数据集。

library(mlbench)
data(Sonar)

rows <- sample(nrow(Sonar))
Sonar <- Sonar[rows, ]


split <- round(nrow(Sonar) * 0.6)
adiestramiento <- Sonar[1:split, ]
experimental <- Sonar[(split + 1):nrow(Sonar), ]

model <- glm(Class ~ ., family = binomial(link = "logit"), adiestramiento)
p <- predict(model, experimental, type = "response")
p_class <- ifelse(p > 0.5, "M", "R")

library(caret)
confusionMatrix(p_class, experimental[["Class"]])

The error I am getting when running confusionMatrix() is我在运行confusionMatrix()时遇到的错误是

Error: data and reference should be factors with the same levels`错误: datareference应该是具有相同水平的因素`

I checked that both p_class and experimental[["Class"]] have the same number of objetcs (83).我检查了p_classp_class experimental[["Class"]]具有相同数量的 objetcs (83)。

Any idea what's going on?知道发生了什么吗?

The issue is that data or, in this case, p_class has to be a factor.问题是data或在这种情况下p_class必须是一个因素。 So, instead we should use所以,我们应该使用

confusionMatrix(factor(p_class), experimental[["Class"]])
# Confusion Matrix and Statistics
# 
#           Reference
# Prediction  M  R
#          M 17 20
#          R 33 13
# ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用混淆矩阵`data`和`reference`的错误应该是具有相同水平的因素 - error using confusionMatrix `data` and `reference` should be factors with the same levels confusionMatrix - 错误:`data` 和 `reference` 应该是具有相同水平的因素 - confusionMatrix - Error: `data` and `reference` should be factors with the same levels ConfusionMatrix 错误:`data` 和 `reference` 应该是具有相同水平的因素 - ConfusionMatrix Error: `data` and `reference` should be factors with the same levels 错误:`data` 和 `reference` 应该是相同级别的因子。 Logistic 回归的混淆矩阵 - Error: `data` and `reference` should be factors with the same levels. Confusion matrix for Logistic Regression ConfusionMatrix 中的误差数据和参考因子必须具有相同的级别数 - Error in ConfusionMatrix the data and reference factors must have the same number of levels R 混淆矩阵误差数据和同级参考因子 - R confusionMatrix error data and reference factors with same levels 为什么我的逻辑回归模型不输出 2 个水平的因子? (错误:`data` 和 `reference` 应该是具有相同水平的因素。) - Why isnt my logistic regression model output a factor of 2 levels? (Error: `data` and `reference` should be factors with the same levels.) 混淆矩阵错误:错误:`data`和`reference`应该是具有相同水平的因子 - Confusion Matrix Error: Error: `data` and `reference` should be factors with the same levels 什么地方出了错? 错误:`data` 和 `reference` 应该是具有相同水平的因素 - What went wrong? Error: `data` and `reference` should be factors with the same levels r - 错误:`data` 和 `reference` 应该是具有相同水平的因素 - r - Error: `data` and `reference` should be factors with the same levels
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM