# 贝叶斯分层模型

贝叶斯分层模型是一种统计模型，其先验分布中的参数本身也被赋予概率分布并由数据估计，从而通过部分汇聚在各组观测之间共享信息。它专为某些组观测稀少的分组数据而设计：既不孤立地分析每一组，也不把所有组混在一起，而是估计各组参数的同时将其拉向一个共同的总体分布。

在文献中，这类模型被称为分层模型、多水平模型、混合效应模型或随机效应模型；这些术语指的是同一种结构。<sup>[1](https://www.bayesrulesbook.com/chapter-15)</sup> 拟合的输出是组水平参数与总体水平超参数上的完整后验分布，其获得方式是以观测数据为条件，并评估模型拟合与对假设的敏感性。<sup>[2](https://users.aalto.fi/~ave/BDA3.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 产出什么 | 组参数与超参数的联合后验，组估计值部分汇聚至总体均值<sup>[2](https://users.aalto.fi/~ave/BDA3.pdf)</sup> |
| 核心结构 | 似然、组参数上的总体分布、超先验；先验本身也有先验<sup>[3](https://vioshyvo.github.io/Bayesian_inference/hierarchical-models.html)</sup><sup> • </sup><sup>[4](https://solomon.quarto.pub/dbda2/09.html)</sup> |
| 收缩规则（Beta-二项） | 后验均值 \( E(p^{*} \mid y^{*}) = (1 - \lambda) \cdot y^{*}/n^{*} + \lambda \mu \)，其中 \( \lambda = \eta/(n^{*} + \eta) \)<sup>[5](https://bayesball.github.io/BOOK/bayesian-hierarchical-modeling.html)</sup> |
| 汇聚因子 | \( \omega \) 从 0（无汇聚）到 1（完全汇聚）<sup>[6](https://stat.columbia.edu/~gelman/research/published/rsquared.pdf)</sup> |
| 预测增益 | 氡气示例：交叉验证 RMSE 为 0.84（完全汇聚）、0.86（无汇聚）、0.79（多水平）<sup>[7](https://people.eecs.berkeley.edu/~russell/classes/cs294/f05/papers/gelman-2005.pdf)</sup> |
| 主要失效模式 | 数据稀疏时出现漏斗形后验；通过非中心参数化解决<sup>[8](https://ar5iv.labs.arxiv.org/html/1312.0906)</sup> |
| 收敛目标 | 拆分 R-hat ≤ 1.01；每条链有效样本量 ≥ 100（四条链为 400）<sup>[9](https://link.springer.com/article/10.3758/s13428-023-02204-3)</sup> |

## 工作原理

该结构分为三个阶段。<sup>[3](https://vioshyvo.github.io/Bayesian_inference/hierarchical-models.html)</sup> 第一，组 \( j \) 内的观测服从似然 \( p(y_{ij} \mid \theta_{j}) \)。第二，组参数 \( \theta_{j} \) 抽取自以 \( \phi \) 为参数的总体分布，例如 \( \mu_{j} \mid \mu, \tau \sim \mathrm{Normal}(\mu, \tau) \)。第三，\( \phi \) 接受一个超先验 \( p(\phi) \)。

其基础是可交换性。若观测或组被判定为可交换的，德菲内蒂（de Finetti）1931年的表示定理便为将其建模为来自共同总体分布的抽样提供了依据，而这正是层级结构的第二阶段。<sup>[10](https://bruno.nicenboim.me/bayescogsci/ch-hierarchical.html)</sup> 克鲁施克（Kruschke）的表述直截了当地指出了定义性特征：当一个模型分解为依赖链 \( p(D \mid \theta) \, p(\theta \mid \omega) \, p(\omega) \) 时，它就是分层的，因为“先验本身也有先验”。<sup>[4](https://solomon.quarto.pub/dbda2/09.html)</sup>

**部分汇聚**是两个极端之间的中间地带。<sup>[1](https://www.bayesrulesbook.com/chapter-15)</sup> 完全汇聚对所有数据拟合一个通用模型，忽略组间差异。无汇聚为每一组拟合独立模型，忽略共享信息。总体尺度 \( \tau \) 控制模型在两者之间的位置：当 \( \tau \to 0 \) 时，组均值被强行拉在一起（完全汇聚）；当 \( \tau \to \infty \) 时，各组变得互不相关（无汇聚）。<sup>[5](https://bayesball.github.io/BOOK/bayesian-hierarchical-modeling.html)</sup>

收缩可以用代数方式量化。在Beta-二项分层结构中，组比例的后验均值为 \( E(p^{*} \mid y^{*}) = (1 - \lambda) \cdot y^{*}/n^{*} + \lambda \mu \)，其中 \( \lambda = \eta/(n^{*} + \eta) \) 是收缩比例：组的样本量 \( n^{*} \) 越小，其估计值越向总体均值 \( \mu \) 移动。<sup>[5](https://bayesball.github.io/BOOK/bayesian-hierarchical-modeling.html)</sup> 格尔曼（Gelman）的汇聚因子 \( \omega \) 对此做了推广：\( \omega = 0 \) 表示组估计值等于其自身数据均值，\( \omega = 1 \) 表示它等于总体均值。<sup>[6](https://stat.columbia.edu/~gelman/research/published/rsquared.pdf)</sup>

实际效果是估计值从总均值“借力”：当组数据稀疏时，后验估计 \( \mu_{i} \) 主要由 \( \mu \) 的后验决定。<sup>[10](https://bruno.nicenboim.me/bayescogsci/ch-hierarchical.html)</sup> 在各医院中，西奈山罗斯福医院（Mount Sinai Roosevelt）的原始死亡率为 6/46 = 0.13043，高于纽约长老会艾伦医院（NYP - Allen）的 13/105 = 0.12381，但纽约长老会艾伦医院的后验死亡率却高于西奈山罗斯福医院，因为西奈山罗斯福医院的样本量较小，其估计值被拉向总体比率。<sup>[5](https://bayesball.github.io/BOOK/bayesian-hierarchical-modeling.html)</sup>

## 实施方法

工作流程遵循贝叶斯数据分析的三个步骤：建立完整的概率模型，以观测数据为条件获得后验，并评估模型拟合与对假设的敏感性。<sup>[2](https://users.aalto.fi/~ave/BDA3.pdf)</sup>

**模型与先验设定。** 有人提出了一个四步程序用于在分层模型中选择先验，其中包括先验预测检验，即将从先验模拟的数据与实质性预期进行比较。<sup>[9](https://link.springer.com/article/10.3758/s13428-023-02204-3)</sup> 合理的先验还能让MCMC机制运行得更快，而不仅仅是更科学。<sup>[11](https://www.pymc.io/projects/examples/en/2022.01.0/case%5Fstudies/multilevel%5Fmodeling.html)</sup> 对于方差参数，弱信息先验是标准建议。<sup>[2](https://users.aalto.fi/~ave/BDA3.pdf)</sup> 对于多变量组效应，Stan建议将协方差矩阵分解为 \( \Sigma = \mathrm{diag\_matrix}(\tau) \cdot \Omega \cdot \mathrm{diag\_matrix}(\tau) \)，对尺度 \( \tau_{k} > 0 \) 使用半柯西(0, 2.5)先验，并使用LKJ先验 \( \Omega \sim \mathrm{LKJCorr}(\eta) \)，其中 \( \mathrm{LKJCorr}(\Sigma \mid \eta) \propto \det(\Sigma)^{\eta - 1} \)，\( \eta = 1 \) 时在相关矩阵上均匀分布。<sup>[12](https://mc-stan.org/docs/2_27/stan-users-guide/multivariate-hierarchical-priors-section.html)</sup>

**拟合。** 主流方法是MCMC。Stan的rstan接口使用无掉头采样器（No-U-Turn Sampler），这是一种哈密顿蒙特卡洛变体。<sup>[9](https://link.springer.com/article/10.3758/s13428-023-02204-3)</sup> 在中心参数化下的分层模型上，欧几里得HMC的效率至少比随机游走Metropolis和Metropolis-within-Gibbs高一个数量级。<sup>[8](https://ar5iv.labs.arxiv.org/html/1312.0906)</sup>

**检验。** 收敛性通过拆分R-hat（不应超过1.01）和每条链至少100的有效样本量（四条链为400）来判断，其中主体ESS和尾部ESS分别评估。<sup>[9](https://link.springer.com/article/10.3758/s13428-023-02204-3)</sup> 先验预测检验是PyMC工作流程的一部分，建议进行先验敏感性分析，即运行若干合理的先验并比较结果，同时保持透明以避免选择性报告结果和过拟合。<sup>[11](https://www.pymc.io/projects/examples/en/2022.01.0/case%5Fstudies/multilevel%5Fmodeling.html)</sup><sup> • </sup><sup>[9](https://link.springer.com/article/10.3758/s13428-023-02204-3)</sup>

## 起源

思想起点是查尔斯·斯坦（Charles Stein）的结果，该结果于1955年提出、1956年发表：对于 \( k \geq 3 \) 个正态总体，样本均值可以通过向一个常向量收缩而得到一致改进；多元正态均值的通常估计量是不可容许的。<sup>[13](https://ar5iv.labs.arxiv.org/html/1203.5610)</sup><sup> • </sup><sup>[14](https://doi.org/10.1525/9780520313880-018)</sup> 随后，布拉德利·埃夫隆（Bradley Efron）和卡尔·莫里斯（Carl Morris）从经验贝叶斯视角引入了分层正态模型的研究，见《斯坦的估计规则及其竞争者：一种经验贝叶斯方法》（Stein's Estimation Rule and its Competitors, An Empirical Bayes Approach，《美国统计协会杂志》，1973年）。<sup>[13](https://ar5iv.labs.arxiv.org/html/1203.5610)</sup><sup> • </sup><sup>[15](https://doi.org/10.1080/01621459.1973.10481350)</sup>

完全贝叶斯分层表述来自D. V. 林德利（D. V. Lindley）1970年的论文《多参数的估计》（The Estimation of Many Parameters，ETS研究公报系列），该文以可交换性作为估计多参数的先验知识。<sup>[16](https://doi.org/10.1002/j.2333-8504.1970.tb00411.x)</sup> 分层参数线性模型后来由加默曼（Gamerman）和米贡（Migon）（1993）与动态线性模型合并。<sup>[17](https://rss.onlinelibrary.wiley.com/doi/10.1111/j.2517-6161.1993.tb01928.x)</sup>

实际应用有待软件的发展。BUGS、JAGS和Stan等规范语言使用户指定的分层模型的MCMC拟合成为常规操作。<sup>[18](https://ppw.kuleuven.be/okp/_pdf/KruschkeBEIHM.pdf)</sup>

## 变体

**线性与广义线性形式。** 分层正态模型是基础情形。贝叶斯非线性混合效应模型，也称为贝叶斯分层非线性模型，自20世纪90年代初以来被广泛使用，其工作流程包括专家先验引出、先验预测检验、MCMC拟合和模型选择。<sup>[19](https://www.mdpi.com/2227-7390/10/6/898)</sup> 在重复测量研究中，“最大”分层模型为组水平参数指定完整的方差-协方差矩阵，brms语法如 `(1 + c_cloze || subj)` 控制是否对相关性建模；LKJ先验的参数 \( \eta \) 控制相关性向零收缩的强度。<sup>[10](https://bruno.nicenboim.me/bayescogsci/ch-hierarchical.html)</sup>

**非参数扩展。** 弗格森（Ferguson）（1973）以狄利克雷过程先验创立了贝叶斯非参数方法，埃斯科巴（Escobar）和韦斯特（West）（1995）探索了DP混合的MCMC后验计算。<sup>[20](https://link.springer.com/article/10.1007/s42081-023-00210-5)</sup> 分层狄利克雷过程使狄利克雷过程的基测度 \( G_{0} \) 本身成为另一个狄利克雷过程的抽样，从而将聚类变量跨组关联起来；HDP混合模型是在随机概率之间引入依赖关系的最流行方式之一。<sup>[21](https://www.stats.ox.ac.uk/~teh/research/npbayes/TehJor2010a.pdf)</sup>

**软件。** Stan是一种概率编程语言，用于具有自动微分和HMC的完全贝叶斯推断。<sup>[22](https://doi.org/10.18637/jss.v076.i01)</sup> brms是一个在Stan上构建贝叶斯多水平模型的R包，将R公式语法翻译为Stan代码。<sup>[23](https://doi.org/10.18637/jss.v080.i01)</sup> PyMC提供Python原生工作流程，内置先验和后验预测检验。<sup>[11](https://www.pymc.io/projects/examples/en/2022.01.0/case%5Fstudies/multilevel%5Fmodeling.html)</sup> BUGS和JAGS是仍在使用的早期规范语言。<sup>[18](https://ppw.kuleuven.be/okp/_pdf/KruschkeBEIHM.pdf)</sup> 在频率学派方面，lme4的 `lmer()` 拟合线性混合效应模型，“在表述上本质上是贝叶斯的”，只是它不对 \( \mu \) 放置先验。<sup>[10](https://bruno.nicenboim.me/bayescogsci/ch-hierarchical.html)</sup><sup> • </sup><sup>[24](https://doi.org/10.18637/jss.v067.i01)</sup>

## 应用

**多组估计。** 在重复测量的心理语言学和认知设计中，分层模型处理嵌套于更高层组织中的组内个体；一个应用示例拟合了84维后验（进食障碍中的注意力分配），使用3条MCMC链，在预热4,000步后运行100,000步，分层模型中的收缩减轻了多重比较中的假警报，而无需显式校正。<sup>[18](https://ppw.kuleuven.be/okp/_pdf/KruschkeBEIHM.pdf)</sup>

**环境与流行病学预测。** 在明尼苏达氡气研究中，多水平建模改善了预测：去除单个数据点的交叉验证RMSE在完全汇聚下为0.84，无汇聚下为0.86，多水平建模下为0.79。<sup>[7](https://people.eecs.berkeley.edu/~russell/classes/cs294/f05/papers/gelman-2005.pdf)</sup> 估计的地下室系数为0.67（标准误0.06），意味着有地下室的房屋氡气水平为无地下室房屋的 \( \exp(0.67) \approx 2.0 \) 倍，不过格尔曼指出，对于观测数据，这些效应不一定能作因果解释。<sup>[7](https://people.eecs.berkeley.edu/~russell/classes/cs294/f05/papers/gelman-2005.pdf)</sup>

## 局限与替代方案

**先验敏感性。** 对 \( \tau^{2} \) 使用逆伽马(1, 1)先验，虽然看似无信息，但在一个测验分数示例中将 \( \tau \) 的后验几乎拉至零，这说明了为什么需要敏感性分析。<sup>[3](https://vioshyvo.github.io/Bayesian_inference/hierarchical-models.html)</sup> 非正规先验可能产生软件未必能标记的非正规后验；非正规先验 \( p(\mu, \tau^{2}) \propto (\tau^{2})^{-1} \) 对于八学校型模型不会产生正规后验，而 \( p(\mu, \tau) \propto 1 \)（\( \tau > 0 \)）在组数 \( J \geq 3 \) 时会产生正规后验。<sup>[9](https://link.springer.com/article/10.3758/s13428-023-02204-3)</sup><sup> • </sup><sup>[3](https://vioshyvo.github.io/Bayesian_inference/hierarchical-models.html)</sup> \( \tau \) 上的均匀先验集中于无汇聚极限，赋予模型过多灵活性，可能助长过拟合；而集中于 \( \tau = 0 \) 的先验（如半正态）适合异质性可能可忽略的情形。<sup>[25](https://betanalpha.github.io/assets/case_studies/hierarchical_modeling.html)</sup>

**可识别性与过度参数化。** 最大混合效应模型无法收敛通常不是次优算法的问题，而是拟合了一个过于复杂、数据无法支撑的模型的后果，无论估计是最大似然还是使用无信息或弱信息先验的贝叶斯分层建模。<sup>[26](https://arxiv.org/abs/1506.04967v2)</sup>

**漏斗几何。** 当数据稀疏时，分层后验形成“漏斗”：低密度高体积区域下方的高密度低体积区域，要求采样器处理剧烈的曲率变化。<sup>[8](https://ar5iv.labs.arxiv.org/html/1312.0906)</sup> 在中心参数化下的八学校模型中，发散的HMC转移聚集在小 \( \tau \) 处，由此产生的MCMC估计系统性地低估组水平参数的方差；将Stan的 `adapt_delta` 从默认的0.8提高到接近1并不能消除偏差。<sup>[27](https://mc-stan.org/learn-stan/case-studies/divergences_and_bias.html)</sup> 非中心参数化将依赖关系分解为层间的确定性变换，消除了这一病态：发散变为可通过减小步长消除的假阳性，每次迭代的有效样本量大幅提高。<sup>[8](https://ar5iv.labs.arxiv.org/html/1312.0906)</sup><sup> • </sup><sup>[27](https://mc-stan.org/learn-stan/case-studies/divergences_and_bias.html)</sup>

**替代方案。** 经验贝叶斯将超参数的最大似然估计代入先验；这低估了来自估计超参数的不确定性，而完全贝叶斯方法对超参数后验进行积分。<sup>[3](https://vioshyvo.github.io/Bayesian_inference/hierarchical-models.html)</sup> 通过lme4的频率学派混合模型给出相同的部分汇聚结构，但不对总体参数放置先验。<sup>[10](https://bruno.nicenboim.me/bayescogsci/ch-hierarchical.html)</sup> 当非中心参数化不适用时，使用SoftAbs度量的黎曼HMC可以补偿位置相关的相关性。<sup>[8](https://ar5iv.labs.arxiv.org/html/1312.0906)</sup>

## References

1. [Chapter 15 Hierarchical Models are Exciting, Bayes Rules! (Johnson, Ott, Dogucu)](https://www.bayesrulesbook.com/chapter-15)
2. [Bayesian Data Analysis (3rd ed., Gelman et al.)](https://users.aalto.fi/~ave/BDA3.pdf)
3. [Chapter 6 Hierarchical models, Bayesian Inference (Aalto course notes, Vehtari group)](https://vioshyvo.github.io/Bayesian_inference/hierarchical-models.html)
4. [Chapter 9 Hierarchical Models, Doing Bayesian Data Analysis in brms and the tidyverse (Kurz)](https://solomon.quarto.pub/dbda2/09.html)
5. [Chapter 10 Bayesian Hierarchical Modeling, Probability and Bayesian Modeling (Albert & Hu)](https://bayesball.github.io/BOOK/bayesian-hierarchical-modeling.html)
6. [An R^2 measure for hierarchical models (Gelman, working paper)](https://stat.columbia.edu/~gelman/research/published/rsquared.pdf)
7. [Multilevel (hierarchical) modeling: what it can and can't do (Gelman, 2005/2006)](https://people.eecs.berkeley.edu/~russell/classes/cs294/f05/papers/gelman-2005.pdf)
8. [Hamiltonian Monte Carlo for Hierarchical Models (Betancourt & Girolami)](https://ar5iv.labs.arxiv.org/html/1312.0906)
9. [Bayesian hierarchical modeling: an introduction and reassessment (Behavior Research Methods, 2023)](https://link.springer.com/article/10.3758/s13428-023-02204-3)
10. [Chapter 5 Bayesian hierarchical models, Nicenboim, Schad, Vasishth](https://bruno.nicenboim.me/bayescogsci/ch-hierarchical.html)
11. [A Primer on Bayesian Methods for Multilevel Modeling (PyMC case study)](https://www.pymc.io/projects/examples/en/2022.01.0/case%5Fstudies/multilevel%5Fmodeling.html)
12. [Stan User's Guide: Multivariate priors for hierarchical models](https://mc-stan.org/docs/2_27/stan-users-guide/multivariate-hierarchical-priors-section.html)
13. [Shrinkage Estimation in Multilevel Normal Models (review)](https://ar5iv.labs.arxiv.org/html/1203.5610)
14. [Charles Stein (1956). INADMISSIBILITY OF THE USUAL ESTIMATOR FOR THE MEAN OF A MULTIVARIATE NORMAL DISTRIBUTION. .](https://doi.org/10.1525/9780520313880-018)
15. [Bradley Efron, Carl Morris (1973). Stein's Estimation Rule and its Competitors, An Empirical Bayes Approach. Journal of the American Statistical Association.](https://doi.org/10.1080/01621459.1973.10481350)
16. [D. V. Lindley (1970). THE ESTIMATION OF MANY PARAMETERS. ETS Research Bulletin Series.](https://doi.org/10.1002/j.2333-8504.1970.tb00411.x)
17. [Dynamic Hierarchical Models (Gamerman & Migon, 1993, JRSS B)](https://rss.onlinelibrary.wiley.com/doi/10.1111/j.2517-6161.1993.tb01928.x)
18. [Bayesian estimation of hierarchical models (Kruschke et al.)](https://ppw.kuleuven.be/okp/_pdf/KruschkeBEIHM.pdf)
19. [Bayesian Nonlinear Models for Repeated Measurement Data (MDPI Mathematics)](https://www.mdpi.com/2227-7390/10/6/898)
20. [Bayesian modeling via discrete nonparametric priors (Japanese Journal of Statistics and Data Science)](https://link.springer.com/article/10.1007/s42081-023-00210-5)
21. [Hierarchical Bayesian Nonparametric Models with Applications (Teh & Jordan)](https://www.stats.ox.ac.uk/~teh/research/npbayes/TehJor2010a.pdf)
22. [Bob Carpenter and colleagues (2017). Stan : A Probabilistic Programming Language. Journal of Statistical Software.](https://doi.org/10.18637/jss.v076.i01)
23. [Paul-Christian Bürkner (2017). brms : An R Package for Bayesian Multilevel Models Using Stan. Journal of Statistical Software.](https://doi.org/10.18637/jss.v080.i01)
24. [Douglas Bates and colleagues (2015). Fitting Linear Mixed-Effects Models Using lme4. Journal of Statistical Software.](https://doi.org/10.18637/jss.v067.i01)
25. [Hierarchical Modeling (case study, Michael Betancourt)](https://betanalpha.github.io/assets/case_studies/hierarchical_modeling.html)
26. [Parsimonious Mixed Models (Bates, Kliegl, Vasishth, Baayen)](https://arxiv.org/abs/1506.04967v2)
27. [Diagnosing Biased Inference with Divergences (Stan case study, Betancourt)](https://mc-stan.org/learn-stan/case-studies/divergences_and_bias.html)

---
*Initially written Sep 29, 2026 · Edited: Oct 15, 2026*

Topic: 物质世界与数学 › 数学与统计学 › 统计学与概率论 › Bayesian statistics › Bayesian model selection, design, and applications › Applied Bayesian modeling

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/bayesian-hierarchical-model

English original: https://www.edgepedia.org/bayesian-hierarchical-model
