R中的回归(逻辑):查找特定y值的x值(预测变量)(结果) [英] Regression (logistic) in R: Finding x value (predictor) for a particular y value (outcome)

查看:1146
本文介绍了R中的回归(逻辑):查找特定y值的x值(预测变量)(结果)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了逻辑回归模型,该模型可以根据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值(预测变量)(结果)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆