h1

Bayesian Longitudinal Modeling in R

May 1, 2009

A lot of my interests and the data I seem to get my hands on are longitudinal. I frequently use the lme4() package in R to do my analyses (with both discrete and continuous data). However, I recently learned about a package called MCMCglmm() written by Jarrod Hadfield, which allows a full Bayesian analysis on longitudinal discrete or continuous data. I strongly recommend this package and Jarrod is incredibly helpful when you encounter walls.

To use the package:
install.packages("MCMCglmm")
library(MCMCglmm)
my.model <- MCMCglmm(y ~ 1 + predictor + time + time*predictor, random= ~ id, family="poisson", data=mydata, verbose=FALSE, DIC=TRUE)

This code will model my dependent variable (y) by time, my predictor, and a time-predictor interaction. I have allowed only random intercepts in my model (random = ~ id), and specified the poisson family. The more crucial bit of code is the DIC=TRUE, if you want to use DIC for model comparison.

To plot the posterior distributions of my parameters:
plot(my.model$Sol)

To see the posterior mode of the parameters:
posterior.mode(my.model$Sol[,1] ## For Intercept
posterior.mode(my.model$Sol[,2] ## For Predictor (lets assume 2 level dummy)
posterior.mode(my.model$Sol[,3] ## For Time
posterior.mode(my.model$Sol[,4] ## For Time*Predictor

Credible intervals can be obtained by the following:
HPDinterval(my.model$Sol[,1], 0.95) ## For Intercept … and so on …

For the DIC:
my.model$DIC

For more information about this package read the vignette, e.g. specifying priors, examining random effects.
library(MCMCglmm)
vignette("Tutorial")

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: