Last active
May 2, 2023 17:16
-
-
Save rasmusab/a453c564f30692c63d38 to your computer and use it in GitHub Desktop.
Test script for "Introduction to Bayesian data analysis with R"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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