我有一些带有预测因子和二元目标的数据。例如:
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”。这是一个正确的假设吗?即使每个类别的人口可能不相等(或接近相等)?或者,有没有办法估计这个门槛?
发布于 2014-04-23 17:54:34
在glm模型中使用的最佳阈值(或截断点)是使特异性和敏感性最大化的点。这个临界点可能不会在你的模型中给出最高的预测,但它不会偏向积极或消极。ROCR包中包含可以帮助您完成此操作的函数。检查此包中的performance()函数。它会帮你找到你想要的东西。这是一张你期望得到的图片:

在找到截止点之后,我通常自己编写一个函数来查找预测值高于截止点的数据点的数量,并将其与它们所属的组进行匹配。
发布于 2014-04-23 17:39:21
确定好的模型参数的黄金标准是cross-validation.,包括逻辑回归的“我应该设置什么阈值
一般的想法是支持训练集的一个或多个部分,并选择阈值,以最大化该支持集上的正确分类数量,但Wikipedia可以为您提供更多详细信息。
发布于 2018-12-04 04:44:19
尝试复制第一个图的工具。给定一个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")

https://stackoverflow.com/questions/23240182
复制相似问题