# Blocked Gibbs Sampling in R for Bayesian Multiple Linear Regression

This article is originally published at https://stablemarkets.wordpress.com

In a previous post, I derived and coded a Gibbs sampler in R for estimating a simple linear regression.

In this post, I will do the same for multivariate linear regression. I will derive the conditional posterior distributions necessary for the blocked Gibbs sampler. I will then code the sampler and test it using simulated data.

R code for simulating data and implementing the blocked Gibbs is in by GitHub repo.

**A **Bayesian Model

Suppose we have a sample size of subjects. We observe an outcome vector . The Bayesian multivariate regression assumes that this vector is drawn from a multivariate normal distribution where the mean vector is and covariance matrix . Here, is an observed matrix of covariates. Note, the first column of this matrix is identity. The parameter vector is , is a common variance parameter, and is the identity matrix. By using the identity matrix, we are assuming independent observations. Formally,

So far, this is identical to the multivariate normal regression seen in the frequentist setting. Assuming is full column rank, maximizing the likelihood yields the solution:

A Bayesian model is obtained by specifying a prior distribution for and . For this example, I will use a flat, improper prior for and a inverse gamma prior for :

We assume the hyperparamters are known for simplicity.

# Joint Posterior Distribution

The joint posterior distribution is proportional to

We can write this because we assumed prior independence. That is,

.

Substituting the distributions,

# Blocked Gibbs Sampler

Before coding the sampler, we need to derive the components of the Gibbs sampler – the posterior conditional distributions of each parameter.

The conditional posterior of is found by dropping factors independent of from the joint posterior and rearranging. This case is easy since there’s nothing to drop:

The conditional posterior of takes some more linear algebra.

This is a really nifty and intuitive result. Because we use a flat prior on the parameter vector, the conditional posterior of the parameter vector is centered around the maximum likelihood estimate . The covariance matrix of the conditional posterior is the frequentist estimate of the covariance matrix,

Also note that the conditional posterior is a multivariate distribution since is a vector. So in each iteration of the Gibbs sampler, we draw a whole vector, or “block”, from the posterior. This is much more efficient than drawing from the conditional distribution of each parameter conditional on the others, one at a time. This is why the procedure is called a “blocked” sampler.

# Simulation

All code referencing in the following can be found in my GitHub repo.

I simulate a outcome vector from . Here is the identity matrix and is a model matrix. The true parameter vector, is

Running the blocked Gibbs sampler (the block_gibbs() function) produces estimates of the true coefficient and variance parameters. 500,000 iterations were run. A burn-in period of 100,000 with a trimming of 10 iterations.

Below are the plots of the MCMC chains with true values indicated with a red line.

Here are the posterior distributions of the parameters after applying burn-in and trimming:

Seems like we’re able to get reasonable posterior estimates of these parameters. The distributions are not exactly centered around the truth because our data set is only one realization of the truth. To make sure the Bayesian estimator is working properly, I repeat this exercise for 1,000 simulated datasets.

This will yield a 1,000 sets of posterior means and 1,000 sets of 95% credible intervals. On average, these 1,000 posterior means should be centered around the truth. And on average the true parameter values should be within the credible interval 95% of the time.

Below is a summary of these evaluations.

The “Estimator Means” column is the average posterior mean across all 1,000 simulations. Pretty good. The percent bias is all less than 5%. The coverage of the 95% CI is around 95% for all parameters.

# Extensions

There are many extensions we can make to this model. For example, other distributions aside from the Normal could be used in order to accommodate different types of outcomes. For example, if we had binary data, we could model it as:

And then place a prior distribution on . This idea generalizes Bayesian linear regression to Bayesian GLM.

In the linear case outlined in this post, it’s possible to have modeled the covariance matrix more flexibly. Instead, it is assumed that the covariance matrix is diagonal with a single common variance. This is the homoskedasticity assumption made in multiple linear regression. If the data is clustered (e.g., multiple observations per subject), we could use an inverse-Wishart distribution to model the whole covariance matrix.

Thanks for visiting r-craft.org

This article is originally published at https://stablemarkets.wordpress.com

Please visit source website for post related comments.