我已经安装了逻辑回归模型,该模型可以根据vs
(mpg
数据集)预测二进制结果mtcars
。该图如下所示。如何确定任何特定mpg
值的vs
值?例如,当mpg
的概率为0.50时,我有兴趣找出vs
的值是多少。感谢任何人都能提供的帮助!
model <- glm(vs ~ mpg, data = mtcars, family = binomial)
ggplot(mtcars, aes(mpg, vs)) +
geom_point() +
stat_smooth(method = "glm", method.args = list(family = "binomial"), se = FALSE)
最佳答案
从模型计算预测值的最简单方法是使用predict()
函数。然后,您可以使用数值求解器查找特定的截距。例如
findInt <- function(model, value) {
function(x) {
predict(model, data.frame(mpg=x), type="response") - value
}
}
uniroot(findInt(model, .5), range(mtcars$mpg))$root
# [1] 20.52229
在这里,
findInt
仅获取模型和特定目标值,并返回uniroot
可以将其求解为0的函数以找到您的解决方案。
关于R : Finding x value (predictor) for a particular y value (outcome)中的回归(物流),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32040504/