【发布时间】:2021-04-12 16:50:15
【问题描述】:
我试图分析 caret 包提供的示例,用于混淆矩阵,即
lvs <- c("normal", "abnormal")
truth <- factor(rep(lvs, times = c(86, 258)),
levels = rev(lvs))
pred <- factor(
c(
rep(lvs, times = c(54, 32)),
rep(lvs, times = c(27, 231))),
levels = rev(lvs))
xtab <- table(pred, truth)
confusionMatrix(xtab)
但可以肯定的是,我不太了解它。让我们以这个非常简单的模型为例:
set.seed(42)
x <- sample(0:1, 100, T)
y <- rnorm(100)
glm(x ~ y, family = binomial('logit'))
而且我不知道如何为这个 glm 模型类似地执行混淆矩阵。你知道怎么做吗?
编辑
我尝试运行 cmets 中提供的示例:
train <- data.frame(LoanStatus_B = as.numeric(rnorm(100)>0.5), b= rnorm(100), c = rnorm(100), d = rnorm(100))
logitMod <- glm(LoanStatus_B ~ ., data=train, family=binomial(link="logit"))
library(caret)
# Use your model to make predictions, in this example newdata = training set, but replace with your test set
pdata <- predict(logitMod, newdata = train, type = "response")
confusionMatrix(data = as.numeric(pdata>0.5), reference = train$LoanStatus_B)
但我得到错误:dataandreference`应该是具有相同水平的因素
我做错了吗?
【问题讨论】:
-
可能是this帮助
标签: r regression glm confusion-matrix