ฟังก์ชัน lm() พื้นฐานกับ glm()

Generalized Linear Models ใน R

Richard Erickson

Instructor

การโต้ตอบกับออบเจกต์โมเดล

  • โต้ตอบกับผลลัพธ์ของโมเดลได้
  • ฟังก์ชัน Base R ใช้กับ glm() ได้
  • ทางลัดที่มีประโยชน์
Generalized Linear Models ใน R

การพิมพ์โมเดล

  • print() มักเป็นค่าเริ่มต้น
print(poisson_out)
Call:  glm(formula = y ~ x, family = "poisson", data = dat)

Coefficients:
(Intercept)            x  
   -1.43036      0.05815  

Degrees of Freedom: 29 Total (i.e. Null);  28 Residual
Null Deviance:        35.63 
Residual Deviance: 30.92     AIC: 66.02
Generalized Linear Models ใน R

สรุปโมเดล

  • summary() แสดงรายละเอียดเพิ่มเติม
summary(poisson_out)
#...
Deviance Residuals: 
    Min       1Q   Median       3Q      Max  
-1.6547  -0.9666  -0.7226   0.3830   2.3022

Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) -1.43036 0.59004 -2.424 0.0153 * x 0.05815 0.02779 2.093 0.0364 *
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 35.627 on 29 degrees of freedom Residual deviance: 30.918 on 28 degrees of freedom AIC: 66.024
Number of Fisher Scoring iterations: 5
Generalized Linear Models ใน R

ผลลัพธ์แบบ Tidy

  • Tidyverse ให้ผลลัพธ์โมเดลในรูปแบบมาตรฐาน
  • tidy() จาก Broom package
library(broom)
tidy(poisson_out)
         term    estimate  std.error statistic    p.value
1 (Intercept) -1.43035579 0.59003923 -2.424171 0.01534339
2           x  0.05814858 0.02778801  2.092578 0.03638686
Generalized Linear Models ใน R

ค่าสัมประสิทธิ์การถดถอย

  • coef() แสดงค่าสัมประสิทธิ์การถดถอย
coef(poisson_out)
(Intercept)           x 
-1.43035579  0.05814858
Generalized Linear Models ใน R

ช่วงความเชื่อมั่น

  • confint() ประมาณช่วงความเชื่อมั่น
confint(poisson_out)
Waiting for profiling to be done...
               2.5 %     97.5 %
(Intercept) -2.725545344 -0.3897748
x            0.005500767  0.1155564
Generalized Linear Models ใน R

การพยากรณ์

  • predict(model, new_data)
  • อาร์กิวเมนต์ new_data:
    • ไม่ระบุ: predict() คืนค่าพยากรณ์จากข้อมูลที่ใช้ fit โมเดล
    • ระบุ: predict() คืนค่าพยากรณ์สำหรับ new_data
Generalized Linear Models ใน R

ชุดข้อมูลการบาดเจ็บจากเพลิงไหม้

  • การบาดเจ็บของพลเรือนรายวัน
  • Louisville, KY
  • ข้อมูลนับ มีค่าศูนย์จำนวนมาก

ข้อมูลการบาดเจ็บจากเพลิงไหม้รายวันใน Louisville, KY

Generalized Linear Models ใน R

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

Generalized Linear Models ใน R

Preparing Video For Download...