การตีความความพอดีของโมเดล

Generalized Linear Models ใน Python

Ita Cirovic Donev

Data Science Consultant

การประมาณค่าพารามิเตอร์

  • Maximum likelihood estimation (MLE)
  • Iteratively reweighted least squares (IRLS)
Generalized Linear Models ใน Python

ฟังก์ชันตอบสนอง

  • โมเดล Poisson regression $$ log(\lambda)=\beta_0+\beta_1x_1 $$

  • ฟังก์ชันตอบสนอง: $$ \lambda=exp(\beta_0 + \beta_1x_1) $$                                                  or $$ \lambda=exp(\beta_0) \times exp(\beta_1x_1) $$

Generalized Linear Models ใน Python

ฟังก์ชันตอบสนอง

  • โมเดล Poisson regression $$ log(\lambda)=\beta_0+\beta_1x_1 $$

  • ฟังก์ชันตอบสนอง: $$ \lambda=exp(\beta_0 + \beta_1x_1) $$                                                  or $$ \lambda=exp(\beta_0) \color{red}{\times} exp(\beta_1x_1) $$

Generalized Linear Models ใน Python

การตีความพารามิเตอร์

  • $exp(\beta_0)$

    • ผลต่อค่าเฉลี่ย $\lambda$ เมื่อ $x=0$
  • $exp(\beta_1)$

    • ผลเชิงการคูณ ต่อค่าเฉลี่ย $\lambda$ เมื่อ $x$ เพิ่มขึ้น 1 หน่วย
Generalized Linear Models ใน Python

การตีความผลของสัมประสิทธิ์

  • ถ้า $\color{#FF550D}{\beta_1 > 0}$
    • $exp(\beta_1)>1$
    • $\lambda$ มีค่า $\color{#FF550D}{exp(\beta_1)\text{ เท่า}}$ มากกว่าเมื่อ $x=0$
  • ถ้า $\color{#D04A73}{\beta<0}$
    • $exp(\beta_1)<1$
    • $\lambda$ มีค่า $\color{#D04A73}{exp(\beta_1) \text{ เท่า}}$ น้อยกว่าเมื่อ $x=0$
  • ถ้า $\color{#0099FF}{\beta_1 = 0}$
    • $exp(\beta_1)=1$
    • $\lambda=exp(\beta_0)$
    • ตัวคูณเท่ากับ 1
    • $y$ และ $x$ $\text{\color{#0099FF}{ไม่มีความสัมพันธ์กัน}}$
Generalized Linear Models ใน Python

ตัวอย่าง

model = glm('sat ~ weight', data = crab, 
            family = sm.families.Poisson()).fit()
                 Generalized Linear Model Regression Results (print cut)                 
=============================================================================
                 coef    std err          z      P>|z|      [0.025     0.975]
-----------------------------------------------------------------------------
Intercept     -0.4284      0.179     -2.394      0.017      -0.779     -0.078
weight         0.5893      0.065      9.064      0.000       0.462      0.717
=============================================================================
Generalized Linear Models ใน Python

ตัวอย่าง — การตีความค่า beta

  • ดึงค่าสัมประสิทธิ์ของโมเดล
    model.params
    
Intercept   -0.428405
weight       0.589304
  • คำนวณผลกระทบ
    np.exp(0.589304)
    
1.803
Generalized Linear Models ใน Python

ช่วงความเชื่อมั่นสำหรับ ...

  • $\beta_1$
print(model.conf_int())
                  0         1
Intercept -0.779112 -0.077699
weight     0.461873  0.716735
  • ผลเชิงการคูณต่อค่าเฉลี่ย
print(np.exp(crab_fit.conf_int()))
                  0         1
Intercept  0.458813  0.925243
weight     1.587044  2.047737
Generalized Linear Models ใน Python

มาฝึกกันเถอะ!

Generalized Linear Models ใน Python

Preparing Video For Download...