Tutorials › Pharmacometric Workflows and Reproducible Modeling › Normalizing Residual and Prediction Outputs

Automating Diagnostics and Reports · Tutorial 278 of 399

Normalizing Residual and Prediction Outputs

Learn a reproducible approach to converting residual and prediction outputs into long-form records while retaining the context needed to interpret each value.

Intermediate 10 min read

What You'll Learn

  • Define a long-form row for each record and output quantity.
  • Preserve compound record identifiers when reshaping or joining outputs.
  • Retain units, prediction types, and residual definitions as explicit fields.
  • Distinguish missing outputs from values that are not applicable.
  • Check normalized tables for lost records, duplicated keys, and changed values.
  • Choose between long-form, wide-form, and separate output tables based on the task.

Why Normalize Model Outputs?

Diagnostic results often arrive in layouts that are convenient for a modeling program but awkward to compare, filter, or plot consistently. One file may place predictions and residuals in separate columns, another may store one prediction type per file, and a third may provide a matrix whose row and column labels carry essential identifiers. Normalizing these layouts means transforming them into a predictable table structure while preserving what each value refers to.

Normalization is a structural operation, not a statistical reinterpretation. It does not make different residual definitions equivalent, select a preferred prediction, or convert units automatically. Its purpose is to make records comparable in form while keeping their meaning explicit. That distinction matters: a column called “residual” is not enough information if one source reports an unscaled difference and another reports a scaled quantity.

Definition: A normalized long-form output table contains one row for each combination of a source record and an output quantity. Each row retains the record identifiers, the quantity’s value and unit, the output role, and any applicable prediction type or residual definition. A source record is identified by the complete key needed to distinguish it from every other record, not necessarily by subject and time alone.

A practical schema can include subject_id, occasion_id, and record_id; the record time and its unit; output_role (such as observed value, prediction, or residual); source_variable (the original field name); value; unit; prediction_type; and residual_definition. A project may use different column names, but it should preserve the same information. Identifiers and descriptive fields repeat across long-form rows because that repetition makes each value independently traceable.

Use a key that matches the source data’s actual granularity. If a subject can have two records at the same time, subject and time do not uniquely identify a row. Include an occasion, replicate, event, or source-row identifier as needed. If the source has no stable record identifier, create one from stable source coordinates, such as the file identifier and original row number, and retain those coordinates. Do not rely on the current row order after sorting or filtering.

Transforming Wide Outputs

A common input layout has one row per model record and separate columns for observed values, predictions, and residuals. To reshape it, retain the record identifiers, convert each output column into a row, and record the original column name and its meaning. The value should be carried across unchanged; the transformation should not silently calculate a replacement value.

Worked Example: Reshaping a Wide Diagnostic File

Suppose a hypothetical source file uses concentrations in milligrams per liter and contains the following records. The source defines IPRED as an individual prediction and IWRES as a weighted residual based on that prediction.

subject_idrecord_idtime (h)DV (mg/L)PRED (mg/L)IPRED (mg/L)IWRES (dimensionless)
S01R0118.07.27.80.20
S01R0244.04.54.2-0.10

The complete record identifiers are subject_id and record_id. Time is retained with its unit, while each output value gets its own row. For R01, the normalized rows include (observed, DV, 8.0 mg/L), (prediction, PRED, 7.2 mg/L), (prediction, IPRED, 7.8 mg/L), and (residual, IWRES, 0.20, dimensionless). R02 produces the corresponding four rows with values 4.0, 4.5, 4.2, and -0.10. The normalized table therefore has eight rows.

On the PRED rows, set prediction_type to “population prediction”; on the IPRED rows, set it to “individual prediction.” On the IWRES rows, retain the source’s residual definition, for example “IWRES as supplied; weighted residual based on IPRED,” rather than calling it an unqualified residual. For observed values and residuals, prediction_type can be “not applicable”; that is different from a prediction type that should be present but is missing.

The values remain exactly those in the source. In particular, do not infer a different IWRES by subtracting DV and IPRED: the listed residual is already defined and calculated by the source model output. Normalization records its value and definition; it does not overwrite that definition.

The long-form result is most useful when a task needs to select or compare output types by name, such as plotting each prediction type using the same code. A wide table may still be preferable for a single calculation that needs several quantities side by side. Keeping a normalized table does not require discarding the source file; retain the original input so that the transformation can be checked and reproduced.

Preserving Definitions and Units

A value is interpretable only with its measurement unit and role. Predictions usually have the same unit as the modeled observation, while residual outputs can have that unit or be dimensionless, depending on their definition. Therefore, record the unit for each output quantity rather than assuming that all columns share one. Retain time units separately from the units of the output value.

A residual definition should be sufficiently specific to distinguish the calculation and its inputs. For example, a raw residual can be defined as the observed value minus a named prediction; a scaled residual additionally identifies the scale used. If the source provides a named residual but not enough documentation to expand its formula, preserve the name and state that the definition is as supplied. Do not guess a formula from a column label.

Key distinction: A prediction type describes what a prediction represents, such as population-level or individual-level prediction. A residual definition describes how a residual was calculated, including which prediction and any scaling are involved. Neither should be inferred solely from a generic output role.

Worked Example: Keeping Residual Definitions Distinct

A hypothetical record has an observed concentration \(DV=10.0\) mg/L and two predictions: \(PRED=8.0\) mg/L and \(IPRED=9.0\) mg/L. The output file also contains two residual columns, defined by its documentation as \(RES=DV-PRED\) and \(IRES=DV-IPRED\).

The raw residuals are:

$$ RES=10.0-8.0=2.0\ \text{mg/L}, \qquad IRES=10.0-9.0=1.0\ \text{mg/L}. $$

The normalized table stores separate residual rows: one with source_variable RES, value 2.0, unit mg/L, and definition “DV minus PRED”; the other with source_variable IRES, value 1.0, unit mg/L, and definition “DV minus IPRED.” It does not merge them under a single generic residual label. If a further output were defined as \(RES/2.0\) with a 2.0 mg/L scale, its value would be \(2.0/2.0=1.0\), dimensionless; that row would need its own definition and unit.

These calculations illustrate how to check a documented definition, not a rule that every residual column should be recomputed. If the source’s residual definition is unknown, the appropriate normalized record preserves the supplied value and marks the definition as undocumented or as supplied. A plausible arithmetic reconstruction is not a substitute for source documentation.

Joining Separate Output Files

Another common layout stores predictions and residuals in different files. These should be combined using the complete record key, not row position. Before joining, check that the intended key identifies at most one value of each output type per record. If a file contains multiple prediction types for the same key, include prediction type in the relevant key or keep those outputs as separate long-form rows. A join on subject alone can associate a prediction with the wrong time or occasion.

Worked Example: Joining Outputs by a Compound Key

Suppose two hypothetical files describe records for subject S02 in occasion O1. The prediction file contains (S02, O1, R11, time 2 h, PRED 5.0 mg/L) and (S02, O1, R12, time 6 h, PRED 2.5 mg/L). The residual file contains (S02, O1, R12, time 6 h, RES -0.5 mg/L) and (S02, O1, R11, time 2 h, RES 1.0 mg/L); its rows happen to be in the opposite order.

Join using subject_id, occasion_id, and record_id. The match for R11 is PRED 5.0 with RES 1.0; the match for R12 is PRED 2.5 with RES -0.5. Row position would happen to pair 5.0 with -0.5 and 2.5 with 1.0, which is wrong. The negative residual for R12 is consistent with a definition \(RES=DV-PRED\) only if the observed value is 2.0 mg/L, but that value is not needed to carry out the join.

After matching, represent the prediction and residual as separate normalized rows for each record, retaining each source field, unit, prediction type, and residual definition. If R12 is absent from the residual file, keep its prediction row and mark the residual output as unavailable only if the chosen table design explicitly represents unavailable outputs. Do not manufacture a residual value or drop the prediction row just to make the files rectangular.

A Justification for the Reshape

A reshape is reliable when it preserves a reversible mapping between source values and normalized rows. Consider a wide table with key \(k\) and output columns \(c_1,\ldots,c_m\). For every non-key source value \(x_{k,c_j}\), create a long-form row containing the same key \(k\), a source-variable label \(c_j\), and value \(x_{k,c_j}\). Retain the metadata that explains the meaning and unit of \(c_j\).

$$ (k,c_j,x_{k,c_j}) \longleftrightarrow \text{one normalized row for key }k\text{ and source variable }c_j. $$

If the key and source-variable label uniquely identify the output cell, then each original cell maps to exactly one normalized row. Conversely, grouping normalized rows by key and source-variable label recovers the source cells, provided no rows were dropped, duplicated, or overwritten. This gives a practical check: reshape the output back to wide form and compare the recovered values with the original, allowing only explicitly documented changes in representation.

The condition matters. If there are two residual outputs both called “RES” for the same record, the pair of record key and source-variable label is not unique. Include another distinguishing field, such as residual definition or output instance. Otherwise, a pivot may overwrite one value or create an ambiguous duplicate. Structural uniqueness is not guaranteed by calling a table “long.”

Validation and Common Failure Modes

After transforming or joining outputs, run checks that test both structure and content. These checks do not establish that the model is correct; they establish that the data handling has not silently changed or misidentified the supplied outputs.

  • Check record coverage. Compare the distinct source record keys before and after transformation. Each expected record should still be represented, including records with only some output types.
  • Check uniqueness at the declared granularity. Confirm that the chosen key plus output role and distinguishing metadata identify a single row, unless repeated values are explicitly intended.
  • Check value preservation. Compare counts and values by source variable, including missing and nonfinite values. Do not silently replace, remove, or recalculate them during reshaping.
  • Check labels and units. Confirm that every prediction row has the source-defined prediction type, and every residual row has its definition recorded. Verify that output units and time units have not been confused.
  • Check joins explicitly. Count unmatched keys on each side and investigate duplicates before joining. Unexpected row multiplication often indicates a many-to-many join caused by an incomplete key.

A frequent mistake is to treat a wide column name as a complete definition: “IWRES is a residual, so all residuals can be stored under that label.” This loses information when files use different formulas or scaling conventions. Preserve the original variable name, the definition available in the source, and the unit; normalize the layout without claiming that distinct quantities are interchangeable.

Another mistake is to join by row number or subject alone. Matching files can have different sort orders, missing records, or repeated occasions. Use the full stable key and report unmatched records instead of silently pairing or discarding them. Similarly, a missing value should not be converted to zero: zero is a numerical result, whereas missing means that the output is unavailable or unreported.

Choosing a Table Layout

LayoutUseful whenMain risk
Long-form normalized tableApplying common filters, summaries, or plotting code across many output typesMeaning is lost if output role, type, definition, or units are omitted
Wide tableWorking record by record with several quantities side by sideDifferent sources may use inconsistent column names or omit some output columns
Separate tables by output typeOutputs have substantially different structure or granularityRecord matching and cross-output comparisons require careful keyed joins

There is no requirement that every artifact use one physical table. Long form is a useful common representation, but retaining a source-shaped file or keeping outputs separate can be sensible when their record structures differ. The essential requirement is that any transformation has explicit key rules, preserves source meaning, and can be checked against its inputs.

Key takeaway: Normalize the arrangement of model outputs, not their scientific meaning. Keep stable record identifiers, source-variable labels, units, prediction types, and residual definitions attached to each value, then validate that the transformation preserves records and values.

Check Your Understanding

Answer each question, then compare your reasoning with the solutions.

  1. A file has two records for the same subject and time but different occasions. Is subject and time a sufficient key? What should be retained?
  2. A prediction file has one row per record and a residual file has the same rows in a different order. What is a safe way to match them?
  3. A normalized residual row has a value of 0.8 but no unit or definition. Why is that record incomplete?
  4. In a wide-to-long reshape, what information is needed to recover which source column each value came from?
  5. A source contains a missing residual for one record but a valid prediction. What should normalization avoid doing?

Solutions. (1) No. Include occasion in the key, along with any other identifier needed to distinguish records. (2) Join on the complete stable record key, such as subject, occasion, and record_id; do not match by row position. (3) The value cannot be interpreted reliably: its unit might be the observation unit or dimensionless, and its formula or scaling is unknown. Preserve the available source definition or explicitly mark it undocumented. (4) Retain a source-variable label or an equivalent field identifying the original column, alongside the record key and value. (5) It should not replace missing with zero, invent a residual, or discard the valid prediction row; preserve the missingness and the available output information.

Clinical Biostats