RJAGS中多链采样时各链参数初始值是否一致?——coda.samples使用中的疑问
Great question—this is a common point of confusion between core JAGS behavior and how the rjags R package wraps it. Let’s break this down clearly:
The Short Answer
When you don’t manually specify initial values in rjags::coda.samples() (or jags.model()), RJAGS automatically generates distinct random initial values for each chain—even though the official JAGS manual states that chains use identical initial values by default. This is a deliberate design choice in the R package to make convergence diagnostics easier out of the box.
Why the Discrepancy?
The JAGS command-line tool does default to identical initial values for all chains. But the rjags package wraps JAGS to be more user-friendly for R users:
- Starting all chains at the exact same point can make convergence checks (like the Gelman-Rubin statistic) less useful early on, since chains won’t diverge even if the model has issues.
- RJAGS solves this by generating independent random initial values for each chain when you don’t provide your own, which aligns with best practices for MCMC convergence testing.
How to Verify This
You can quickly confirm this behavior with a simple test. Run this code to check the initial values of 3 chains for a basic model:
library(rjags) # Define a simple normal model model_code <- " model { y ~ dnorm(mu, precision = 1) mu ~ dnorm(0, precision = 1) } " # Data and model setup data_list <- list(y = 4.2) model <- jags.model(textConnection(model_code), data = data_list, n.chains = 3) # Extract and print initial values for mu across chains initial_values <- get.samples(model, variable.names = "mu", n.iter = 1)[[1]] for (chain in 1:3) { cat(sprintf("Chain %d initial mu: %.4f\n", chain, initial_values[chain])) }
You’ll see three different initial values for mu—proof that RJAGS is auto-generating distinct starting points.
Forcing Identical Initial Values
If you do want all chains to start at the same point (matching JAGS core behavior), just pass a single initial value list to the inits argument. RJAGS will apply this value to every chain:
# Use the same initial value for all chains shared_init <- list(mu = 0) model <- jags.model(textConnection(model_code), data = data_list, inits = shared_init, n.chains = 3) # Check initial values again—all will be 0 initial_values <- get.samples(model, variable.names = "mu", n.iter = 1)[[1]] for (chain in 1:3) { cat(sprintf("Chain %d initial mu: %.4f\n", chain, initial_values[chain])) }
Key Takeaway
- No manual inits: RJAGS creates unique random initial values per chain (R package-specific behavior)
- Manual inits (single list): All chains use the same initial value (matches JAGS core default)
- Manual inits (list of lists): You can specify unique values for each chain explicitly (e.g.,
inits = list(list(mu=1), list(mu=2), list(mu=3)))
内容的提问来源于stack exchange,提问作者user128949

