Plot Observed vs. Fitted Cases
Description
This function creates a time series plot comparing observed cases with fitted values (i.e., mean predicted counts) from one or more models in a GHRmodels object.
plot_fit(
models,
mod_id = NULL,
time = NULL,
unit = NULL,
unit_id = NULL,
ci = FALSE,
aggregate_space = NULL,
mod_label = NULL,
palette = "IDE2",
ref_color = NULL,
obs_color = NULL,
obs_label = NULL,
title = "",
transform = "identity",
xlab = "Time",
ylab = "Cases",
xlim = NULL,
legend = "Model"
)Arguments
models: AGHRmodelsobject containing fitted model output.mod_id: Character vector of model identifiers (frommodels$mod_gof$model_id) to plot.time: Character; name of the time variable column inmodels$data. Its values must be in date format (“yyyy-mm-dd”) representing the day of observation for daily data, the first day of the week for weekly, or the first day of the month for monthly observations.unit: Optional; character name of the column inmodels$datathat identifies the different units for which a time series is available, typically a spatial unit.unit_id: Optional character vector of specific unit values to subset ifunitis provided. Cannot be used ifaggregate_spaceis also used.ci: Logical; ifTRUE, adds 95% credible interval ribbons for model fits. Can only be used ifaggregate_spaceis NULL.aggregate_space: Name of variable used to define spatial aggregation groups that contain the different units. Counts within that aggregation group will be summed. Use"all"to aggregate all spatial units. Cannot be used with models using a gaussian family.mod_label: Optional custom labels for each model. Can be a named vector (e.g.,c("mod1" = "Base")) or an unnamed vector with the same length and order asmod_id.palette: Character; name of the color palette for fitted lines. Default is"IDE2".ref_color: Optional color to override the first model’s line (reference model).obs_color: Color for observed data line. Default is"black".obs_label: Legend label for observed data. Default is"Observed".title: Character; title of the plot.transform: Character string for y-axis transformation. Defaults to"identity"(no transform). Other options include"log10p1","log1p","sqrt", etc.xlab: Label for the x-axis. Default is"Time".ylab: Label for the y-axis. Default is"Cases".xlim: Date vector of length two (e.g.,as.Date(c("2010-01-01", "2020-12-31"))). UseNAto leave one side open (e.g.,as.Date(c("2015-01-01", NA))).legend: Legend title for model lines. Default is"Model".
Returns
A ggplot2 object with the time series line plot of observed vs fitted cases.
See Also
fit_models to generate GHRmodels.
Examples
# Load example GHRmodels object from the package:
model_list_file <- system.file("examples", "model_list.rds",
package = "GHRmodel")
model_list <- readRDS(model_list_file)
# Plot observed vs. fitted cases over time for three selected models
plot_fit(
models = model_list,
mod_id = c("mod1", "mod3", "mod5"),
mod_label = c("Baseline", "tmin.l1.nl", "pdsi.l1.nl_tmin.l1.nl"),
time = "date",
unit = "micro_name",
xlim = as.Date(c("2015-01-01", NA))
)