Bayesian Modeling with RJAGS
Alicia Johnson
Associate Professor, Macalester College
prior: $p \sim \text{Beta}(45, 55)$
Prior: $p \sim \text{Beta}(45, 55)$
Likelihood: $X \sim \text{Beta}(10, p)$
Prior: $p \sim \text{Beta}(45, 55)$
Likelihood: $X \sim \text{Bin}(10, p)$
Prior: $p \sim \text{Beta}(45, 55)$
Likelihood: $X \sim \text{Bin}(10, p)$
Bayes' Rule:
posterior $\propto$ prior $\times$ likelihood
RJAGS
combines the power of R
with the JAGS
(Just Another Gibbs Sampler) engine. To get started:
JAGS
program outside R
R
, install the rjags
package# DEFINE the model
vote_model <- "model{
# Likelihood model for X
X ~ dbin(p, n)
# Prior model for p
p ~ dbeta(a, b)
}"
rjags
function dbin()
is different than base dbinom()
# DEFINE the model
vote_model <- "model{
# Likelihood model for X
X ~ dbin(p, n)
# Prior model for p
p ~ dbeta(a, b)
}"
# COMPILE the model
vote_jags_A <- jags.model(textConnection(vote_model),
data = list(a = 45, b = 55, X = 6, n = 10),
inits = list(.RNG.name = "base::Wichmann-Hill", .RNG.seed = 100))
# DEFINE the model
vote_model <- "model{
# Likelihood model for X
X ~ dbin(p, n)
# Prior model for p
p ~ dbeta(a, b)
}"
# COMPILE the model
vote_jags <- jags.model(textConnection(vote_model),
data = list(a = 45, b = 55, X = 6, n = 10),
inits = list(.RNG.name = "base::Wichmann-Hill", .RNG.seed = 100))
# SIMULATE the posterior
vote_sim <- coda.samples(model = vote_jags,
variable.names = c("p"),
n.iter = 10000)
# PLOT the simulated posterior
plot(vote_sim, trace = FALSE)
Bayesian Modeling with RJAGS