简体   繁体   English

confusionMatrix.matrix(data = df_name, reference = numeric_vector_name, positive = "increase") 错误:矩阵必须具有相等的维度

[英]Error in confusionMatrix.matrix(data = df_name, reference = numeric_vector_name, positive = "increase" ) : matrix must have equal dimensions

I have had the error in the title of this post occur 3 times in a row now and none of the first couple of resulting similar Questions on Stack Overflow when I entered this error message into Google or Bing had answers that work for my particular situation.我已经连续 3 次在这篇文章的标题中出现错误,当我将此错误消息输入 Google 或 Bing 时,在 Stack Overflow 上出现的前几个类似问题都没有适合我的特定情况的答案。 The script which the following lines of code come from is in my GitHub Repository for this analytics project in the R script file called "AIT 622 Big Data Analytics Project script".以下代码行来自的脚本位于我的GitHub 存储库中,该分析项目位于名为“AIT 622 大数据分析项目脚本”的 R 脚本文件中。

### Classification Forecasting Model #5: Multivariate Adaptive Regression Splines
library(earth)
library(plotmo)
library(plotrix)
marsGrid = expand.grid(.degree = 1:2, .nprune = 2:38)
set.seed(100)
marsModelR1 = train(x = data2014, y = pr_var2014, method = "earth", 
                    preProc = c("center", "scale"), tuneGrid = marsGrid)

# compare the expected classifications in 2015 to the observed classifications in 2015
marsR1Pred = predict(marsModelR1, newdata = data2015)
> dim(marsR1Pred)
[1] 4120    1

I am adding in the following here to show what I believe might be the source of the error message/warning.我在此处添加以下内容以显示我认为可能是错误消息/警告的来源。 > length(pr_var2014) 1 3808 > 长度(pr_var2014) 1 3808

marsR1_PR = postResample(pred = marsR1Pred, obs = pr_var2014)
> marsR1_PR
       RMSE    Rsquared         MAE 
         NA 1.24489e-06          NA

marsModelR1_CFM <- confusionMatrix(data = marsR1Pred, reference = pr_var2014,
                                   positive = "Increase")
> marsModelR1_CFM <- confusionMatrix(data = marsR1Pred, reference = pr_var2014,
+                                    positive = "Increase")
Error in confusionMatrix.matrix(data = marsR1Pred, reference = pr_var2014,  : 
  matrix must have equal dimensions

I have tried the following 2 attempted fixes already with the same result for both我已经尝试了以下 2 次尝试修复,两者的结果相同

> marsModelR1_CFM <- confusionMatrix(data = marsR1Pred, 
+                                    reference = sample(pr_var2014, length(marsR1Pred)),
+                                    positive = "Increase")
Error in confusionMatrix.matrix(data = marsR1Pred, reference = sample(pr_var2014,  : 
  matrix must have equal dimensions

> length(pr_var2014)
[1] 3808
> length(marsR1Pred)
[1] 4120
> marsModelR1_CFM <- confusionMatrix(data = marsR1Pred, 
+                                    reference = sample(pr_var2014, 4120),
+                                    positive = "Increase")
Error in confusionMatrix.matrix(data = marsR1Pred, reference = sample(pr_var2014,  : 
  matrix must have equal dimensions

Any suggestions would be much appreciated, Also, all of these commands are in the final section of my script.任何建议将不胜感激,此外,所有这些命令都在我的脚本的最后部分。 Part 4 at the bottom.第 4 部分在底部。

Make sure you are working with a factor for the data argument, not eg matrix .确保您使用的是数据参数的因子,而不是matrix

# this will fail with similar error to yours
confusionMatrix(as.matrix(iris$Species), sample(iris$Species))
# Error in confusionMatrix.matrix(as.matrix(iris$Species), sample(iris$Species)) : 
#  matrix must have equal dimensions

# this will pass as I convert the variable to be a factor variable
confusionMatrix(as.factor(as.matrix(iris$Species)), sample(iris$Species))

for your case:对于你的情况:

marsModelR1_CFM <- confusionMatrix(data = as.factor(marsR1Pred), 
                                   reference = sample(as.factor(pr_var2014), 4120),
                                   positive = "Increase")

暂无
暂无

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

相关问题 错误:矩阵必须具有相等的维度 - error : Matrix must have equal dimensions “错误:矩阵必须具有相等的维度”尽管看似相等的维度 - "Error: Matrix must have equal dimensions" despite seemingly equal dimensions ConfusionMatrix 中的误差数据和参考因子必须具有相同的级别数 - Error in ConfusionMatrix the data and reference factors must have the same number of levels 制作矩阵数字和名称顺序 - Making matrix numeric and name orders (R)solve()中的错误&#39;a&#39;必须是数字矩阵 - (R) Error in solve() 'a' must be a numeric matrix 热图返回错误:“ x”必须是数字矩阵,但x是数字矩阵 - Heatmap returning error: 'x' must be a numeric matrix, but x is a numeric matrix 混淆矩阵错误:数据和参考因素必须具有相同的水平数 - Error in Confusion Matrix : the data and reference factors must have the same number of levels 表中的错误(数据,参考,dnn = dnn,...):在 R 中运行带有插入符号的混淆矩阵时,所有 arguments 必须具有相同的长度 - Error in table(data, reference, dnn = dnn, …) : all arguments must have the same length when run confusionMatrix with caret, in R R数据框行名称显示矩阵而不是数值 - R data frame row name shows matrix instead of numeric value is.na(x)` 是一个矩阵,它必须与 R 中的输入错误具有相同的维度 - is.na(x)` is a matrix, it must have the same dimensions as the input-error in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM