Skip to content

Instantly share code, notes, and snippets.

@rasmusab
Last active May 2, 2023 17:16
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rasmusab/a453c564f30692c63d38 to your computer and use it in GitHub Desktop.
Save rasmusab/a453c564f30692c63d38 to your computer and use it in GitHub Desktop.
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