On this page

Compare in-sample fit errors, R-squared and information criteria of two fits to the same survey data as horizontal bars.

Usage

plot_compare_metrics(
  model1,
  model2,
  data.CatL,
  model1_name = NULL,
  model2_name = NULL,
  colors = NULL,
  metrics = NULL,
  facet_ncol = NULL,
  legend_pos = NULL,
  label_size = 3.2,
  bar_width = 0.6,
  bar_alpha = 0.85,
  ...,
  title = NULL
)

Arguments

ArgumentDefaultDescription
model1required

The first fit.

model2required

The second fit.

data.CatLrequired

The survey index table used for both fits, with a length-bin column followed by one column of survey indices per time step.

model1_nameNULL

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_nameNULL

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.

colorsNULL

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.

metricsNULL

Character vector or NULL. Panels to show, from "error" (root mean squared error, RMSE, and mean absolute error, MAE), "R2" (coefficient of determination), "MAPE" (mean absolute percentage error), "IC" (Akaike and Bayesian information criteria, AIC and BIC, and the negative log-likelihood) and "npar" (number of estimated parameters). NULL (default) shows all five panels; for example metrics = c("error", "R2", "IC"). Letter case does not matter.

facet_ncolNULL

Integer or NULL. Number of columns of panels. NULL chooses it from the number of panels, and the legend fills an empty cell.

legend_posNULL

Character or NULL. Legend position, such as "bottom", "right" or "none". NULL uses the global compare_legend_pos setting. Letter case does not matter.

label_size3.2

Numeric. Size of the values printed beyond the ends of the bars, in mm as for ggplot2 text; a size below 8 pt (about 2.8 mm) is raised to 8 pt. Default 3.2.

bar_width0.6

Numeric. Height of the pair of bars of one statistic, as a share of its row. Default 0.6.

bar_alpha0.85

Numeric. Transparency of the bars. Default 0.85.

...…

The former argument name ncol (for facet_ncol), accepted with a warning.

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.

Value

A patchwork object with one panel per group of statistics, which can be displayed or saved with acl_save_plot().

Details

The panels have different units, and for some statistics smaller values are better while for others larger values are. Check the numerical checks of both fits and whether their likelihoods are comparable before reading the ranks.

Each panel has one row of horizontal bars per statistic, the first fit above the second, and prints each value with three significant digits just beyond the end of its bar. The R-squared axis runs from 0 to 1, with room for the values. title is the title of the whole figure; the panel titles stay when it is removed, because they name the panels.

The bars use the same calculation as diagnostic_metrics(), in which only finite positive observations contribute, so missing and zero observations do not make the error metrics NA. Nonfinite predictions at observed cells and mismatched dimensions are errors. Undefined measures, such as R-squared for constant observations, are NA. The figure describes the two fits; use compare_models() to check that they are comparable and pass their numerical checks before interpreting differences.

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.

The values printed at the bars use the global font_family setting.

Additional details from R help

Each panel shows one row of horizontal bars per statistic, the first model above the second, with each value printed horizontally just beyond the end of its bar, with three significant digits. R-squared runs from 0 to 1 plus room for the values.

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_metrics(a, b, dat)
Example figure
Example figure: plot_compare_metrics(a, b, dat)

See also

Search documentation