การถดถอยโลจิสติกแบบหลายตัวแปร

Generalized Linear Models ใน Python

Ita Cirovic Donev

Data Science Consultant

การตั้งค่าแบบหลายตัวแปร

  • สูตรโมเดล $$ \text{logit}(y) = \beta_0+\beta_1\color{red}{x_1} $$
Generalized Linear Models ใน Python

การตั้งค่าแบบหลายตัวแปร

  • สูตรโมเดล $$ \text{logit}(y) = \color{blue}{\beta_0}+\color{blue}{\beta_1}\color{red}{x_1} $$
Generalized Linear Models ใน Python

การตั้งค่าแบบหลายตัวแปร

  • สูตรโมเดล $$ \text{logit}(y) = \beta_0+\beta_1x_1 + \beta_2\color{red}{x_2} + ... + \beta_p \color{red}{x_p} $$
Generalized Linear Models ใน Python

การตั้งค่าแบบหลายตัวแปร

  • สูตรโมเดล $$ \text{logit}(y) = \beta_0+\beta_1x_1 + \color{blue}{\beta_2}\color{red}{x_2} + ... + \color{blue}{\beta_p}\color{red}{x_p} $$

  • ใน Python

    model = glm('y ~ x1 + x2 + x3 + x4', 
              data = my_data, 
              family = sm.families.Binomial()).fit()
    
Generalized Linear Models ใน Python

ตัวอย่าง - การเปลี่ยนบ่อน้ำ

formula = 'switch ~ distance100 + arsenic'
wells_fit = glm(formula = formula, data = wells, 
                family = sm.families.Binomial()).fit()
===============================================================================
                  coef    std err          z      P>|z|      [0.025      0.975]
-------------------------------------------------------------------------------
Intercept       0.0027      0.079      0.035      0.972      -0.153       0.158
distance100    -0.8966      0.104     -8.593      0.000      -1.101      -0.692
arsenic         0.4608      0.041     11.134      0.000       0.380       0.542
===============================================================================
Generalized Linear Models ใน Python

ตัวอย่าง - การเปลี่ยนบ่อน้ำ

                  coef    std err          z      P>|z|      [0.025      0.975]
-------------------------------------------------------------------------------
Intercept       0.0027      0.079      0.035      0.972      -0.153       0.158
distance100    -0.8966      0.104     -8.593      0.000      -1.101      -0.692
arsenic         0.4608      0.041     11.134      0.000       0.380       0.542
  • ค่าสัมประสิทธิ์ทั้งสองมีนัยสำคัญทางสถิติ
  • เครื่องหมายของค่าสัมประสิทธิ์สอดคล้องกับตรรกะ
  • การเปลี่ยนแปลงหนึ่งหน่วยใน distance100 ทำให้ logit ลดลง 0.89
  • การเปลี่ยนแปลงหนึ่งหน่วยใน arsenic ทำให้ logit เพิ่มขึ้น 0.46
Generalized Linear Models ใน Python

ผลกระทบของการเพิ่มตัวแปร

  • ผลกระทบของตัวแปร arsenic
  • distance100 เปลี่ยนจาก -0.62 เป็น -0.89
  • ยิ่งอยู่ห่างจากบ่อน้ำที่ปลอดภัย
    • ยิ่งมีแนวโน้มที่ระดับสารหนูจะสูงขึ้น
                  coef    std err 
---------------------------------
Intercept       0.0027      0.079
distance100    -0.8966      0.104
arsenic         0.4608      0.041  
                  coef    std err
---------------------------------
Intercept       0.6060      0.060
distance100    -0.6291      0.097  
Generalized Linear Models ใน Python

ภาวะพหุสัมพันธ์ (Multicollinearity)

  • ตัวแปรที่มีความสัมพันธ์กับตัวแปรอื่นในโมเดล

โครงสร้างของตัวแปรสองตัวที่มีความสัมพันธ์ที่ระดับ 0.8, 0.4, 0, -0.4 และ -0.8 ตามลำดับ

  • ค่าความผิดพลาดมาตรฐานของสัมประสิทธิ์เพิ่มขึ้น
    • สัมประสิทธิ์อาจไม่มีนัยสำคัญทางสถิติ
1 https://en.wikipedia.org/wiki/Correlation_and_dependence
Generalized Linear Models ใน Python

การตรวจพบภาวะพหุสัมพันธ์

สิ่งที่ควรสังเกต

  • ค่าสัมประสิทธิ์ไม่มีนัยสำคัญ แต่ตัวแปรมีความสัมพันธ์สูงกับ $y$
  • การเพิ่ม/ลบตัวแปรทำให้ค่าสัมประสิทธิ์เปลี่ยนแปลงอย่างมีนัยสำคัญ
  • เครื่องหมายของสัมประสิทธิ์ไม่สอดคล้องกับตรรกะ
  • ตัวแปรมีความสัมพันธ์สูงกันเป็นคู่
Generalized Linear Models ใน Python

ปัจจัยพองตัวของความแปรปรวน (VIF)

  • วิธีวินิจฉัยที่ใช้กันแพร่หลายที่สุดสำหรับภาวะพหุสัมพันธ์
    • คำนวณสำหรับตัวแปรอธิบายแต่ละตัว
    • วัดระดับความพองตัวของความแปรปรวนของค่าสัมประสิทธิ์
  • เกณฑ์ที่แนะนำ VIF > 2.5
  • ใน Python
from statsmodels.stats.outliers_influence import variance_inflation_factor
Generalized Linear Models ใน Python

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

Generalized Linear Models ใน Python

Preparing Video For Download...