plot_compare_residuals()
View source ↗On this page
Compare the in-sample log residuals of two fits to the same survey data in four panels, by distribution, normal quantiles, time step and length bin.
Usage
plot_compare_residuals(
model1,
model2,
data.CatL,
model1_name = NULL,
model2_name = NULL,
colors = NULL,
linetypes = NULL,
line_size = NULL,
point_size = NULL,
legend_pos = NULL,
bins = 35,
qq_alpha = 0.6,
errorbar_width = NULL,
...,
title = NULL
)
Arguments
| Argument | Default | Description |
|---|---|---|
model1 | required | The first fit. |
model2 | required | The second fit. |
data.CatL | required | The survey index table used for both fits, with a length-bin column followed by one column of survey indices per time step. |
model1_name | NULL | Character or NULL. Name of the first fit in the plot and in the default title. NULL takes the model type ("ACL" or "ALSCL"); when both fits have the same type, the F structure, or 1 and 2, is added in parentheses. |
model2_name | NULL | Character or NULL. Name of the second fit in the plot and in the default title. NULL takes the model type ("ACL" or "ALSCL"); when both fits have the same type, the F structure, or 1 and 2, is added in parentheses. |
colors | NULL | Character vector of two colors, named by model or in model order. NULL uses the global compare_colors setting; fewer than two colors give a warning, and the global setting is used. |
linetypes | NULL | Character vector of two line types, with the same rules as |
line_size | NULL | Numeric or NULL. Line width. NULL uses the global compare_line_size setting. |
point_size | NULL | Numeric or NULL. Size of the points of the QQ plot and of the mean-residual panel. NULL uses the global compare_point_size setting, or point_size (default 2.5) when that is NULL. |
legend_pos | NULL | Character or NULL. Legend position, such as "bottom", "right" or "none". NULL uses the global compare_legend_pos setting (default "bottom"). Letter case does not matter. |
bins | 35 | Integer. Number of bins of the residual histogram. Default 35. |
qq_alpha | 0.6 | Numeric. Transparency of the points of the QQ plot. Default 0.6. |
errorbar_width | NULL | Numeric or NULL. Cap width, in years, of the standard deviation bars of the mean residuals. NULL (default) uses a quarter of the time step. |
... | … | The former argument name linewidth (for line_size), accepted with a warning. |
title | NULL | NULL (default) draws the default title, FALSE, NA or "" draws no title, and a character string draws that string; see |
Value
A patchwork object with four panels (the distribution of the log residuals, a normal QQ plot, the mean log residual by time step and the absolute log residual by length bin), which can be displayed or saved with acl_save_plot().
Details
Compare fits to the same observations and look at both the distributions and the systematic patterns. Similar residuals do not mean similar population estimates.
These are in-sample residuals. The time labels, length labels, positive observations and pattern of missing data must match both fits, and zero and missing observations are left out as in fitting. A saved fit without stored observations can be plotted, but its alignment with the data cannot be checked in full. title is the title of the whole figure; the panel titles stay when it is removed, because they name the panels.
Two fits of the same model get distinct default names, such as ACL (group_year) and ACL (year); model1_name and model2_name set other names, which must differ. Before matching observations to predictions, the function checks the time and length metadata of both fits against data.CatL and stops when they differ. A visual comparison does not show that the likelihoods or the biological assumptions of the two fits are comparable.
Points take the global point_size setting (default 2.5) unless the global compare_point_size setting is given; a point_size in the call takes priority. acl_theme_set(compare_point_size = NULL) returns to the global point_size.
The axis of the mean-residual panel covers every fitted time step, including a first or last time step without observations; such a time step has its place on the axis but no residual point or error bar. A time step without observations breaks the mean-residual line, isolated observed time steps are drawn as points, and a standard deviation bar is drawn only when the time step has at least two observed length bins.
Additional details from R help
These are in-sample residuals. The time labels, length labels, positive observations and missing values that a fit records must match both fitted results. Zeros and NA are left out as in fitting. For a saved fit that does not store its observations, the alignment with the data cannot be fully checked. Data frames, matrices and tibbles are converted to numbers as in fitting. The mean-residual axis keeps the full fitted time range, including time steps without observations at the start or the end, and the line breaks at time steps without observations. A time step observed alone stays visible as a point, and a standard deviation bar needs at least two observed length bins in that time step.
title is the title of the whole figure. The titles of the
panels stay when it is removed, because they name the panels.
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)
# Use objects defined above
plot_compare_residuals(a, b, dat)

plot_compare_residuals(a, b, dat)