Regression (logistics) in R: searching for x (predictor) for a specific y (result)

I installed a logistic regression model that predicts the binary result vs from mpg ( mtcars dataset). The plot is shown below. How to determine mpg value for any vs value? For example, I am interested in knowing what mpg means 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) 

enter image description here

+4
source share
2 answers

The easiest way to calculate predicted values ​​from your model is with the predict() function. You can then use the numerical solver to search for specific intercepts. for instance

 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 simply takes the model and the specific target value and returns a function that uniroot can solve for 0 to find your solution.

+7
source

You can solve for mpg directly as follows:

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

Detailed explanation:

When you approach the regression model, you should consider the following equation:

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

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

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

Reordering

 mpg = 8.8331/0.4304 = 20.523 

In general, to solve mpg for any p value:

 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] 
+1
source

All Articles