【问题标题】:Confusion matrix for multinomial logistic regression & ordered logit多项逻辑回归和有序 logit 的混淆矩阵
【发布时间】:2018-07-09 06:57:43
【问题描述】:

我想为多项逻辑回归和比例优势模型创建混淆矩阵,但我坚持使用 R 中的实现。我在下面的尝试似乎没有给出所需的输出。

这是我目前的代码:

CH <- read.table("http://data.princeton.edu/wws509/datasets/copen.dat", header=TRUE)
CH$housing <- factor(CH$housing)
CH$influence <- factor(CH$influence)
CH$satisfaction <- factor(CH$satisfaction)
CH$contact <- factor(CH$contact)
CH$satisfaction <- factor(CH$satisfaction,levels=c("low","medium","high"))
CH$housing <- factor(CH$housing,levels=c("tower","apartments","atrium","terraced"))
CH$influence <- factor(CH$influence,levels=c("low","medium","high"))
CH$contact <- relevel(CH$contact,ref=2)
model <- multinom(satisfaction ~ housing + influence + contact, weights=n, data=CH)
summary(model)
preds <- predict(model)
table(preds,CH$satisfaction)

omodel <- polr(satisfaction ~ housing + influence + contact, weights=n, data=CH, Hess=TRUE)
preds2 <- predict(omodel)
table(preds2,CH$satisfaction)

我非常感谢一些关于如何为我的 2 个模型正确生成混淆矩阵的建议!

【问题讨论】:

  • table(preds,CH$satisfaction) 为您提供混淆矩阵。如果您想为您的预测提供更多统计信息,您可以使用 caret 包中的 confusionMatrix 函数。
  • 我相信 table(preds,CH$satisfaction) 很遗憾没有考虑权重。所以总数只是行数,而不是总观察数。有没有办法合并权重?
  • 那么也许你可以重塑你的数据集而不是权重列,以将 n 作为行数。在这种情况下,每一行都是一个观察结果,而不是观察结果的集合。您可以像这样创建重构的数据集:CH %&gt;% rowwise() %&gt;% mutate(id = list(seq(1:n))) %&gt;% unnest(id) %&gt;% select(-n) 并使用它构建模型。

标签: r statistics logistic-regression confusion-matrix multinomial


【解决方案1】:

你可以参考—— Predict() - Maybe I'm not understanding it

在 predict() 中,您需要传递看不见的数据进行预测。

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 2019-05-26
    • 2020-07-08
    • 2021-11-30
    • 2021-09-03
    • 2018-04-20
    • 2018-11-19
    • 2018-10-31
    • 2021-03-15
    相关资源
    最近更新 更多