Skip to contents

Computes goodness-of-fit statistics after model estimation by generating posterior predictive networks from the saved MCMC samples. This is useful when the model was fitted with gof = FALSE to speed up MCMC sampling, or when you want to evaluate custom GOF statistics without re-running the model.

Usage

gof(fit, Y = NULL, custom_gof = NULL, nsim = 100, verbose = TRUE)

Arguments

fit

An ame or lame model object that was run with posterior sampling enabled, or an ALS fit (method = "als", ame_als, lame_als)

Y

Original data. For ame objects, an n x n matrix (or nA x nB for bipartite). For lame objects, a list of matrices (one per time period). If NULL, extracted from the fit object.

custom_gof

Optional custom GOF function(s), each accepting one period's matrix. A function can return a named numeric vector; a list contains scalar-valued functions. Observed and replicated statistics are both averaged across the same fitted periods.

nsim

Number of posterior predictive simulations to generate (default 100). Posterior rows are sampled with replacement, so this may exceed the number of stored rows. If NULL, uses the number of post-initial rows.

verbose

Logical; print progress information

Value

A (nsim + 1) x n_stat matrix of GOF statistics: the first row holds the observed statistics, the remaining rows the posterior predictive ones. For an ame fit this is the format of fit$GOF; a lame fit stores its own goodness of fit as a per-statistic list of period-by-draw matrices instead, so the two are not interchangeable. Attributes replication_method and draw_index record joint replication and the sampled MCMC rows.

Details

Gaussian bipartite checks use weighted four.cycle.dep; a nonzero count would be fixed by the missing-cell pattern. Each replicate is evaluated on the same observed cells as its corresponding data slice, aligning actor labels before applying the mask. Named data periods are matched to the fit; unnamed periods must be in fitted order. All custom functions receive individual matrices for both observed and replicated data, and their values are averaged over periods with equal weight.

For the ordinal family the posterior predictive draws are quantile-matched to the observed category frequencies (the cutpoints are induced from the data), so the simulated marginal distribution always equals the observed one: the GOF statistics and YPM cannot show a marginal misfit for ordinal outcomes and only assess the dependence structure (degree spread, dyadic and triadic dependence).

A panel whose actor set changes is checked period by period on the actors present in each period, as the sampler's own goodness of fit is; an actor absent from a period has no observed cell there.

An ALS fit has no posterior draws: its replicates are drawn from simulate.ame_als at the estimate, with the noise resampled (attribute replication_method "point estimate"), the replicates gof_plot draws for it. The parameters are held at the estimate; the posterior predictive check, which also draws them, is the one of an ame or lame (MCMC) fit.

For a sampler fit this function requires joint posterior replication and refuses fits that lack aligned saved draws; it never substitutes conditional mean trajectories. This function requires that the model was estimated with posterior sampling of the parameters needed to generate posterior predictive datasets. Specifically, it needs:

  • BETA: regression coefficients

  • VC: variance components

  • For models with random effects: samples of a, b

  • For models with latent factors: U_samples, V_samples

To enable posterior sampling during model estimation, use: posterior_opts = posterior_options(save_UV = TRUE, save_ab = TRUE, thin_UV = 1, thin_ab = 1). At rank zero, omit save_UV.

Computing GOF post-hoc has several advantages:

  • Faster MCMC sampling (no GOF overhead)

  • Can experiment with different GOF statistics without re-running model

  • Can control number of posterior predictive simulations independently

See also

Author

Cassy Dorff, Shahryar Minhas, Tosin Salau

Examples

# \donttest{
# Run model without GOF during fitting
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, R = 2, gof = FALSE,
           nscan = 100, burn = 10, odens = 1, verbose = FALSE,
           posterior_opts = posterior_options(save_ab = TRUE, save_UV = TRUE,
                                              thin_ab = 1, thin_UV = 1), chains = 1)

# Compute GOF post-hoc
gof_result <- gof(fit)
#> ℹ Computing GOF statistics post-hoc
#> ℹ Using 100 posterior predictive simulations
#> ✔ GOF computation complete
# }