Logistic Regression สำหรับพยากรณ์ความน่าจะเป็น

Supervised Learning ใน R: การถดถอย

Nina Zumel and John Mount

Win-Vector LLC

การพยากรณ์ความน่าจะเป็น

  • การพยากรณ์ว่าเหตุการณ์จะเกิดขึ้นหรือไม่ (ใช่/ไม่ใช่): classification
  • การพยากรณ์ความน่าจะเป็นที่เหตุการณ์จะเกิดขึ้น: regression
  • Linear regression: พยากรณ์ค่าในช่วง [$-\infty$, $\infty$]
  • ความน่าจะเป็น: จำกัดอยู่ในช่วง [0,1]
    • จึงเรียกว่า non-linear
Supervised Learning ใน R: การถดถอย

ตัวอย่าง: การพยากรณ์โรค Duchenne Muscular Dystrophy (DMD)

  • ผลลัพธ์: has_dmd    ตัวแปรนำเข้า: CK, H
Supervised Learning ใน R: การถดถอย

โมเดล Linear Regression

model <- lm(has_dmd ~ CK + H, 
            data = train)

test$pred <- predict(
    model, 
    newdata = test
)

ผลลัพธ์: has_dmd $\in$ {0,1}

  • 0: FALSE
  • 1: TRUE

โมเดลพยากรณ์ค่าที่อยู่นอกช่วง [0:1]

Supervised Learning ใน R: การถดถอย

Logistic Regression

$$ log(\frac{p}{1-p}) = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + ... $$

glm(formula, data, family = binomial)
  • Generalized linear model
  • สมมติให้ตัวแปรนำเข้าเป็นแบบบวกและเชิงเส้นใน log-odds: $log( p/(1-p) )$
  • family: ระบุการกระจายของค่าความผิดพลาดของโมเดล
    • logistic regression: family = binomial
Supervised Learning ใน R: การถดถอย

โมเดล DMD

model <- glm(has_dmd ~ CK + H, data = train, family = binomial)
  • ผลลัพธ์: สองคลาส เช่น $a$ และ $b$
  • โมเดลคืนค่า $Prob(b)$
    • แนะนำ: ใช้ 0/1 หรือ FALSE/TRUE
Supervised Learning ใน R: การถดถอย

การตีความโมเดล Logistic Regression

model
Call:  glm(formula = has_dmd ~ CK + H, family = binomial, data = train)

Coefficients:
(Intercept)           CK            H  
  -16.22046      0.07128      0.12552  

Degrees of Freedom: 86 Total (i.e. Null);  84 Residual
Null Deviance:       110.8 
Residual Deviance: 45.16     AIC: 51.16
Supervised Learning ใน R: การถดถอย

การพยากรณ์ด้วยโมเดล `glm()`

predict(model, newdata, type = "response")
  • newdata: ค่าเริ่มต้นคือข้อมูลฝึก
  • หากต้องการความน่าจะเป็น: ใช้ type = "response"
    • ค่าเริ่มต้น: คืนค่า log-odds
Supervised Learning ใน R: การถดถอย

โมเดล DMD

model <- glm(has_dmd ~ CK + H, data = train, family = binomial)
test$pred <- predict(model, newdata = test, type = "response")

Supervised Learning ใน R: การถดถอย

การประเมินโมเดล Logistic Regression: pseudo-$R^2$

$$ R^2 = 1 - \frac{RSS}{SS_{Tot}} $$

$$ pseudo R^2 = 1 - \frac{deviance}{null.deviance} $$

  • Deviance: คล้ายคลึงกับ variance (RSS)
  • Null deviance: คล้ายคลึงกับ $SS_{Tot}$
  • pseudo R^2: ความสามารถในการอธิบาย deviance
Supervised Learning ใน R: การถดถอย

Pseudo-$R^2$ บนข้อมูลฝึก

ใช้ broom::glance()

glance(model) %>% 
  summarize(pR2 = 1 - deviance/null.deviance)
   pseudoR2
1 0.5922402

ใช้ sigr::wrapChiSqTest()

wrapChiSqTest(model)
"... pseudo-R2=0.59 ..."
Supervised Learning ใน R: การถดถอย

Pseudo-$R^2$ บนข้อมูลทดสอบ

# Test data
test %>% 
  mutate(pred = predict(model, newdata = test, type = "response")) %>%
  wrapChiSqTest("pred", "has_dmd", TRUE)

อาร์กิวเมนต์:

  • data frame
  • ชื่อคอลัมน์ผลการพยากรณ์
  • ชื่อคอลัมน์ผลลัพธ์
  • ค่าเป้าหมาย (เหตุการณ์ที่สนใจ)
Supervised Learning ใน R: การถดถอย

กราฟ Gain Curve

GainCurvePlot(test, "pred","has_dmd", "DMD model on test")

Supervised Learning ใน R: การถดถอย

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

Supervised Learning ใน R: การถดถอย

Preparing Video For Download...