Multilevel Analysis

It is so common that data structures are in multilevel in terms of  correlative errors across different units. I find it very help to conduct statistical analysis in my research area. Here is a short intro to how! Obviously, complexity will increase along with research directions.

Running a multilevel analysis in R: 

1) First,  make sure that “R2WinBUGS” packages are installed in your R directory.



2) Second, have a program (bugs).file ready. This model requires *.bugs file as described below for example:

model {
for (j in 1:J){
y[j] ~ dnorm (theta[j], tau.y[j])
theta[j] ~ dnorm (mu.theta, tau.theta)
tau.y[j] <- pow(sigma.y[j], -2)
mu.theta ~ dnorm (0.0, 1.0E-6)
tau.theta <- pow(sigma.theta, -2)
sigma.theta ~ dunif (0, 1000)

3) Third, you also need a data file (*.txt) to work with step 4 as described below for example:

school estimate sd
A  28  15
B   8  10
C  -3  16
D   7  11
E  -1   9
F   1  11
G  18  10
H  12  18

4) Do the following command in R to run multilevel analysis finally:

schools <- read.table (“schools.dat”, header=TRUE)
J <- nrow(schools)
y <- schools$estimate
sigma.y <- schools$sd
data <- list (“J”, “y”, “sigma.y”)
inits <- function() {list (theta=rnorm(J,0,100), mu.theta=rnorm(1,0,100), sigma.theta=runif(1,0,100))}
parameters <- c(“theta”, “mu.theta”, “sigma.theta”)
schools.sim <- bugs (data, inits, parameters, “schools.bug”, n.chains=3, n.iter=1000)

Step 1 – Step 4 are a simple example given by Andrew Gelman (see his

Reference on Bayesian inference Using Gibbs Sampling method

I read these two books closely to run multilevel analysis in R.  Hope you find them all interesting and helpful.  Of course, step 1 – step 4 are a short excerpt from mostly Gelman and Hill (2007) .  It will get you enough starting what those individual codes would mean.


Leave a comment for questions / Suggestion. Thanks.

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

You are commenting using your 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