R中glm逻辑回归模型的决定阈值

问题描述 投票:0回答:7

我有一些带有预测变量和二元目标的数据。例如:

df <- data.frame(a=sort(sample(1:100,30)), b= sort(sample(1:100,30)), 
                 target=c(rep(0,11),rep(1,4),rep(0,4),rep(1,11)))

我使用

glm()

训练了逻辑回归模型
model1 <- glm(formula= target ~ a + b, data=df, family=binomial)

现在我正在尝试预测输出(例如,相同的数据就足够了)

predict(model1, newdata=df, type="response")

这会生成概率数向量。但我想预测实际的课程。我可以对概率数字使用 round(),但这假设低于 0.5 的任何内容都是“0”类,高于 0.5 的任何内容都是“1”类。这是一个正确的假设吗?即使每个阶层的人口可能不相等(或接近相等)?或者有没有办法估计这个阈值?

r glm predict logistic-regression
7个回答
8
投票

glm 模型中使用的最佳阈值(或截止)点是使特异性和灵敏度最大化的点。该阈值点可能不会在模型中给出最高的预测,但它不会偏向正数或负数。

ROCR
包包含可以帮助您执行此操作的函数。检查这个包中的
performance()
功能。它会给你你正在寻找的东西。这是您期望得到的图片:

enter image description here

找到截止点后,我通常会自己编写一个函数来查找预测值高于截止点的数据点的数量,并将其与它们所属的组进行匹配。


5
投票

确定良好模型参数(包括逻辑回归的“我应该设置什么阈值”)的黄金标准是交叉验证

总体思路是保留训练集的一个或多个部分,并选择最大化该保留集上正确分类数量的阈值,但是维基百科可以为您提供更多详细信息。


4
投票

围绕尝试复制第一张图进行工具化。给定一个

predictions <- prediction(pred,labels)
对象,那么:

baseR 方法

plot(unlist(performance(predictions, "sens")@x.values), unlist(performance(predictions, "sens")@y.values), 
     type="l", lwd=2, ylab="Specificity", xlab="Cutoff")
par(new=TRUE)
plot(unlist(performance(predictions, "spec")@x.values), unlist(performance(predictions, "spec")@y.values), 
     type="l", lwd=2, col='red', ylab="", xlab="")
axis(4, at=seq(0,1,0.2),labels=z)
mtext("Specificity",side=4, padj=-2, col='red')

ggplot2 方法

sens <- data.frame(x=unlist(performance(predictions, "sens")@x.values), 
                   y=unlist(performance(predictions, "sens")@y.values))
spec <- data.frame(x=unlist(performance(predictions, "spec")@x.values), 
                   y=unlist(performance(predictions, "spec")@y.values))

sens %>% ggplot(aes(x,y)) + 
  geom_line() + 
  geom_line(data=spec, aes(x,y,col="red")) +
  scale_y_continuous(sec.axis = sec_axis(~., name = "Specificity")) +
  labs(x='Cutoff', y="Sensitivity") +
  theme(axis.title.y.right = element_text(colour = "red"), legend.position="none") 


2
投票

要以编程方式获取数据中具有最接近的灵敏度和特异性值(即上图中的交叉)的阈值,您可以使用这段非常接近的代码:

predictions = prediction(PREDS, LABELS)

sens = cbind(unlist(performance(predictions, "sens")@x.values), unlist(performance(predictions, "sens")@y.values))
spec = cbind(unlist(performance(predictions, "spec")@x.values), unlist(performance(predictions, "spec")@y.values))
sens[which.min(apply(sens, 1, function(x) min(colSums(abs(t(spec) - x))))), 1]

1
投票

PresenceAbsence::optimal.thresholds
包的
PresenceAbsence
函数中实现了12个方法。

Freeman, E. A. 和 Moisen, G. G. (2008) 也对此进行了介绍。就预测患病率和 kappa 而言,二元分类阈值标准的性能比较。生态建模,217(1-2), 48-58.


0
投票

predict.glm
只有
type = c("link", "response", "terms")
,而
"response"
是预测的类别概率,几乎总是你想要的。逻辑回归的标准是使用 0.5 作为阈值,作为 argmax 条件类概率,argmax_y P(Y_train=y | X_train=x) 是贝叶斯(最优)分类器。防风草的
predict()
代表
logistic_reg()
默认执行此操作。条件概率与输入数据中响应变量的比例不同,它是边际 P(Y_train = y),不依赖于 X_train。

但是,您不必使用 0.5 作为阈值,因为您可以根据类预测中的任何性能指标来调整阈值,例如最大化 F1 分数或平衡精度/召回率。


-3
投票

您可以尝试以下方法:

perfspec <- performance(prediction.obj = pred, measure="spec", x.measure="cutoff")

plot(perfspec)

par(new=TRUE)

perfsens <- performance(prediction.obj = pred, measure="sens", x.measure="cutoff")

plot(perfsens)
© www.soinside.com 2019 - 2024. All rights reserved.