使用 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 中的广义线性模型

火灾伤害数据集

  • 每日平民伤害
  • 肯塔基州路易斯维尔
  • 计数数据,零很多

来自肯塔基州路易斯维尔的每日火灾伤害

R 中的广义线性模型

开始练习!

R 中的广义线性模型

Preparing Video For Download...