由于您没有提供数据,我尝试复制它,这是我遵循的过程:
复制数据:
Level <- c("strongly disagree", "disagree", "no answer", "agree", "strongly agree")
Question5 <- c("strongly agree", "agree", "no answer", "disagree", "strongly disagree", "disagree", "no answer", "agree", "strongly disagree")
Question5 <- factor(Question5, levels=Level, ordered=T)
train <- data.frame(a=c(2,3,5,1,2,1,4,1,4), b=c(4,1,3,2,5,3,4,1,2), Question5)
Question5 <- c("strongly disagree", "no answer", "agree", "strongly disagree", "disagree", "strongly agree", "no answer", "disagree", "strongly agree")
Question5 <- factor(Question5, levels=Level, ordered=T)
test <- data.frame(a=c(4,3,5,2,1,3,4,2,5), b=c(5,2,3,1,4,3,2,4,1), Question5)
应用随机森林:
> library(randomForest)
> rf1 <- randomForest(Question5~., data=train, ntree=500)
> p1 <- predict(rf1, test, type='response')
> table(p1, test$Question5)
p1 strongly disagree disagree no answer agree strongly agree
strongly disagree 0 0 2 0 1
disagree 0 1 0 0 0
no answer 1 0 0 1 0
agree 1 0 0 0 1
strongly agree 0 1 0 0 0
当您对数据执行此过程时,您应该会得到一个类似于我上面得到的表格。当你把这张表的对角线元素相加时,你会得到正确预测的总数(在上面的例子中是 9 个中的 1 个)。