我有一个数据框,有395个观察值和36个变量。我正在进行交叉验证,以选择最佳的几个变量来对学生进行分类。我写了这个代码:LDA交叉验证和变量选择
k<-5
error <- c()
for(l in 1:35){
if(l!=31 && l!=32 && l!=33){
x<-0
for (i in 1:k){
train<-rep(TRUE, dim(student.mat)[1])
for(j in 1:dim(student.mat)[1]/k){
train[(i-1)*dim(student.mat)[1]/k+j]<-FALSE
}
test=!train
student.test=student.mat[test,]
student.train=student.mat[train,]
nota3.test=nota3[test]
lda.fit<-lda(nota3~student.mat[,i], data=student.mat, subset=train)
lda.pred<-predict(lda.fit, student.test)
table(lda.pred$class, nota3.test)
y<-mean(lda.pred$class!=nota3.test)
x<-x+y
#cat("k = ", i, "error: ", y*100,"%", "\n")
}
#cat("Media del error = ", x/k*100,"%", "\n")
error <- c(error, x/k)
}else{
error <- c(error, 100)
}
}
error
names(student.mat)[which.max(error)]
,我得到这个错误:
错误表(lda.pred $类,nota3.test): 所有参数必须具有相同的长度 另外:丢失的警告消息 “newdata”有79行,但发现变量具有395行
,但如果我写的数据集,而不是student.mat[,i]
的一个变量的名称,它的工作原理。 lda函数没有正确读取student.mat[,i]
。
人们似乎对这个不久前涉及[R LDA问题,疹子。有没有一种课程可以在分类中分配问题? –
它是我的大学学位 – ajncespedes