On this page

Plot the log residuals of the survey index, the observed minus the fitted log index, by length bin over time or by time step over length.

Usage

plot_residuals(
  model_result,
  f = 0.4,
  line_color = NULL,
  smooth_color = NULL,
  hline_color = NULL,
  line_size = NULL,
  facet_scales = "free",
  facet_ncol = NULL,
  facet_by = c("length", "year"),
  resid_cap = NULL,
  return_data = FALSE,
  title = NULL,
  xlab = NULL,
  ylab = NULL,
  font_family = NULL,
  title_size = NULL,
  axis_title_size = NULL,
  axis_text_size = NULL,
  strip_text_size = NULL,
  legend_text_size = NULL,
  x_breaks = NULL,
  base_theme = NULL,
  title_hjust = NULL,
  ...
)

Arguments

ArgumentDefaultDescription
model_resultrequired

A fit from run_acl() or run_alscl(), retaining report and observation information.

f0.4

Numeric greater than 0 and at most 1. Span of the loess trend, the proportion of points that influence the trend at each value; larger values give smoother trends. The span covers at least five observations. Default 0.4.

line_colorNULL

Character or NULL. Color of the residual lines and points. NULL inherits the global line_color setting.

smooth_colorNULL

Character or NULL. Color of the dashed trend lines. NULL inherits the global smooth_color setting.

hline_colorNULL

Character or NULL. Color of the zero reference line. NULL inherits the global hline_color setting.

line_sizeNULL

Numeric or NULL. Line width. NULL inherits the global line_size setting (1 under the default style, 1.5 under "classic"). While that setting keeps the value of the style, facets of more than four panels draw thinner lines.

facet_scales"free"

Character. Axis scales of the panels, "fixed", "free", "free_x" or "free_y". Letter case does not matter.

facet_ncolNULL

Integer or NULL. Number of columns of panels. NULL chooses it from the number of panels, for example 5 × 3 for 15 panels and 6 × 4 for 23.

facet_byc("length", "year")

Character. "length" (default) draws one panel per length bin with time on the x axis; "year" draws one panel per time step with length on the x axis. Letter case does not matter.

resid_capNULL

Finite positive number or NULL. Caps the displayed residuals at plus and minus this value and prints a message with the number of capped residuals and their length bins; NULL (default) leaves them uncapped. The cap changes the display only.

return_dataFALSE

Logical. TRUE returns a list with the plot and the plotted data, described under the return value. Default FALSE.

titleNULL

NULL (default) draws the default title, FALSE, NA or "" draws no title, and a character string draws that string; see acl_theme_set() for the default titles and the show_title setting.

xlabNULL

Character or NULL. Label of the x axis. NULL uses the global theme setting.

ylabNULL

Character or NULL. Label of the y axis. NULL uses the global theme setting.

font_familyNULL

Character or NULL. Font family of the text, which must be installed on the computer. NULL uses the global theme setting (default "sans").

title_sizeNULL

Numeric or NULL. Size of the plot title in pt. NULL uses the global theme setting (default 14).

axis_title_sizeNULL

Numeric or NULL. Size of the axis titles in pt. NULL uses the global theme setting (default 12).

axis_text_sizeNULL

Numeric or NULL. Size of the axis tick labels in pt. NULL uses the global theme setting (default 10).

strip_text_sizeNULL

Numeric or NULL. Size of the panel labels in pt. NULL uses the global theme setting (default 10).

legend_text_sizeNULL

Numeric or NULL. Size of the legend text in pt. NULL uses the global theme setting (default 10).

x_breaksNULL

Numeric vector or NULL. Positions of the x-axis breaks. NULL inherits the global x_breaks setting when at least one of its values lies on this axis; otherwise the breaks are chosen automatically.

base_themeNULL

Character or NULL. Name of the base ggplot2 theme, such as "bw", "minimal" or "theme_bw". NULL uses the global theme setting. Letter case does not matter.

title_hjustNULL

Numeric or NULL. Horizontal position of the title, 0 for left, 0.5 for centered and 1 for right. NULL uses the global theme setting.

...…

The former argument name type, accepted with a warning.

Value

A ggplot object; with return_data = TRUE, a list with the plot plot and the plotted data data (columns residual, LengthGroup and year for facet_by = "length"; length, Year and residual for facet_by = "year").

Details

Residuals are the observed log survey index minus the fitted log index Elog_index. facet_by = "length" draws one panel per length bin with time on the x axis; facet_by = "year" draws one panel per model time step, including quarterly time steps, with length on the x axis. Look at the structure of the residuals over time and length as well as at their size.

Within each panel, a missing or excluded observation breaks the residual line and its trend, and a single observed value is drawn as a point. A run of 2-5 consecutive observations has a linear trend, and a run of 6 or more a loess trend.

The plot uses the saved observations, so it shows the same positive observations as the fit; a fit without observations takes them from the data of its TMB object obj, and a fit that keeps observations can be plotted after obj is removed. Missing values and zeros excluded from the fit never become observations or zero residuals, so a gap in the plot is not a good fit.

Both layouts use the title, xlab, ylab and title_hjust given in the call, for example plot_residuals(b, facet_by = "year", title = "Survey residuals", xlab = "Length (cm)", ylab = "Log residual", title_hjust = 0). Grid lines follow the grid setting of acl_theme_set(); minor grid lines are never 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)
# Use objects defined above
plot_residuals(b, facet_by = "year", line_size = 0.6)
Example figure
Example figure: plot_residuals(b, facet_by = "year", line_size = 0.6)

See also

Search documentation