Skip to content

Calibration Fit (R)

Goodness-of-fit statistics for a calibrated model, computed from the long table that Wide_To_Long writes from the output of an Experimenter Run or Optimization. The script takes the simulated series from the job rows and the observed series from the saved-run rows. It matches them on variable and on time, and scores each variable. Experimenter computes no fit statistics, so this part happens outside the application, where a person can read it and change it.

The app bundles this script as Calibration_Fit.R. Type the bare file name as a Script’s File, or pick it with Bundled scripts… on a Script row. A copy beside the Experiment file runs in place of the bundled one. See Run your own R or Python. Back to the Scripts.

Calibration_Fit.R needs Rscript. Base R only, no package. It agrees with Calibration_Fit.py (the Python folder) to 1e-9 on the same input, which the test suite checks on every fixture.

Requirements

LanguageNeeds
RR 4.3 or later. No package.

The test suite runs on these versions in GitHub Actions. Older versions are not tested.

Inputs and Outputs

This script does one task. It computes the fit. It does not convert the layout. Run wide-to-long before it, in the same folder.

InputSourceRead as
The folderFirst command-line argument, then EXPERIMENTER_OUTPUT_DIRThe folder that holds the converter’s file
Wide_To_Long.csvInside the folder. Wide_To_Long_<dataset>.csv when EXPERIMENTER_DATASET has a valueThe simulated and observed series, one long table
The name of the saved runEXPERIMENTER_COMPARISON_RUN, optionalA source value on rows whose source_kind is saved_run
The dataset nameEXPERIMENTER_DATASETNames the input and the output file. Without it, the output is Calibration_Fit.csv. Filters the input when it holds more than one dataset
The Experimenter versionEXPERIMENTER_VERSIONCompared against the version this script was written for

The columns are source, source_kind, variable, value, a time column named for the time unit of the model (for example Year), and any of sequence, dataset, level, group, sample a Run added as context. source_kind reads job on a simulated row and saved_run on an observed row.

OutputWritten toHolds
Calibration_Fit.csvBeside the input, in the same folderOne row per shared variable, plus a Mean summary row. Written when no dataset name is given. The Dataset column reads Base
Calibration_Fit_<dataset>.csvBeside the input, in the same folderThe same table for one dataset. Written when EXPERIMENTER_DATASET has a value

The script writes no roll-up for a sweep. Collect_Datasets skips Calibration_Fit.csv, because it names no dataset. To get one file for every dataset, run collect-datasets after it. Collect_Datasets --stem Calibration_Fit reads each Calibration_Fit_<dataset>.csv and writes Calibration_Fit_All.csv in the parent folder. It rebuilds that file from the files present, so the roll-up keeps every dataset that has a file, the one that just ran included. The chaining rules apply.

A run order with the chain:

model: Model.stmx
runs:
  fit: {}
  to-long:
    script: "Rscript Wide_To_Long.R"
  score:
    script: "Rscript Calibration_Fit.R"

Each output row carries Dataset, Variable, N, RMSPE, R2, Theil_U, Theil_Um, Theil_Us and Theil_Uc.

Core Mechanics

For each variable that is in both series, over the times the two share, the script computes:

RMSPE, the root mean square percentage error, in percent1. A time where the observed value is zero or missing does not contribute.

R squared against the observed series. 1 is a perfect fit, and it goes negative when the model tracks the data worse than the mean of the data does.

Theil’s inequality coefficient U and its decomposition1, 2. U is 0 for a perfect fit and 1 for the worst. The mean square error splits into three parts that sum to 1: Um (bias, the two means differ), Us (variance, the two amplitudes differ) and Uc (covariance, the point by point timing differs, and is tolerable when the model tracks the trend).

The source_kind column says which rows are which, and a name never does. Job rows hold the simulated series. Saved-run rows hold the observed series. A calibration writes one job, and the re-run at the fitted values writes another. Among several jobs the script keeps the longest series per variable, which is the full trajectory and not a sparse leftover. Job name breaks a tie.

Like every other script in this library, the output folder is the first command-line argument, and it wins over EXPERIMENTER_OUTPUT_DIR:

RscriptCalibration_Fit.R /path/to/output

EXPERIMENTER_COMPARISON_RUN is optional. Experimenter sets it on every Script run. The value names a saved run only when the scope has exactly one comparison candidate. Otherwise it stays empty, inside a Sensitivity scope for example. The script resolves the saved run this way:

  • One saved run in the file and no name set: the script uses that run. It assumes no name and prints no fallback note.
  • Several saved runs: the variable selects one.
  • Several saved runs and no name set: the script stops.
  • A name that is not a saved run stops the script, even when the file holds one saved run. A job name is one example.

Critical Information

The script names every column it knows and stops on one it does not, which turns a format change into a message instead of a wrong number: Experimenter does not freeze the format of Output.csv, and this script names the version it was written for (Written_For in the file) so a mismatch prints one line and carries on rather than reporting a number that looks right.

A sweep collates every dataset into one file, and the converter copies all of them. A file with more than one dataset needs EXPERIMENTER_DATASET to select one; without it, the script refuses to mix them.

Both scripts write numbers to 12 significant digits. The two outputs agree to 1e-9.

Likely Issues and Error Types

Every failure below stops the script (a non-zero exit) with a message on standard error, rather than writing a result:

  • No output folder is given. Pass it as the first argument, or set EXPERIMENTER_OUTPUT_DIR.
  • The output folder does not exist.
  • The folder has no Wide_To_Long.csv (or Wide_To_Long_<dataset>.csv). The message names the file. Run Wide_To_Long first.
  • The file lacks source, source_kind, variable or value. The message names the missing columns. A file with a run column and no source is an older layout, and the script does not read it.
  • source_kind holds a value other than job or saved_run.
  • The file has no saved_run row. There is no observed series.
  • The file carries a sample column. It holds a Sensitivity’s samples, and this script scores one simulated series against one observed series. Point it at the output of a Run or an Optimization instead.
  • The file has no time column, or more than one unrecognized column. Its format has changed since Written_For.
  • The file holds several saved runs and no name selects one. The message lists them. Set EXPERIMENTER_COMPARISON_RUN.
  • The named run is not a saved run in the file. The name may belong to a job. The message lists the saved runs the file does hold.
  • The file holds more than one dataset and EXPERIMENTER_DATASET does not select one. The script refuses to mix them rather than guess.
  • No variable is in both the saved run and a job. The saved run and the model export likely disagree on variable names.
  • The file has no job row. Nothing remains to score against the saved run.

References

  1. Sterman, J.D. (1984). Appropriate summary statistics for evaluating the historical fit of system dynamics models. Dynamica, 10 (Winter), 51-66. No DOI; listed on Sterman’s MIT publication page. Defines RMSPE and the bias, variance and covariance decomposition of the mean square error (Um, Us, Uc) this script implements.
  2. Theil, H. (1966). Applied Economic Forecasting. North-Holland. No DOI; WorldCat OCLC 170823. Sterman (1984) calls Um, Us and Uc “the Theil inequality statistics” and credits Theil for them.