【问题标题】:Error in table: all arguments must have the same length表中的错误:所有参数必须具有相同的长度
【发布时间】:2021-07-12 00:16:21
【问题描述】:

我正在尝试使用 naiveBayes 对具有二进制(位置 = 3 或不)结果的数据进行拟合和分类,并通过重采样评估其在测试数据上的性能。

我收到一个错误代码: 表中的错误(train$prog,trainPred): 所有参数的长度必须相同

这是我的可重现代码:

wifiDat <- read.table("wifi_localization.txt",sep="\t")
colnames(wifiDat) 
dim(wifiDat)
summary(wifiDat)
head(wifiDat)
colnames(wifiDat) <- c("WF1", "WF2", "WF3", "WF4", "WF5", "WF6",  "WF7", "RM")
wifiDat$RM <- factor(wifiDat$RM)
library(e1071)
wifiDat$RM <- factor(wifiDat$RM)
set.seed(1)
row.number = sample(1:nrow(wifiDat), 0.25*nrow(wifiDat))
train = wifiDat[row.number,]
test = wifiDat[-row.number,]
NBclassfier=naiveBayes(RM~WF1+WF2+WF3+WF4+WF5+WF6+WF7, data=train)
print(NBclassfier)
printALL=function(model){
trainPred=predict(model, newdata = train, type = "class")
trainTable=table(train$prog, trainPred)
testPred=predict(NBclassfier, newdata=test, type="class")
testTable=table(test$prog, testPred)
trainAcc=(trainTable[1,1]+trainTable[2,2]+trainTable[3,3])/sum(trainTable)
testAcc=(testTable[1,1]+testTable[2,2]+testTable[3,3])/sum(testTable)
message("Contingency Table for Training Data")
print(trainTable)
message("Contingency Table for Test Data")
print(testTable)
message("Accuracy")
print(round(cbind(trainAccuracy=trainAcc, testAccuracy=testAcc),3))
}
printALL(NBclassfier)

这是我的示例数据

V1. V2. V3. V4. V5. V6. V7. V8
-64 -56 -61 -66 -71 -82 -81 1
-68 -57 -61 -65 -71 -85 -85 2
-63 -60 -60 -67 -76 -85 -84 3
-61 -60 -68 -62 -77 -90 -80 4
-63 -65 -60 -63 -77 -81 -87 1
-64 -55 -63 -66 -76 -88 -83 1
-65 -61 -65 -67 -69 -87 -84 2
-61 -63 -58 -66 -74 -87 -82 3
-65 -60 -59 -63 -76 -86 -82 4
-62 -60 -66 -68 -80 -86 -91 1
-67 -61 -62 -67 -77 -83 -91 1
-65 -59 -61 -67 -72 -86 -81 1
-63 -57 -61 -65 -73 -84 -84 1

请问我该如何解决这个错误?

【问题讨论】:

  • 嗨,吉米!您能否更新您的代码以包含创建对象wifiDat 的命令?这会让其他人更容易帮助你。
  • 。谢谢你完成@stragu

标签: r


【解决方案1】:

如果您查看错误的回溯,您应该会发现问题出在函数的 table(train$prog, trainPred) 部分。 base::table() 函数要求您为 ... 参数提供相同长度的对象,如

单步执行您的自定义函数,您会看到trainPred 的长度为3,而train$prog 不存在(并且train 的所有列的长度无论如何都是10)。因此出现错误。

你是否在某个地方漏掉了一步?

【讨论】:

    猜你喜欢
    • 2017-06-22
    • 2019-05-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    相关资源
    最近更新 更多