Data science, statistics or machine learning in broken English

In 2 previous posts, you learned what Bayesian modeling and Stan are and how to install them. Now you are ready to try it on some very Bayesian problems - as many people love - such as hierarchical Bayesian model.

Definition of hierarchical Bayesian models

Prior to tackling with a practical example, let's overview what and how hierarchical Bayesian model is. A famous book on Bayesian modeling with MCMC, written by Toshiro Tango and Taeko Becque and published in Japan, describes as below*1.

In a fixed-effects model of frequentist, each result is assumed to have a common average .

On the other hand, in a random-effects model, each result is assumed to have a distinct average and it is distributed around a global average .

Bayesian hierarchical models assume prior probability for parameters of a probability distribution of in a random-effects model, such as

It is said that such models have a hierarchical structure with two levels, that is,

1st level: a probability distribution is assumed for

2nd level: one more probability distribution is assumed for parameters of the 1st level

This is a textbook definition of hierarchical models, but I think it can be understood more intuitively; in hierarchical Bayesian models, often the models have to handle some excessive fluctuations as nonlinear effects more than expected in usual frequentist's models. Priors used in such models can be seen as an "absorber" that can absorb various kinds of fluctuations distributed around true parameters.

A simple practice of an univariate logistic regression model with random effects

In this post, I follow an example from another famous textbook written by Takuya Kubo.

This example assumes an experiment in which we take 8 seeds from each of a certain 100 plants with a various number of leaves (unknown), thus its dataset contains 1) ID of each plant and 2) the number of survived seeds in 100 rows. From the viewpoint of hierarchical Bayesian modeling, unknown number of leaves of each plant can be random effects*2. You can get it with R as below.

Please see some important points in this Stan script. The transformed parameters block contains a transform of dependent variable from raw data to an inverse logit, but with individual biases (random effects). The model block contains some sampling statements in addition to an usual sampling procedure of logistic regression.

The order of statements in the model block may affect its result, even some changes cause compilation error. In principle an order of sampling statements would affect a structure of computation in the model block.

You can see how random effects r[i] fluctuates across individual plants. Even if you don't know how much the number of leaves of each plant varies, you'll guess it can be caused by some obvious individual biases, including the number of leaves.

Finally, although almost all Rhat values show good convergence, let's check its convergence.

Conclusions

It can be regarded as an "absorber" that can absorb fluctuations given by such random effects

Stan can easily handle it, but be careful for writing the model block

In practical modeling, how to set hierarchical structures and how to give (un)informative priors would determine whether its model fits well or not. It requires a lot of trials and errors for everybody, but you can get some tips from a textbook below.