【问题标题】:Using predict() and calculating manually in Logistic regression in R does not match. What is the reason?在 R 的逻辑回归中使用 predict() 和手动计算不匹配。是什么原因?
【发布时间】:2026-01-02 09:40:01
【问题描述】:

当我运行逻辑回归并使用 predict() 函数以及使用公式 p=1/(1+e^-(b0+b1*x1...)) 手动计算时,我无法得到相同的答案。可能是什么原因?

>test[1,]
      loan_status loan_Amount interest_rate period      sector    sex age grade
10000           0         608      41.72451     12 Online Shop Female  44    D3

部门和时期无关紧要,因此我将其从回归中删除。 glm 给出:

 Coefficients:
                Estimate Std. Error z value Pr(>|z|)    
(Intercept)   -1.1542256  0.7610472  -1.517  0.12936    
interest_rate -0.0479765  0.0043415 -11.051  < 2e-16 ***
sexMale       -0.8814945  0.0656296 -13.431  < 2e-16 ***
age           -0.0139100  0.0035193  -3.953 7.73e-05 ***
gradeB         0.3209587  0.8238955   0.390  0.69686    
gradeC1       -0.7113279  0.8728260  -0.815  0.41509    
gradeC2       -0.4730014  0.8427544  -0.561  0.57462    
gradeC3        0.0007541  0.7887911   0.001  0.99924    
gradeD1        0.5637668  0.7597531   0.742  0.45806    
gradeD2        1.3207785  0.7355950   1.796  0.07257 .  
gradeD3        0.9201400  0.7303779   1.260  0.20774    
gradeE1        1.7245351  0.7208260   2.392  0.01674 *  
gradeE2        2.1547773  0.7242669   2.975  0.00293 ** 
gradeE3        3.1163245  0.7142881   4.363 1.28e-05 ***
>predictions_1st <- predict(Final_Model, newdata = test[1,], type = "response")
>predictions_1st
answer: **0.05478904** 

但是当我这样计算时:

>prob_1 <- 1/(1+e^-((-0.0479764603)*41.72451)-0.0139099563*44)
>prob_1
answer: 0.09081154

我也用微不足道的系数计算,但答案仍然不一样。可能是什么原因?

【问题讨论】:

    标签: r glm predict


    【解决方案1】:

    您还有一个(Intercept) -1.1542256 和一个gradeD3 0.9201400

    1/(1+exp(-1*(-1.1542256 -0.0479764603*41.72451 -0.0139099563*44 + 0.9201400)))
    #[1] 0.05478904
    

    【讨论】:

    • 我也做了那个计算,但是我用同一​​个变量来计算很多案例,它给了我不同的答案。如果我创建新变量,那么它会更正它(我仍然不知道它为什么这样做)。无论如何,非常感谢。
    • @Tsotne 也许其他数据线有不同的grade。这仅适用于D3