MCMC trace plots and density plots for AME/LAME model parameters
Source:R/trace_plot.R
trace_plot.RdCreates diagnostic plots for Markov Chain Monte Carlo (MCMC) samples from AME or LAME models. Displays trace plots to assess convergence and mixing, alongside density plots to visualize posterior distributions.
Usage
trace_plot(
fit,
params = c("all", "beta", "variance", "dynamic"),
include = NULL,
exclude = NULL,
ncol = 3,
nrow = NULL,
burn.in = 0,
thin = 1,
title = NULL
)Arguments
- fit
An object of class "ame" or "lame" containing MCMC samples
- params
Character vector specifying which parameters to plot: "beta" for regression coefficients, "variance" for variance components, "dynamic" for the dynamic hyperparameters, or "all" (default) for all three
- include
Character vector of specific parameter names to include. Raw names ("va", "ve", "rho", "rho_uv", "sigma_ab", ...) and their display labels ("Error variance", "Position persistence (rho_uv)", ...) are both accepted; display labels match regardless of case. For dynamic_beta fits, per-period traces are named like
"x_dyad[t1]"; supplying the base name (e.g."x_dyad") selects all of its periods- exclude
Character vector of specific parameter names to exclude. Base names match all per-period traces, as with
include- ncol
Number of columns for plot layout (default 3)
- nrow
Number of rows for plot layout (default NULL, determined automatically)
- burn.in
Number of initial iterations of each chain to exclude as burn-in (default 0, assumes burn-in already removed)
- thin
Thinning interval for display, within each chain (default 1, no thinning)
- title
Optional title for the plot
Details
This function produces two types of diagnostic plots:
- Trace plots
Show the evolution of parameter values across MCMC iterations. Good mixing is indicated by rapid exploration of the parameter space with no trends or stuck periods.
- Density plots
Show the posterior distribution of parameters. Multiple modes may indicate identification issues or convergence problems.
The plots help diagnose:
Convergence: Has the chain reached the stationary distribution?
Mixing: Is the chain exploring the parameter space efficiently?
Autocorrelation: Are successive samples highly correlated?
Parameters displayed include:
Regression coefficients (beta)
Variance components (va, vb, cab, rho, ve)
Dynamic hyperparameters, when the fit has them: the persistence and innovation of the actor effects (
rho_ab,sigma_ab) and of the latent positions (rho_uv,sigma_uv), the reset rate of snap positions (pi_snap), the per-group persistence and innovation of dynamic coefficients (rho_beta,sigma_beta), the long-run mean of each AR(1) coefficient path (mu_beta) and the per-period dyadic correlation ofdynamic_rhofits
A quantity the model fixes draws no panel: the error variance of the
binary and ordinal families (fixed at 1), the dyadic correlation
and sender-receiver covariance of bipartite fits, the dyadic correlation of
a fit with dcor = FALSE and the receiver terms of a symmetric fit.
Such a quantity is left out only when its draws are constant, so a stuck
chain on an estimated quantity always shows. A quantity that
summary() reports as not estimated (for example the effect
variances of a sparse panel) is left out as well, with a message naming it.
For a fit with several chains, burn.in and thin apply within
each chain.
A quantity with one value per period (a coefficient under
dynamic_beta, including the intercept path of a dynamic_ab
fit, and the per-period dyadic correlation of a dynamic_rho fit)
draws one panel, named "<name> by period", holding the traces and
densities of every period, each labelled with its period at the right of
the trace and above the peak of the density. A single period is selected
with include, e.g. include = "intercept[t3]". A coefficient
that a dynamic_beta fit holds static draws one panel, as in a
static fit.
Examples
# \donttest{
# Fit an AME model
data(YX_nrm)
# chains = 1 keeps the example short; the default runs two chains and compares them
fit <- ame(YX_nrm$Y, Xdyad = YX_nrm$X,
nscan = 50, burn = 10, odens = 1, verbose = FALSE, chains = 1)
# Basic trace plots for all parameters
trace_plot(fit)
# Only regression coefficients
trace_plot(fit, params = "beta")
# Only variance components
trace_plot(fit, params = "variance")
# The persistence and innovation of dynamic actor effects
Y <- lapply(1:3, function(t) YX_nrm$Y[1:20, 1:20])
fit_dyn <- lame(Y, R = 0, dynamic_ab = TRUE, nscan = 100, burn = 20, odens = 2,
verbose = FALSE, chains = 1)
trace_plot(fit_dyn, params = "dynamic")
# }