library(rjags)
library(knitr)
# Prepare the data (same as the Mock Data table)
data_list <- list(
r = matrix(c(20, 40, 20, 50, 30, 40), nrow = 3, byrow = TRUE),
n = matrix(c(100, 100, 100, 100, 100, 100), nrow = 3, byrow = TRUE)
)
# Initialize the model
jags_model <- jags.model(
file = "nma_model.txt",
data = data_list,
n.chains = 3,
inits = lapply(1:3, function(i) list(.RNG.name = "base::Mersenne-Twister", .RNG.seed = 41 + i)), # fixed seed per chain
n.adapt = 1000,
quiet = TRUE
)
# Sample from the posterior
samples <- coda.samples(
model = jags_model,
variable.names = c("d_AB", "d_AC", "OR_AB", "OR_AC", "OR_BC"),
n.iter = 10000
)
# Summarise the posterior
pars <- c("d_AB", "d_AC", "OR_AB", "OR_AC", "OR_BC")
draws <- as.matrix(samples)[, pars]
jags_tbl <- data.frame(
Mean = colMeans(draws),
SD = apply(draws, 2, sd),
Lower = apply(draws, 2, quantile, 0.025),
Median = apply(draws, 2, median),
Upper = apply(draws, 2, quantile, 0.975),
ESS = effectiveSize(samples)[pars],
Rhat = gelman.diag(samples, multivariate = FALSE)$psrf[pars, "Point est."]
)
kable(jags_tbl, digits = c(3, 3, 3, 3, 3, 0, 3), col.names = c("Mean", "SD", "2.5%", "Median", "97.5%", "ESS", "R-hat"))