Category Archives: Big Data

Example Bayesian Inference using R and OpenBUGS

  1. Open the R console
    Go to File/Change dir and set the working directory for R.
  2. Use your favorite editor to save the following code as schools.bug in your R working directory.
    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. Save this data as schools.dat in your R working directory.
    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. Go back to the R console and type the following commands.
    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”)
    library (“BRugs”)

    schools.sim <- bugs (data, inits, parameters, “schools.bug”, n.chains=3, n.iter=1000, program=”openbugs”)

 

From an example at columbia.edu.