View source: R/predictive_checks_residuals.R
| check_dyad_residuals | R Documentation |
A model should reproduce how responses are distributed around its
predictions. This check finds where each observed outcome falls among its
simulated values (PIT residuals), using complete datasets from
simulate_dyad_responses(). This helps identify mismatches in the model's
assumptions, such as the distribution family, dispersion, or predictor
effects.
For checks on the outcome scale, also run check_dyad_outcomes() with the
same simulations.
For guidance on how to read the plots, see plot.dyadMLM_residual_check().
check_dyad_residuals(
simulations,
dyad = NULL,
role = NULL,
member = NULL,
predictors = NULL,
seed = 123,
plot = TRUE,
ask = NULL,
panels = TRUE,
data = NULL,
quartile_lines = c("bins", "qgam")
)
simulations |
An object from |
dyad |
Dyad ID column, with or without quotes. Required when |
role |
Role column, with or without quotes, such as gender. |
member |
Person ID column, with or without quotes. Required with |
predictors |
Optional quoted column names, e.g. |
seed |
Seed for randomized PIT residuals. The caller's random-number
state is restored afterwards. With |
plot |
If |
ask |
Whether to pause between figures on an interactive device.
|
panels |
If |
data |
The unchanged data used to fit the model. Supply it when the
columns you name (such as |
quartile_lines |
Quantile regression lines ( |
Invisibly returns a dyadMLM_residual_check list with the pit matrix
(after any centring) and calculated summaries in compositions. pit has
one row per fitted observation and one column per dataset (observed first,
then all simulated). Optionally, suppress plotting with plot = FALSE and
draw the saved result later with plot(result), which also takes ask and
panels.
Complete simulated datasets keep the fitted partner and time dependence and
any modeled differences in variability between roles. Unlike DHARMa's default
(since version 0.5.0), simulations redraw all random effects. Dyadic models
often fix the residual variance near zero (dispformula = ~ 0) and model the
partners' residual variances and covariance as random effects. If held
fixed, these random effects would reproduce the data almost exactly.
Parameters stay fixed at their fitted values, so parameter uncertainty is not
included. These are descriptive checks, not significance tests, and
differ from DHARMa's plots and tests (see the
distribution-checks article).
They do not directly assess serial correlation.
Predicted outcomes are mean predictions with random effects set to zero (see
simulate_dyad_responses()). The checks use the same fitted rows and
families as simulate_dyad_responses() and weight all observations equally.
Check partner correlations with check_partner_dependence().
The internal calculation follows the simulation-based PIT approach used in Florian Hartig's DHARMa package and the randomized quantile residual method of Dunn and Smyth (1996). We implement the definition directly and keep the uniform 0–1 scale.
At each fitted row, rank the observed and all simulated outcomes together,
breaking ties randomly. For rank r among m datasets, use (r - U) / m,
where U is uniform between 0 and 1.
When the model freely estimates an overall location (an intercept or separate role means) and is not ordinal, PIT is centred automatically: each dataset's PIT is transformed to normal scores, centred on its median across all fitted rows, and transformed back. Panels then show residuals relative to this overall location, so all panels can show less of an overall shift. Centring also partly offsets how fitting makes departures look smaller.
Global envelopes use extreme-rank-length ordering (Myllymäki et al., 2017).
Dunn, P. K., & Smyth, G. K. (1996). Randomized quantile residuals. Journal of Computational and Graphical Statistics, 5(3), 236–244. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.2307/1390802")}
Fasiolo, M., Wood, S. N., Zaffran, M., Nedellec, R., & Goude, Y. (2021). qgam: Bayesian nonparametric quantile regression modeling in R. Journal of Statistical Software, 100(9), 1–31. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.18637/jss.v100.i09")}
Hartig, F. (2026). DHARMa: Residual diagnostics for hierarchical (multi-level/mixed) regression models (Version 0.5.0) [R package]. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.32614/CRAN.package.DHARMa")}
Myllymäki, M., Mrkvička, T., Grabarnik, P., Seijo, H., & Hahn, U. (2017). Global envelope tests for spatial processes. Journal of the Royal Statistical Society: Series B (Statistical Methodology), 79(2), 381–404. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1111/rssb.12172")}
check_dyad_outcomes(), check_partner_dependence()
example_data <- prepare_dyad_data(
dyads_cross,
dyad = coupleID,
member = personID,
model_types = "none",
seed = 123
)
# Exchangeable dyads: the two random-effect blocks represent the partners'
# residual variances and covariance.
model <- glmmTMB::glmmTMB(
closeness ~ gender + provided_support +
us(1 | coupleID) + us(0 + .member_contrast_arbitrary | coupleID),
dispformula = ~ 0,
data = example_data
)
simulations <- simulate_dyad_responses(
model,
nsim = 200, # Low for a quick example. Use at least 1,000.
seed = 123
)
check_dyad_residuals(
simulations,
dyad = coupleID,
role = gender,
predictors = "provided_support",
ask = FALSE
)
# Save a pooled check without drawing, then plot each figure separately.
result <- check_dyad_residuals(
simulations,
role = NULL,
predictors = "provided_support",
plot = FALSE
)
plot(result, panels = FALSE, ask = FALSE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.