使用ROC曲线为我在R中的加权二元logistic回归(glm)找到最佳截止点

问题描述 投票:1回答:1

我已经为Rstudio中的客户流失预测建立了二进制逻辑回归。由于此模型使用的数据不平衡,因此我也包括了权重。然后,我尝试通过反复尝试找到最佳截止点,但是,为了完成我的研究,我必须结合ROC曲线来找到最佳截止点。下面,我提供了用于构建模型的脚本(fit2)。重量存储在“ W”中。这表明,错误地识别一个搅局者的成本是错误地识别一个非搅局者的成本的14倍。

#CH1 logistic regression

library(caret)
W = 14
lvl = levels(trainingset$CH1)
print(lvl)
#if positive we give it the defined weight, otherwise set it to 1
fit_wts = ifelse(trainingset$CH1==lvl[2],W,1)
fit2 = glm(CH1 ~ RET + ORD + LVB + REVA + OPEN + REV2KF + CAL + PSIZEF + COM_P_C + PEN + SHOP, data = trainingset, weight=fit_wts, family=binomial(link='logit'))
# we test it on the test set
predlog1 = ifelse(predict(fit2,testset,type="response")>0.5,lvl[2],lvl[1])
predlog1 = factor(predlog1,levels=lvl)
predlog1
confusionMatrix(pred,testset$CH1,positive=lvl[2])

为了进行这项研究,我还使用pROC软件包为决策树构建了ROC曲线。但是,当然,对于Logistic回归,相同的脚本无法正常工作。我已使用以下脚本为逻辑回归创建了ROC曲线。

prob=predict(fit2, testset, type=c("response"))
testset$prob=prob
library(pROC)
g <- roc(CH1 ~ prob, data = testset, )
g
plot(g)

导致下面的ROC曲线。

enter image description here

如何从该ROC曲线获得最佳截止?

r logistic-regression glm roc proc-r-package
1个回答
0
投票

获得“最佳”截止值完全独立于模型类型,因此您可以像使用pROC的任何其他类型的模型一样获得它。使用coords功能:

 coords(g, "best", transpose = FALSE)

或直接在地块上:

plot(g, print.thres=TRUE)

现在以上内容仅使灵敏度和特异性之和最大化。这通常太简单了,您可能需要一个明确的“最佳”定义,以适合您的用例。这基本上超出了此问题的范围,但是作为起点,您应该查看Best Thresholds section of the documentation of the coords function中的一些基本选项。

© www.soinside.com 2019 - 2024. All rights reserved.