在逻辑回归中给出概率的协变量值

时间:2017-04-06 08:48:01

标签: r logistic-regression glm

我有模特

am.glm = glm(formula=am ~ hp + I(mpg^2), data=mtcars, family=binomial)

给出了

> summary(am.glm)

Call:
glm(formula = am ~ hp + I(mpg^2), family = binomial, data = mtcars)

Deviance Residuals: 
    Min       1Q   Median       3Q      Max  
-1.5871  -0.5376  -0.1128   0.1101   1.6937  

Coefficients:
             Estimate Std. Error z value Pr(>|z|)  
(Intercept) -18.71428    8.45330  -2.214   0.0268 *
hp            0.04689    0.02367   1.981   0.0476 *
I(mpg^2)      0.02811    0.01273   2.207   0.0273 *
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 43.230  on 31  degrees of freedom
Residual deviance: 20.385  on 29  degrees of freedom
AIC: 26.385

Number of Fisher Scoring iterations: 7

给定值hp我希望找到mpg的值,这将导致am的概率为50%。

我没有找到任何可用于输出此类预测的内容。我已设法使用

编写代码
#Coefficients
glm.intercept<-as.numeric(coef(am.glm)[1])
glm.hp.beta<-as.numeric(coef(am.glm)[2])
glm.mpg.sq.beta<-as.numeric(coef(am.glm)[3])
glm.hp.mpg.beta<-as.numeric(coef(am.glm)[4])

#Constants
prob=0.9
c<-log(prob/(1-prob))
hp=120

polyroot(c((glm.hp.beta*hp)+glm.intercept-c, glm.hp.mpg.beta*hp,glm.mpg.sq.beta))

有更优雅的解决方案吗?也许predict函数等价?

1 个答案:

答案 0 :(得分:1)

有趣的问题!

下面的解决方案怎么样?基本上,创建目标变量为观察值范围采样的newdata。预测这些值的向量,并找到符合条件的最小值

# Your desired threshold
prob = 0.5

# Create a sampling vector
df_new <- data.frame(
  hp = rep(120, times = 100),
  mpg = seq(from = range(mtcars$mpg)[1], 
            to = range(mtcars$mpg)[2], 
            length.out = 100))

# Predict on sampling vector
df_new$am <- predict(am.glm, newdata = df_new)
# Find lowest value above threshold
df_new[min(which(df_new$am > prob)), 'mpg']