Extracts the standard model-checking diagnostics for a fitted
fit_gamm() object, packaged as plain numeric tables and data frames
so a caller (e.g. the Shiny app) can render them however it likes.
Mirrors what mgcv::gam.check() reports, but returns the pieces as
data rather than drawing base-graphics plots.
Arguments
- gamm_obj
An object of class
"shinytone_gamm"fromfit_gamm().
Value
An S3 object of class "shinytone_gamm_diag", a list with:
resid_df: data frame withresidual(deviance),fitted, andobserved.k_check: data frame version ofmgcv::k.check()with aSmoothcolumn and ak_flagcolumn ("ok","low", or"na"), orNULLif it could not be computed.concurvity: data frame version ofmgcv::concurvity(), orNULL.acf: data frame withlagandacf(per token; AR1-whitened when the fit used an AR1 correction).acf_ci: half-width of the white-noise confidence band.acf_grouped,acf_whitened: logicals recording whether the ACF was computed per token and whether it was AR1-whitened.n: number of residuals.rho,use_ar1: AR1 information carried over from the fit.family: the model's error family.
Details
The returned diagnostics answer the three questions that decide whether a GAMM fit can be trusted:
Are the residuals well-behaved?
resid_dfholds the deviance residuals, fitted values, and observed response, enough to draw a Q-Q plot, a residuals-vs-fitted plot, a residual histogram, and an observed-vs-fitted plot.Is the basis dimension
klarge enough?k_checkismgcv::k.check()'s table (k', edf, k-index, p-value) with the term names mapped back to the user's columns. Ak_flagcolumn marks the spline smooths where a low k-index together with a small p-value suggestskis too low and the model should be refitted with a largerk. Random-effect terms (by-speaker / by-item) are flagged"na"because the k-index is not a meaningful check for them.Is there leftover temporal autocorrelation?
acfis the lag / autocorrelation of the residuals with the white-noise band inacf_ci. Autocorrelation decaying slowly across lags is the signal that an AR1 correction (use_ar1 = TRUEinfit_gamm()) is worth turning on.
concurvity is mgcv::concurvity()'s full table (0-1, higher means
more confounding between smooths) when it can be computed.
The ACF is computed per token (the residuals are split by token and
the within-token lag products are pooled, in the spirit of
itsadug::acf_resid()) so that token boundaries do not contaminate the
low lags — important because f0 tokens are short. When the fit used an
AR1 correction the residuals are first whitened within each token
(e_i - rho * e_{i-1}, reset at each token start), so the ACF then
shows whether the AR1 term actually removed the autocorrelation rather
than the pre-correction picture. acf_grouped / acf_whitened record
which path was taken (a plain concatenated ACF is used only as a
fallback if residuals cannot be aligned to tokens).
References
Wood, S. N. (2017). Generalized Additive Models: An Introduction with R (2nd ed.). Chapman and Hall/CRC.
See also
fit_gamm() for the model fit, mgcv::gam.check() and
mgcv::k.check() for the underlying diagnostics.
Examples
if (FALSE) { # \dontrun{
data(sample_f0)
normed <- normalise_f0(sample_f0, f0 = "f0_Hz",
speaker = "speaker", tone = "tone")
gamm <- fit_gamm(normed, f0 = "f0_st", time = "time", token = "token",
tone = "tone", speaker = "speaker", item = "char")
diag <- diagnose_gamm(gamm)
head(diag$k_check)
} # }
