问题描述
我已经安装了逻辑回归模型,该模型可以根据mpg
(mtcars
数据集)预测二进制结果vs
.该图如下所示.如何确定任何特定vs
值的mpg
值?例如,我有兴趣找出vs
的概率为0.50时mpg
的值是什么.感谢任何人都可以提供的帮助!
I've fitted a logistic regression model that predicts the a binary outcome vs
from mpg
(mtcars
dataset). The plot is shown below. How can I determine the mpg
value for any particular vs
value? For example, I'm interested in finding out what the mpg
value is when the probability of vs
is 0.50. Appreciate any help anyone can provide!
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()
函数.然后,您可以使用数值求解器查找特定的截距.例如
The easiest way to calculate predicted values from your model is with the predict()
function. Then you can use a numerical solver to find particular intercepts. For example
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的函数以找到您的解决方案.
Here findInt
just takes the model and a particular target value and returns a function that uniroot
can solve for 0 to find your solution.
这篇关于R中的回归(逻辑):查找特定y值的x值(预测变量)(结果)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!