Regression (logistic) in R: Finding x value (predictor) for a particular y value (outcome)

杀马特。学长 韩版系。学妹 提交于 2019-11-30 19:39:16

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

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.

You can solve for mpg directly as follows:

mpg = (log(p/(1-p)) - coef(model)[1])/coef(model)[2]

Detailed explanation:

When you fit the regression model, the equation you are fitting is the following:

log(p/(1-p)) = a + b*mpg

Where p is the probability that vs=1, a is the intercept and b is the coefficient of mpg. From the model fit results (just type model or summary(model)) we see that a = -8.8331 and b = 0.4304. We want to find mpg when p=0.5. So, the equation we need to solve is:

log(0.5/(1-0.5)) = -8.331 + 0.4304*mpg
log(1) = 0 = -8.331 + 0.4303*mpg

Rearranging,

mpg = 8.8331/0.4304 = 20.523

In general, to solve for mpg for any value of p:

mpg = (log(p/(1-p)) + 8.8331)/0.4304

Or, to make it more easily reproducible:

mpg = (log(p/(1-p)) - coef(model)[1])/coef(model)[2]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!