带有混淆矩阵的有序Logit模型

时间:2015-11-04 06:52:50

标签: r

我需要R代码,它可以执行有序的logit模型,也可以给出混淆矩阵。(对验证部分有帮助。)

我有6个分类因变量,所以我想要这个模型。

1 个答案:

答案 0 :(得分:2)

从UCI机器学习库下载数据集葡萄酒质量

wine <- read.csv("winequality-white.csv", sep = ";")
wine$quality <- factor(ifelse(wine$quality < 5, 0,ifelse(wine$quality > 6, 2, 1)))
set.seed(7644)
wine_sampling_vector <- createDataPartition(wine$quality, p =
0.80, list = FALSE)
wine_train <- wine[wine_sampling_vector,]
wine_test <- wine[-wine_sampling_vector,]

您可以将以下代码用于有序响应变量

library(MASS)
wine_model <- polr(quality ~ ., data = wine_train, Hess = T)
summary(wine_model)
wine_predictions <- predict(wine_model, wine_train)

mean(wine_predictions == wine_train$quality)

用于计算混淆矩阵

table(predicted = wine_predictions,actual = wine_train$quality)