预测响应与手动逻辑回归概率

时间:2021-06-09 15:06:26

标签: r statistics

我正在尝试使用逻辑回归模型手动计算给定 x 的概率。

我的模型看起来像这样 fit2 <- glm(ability~stability, data = df2)

我创建了一个给我响应的函数:

estimator <- function(x){
  predict(fit2, type = "response", newdata = data.frame(stability=x))
}

这个函数给了我 0.5304603 的值 x=550

然后我创建手动版本。为此,我使用函数 p = e^(B0+B1*x)/(1 + e^(B0+B1*x))

所以我们的代码看起来像这样

est <- function(par, x){
  x = c(1,x)
  exp(par%*%x)/(1+exp(par%*%x))
}

其中 par = fit2$coefficients, x = 550

但此代码返回 0.6295905

为什么?

编辑:

summary(fit2):
Call:
glm(formula = ability ~ stability, data = df2)

Deviance Residuals: 
     Min        1Q    Median        3Q       Max  
-0.78165  -0.33738   0.09462   0.31582   0.72823  

Coefficients:
             Estimate Std. Error t value Pr(>|t|)   
(Intercept) -1.531574   0.677545   -2.26  0.03275 * 
stability    0.003749   0.001229    3.05  0.00535 **
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for gaussian family taken to be 0.1965073)

    Null deviance: 6.7407  on 26  degrees of freedom
Residual deviance: 4.9127  on 25  degrees of freedom
AIC: 36.614

Number of Fisher Scoring iterations: 2

0 个答案:

没有答案