check_fit()
View source ↗On this page
Check whether the survey-index residuals of a fit show patterns over time and length, correlation between time steps or departures from normality.
Usage
check_fit(fit, max_lag = 5)
Arguments
| Argument | Default | Description |
|---|---|---|
fit | required | An ACL or ALSCL fit, or a saved fit read with |
max_lag | 5 | Positive integer. Maximum lag in model time steps; for a quarterly model four time steps are one year. |
Value
A list with residuals (the table of standardized residuals), lag_correlations (the columns length, lag, elapsed_time, n_pairs, correlation and status), qq_data (the pooled residuals of the QQ plot), plots (heatmap, lag and qq), time_step and notes on interpretation. A correlation with fewer than three pairs or with constant values is NA, and status gives the reason.
Details
residuals(fit, type = "log") returns a long table of the observed minus the fitted log index; type = "standardized" divides it by the fitted standard deviation of the observations. The table keeps the observations, the model times and the length coordinates and marks the excluded cells; zero or missing observations have NA residuals. check_fit() uses the standardized residuals. Missing time steps are kept as gaps, so a lag of one is always one model time step. A correlation needs at least three pairs of finite values at the requested lag, and values that are not constant on either side. The residuals are conditional on the fitted process deviations, so these are descriptive checks, not tests of independence or of the adequacy of the model. The lag and QQ plots follow the grid setting of acl_theme_set(); the heat map has no grid lines.
The quantile-quantile (QQ) plot shows the pooled standardized residuals against standard normal quantiles, with the line of equality as the reference. Under a correct model the residuals do not follow this line either. The fitted process deviations absorb part of the observation error, so the points of a correctly specified model have a slope below one (0.91 to 0.96 in simulation tests with the package examples) and lie inside the line at both tails. Read curvature and asymmetry of the points as signs of skewness or heavy tails, not their distance from the line. A standard deviation close to one does not show a good fit, and a value below one does not show that the observation error is overestimated. No simulation envelope is drawn.
Additional details from R help
Time steps without observations are kept, so lag one is always
one time step, also for quarterly data, and correlations use
observations separated by the requested time lag. The checks are
descriptive. They use the fitted random effects, so they are not
independent residual tests, and they do not show that the model is
adequate. The plots follow the settings of
acl_theme_set(); the heat map never shows grid lines. Their titles are
the title keys check_residuals, check_lag and check_qq, which
acl_theme_set(titles = ) changes or removes;
acl_theme_set(show_title = FALSE) removes all three.
The quantile-quantile (QQ) plot shows the pooled conditional log residuals divided by the fitted observation standard deviation (SD) against standard normal quantiles, with the line of equality as the reference. This reference is not the distribution of the residuals under a correct model, because the fitted random effects absorb part of the observation error, so the points of a correctly specified model have a slope below one (0.91 to 0.96 in simulation tests with the package examples) and lie inside the line at both tails. Read curvature and asymmetry of the points, not their distance from the line, as signs of skewness or heavy tails. No simulation envelope is drawn.
Examples
Run the setup before the example. Fitting examples require an R-compatible C++ toolchain; simulation and export examples may write files.
Example setup
library(ALSCL)
library(ggplot2)
library(patchwork)
data("YTF_example")
x <- YTF_example
inputs <- x[c("data.CatL", "data.wgt", "data.mat")]
dat <- x$data.CatL
a <- run_acl(x, silent = TRUE)
b <- run_alscl(x, silent = TRUE)
checks <- check_fit(b, max_lag = 4)
head(residuals(b, type = "standardized"))
checks$lag_correlations
checks$plots$heatmap
checks$plots$lag
checks$plots$qq

check_fit(b, max_lag = 4)$plots$heatmap