Evaluate and Calibrate Models

Model evaluation compares estimates with observations. Calibration searches a parameter space for configurations that improve a selected metric. Keep these steps separate: evaluation is meaningful on any data set, while calibration must be judged on data not used by the optimizer.

This page is the final part of the specification → simulation → visualization and evaluation workflow. Start here after the model definition, scenarios, and output contract have already been checked.

For parameter effects rather than agreement with observations, use the separate Sensitivity Analysis guide.

Compare two data frames

using Cropbox
using DataFrames

obs = DataFrame(
    time = [1, 2, 3]u"d",
    observed_mass = [10, 20, 30]u"g",
)

est = DataFrame(
    time = [1, 2, 3]u"d",
    predicted_mass = [11, 19, 31]u"g",
)

evaluate(obs, est;
    index = :time,
    target = :observed_mass => :predicted_mass,
    metric = :rmse,
)
1.0 g

The pair maps an observation column to an estimate column. Rows are joined by the index rather than assumed to be in the same order.

For two tables, omitting index uses their :time columns. When index names differ, use a pair such as index = :hour => :time; compatible units are normalized without modifying either input. DataFrame views are also accepted. Unmatched rows are excluded, as are rows missing any of the requested targets. If no complete matching rows remain, evaluation raises an ArgumentError instead of returning a score. Non-finite values such as NaN are not dropped.

Choose a metric

RMSE and MAE are convenient starting points. Both the two-table and system forms support the following metrics:

MetricSymbolNotes
Root mean square error:rmseDefault; retains the target unit
Normalized RMSE:nrmseRMSE divided by observation mean
Root mean square percentage error:rmspeRelative residuals
Mean absolute error:maeLess sensitive to large errors than RMSE
Mean absolute percentage error:mapeRelative absolute residuals
Nash–Sutcliffe efficiency:efRelative to the observation mean model
Refined index of agreement:drAgreement measure with bounded interpretation

Percentage metrics are undefined or unstable when observations are zero or near zero. Inspect the data before selecting a metric. A low aggregate error can also hide stage-specific bias, so pair metrics with residual plots.

Error metrics are better when lower. Efficiency and agreement (:ef and :dr) are better when higher, with one indicating a perfect match. calibrate minimizes its objective, so use an error metric or convert a higher-is-better score to a loss such as 1 - score; do not minimize :ef or :dr directly.

Evaluate a system directly

@system LinearGrowth(Controller) begin
    time(context.clock.time) ~ track(u"d")
    rate => 10               ~ preserve(parameter, u"g/d")
    mass(rate)               ~ accumulate(u"g")
end

obs2 = DataFrame(
    time = [1, 2, 3]u"d",
    mass = [9, 21, 29]u"g",
)

config = @config Clock => :step => 1u"d"

evaluate(LinearGrowth, obs2;
    config,
    index = :time,
    target = :mass,
    stop = 3u"d",
    metric = :mae,
)
1.0 g

Cropbox omits rows missing any requested observation target, creates snapshots at observation indices, normalizes compatible index units, and joins rows before applying the metric. No complete observations or no matching output raises an ArgumentError. Check the index, time zone, stop, and snap in that case. Partial matches still use the matching subset: a low score alone does not show that every observation was compared. A warning reports how many complete observations matched, for example 2 of 3 complete observations matched. Rows missing any requested observation target are excluded from this total; for two tables, a match must also have complete estimates. Each observation row is counted once, even if multiple estimates match it. With configs, coverage is combined across configurations, so a site-specific run need not match other sites. This count does not change how duplicate matches contribute to the score. Use warn = false to suppress only this matching warning; it does not change the selected rows, metric, or errors for unusable observations. An explicit snap is preserved and may intentionally select only some indices.

Multiple targets

obs3 = DataFrame(
    time = [1, 2, 3]u"d",
    mass = [9, 21, 29]u"g",
    rate = [10, 10, 10]u"g/d",
)

evaluate(LinearGrowth, obs3;
    config,
    index = :time,
    target = [:mass, :rate],
    stop = 3u"d",
)
(1.0 g, 0.0 g d^-1)

Multiple targets return a tuple of metrics. Their dimensions and magnitudes may differ, which becomes important during multi-objective calibration.

Calibrate one parameter

Use the first three observations for calibration and keep later observations out of the search. This small example runs during the documentation build with a limited optimizer budget. It demonstrates the workflow, not a validation of a biological growth model.

calibration_data = obs2
validation_data = DataFrame(
    time = [4, 5, 6]u"d",
    mass = [41, 48, 62]u"g",
)

fitted = calibrate(LinearGrowth, calibration_data;
    config,
    index = :time,
    target = :mass,
    parameters = LinearGrowth => :rate => (0, 20),
    stop = 3u"d",
    metric = :rmse,
    seed = 42,
    optim = (MaxSteps = 100, PopulationSize = 20, TraceMode = :silent),
)

Config for 1 system:

LinearGrowth
rate=9.84641 g d^-1

The bounds inherit rate's declared unit u"g/d". The result is a Config that can be merged with the base scenario. The fitted rate should be close to 9.9u"g/d"; exact optimizer results can vary between package versions. LinearGrowth is deterministic, so only the search uses randomness here. For a model with ± parameters, see model sampling and the calibration seed.

Evaluate validation data

Use the same fitted configuration for both scores. The observations reserved for validation were not supplied to calibrate.

fitted_config = @config(config + fitted)

scores = DataFrame(
    data = ["calibration", "validation"],
    rmse = [
        evaluate(LinearGrowth, calibration_data;
            config = fitted_config, index = :time, target = :mass, stop = 3u"d"),
        evaluate(LinearGrowth, validation_data;
            config = fitted_config, index = :time, target = :mass, stop = 6u"d"),
    ],
)
2×2 DataFrame
Rowdatarmse
StringQuantity…
1calibration0.951472 g
2validation2.05423 g

This example uses later observations for validation. In a real study, using separate site-years, treatments, or independent experiments for validation may be more suitable than splitting rows from one trajectory.

Join predictions to observations before subtracting them. Keeping the columns named makes the residual direction explicit:

predictions = simulate(LinearGrowth;
    config = fitted_config,
    index = :time,
    target = :predicted => :mass,
    stop = 6u"d",
)

residuals = innerjoin(validation_data, predictions; on = :time)
residuals.residual = residuals.predicted .- residuals.mass
residuals
3×4 DataFrame
Rowtimemasspredictedresidual
Quantity…Quantity…Quantity…Quantity…
14 d41 g39.3856 g-1.61437 g
25 d48 g49.232 g1.23203 g
36 d62 g59.0784 g-2.92156 g

A negative residual means underprediction. Always check the number of matched rows: an inner join omits unmatched indices, and evaluation does not interpolate between model updates.

p = visualize(residuals, :time, :residual;
    backend = :Makie,
    kind = :scatterline,
    xlab = "Time",
    ylab = "Predicted - observed mass",
    title = "Validation residuals",
    legend = false,
)
visualize!(p, [4, 6]u"d", [0, 0]u"g";
    kind = :line,
    color = :lightgray,
    legend = false,
)
Example block output

Also examine residuals against temperature, development stage, and fitted values. Patterns in these plots can reveal timing errors or changing error spread that a single score hides.

Calibrate across environments

Pass configs when one biological parameter set should fit several site-years. Each entry must already contain its shared base settings as well as its environment-specific patch; unlike simulate, calibrate does not accept a nonempty config and configs together.

fitted = calibrate(Model, observations;
    configs = environment_configs,
    index = [:year, :date],
    target = [:emergence, :maturity],
    parameters = Phenology => (
        Tb = (-5, 15),
        requirement = (100, 2000),
    ),
    stop = :finished,
)

Calibration calculates each environment's metric separately, then sums those scores for each target. In contrast, evaluate(...; configs) pools the matching rows before calculating a metric. These objectives are not interchangeable: a sum of site-year RMSE values gives each site-year one score, while pooled RMSE also reflects how many observations each site-year contains. Ensure observation indices uniquely identify rows across environments and choose the objective deliberately.

To avoid repeated messages during optimization, calibrate warns only for the first finite candidate with partial coverage. Those counts describe that candidate, not necessarily the selected solution. Evaluate the fitted configuration afterwards to check its coverage. The warning does not penalize partial matches or change the objective. Each calibrate call starts a new warning scope. In an outer loop or an interactive callback, use warn = false when the repeated warning is not useful, and evaluate the fitted configuration afterwards with warnings enabled. This is separate from verbose = false, which hides simulation progress, and optim = (; TraceMode = :silent), which hides the optimizer's progress log.

Multiple objectives

For several targets, weight changes their aggregate importance. With pareto=true, calibration returns configurations along a Pareto frontier rather than one weighted compromise.

frontier = calibrate(Model, observations;
    configs = environment_configs,
    index = [:site, :date],
    target = [:leaf_area, :bulb_mass],
    parameters = parameter_bounds,
    weight = [0.5, 0.5],
    pareto = true,
    stop = :finished,
)

Equal numerical weights do not imply equal scientific importance when targets have different metrics or scales. Normalize deliberately and report the choice.

A defensible workflow

  1. Validate input units, indices, and missing data.
  2. Run the base model and inspect process-level outputs.
  3. Choose identifiable parameters with defensible bounds.
  4. Split environments or years into calibration and validation sets.
  5. Start with a small optimizer budget and one target.
  6. Inspect convergence and residual structure.
  7. Increase the budget only after the pipeline is correct.
  8. Evaluate the fitted configuration on the validation data.
  9. Report bounds, metric, weights, optimizer options, and package versions. Record the seed (default 0) to reproduce both the parameter search and its model runs.

Calibration cannot repair a structural error, an incorrect unit, or a mismatched observation index. Diagnose those before expanding the search.