Multilevel models (also called hierarchical models) are a class of statistical models that are applied to data that have a natural hierarchical or nested structure. They are useful because they can often out-perform models that don’t take this structure into account. This post will cover multilevel models applied to linear regression however it can be easily extended to logistic regression and generalized linear models.

## Nested Data

Many datasets have a naturally nested or hierarchical structure. Taking the radon dataset as an example, the goal is to predict radon levels for a given house with the county and state of that house provided as potential input features. The nested structure of these features is clear - a house is within a county and a county is within a state. Since it is known that radon levels are highly correlated with location, these nested features are very important. Intuitively, a model that takes into account the hierarchical structure of these features should outperform one that does not.

In terms of notation, when dealing with nested data, the training example is usually referred to as the *individual* and given a subscript of *i* and the larger heirarchical level that the training example falls into is called the *group* which is given a subscript of *j*. So in the radon example, the house is considered the *individual* and the county and state are two *groups*. Also, the notation subscript *j[i]* refers to the group corresponding to individual *i*.

## Simple Multilevel Models

The question now becomes, how do we let the model know that our features have a nested structure? There are various ways of attempting this - some simple (and less effective) and some more complex (and more effective).

### 1. Total Pooling

The simplest approach is to ignore group-level features altogether and train your model with the remaining individual-level features. This is called *total pooling* where all the training examples are “pooled” together regardless of the groups they belong to. The clear problem with this approach is that it throws away group-level features that might be useful - in terms of model selection, this approach tends to significantly underfit your model.

### 2. No Pooling

No pooling is the opposite idea which is to build a separate model for each group where each model is trained with only the training examples that are part of that group. There are several problems with this approach as well such as

- What if a group has only a small number of training examples?
- What if the number of groups is so large such that training that many models becomes unscalable?
- Predictive power between groups is ignored.

In constrast to total pooling, no pooling tends to significantly overfit your model.

### 3. Indicator Variables

Using indicator variables (also called dummy variables) is a good compromise between total pooling and no pooling. With indicator variables, a binary-valued feature is added to your model for each group indicating that individual’s assignment to that group (essentially one-hot encoding). The main advantage with this method is that you can use all training examples for a single model that takes into account the individual’s group assignment.

However this approach has some drawbacks as well such as

- It requires a separate feature for each group.
- If some groups are small, weights corresponding to these indicator variables could be difficult to train.
- It ignores variance between groups. Consider the radon dataset again, some counties are closer together than others yet with indicator variables they would be treated independently. Wouldn't it make more sense for the model to take this similarity into account?

## Multilevel Model Intuition

Multilevel models aim to mitigate some of the drawbacks of the indicator variable approach. Consider a simple linear regression model

where is the output, is the input *design matrix* of features and is the weight vector. The features can be either at the individual level or the group level. For example, in the radon dataset an individual-level feature could be the size of the house and a group-level feature could be the average Uranium level of the county that the house is in. The idea behind multilevel models is to train separate weight coefficients for each group. With a multilevel model we would rewrite the the regression formula as

Here the matrix is still a design matrix but now it is only for the individual-level features. In the bottom equation is the design matrix for the group-level features and is the associated weight vector. Finally represents the group-level contribution to the final output for the group that individual is a part of. Intuitively it looks like there are two regressions going on here - one that predicts the group-level contributions and one that predicts the final output (which is itself a function of the group-level contributions).

Another important aspect of multilevel models is that even though coefficients will be different for each group, they are all taken from the same “feature distribution”. It is this aspect that mitigates the group data size and group variance problems associated with the indicator variable approach (we will see why later).

## Putting it in a Bayesian Framework

Multilevel models are most commonly discussed within a Bayesian framework. Traditional linear regression can also be thought of within a Bayesian framework by treating the model output like a normal distribution centered around with some variance i.e.

likewise the multilevel regression equations can be written as

Placing the model in a Bayesian framework is crucial because it allows us to take into account individual and group-level variances ( and , respectively) which we could not do with the indicator variable approach.

## Learning Parameters

Now that the problem has been placed in a Bayesian framework, the parameters can be learned via Gibbs sampling. The goal is to learn the following parameters given the model and the data: the group-level contributions , the individual-level coefficients , the group-level coefficients , the individual-level variance hyperparameter , and the group-level variance hyperparameter .

The general idea behind the Gibbs sampling procedure is to start with a random initialization of all the parameters then update each of the parameters via that parameter’s particular update equation while holding the other parameters constant. In this way each parameter gets updated based on the most recent update of the other parameters. If you run this long enough it should converge (how to know if it has converged is another issue and beyond the scope of this blog post).

The Gibbs sampling procedure for the multilevel model that we have been focusing on will be given without proof but the intuition should be somewhat clear.

## Gibbs Sampler Procedure

The following are the Gibbs sampling updates for the parameters of interest which are repeated until convergence.

### Step 1: Update

For each group , the group-level contribution can be sampled from where

where is the mean value for group (think no pooling) and is the mean value across all groups (think total pooling). Think of this as a weighted average between the two where the weights depend on the sample size of the group () as well as the individual and group variance ( and , respectively).

However, we are not interested in ’s we are interested in ’s so this doesn’t make sense. For this to make sense we must replace the ’s with ’s where

This should be equivalent to the group-level error where

So once the replacement has been made, the sample from will represent . With that sample, can be recovered from the above equation. It is important to keep in mind that we are holding all other parameters constant in this step.

### Step 2: Update

For each individual , compute

which is the contribution of the individual-level features. Then regress on to get and covariance matrix i.e.

Then sample .

### Step 3: Update

Similary, to update we regress on to get and i.e.

Then sample .

### Step 4: Update

Updating the individual-level variance is just taking the sample variance using the current parameters i.e.

### Step 5: Update

And similarly for ,

## Gibbs Sampler Intuition

The should be no intuition necessary for steps 4 and 5 of the Gibbs sampler - they are just two sample variance computations. Updates 2 and 3 are also fairly intuitive - the coefficients come from the normal equations as with linear regression. Another way of looking at it is taking the MLE of the coefficients given the current versions of the other parameters. It is also important that we sample from a normal distribution around these estimates in order to take the variance into account. Step 1, however, is not very intuitive but generally it estimates the group-level errors given the group-level and individual-level variances then it uses the errors to estimate the ’s’ (which are then used for the subsequent steps).

## References

*Data Analysis Using Regression and Multilevel/Hierarchical Models*, Gelman & Hill.