Plots residuals vs a covariate (time, observed, or predicted values), grouped by "group".
This function visualizes the residuals against time, observed, or predicted (simulated) values, helping to assess model performance.
Usage
plotResidualsVsCovariate(
plotData,
metaData = NULL,
mapping = ggplot2::aes(),
xUnit = NULL,
yUnit = NULL,
residualScale = "log",
xAxis = "observed",
...
)Arguments
- plotData
An object of class
DataCombinedor adata.table. If adata.table, it must include the following:xValues: Numeric time points.yValues: Observed or simulated values (numeric).group: Grouping variable (factor or character).name: Name for the dataset (factor or character).xUnit: Unit of the x-axis values (character).yUnit: Unit of the y-axis values (character).dataType: Specifies data type—eitherobservedorsimulated.Optional:
yErrorType: Type of y error, relative toospsuite::DataErrorType.yErrorValues: Numeric error values.yMin,yMax: Custom ranges for y-axis instead of error types.IndividualId: Used for aggregation of simulated population data.
- metaData
A list containing metadata for the plot. If NULL, a default list is constructed from the data. Expected structure includes information about dimensions and units for both x and y axes.
- mapping
A ggplot2 aesthetic mapping object. Default is
ggplot2::aes(). This is added or replaces the default mapping constructed by the data.- xUnit
A character string specifying the target unit for the time values. (only relevant if
xAxis = "time") IfNULL(default), the most frequent unit in the data is used. For available units, seeospsuite::ospUnits.- yUnit
A character string specifying the target unit for the simulated and observed y-values used for residual calculation and (if
xAxis != "time") displayed on the x-Axis. IfNULL(default), the most frequent unit in the data is used. For available units, seeospsuite::ospUnits.- residualScale
Either "linear", "log", or "ratio" method for computing residuals. Default is
log.- xAxis
A character string specifying what to display on the x-axis. Options are
"time"(time points from xValues),"observed"(observed values, default), or"predicted"(predicted/simulated values).- ...
Arguments passed on to
ospsuite.plots::plotYVsX,ospsuite.plots::plotResVsCovgeomPointAttributesA
listwith arguments which are passed on to the callggplot2::geom_pointgeomComparisonLineAttributesA
listof arguments passed toggplot2::hlineorggplot2::ablineto display comparison lines.geomLLOQAttributesA
listwith arguments which are passed on to the callggplot2::geom_hlinegroupAestheticsA character vector of aesthetic names used for grouping data points when calculating comparison statistics. Data will be grouped by combinations of these aesthetics before computing counts and proportions within comparison lines. Common grouping aesthetics include
"colour","fill","shape".addRegressionA boolean that activates the insertion of a regression line.
xScaleeither 'linear' then
ggplot2::scale_x_continuous()or 'log' thenggplot2::scale_x_log10()is usedxScaleArgslist of arguments passed to
ggplot2::scale_x_continuous()orggplot2::scale_x_log10()yScaleeither 'linear' then
ggplot2::scale_y_continuous()or 'log' thenggplot2::scale_y_log10()is usedcomparisonLineVectorA vector defining the comparison lines.
Details
Residual Calculation
Residuals are calculated by pairing observed and simulated data at matching time points within the same group. Only datasets that can be paired (i.e., have corresponding observed and simulated values) are included in the residual plot. The function automatically removes unpaired datasets with a warning, converts units to ensure consistent comparisons, and computes residuals as the difference between observed and predicted values.
Residual Scales
The residualScale parameter controls how residuals are displayed:
linear: Absolute residuals (Observed - Predicted). Values centered around zero indicate good model fit. Useful for normally distributed errors.log: Log-transformed residuals, calculated as log(Observed / Predicted). Values centered around zero indicate good fit. Preferred for log-normally distributed data or when errors are proportional to magnitude.ratio: Ratio of observed to predicted (Observed / Predicted). Values centered around 1.0 indicate good fit. Useful for understanding relative prediction error.
See also
Other plot functions based on ospsuite.plots:
plotPredictedVsObserved(),
plotQuantileQuantilePlot(),
plotResidualsAsHistogram(),
plotTimeProfile()
Examples
if (FALSE) { # \dontrun{
# Generate a residuals vs observed plot for the provided data
plotResidualsVsCovariate(
myDataCombined,
xUnit = ospUnits$Time$h,
yUnit = ospUnits$`Concentration [mass]`$`µg/l`,
xAxis = "time",
residualScale = 'linear'
)
# Generate a residuals vs predicted plot
plotResidualsVsCovariate(myDataCombined, xAxis = "predicted")
# Generate a residuals vs time plot
plotResidualsVsCovariate(myDataCombined, xAxis = "time")
} # }