Tutorials › Pharmacometric Workflows and Reproducible Modeling › Checking Subject and Event Alignment in Model Inputs

Quality Control and Independent Review · Tutorial 327 of 399

Checking Subject and Event Alignment in Model Inputs

Learn to verify that model-input records belong to the intended subjects and follow the event-ordering rules required by the analysis.

Intermediate 12 min read

What You'll Learn

  • Define the subject and event keys needed to review model-input alignment.
  • Detect missing, unexpected, duplicated, or accidentally merged subject identifiers.
  • Check event chronology without treating every same-time record as an error.
  • Compare source and model-input records using composite keys and explicit value checks.
  • Distinguish useful automated checks from assumptions that require subject-matter review.

Why Subject and Event Alignment Matters

A model input can contain the expected number of rows and still be wrong. An observation may have been assigned to another subject during a join, a subject identifier may have been converted to a different representation, or records may have been placed in an order that changes how the software interprets events. Such errors can be hard to see in a diagnostic summary: they concern which subject a record belongs to and where that record sits in the subject's event history.

This review is about record identity and sequence, not about deciding whether the model equations are correct. The essential question is whether each model-input record corresponds to the intended subject and event, and whether its position is consistent with the documented event rules. Counts and ordering checks provide evidence, but they do not replace understanding what each event means.

Definition: A subject identifier is the field or documented combination of fields used to identify a subject consistently across relevant data sources. An event record is one row describing an event for that subject, such as a dose, an observation, or a covariate update. An event key is the set of fields used to distinguish an event record for review, commonly including subject ID, time, event type, and, when needed, an event sequence or source-record identifier.

No single event key is correct for every dataset. Two observations can legitimately share a subject and time, and a dose and an observation can occur at the same recorded time. If the chosen key omits a field needed to distinguish those records, it can label distinct events as duplicates. Conversely, a key that includes a field altered during processing may fail to match the same event across two files. The key therefore needs to follow the data's documented meaning.

Set the Review Rules Before Checking the Rows

Start with an authoritative subject mapping and a clear account of the event fields. Earlier review of analysis population and exclusion counts establishes which subjects and records are in scope; this check asks whether the in-scope records remain correctly identified and aligned. For each input and any comparison source, establish:

  • Which field or fields identify a subject, and whether IDs are character labels or numeric values.
  • Which fields describe event time, event type, and any within-time sequence.
  • Which combinations of fields should be unique, and which repeated combinations may be valid.
  • Which ordering rules apply, including how tied times should be handled.
  • Which source fields or stable record identifiers can be used to match records between files.

These are review assumptions, not automatic properties of the data. In particular, chronological ordering generally means that times do not decrease within a subject, but the required ordering among records tied at the same time depends on the event definitions and input conventions. A reviewer should not invent a tie-breaking rule from the observed row order.

Review principle: Treat a model-input event as a record with both an identity and a position. Check identity with suitable keys and record-level comparisons; check position against documented chronology and tie rules. A matching row count alone establishes neither.

Check Subject Identifiers and Record Counts

First compare the subject identifiers in the model input with the expected subject set for that input. Report identifiers that are missing, unexpected, or repeated in contexts where a single subject-level record is expected. Then compare record counts by subject and, where useful, by event type. These summaries can reveal an entire subject's records being dropped or an unexpected cluster of records assigned to one subject.

Preserve identifiers in their intended representation. For example, the labels "014" and "14" are different character identifiers unless the data specification explicitly says otherwise. Converting IDs to numbers can erase leading zeros; trimming spaces or changing case can also collapse two distinct labels. Normalization is appropriate only when the mapping rules say those differences are formatting artifacts, and the reviewer should check whether normalization creates collisions.

Worked Example: Finding a Subject-ID Collision

Suppose a hypothetical model input contains these IDs: "014", "14", "021", and "021". The subject reference list contains the three distinct labels "014", "14", and "021". A processing step is suspected of converting IDs to numbers and then formatting them without leading zeros. Check whether the input has the expected subjects and whether conversion would be safe.

As character labels, the input has three distinct IDs: "014", "14", and "021". The reference list also has three distinct IDs, so a distinct-subject count of three would not expose the duplicated "021" record. A frequency check does: "021" appears twice in the input, although it appears once in the reference list for this subject-level check.

Now apply the suspected numeric conversion. Both "014" and "14" become the number 14. Thus the conversion maps two distinct reference labels to one value:

$$ \text{"014"} \mapsto 14,\qquad \text{"14"} \mapsto 14 $$

This is a collision, so numeric conversion cannot preserve subject identity. The appropriate review finding is not that "014" and "14" definitely refer to different people; it is that the proposed conversion cannot distinguish them, and the reference mapping must resolve their meaning before conversion. The duplicate "021" should separately be investigated as a repeated subject-level entry or a potentially valid repeated record, depending on the table's intended grain.

Check Event Order Without Mislabeling Ties

For each subject, inspect event times in the order presented to the model and test whether they follow the stated ordering rule. If chronological nondecrease is required, a simple adjacent-row condition is \(t_{i+1} \geq t_i\) within a subject. A violation identifies a local decrease; it does not by itself prove the data are wrong, because time resets, separate records, or other conventions may be allowed. Resolve each flag against the documented input rules.

Repeated times require particular care. A dose and an observation at the same time may both be valid, while their ordering can matter to the intended event interpretation. If a tie rule exists, check it using the event type and any sequence field. If none is specified, report the tie for clarification rather than asserting that one row order is correct.

Worked Example: Locating a Chronology Violation

A hypothetical input contains the following records for subject P17. The documented rule requires nondecreasing time within a subject. At equal times, dose records must precede observations.

RowTime (h)Event
10Dose
22Observation
34Observation
44Dose
53Observation

Check adjacent times. The four differences are \(2-0=2\), \(4-2=2\), \(4-4=0\), and \(3-4=-1\) hours. The last difference is negative, so row 5 violates the required nondecreasing-time rule. The tied records at rows 3 and 4 are also out of the specified tie order: the observation precedes the dose at 4 hours.

$$ (t_{i+1}-t_i) = (2,\ 2,\ 0,\ -1)\ \text{h} $$

The flags identify two issues to investigate: the row at 3 hours appears after a 4-hour record, and the two 4-hour events are opposite to the documented tie rule. They do not establish the cause. The reviewer should compare these rows with the event source or stable record identifiers and determine whether the input was mis-sorted, the event times were altered, or the documented rules were applied incorrectly.

Match Records Across Sources, Not Just Marginal Totals

When a model input is compared with a source or intermediate dataset, use a key that identifies the same event on both sides. A candidate composite key might be subject ID, time, event type, and analyte; a stable source-record ID may provide a stronger match if it is preserved. Compare the expected and observed record sets by key, then inspect values such as time, event type, dose amount, or observation value. Do not assume that equal numbers of records by subject prove the records are the same.

A useful justification is to compare records at the level where misalignment can occur. Let \(K\) be the chosen event key. For each key \(k\), let \(c_S(k)\) be its count in the comparison source and \(c_M(k)\) its count in the model input. If records are expected to transfer one-for-one, alignment requires \(c_S(k)=c_M(k)\) for every \(k\), as well as agreement on the checked fields. Equal overall totals only imply \(\sum_k c_S(k)=\sum_k c_M(k)\); they do not imply equality for each key. For example, one record can be lost at one key and an unrelated record duplicated at another while the totals remain equal.

Worked Example: Detecting a Faulty Join

A hypothetical source has two observations at 1 hour, one for each of subjects A and B. The model input also has two observations at 1 hour. The join used to create the input may have matched on time alone.

SubjectTime (h)Source observationModel-input observation
A18.2 mg/L5.1 mg/L
B15.1 mg/L8.2 mg/L

The total count is two in the source and two in the model input. The overall sum is also the same: \(8.2+5.1=13.3\) mg/L in each file. Those marginal checks do not detect the swap. Using the composite key \((\text{subject},\text{time})\), the value differences are:

$$ \begin{aligned} \text{A at 1 h: }&5.1-8.2=-3.1\ \text{mg/L}\\ \text{B at 1 h: }&8.2-5.1=3.1\ \text{mg/L} \end{aligned} $$

Both subject-keyed records disagree even though the total count and sum agree. The evidence is consistent with the observations being exchanged between subjects, but the reviewer should confirm this against a trusted source record or stable record identifier before concluding how the error occurred. Matching on time alone was inadequate because both subjects had a record at that time.

Diagnostics, Alternatives, and Limits

A reproducible review can generate subject-level record counts, sorted event listings, duplicate-key reports, and source-to-input comparison tables. These automated checks are useful for broad coverage, especially when a dataset has many subjects. A targeted manual inspection is useful for interpreting flagged records and confirming that the key and tie rules represent the actual event definitions. The two approaches complement each other: automation can apply explicit rules consistently, while review is needed to judge whether the rules and exceptions are appropriate.

Investigate patterns rather than dismissing isolated flags without explanation. Useful signals include:

  • Unexpected subject IDs or a sharp difference in records per subject.
  • Duplicate composite keys, especially when uniqueness is expected.
  • Time decreases, irregular sequences, or same-time records in an undocumented order.
  • Records whose subject, event type, or values differ after matching to their source.
  • Many mismatches concentrated after a particular merge, transformation, or file boundary.

Every diagnostic has limits. A duplicate key can be a real pair of events if the key omits a distinguishing field. A chronological sequence can be correct while an observation is assigned to the wrong subject. Matching records can still carry a consistently incorrect value if the comparison source is not authoritative. Likewise, an ID that looks unusual is not proof of a merge; it is a reason to verify the reference mapping and processing rule.

Common Mistakes

  • Using total rows as proof of alignment. Equal totals can conceal a swap, a missing row paired with an extra row, or incorrect subject assignment. Compare counts and values at an event key.
  • Treating every repeated time as a duplicate. Multiple valid events may share a time. Define a key that distinguishes them and apply documented tie rules.
  • Converting IDs before checking uniqueness. Numeric conversion or normalization can collapse distinct labels. Check for collisions and validate the mapping first.
  • Calling a chronology flag an error without checking conventions. A decrease is a rule violation only if nondecreasing time is required for that input. Investigate against the documented event semantics.
Key takeaway: Reliable alignment review combines subject-set checks, event-level matching, and within-subject ordering checks. State the assumptions behind the identifiers, keys, and tie rules; then retain enough record-level evidence to explain every flagged discrepancy.

Check Your Understanding

For each question, give the conclusion supported by the stated checks. Solutions follow each question.

  1. A model input has the same total number of rows as its source, but one source event key is absent and a different key appears twice. Does the matching total establish alignment? Solution: No. If one-for-one transfer is expected, the per-key counts disagree even though the totals can match. The missing and duplicated keys require investigation.
  2. A subject has a dose and an observation recorded at the same time. Is one necessarily a duplicate? Solution: No. They are different event types and may both be valid. The event key and ordering rule must distinguish and correctly order them.
  3. The labels "007" and "7" both appear in a reference list. What should be checked before converting IDs to numbers? Solution: Check the authoritative subject mapping and whether the labels identify distinct subjects. Numeric conversion maps both to 7, so it is unsafe if the labels are distinct.
  4. A within-subject time sequence is 0, 1, 1, 3 hours, and the stated rule is nondecreasing time. Does the tied time alone violate that rule? Solution: No. The sequence satisfies \(0\leq1\leq1\leq3\). A separate tie-order rule may apply, but none is stated here.
  5. Source and model input each contain observations of 6.0 and 9.0 mg/L at the same time, but the subject assignments are reversed. Which comparison detects this? Solution: Match by a key including subject and time, then compare the values. Overall counts and sums agree, but the subject-keyed observations do not.
Clinical Biostats