profileGLMM_Gibbs() runs a single Markov chain per call.
In practice, assessing convergence reliably requires:
posterior or coda.We start by loading the exposure_data dataset and
preparing the preprocessed model object via
profileGLMM_preprocess(). Note that in real analyses you
may want to increase the number of iterations (nIt) and
burn-in (nBurnIn) beyond the short settings used here for
illustration.
data("exposure_data")
exp_data <- exposure_data$df
theta0 <- exposure_data$theta0
covList <- list(
FE = c("X"),
RE = c("t"),
REunit = c("indiv"),
Lat = c("X"),
Assign = list(Cont = c("Exp1", "Exp2"), Cat = NULL),
Y = c("Y")
)
dataProfile <- profileGLMM_preprocess(
regType = "linear",
covList = covList,
dataframe = exp_data,
nC = 30,
intercept = list(FE = TRUE, RE = FALSE, Lat = TRUE)
)To run multiple chains, we call profileGLMM_Gibbs()
multiple times and refresh the initial parameter values between chains
using theta_init().
set.seed(1)
nIt <- 400
nBurnIn <- 100
# Chain 1
dataProfile$theta$betaFE
#> [1] -682.6170 706.3806
MCMC_Obj <- profileGLMM_Gibbs(
model = dataProfile,
nIt = nIt,
nBurnIn = nBurnIn
)
#> Iteration: 0
# Chain 2 (fresh initialization from prior)
dataProfile$theta <- theta_init(dataProfile$prior, dataProfile$params)
dataProfile$theta$betaFE
#> [1] -99.12192 -37.45010
MCMC_Obj2 <- profileGLMM_Gibbs(
model = dataProfile,
nIt = nIt,
nBurnIn = nBurnIn
)
#> Iteration: 0
# Chain 3 (fresh initialization from prior)
dataProfile$theta <- theta_init(dataProfile$prior, dataProfile$params)
dataProfile$theta$betaFE
#> [1] 769.2192 -1481.0339
MCMC_Obj3 <- profileGLMM_Gibbs(
model = dataProfile,
nIt = nIt,
nBurnIn = nBurnIn
)
#> Iteration: 0Connected line plots (type = "l") make it easier to spot
trends, slow mixing, or poor overlap across chains than point/scatter
plots. Below, we compare the retained draws for the first row of
beta and zeta across the three chains.
y_range <- range(
MCMC_Obj$beta[1, ],
MCMC_Obj2$beta[1, ],
MCMC_Obj3$beta[1, ]
)
plot(
MCMC_Obj$beta[1, ],
type = "l",
ylim = y_range,
xlab = "Post-burn-in iteration",
ylab = "FE intercept",
main = "Line trace plot across three chains"
)
lines(MCMC_Obj2$beta[1, ], col = "red")
lines(MCMC_Obj3$beta[1, ], col = "blue")
legend(
"topright",
legend = c("Chain 1", "Chain 2", "Chain 3"),
col = c("black", "red", "blue"),
lty = 1,
bty = "n"
)y_range <- range(
MCMC_Obj$zeta,
MCMC_Obj2$zeta,
MCMC_Obj3$zeta
)
plot(
MCMC_Obj$zeta,
type = "l",
ylim = y_range,
xlab = "Post-burn-in iteration",
ylab = "zeta",
main = "Line trace plot across three chains"
)
plot(
MCMC_Obj$zeta,
type = "l",
ylim = y_range,
xlab = "Post-burn-in iteration",
ylab = "zeta",
main = "Line trace plot across three chains"
)
lines(MCMC_Obj2$zeta, col = "red")
lines(MCMC_Obj3$zeta, col = "blue")
legend(
"topright",
legend = c("Chain 1", "Chain 2", "Chain 3"),
col = c("black", "red", "blue"),
lty = 1,
bty = "n"
)If chains do not overlap well or show persistent drift, increase
nIt and nBurnIn and re-check both
beta and covariance/random-effect parameters.
posteriorBecause ProfileGLMM delegates formal MCMC diagnostics to
specialized packages, we can convert the multi-chain draws into a
posterior draws array (draws_array) and
compute summary statistics including rhat,
ess_bulk, and ess_tail.
library(posterior)
#> This is posterior version 1.7.0
#>
#> Attaching package: 'posterior'
#> The following objects are masked from 'package:stats':
#>
#> mad, sd, var
#> The following objects are masked from 'package:base':
#>
#> %in%, match
# preparing the draws in posterior format (3D array draws x chains x params )
n_draws = nIt-nBurnIn # nb of draws per chain
n_chains = 3 # nb of chains
param_names = list('FE_int', 'FE_slope','Zeta') # parameter we want to check the convergence
draws_arr <- array(
0,
dim = c(n_draws,
n_chains, # number of chains
length(param_names) # Fe intercept and slope plus zeta
),
dimnames = list(
iteration = seq_len(n_draws),
chain = paste0("chain_", seq_len(n_chains)),
variable = param_names
)
)
draws_arr[, 1, ] = cbind(t(MCMC_Obj$beta), MCMC_Obj$zeta)
draws_arr[, 2, ] = cbind(t(MCMC_Obj2$beta), MCMC_Obj2$zeta)
draws_arr[, 3, ] = cbind(t(MCMC_Obj3$beta), MCMC_Obj3$zeta)
beta_draws <- as_draws_array(draws_arr)
summarise_draws(beta_draws, "mean", "rhat", "ess_bulk", "ess_tail")
#> # A tibble: 3 × 5
#> variable mean rhat ess_bulk ess_tail
#> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 FE_int 2.95 1.00 568. 744.
#> 2 FE_slope 2.03 1.00 729. 735.
#> 3 Zeta 4.28 1.14 15.5 85.3As a general rule of thumb, \(\hat{R}\) close to 1.00
(commonly < 1.01) alongside healthy bulk and tail
effective sample sizes indicates reliable mixing. Here the
zeta chain seems to require longer chain (\(\hat{R} = 1.06\)). Replacing
nIt = 8000 and nBurnIn = 2000 lowers \(\hat{R}\) to 1.004, indicating a better
convergence of the chain.
Once convergence diagnostics are satisfactory, you can merge the
pglmm_mcmc objects into a single pglmm_mcmc
object using combine_chains() and pass the combined result
to profileGLMM_postProcess(). In the following example we
thin each chain taking one sample out of three, keeping the combined
chain’s length equal to the original ones. We do so for sake of speed
and show a commented an example of how the combined chain would directly
fit in the profileGLMM_postProcess function.