• 主页
  • xj[i]中出错:在R中执行LDA时,下标类型list‘无效

xj[i]中出错:在R中执行LDA时,下标类型list‘无效

mydat=structure(list(spent = c(73.5, 73.5, 73.5, 73.5, 73.5, 73.5, 
73.5, 73.5, 73.5, 73.5, 73.5, 29.74, 29.74, 29.74, 29.74, 29.74, 
29.74, 29.74, 29.74, 29.74, 29.74, 29.74, 73.71, 73.71, 73.71, 
73.71, 73.71, 73.71, 73.5, 73.5, 73.5, 73.5, 73.5, 73.5, 73.5, 
73.5, 73.5, 73.5, 73.5, 29.74, 29.74, 29.74, 29.74, 29.74, 29.74, 
29.74, 29.74, 29.74, 29.74, 29.74, 73.71, 73.71, 73.71, 73.71, 
73.71, 73.71), realpurchase_cash = c(501, 502, 503, 504, 505, 
506, 507, 508, 509, 510, 511, 512, 513, 514, 515, 516, 517, 518, 
519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 73.5, 73.5, 
73.5, 73.5, 73.5, 73.5, 73.5, 73.5, 73.5, 73.5, 73.5, 29.74, 
29.74, 29.74, 29.74, 29.74, 29.74, 29.74, 29.74, 29.74, 29.74, 
29.74, 73.71, 73.71, 73.71, 73.71, 73.71, 73.71), id = c(123L, 
123L, 123L, 123L, 123L, 123L, 123L, 123L, 123L, 123L, 123L, 123L, 
123L, 123L, 123L, 123L, 123L, 123L, 123L, 123L, 123L, 123L, 123L, 
123L, 123L, 123L, 123L, 123L, 124L, 124L, 124L, 124L, 124L, 124L, 
124L, 124L, 124L, 124L, 124L, 124L, 124L, 124L, 124L, 124L, 124L, 
124L, 124L, 124L, 124L, 124L, 124L, 124L, 124L, 124L, 124L, 124L
), flag = c(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, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("spent", 
"realpurchase_cash", "id", "flag"), class = "data.frame", row.names = c(NA, 
-56L))

我想执行线性判别分析。(lda函数来自于MASS库)

下面是我的代码

index <- sample(1:nrow(mydat),round(0.70*nrow(mydat)))
train <- mydat[index,]
test <- mydat[-index,]
library("MASS")
fit=lda(flag~spent+realpurchase_cash, mydat, subset = train)
library("caret") 
str(mydat)
library(InformationValue)
predicted <- predict(fit,test,type='response')
optCutOff <- optimalCutoff(test$flag, predicted)[1] 
confusionMatrix(test$flag, predicted, threshold = optCutOff)

但是在这个字符串之后

fit=lda(flag~spent+realpurchase_cash, mydat, subset = train)

我得到了错误

Error in xj[i] : invalid subscript type 'list'

如何修复此错误。怎么了?

转载请注明出处:http://www.cxd6.com/article/20230527/2587757.html