Create a gist now

Instantly share code, notes, and snippets.

Test script for "Introduction to Bayesian data analysis with R"
# Prior to the tutorial make sure that the script below runs without error on your R installation.
# What you need is a working installation of JAGS: http://mcmc-jags.sourceforge.net/
# and the rjags R package that can be installed from R by running
# install.packages("rjags")
# Generating some fake data
set.seed(123)
y <- rbinom(30, size = 1, prob = 0.2015)
# Fitting a simple binomial model using JAGS
library(rjags)
model_string <- "model {
for(i in 1:length(y)) {
y[i] ~ dbern(theta)
}
theta ~ dunif(0, 1)
}"
jags_model <- jags.model(textConnection(model_string), data = list(y = y))
mcmc_samples <- coda.samples(jags_model, variable.names = "theta", n.iter = 1000)
plot(mcmc_samples)
summary(mcmc_samples)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment