贝叶斯线性回归

使用 rstanarm 进行贝叶斯回归建模

Jake Thompson

Psychometrician, ATLAS, University of Kansas

为何使用贝叶斯方法?

  • P 值针对数据概率进行推断,而非参数取值
  • 后验分布:似然与先验的结合
    • 抽样后验分布
    • 汇总样本
    • 用汇总对参数取值做推断
使用 rstanarm 进行贝叶斯回归建模

rstanarm 包

  • 与概率编程语言Stan对接
  • rstanarm 提供对 Stan 的高级接口
  • 允许自定义模型定义
使用 rstanarm 进行贝叶斯回归建模
library(rstanarm)

stan_model <- stan_glm(kid_score ~ mom_iq, data = kidiq)
正在为模型 'continuous' 采样(链 1)。
 梯度评估耗时 0.000408 秒
 1000 次跃蛙步进转移预计耗时
 4.08 秒。
 请相应调整预期!

 迭代:    1 / 2000 [  0%]  (预热)
 迭代:  200 / 2000 [ 10%]  (预热)
 迭代:  400 / 2000 [ 20%]  (预热)
 迭代:  600 / 2000 [ 30%]  (预热)
 迭代:  800 / 2000 [ 40%]  (预热)
 迭代: 1000 / 2000 [ 50%]  (预热)
 迭代: 1001 / 2000 [ 50%]  (采样)
 迭代: 1200 / 2000 [ 60%]  (采样)
 迭代: 1400 / 2000 [ 70%]  (采样)
 迭代: 1600 / 2000 [ 80%]  (采样)
 迭代: 1800 / 2000 [ 90%]  (采样)
 迭代: 2000 / 2000 [100%]  (采样)

  用时:0.37735 秒(预热)
        0.252244 秒(采样)
        0.629594 秒(总计)
使用 rstanarm 进行贝叶斯回归建模
summary(stan_model)
 模型信息:
  function:     stan_glm
  family:       gaussian [identity]
  formula:      kid_score ~ mom_iq
  algorithm:    sampling
  priors:       see help('prior_summary')
  sample:       4000 (posterior sample size)
  observations: 434
  predictors:   2

 估计:
                 mean    sd      2.5%    25%     50%     75%     97.5%
 (Intercept)      25.7     6.0    13.8    21.6    25.7    30.0    37.0
 mom_iq            0.6     0.1     0.5     0.6     0.6     0.7     0.7
 sigma            18.3     0.6    17.1    17.9    18.3    18.7    19.5
 mean_PPD         86.8     1.2    84.3    85.9    86.8    87.6    89.2
 log-posterior -1885.4     1.2 -1888.5 -1886.0 -1885.1 -1884.5 -1884.0

 诊断:
               mcse Rhat n_eff
 (Intercept)   0.1  1.0  4000 
 mom_iq        0.0  1.0  4000 
 sigma         0.0  1.0  3827 
 mean_PPD      0.0  1.0  4000 
 log-posterior 0.0  1.0  1896 

 对每个参数,mcse 为蒙特卡洛标准误,n_eff 为有效样本量的粗略度量,Rhat 为分链的潜在尺度缩减因子
 (收敛时 Rhat=1)。
使用 rstanarm 进行贝叶斯回归建模

rstanarm 摘要:估计

 估计:
                 mean    sd      2.5%    25%     50%     75%     97.5%
 (Intercept)      25.7     6.0    13.8    21.6    25.7    30.0    37.0
 mom_iq            0.6     0.1     0.5     0.6     0.6     0.7     0.7
 sigma            18.3     0.6    17.1    17.9    18.3    18.7    19.5
 mean_PPD         86.8     1.2    84.3    85.9    86.8    87.6    89.2
 log-posterior -1885.4     1.2 -1888.5 -1886.0 -1885.1 -1884.5 -1884.0
  • sigma:误差的标准差
  • mean_PPD:后验预测样本的均值
  • log-posterior:类似于似然
使用 rstanarm 进行贝叶斯回归建模

rstanarm 摘要:诊断

诊断:
             mcse Rhat n_eff
(Intercept)   0.1  1.0  4000 
mom_iq        0.0  1.0  4000 
sigma         0.0  1.0  3827 
mean_PPD      0.0  1.0  4000 
log-posterior 0.0  1.0  1896 

对每个参数,mcse 为蒙特卡洛标准误,
n_eff 为有效样本量的粗略度量,
Rhat 为分链尺度缩减因子
(收敛时 Rhat=1)。
  • Rhat:链内方差相对链间方差的度量
  • 小于 1.1 表示已收敛
使用 rstanarm 进行贝叶斯回归建模

让我们练习吧!

使用 rstanarm 进行贝叶斯回归建模

Preparing Video For Download...