r语言 - 在逻辑回归中查找给定概率的协变量值



我有模型

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的值,这将导致 50% 的概率am .

我还没有找到任何可以用来输出此类预测的东西。我已经设法使用

#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函数等效的?

有趣的问题!

下面的解决方案怎么样?基本上,创建目标变量正在对其观测值范围进行采样的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']

最新更新