Regression (logistic) in R: Finding x value (predictor) for a particular y value (outcome) -
i've fitted logistic regression model predicts binary outcome vs
mpg
(mtcars
dataset). plot shown below. how can determine mpg
value particular vs
value? example, i'm interested in finding out mpg
value when probability of vs
0.50. appreciate 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)
the easiest way calculate predicted values model predict()
function. can use numerical solver find particular intercepts. 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
takes model , particular target value , returns function uniroot
can solve 0 find solution.
Comments
Post a Comment