使用 glm() 的基本 lm() 功能

R 的廣義線性模型

Richard Erickson

Instructor

與模型物件互動

  • 與輸出互動
  • 基礎 R 函式適用於 glm()
  • 實用捷徑
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
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
R 的廣義線性模型

整潔輸出

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
R 的廣義線性模型

迴歸係數

  • coef() 列出迴歸係數
coef(poisson_out)
(Intercept)           x 
-1.43035579  0.05814858
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
R 的廣義線性模型

預測

  • predict(model, new_data)
  • new_data 參數:
    • 未指定:predict() 會用擬合時的原始資料產生預測。
    • 有指定:predict() 會對 new_data 產生預測。
R 的廣義線性模型

火災受傷資料集

  • 每日民眾受傷數
  • Louisville,KY
  • 計數資料,零值多

路易維爾(KY)的每日火災受傷

R 的廣義線性模型

一起來練習吧!

R 的廣義線性模型

Preparing Video For Download...