Skip to content

Instantly share code, notes, and snippets.

@marutter
Created September 6, 2012 01:27
Show Gist options
  • Save marutter/3649634 to your computer and use it in GitHub Desktop.
Save marutter/3649634 to your computer and use it in GitHub Desktop.
Comparing JAGS and STAN
library(R2jags)
runif(1) # Fixes "Error: object '.Random.seed' not found"
Y <- as.matrix(read.csv("rats.csv",header=F))
N <- 30
T <- 5
x <- c(8.0, 15.0, 22.0, 29.0, 36.0)
x.bar <- mean(x)
rats.data <- list("Y", "x", "T", "N","x.bar")
rats.params <- c("tau.c", "sigma", "alpha.c","tau.alpha", "tau.beta", "beta.c", "alpha0")
rats.inits <- function(){
list("alpha"=rep(250.0,N),
"beta"=rep(6.0,N),
"alpha.c"=0, "beta.c"=2, "tau.c"=1, "tau.alpha"=1, "tau.beta"=1)
}
system.time(jags.fit <- jags(data=rats.data,
inits=rats.inits,parameters.to.save=rats.params,
n.chains=3, n.iter=10000, n.burnin=1000, model.file="rats.bug"))
print(jags.fit)
plot(jags.fit)
library(rstan)
y <- read.table('rats.txt', header = TRUE)
x <- c(8, 15, 22, 29, 36)
rats_dat <- list(N = nrow(y), T = ncol(y),
x = x, y = y, xbar = mean(x))
stan.fit <- stan(file = 'rats.stan', data = rats_dat, verbose = FALSE,
warmup=1000,iter = 11000, n_chains = 3)
system.time(stan.fit2 <- stan(fit=stan.fit, data = rats_dat, verbose = FALSE,
warmup=1000,iter = 11000, n_chains = 3))
print(stan.fit2)
plot(stan.fit2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment