Tutorials › Marginal Structural Models › Long-Format Data for Pooled Treatment Models

Inverse Probability of Treatment Weights · Tutorial 86 of 501

Long-Format Data for Pooled Treatment Models

Learn how to create and audit one row per observed treatment decision, and how those rows enter pooled treatment-probability estimation.

Advanced 12 min read

What You'll Learn

  • Define a person-period row and distinguish it from a person-level record.
  • Map repeated treatment records to long format while preserving decision-time order.
  • Identify the treatment, time, and pre-decision history fields in each row.
  • Derive the pooled conditional likelihood while stating when contributions multiply across people.
  • Interpret the pooled intercept-only estimate and recognize the weighting implicit in row-level data.
  • Diagnose missing, duplicated, or temporally misaligned decision rows.

Why Long Format Matters

A pooled treatment model uses records from multiple treatment occasions in one estimation data set. To represent those occasions explicitly, the data are arranged in long format: a person who contributes several observed treatment decisions has several rows, one for each decision. The rows preserve which person and which decision time each treatment value belongs to.

Reshaping is a data-organization step, not a causal adjustment by itself. It does not determine which past variables belong in a treatment model, correct confounding, or solve informative loss to follow-up. Its job is to give each modeled treatment decision a clearly indexed record, with the variables associated with that decision attached to the correct row.

Definition: Let \(i=1,\ldots,n\) index people and \(t=0,\ldots,K-1\) index scheduled treatment-decision times. Let \(R_{it}=1\) when person \(i\) is observed and eligible for the decision at time \(t\), and \(R_{it}=0\) otherwise. A person-period row is one record for a pair \((i,t)\) with \(R_{it}=1\). It contains, at minimum, the person identifier \(i\), decision time \(t\), treatment \(A_{it}\), and the recorded information \(H_{it}\) available before that decision.

For binary treatment, \(A_{it}\) is coded 0 or 1. The time field identifies the decision occasion; it is not a substitute for the person identifier. The history field \(H_{it}\) is a placeholder for the information used to describe the treatment probability at time \(t\). Its exact construction and the coding of past treatments require separate decisions. The essential timing rule here is that information called pre-decision must be recorded before \(A_{it}\) is assigned.

From Repeated Records to Person-Period Rows

A wide data set commonly has one row per person and separate columns for repeated measurements, such as \(A_{i0}, A_{i1}, A_{i2}\). Reshaping converts the treatment columns into rows while retaining the person identifier and matching each period’s information to its own treatment. A row is included only for a decision that was observed and eligible under the study’s definition. A missed or ineligible decision is not silently recoded as \(A_{it}=0\).

Worked Example: Reshaping Three Follow-Up Records

Suppose there are three scheduled decisions, at \(t=0,1,2\). Person 1 is observed at all three; person 2 is observed at \(t=0,1\) and then leaves follow-up; person 3 is observed only at \(t=0\). The recorded treatments and a pre-decision measurement \(L\) are:

PersonObserved decision timesTreatments in time orderPre-decision \(L\) in time order
10, 1, 21, 0, 12, 3, 4
20, 10, 11, 2
3010

The long table has one row for each observed person-time pair:

Person \(i\)Time \(t\)\(A_{it}\)Pre-decision \(L_{it}\)
1012
1103
1214
2001
2112
3010

The row count is \(3+2+1=6\). Person 2 has no row at \(t=2\), because no treatment decision was observed then; there is no treatment value to enter for that occasion. Each treatment remains paired with the measurement from the same decision time. For example, person 1’s \(A_{i1}=0\) is paired with \(L_{i1}=3\), not with \(L_{i0}=2\).

A useful data audit is to verify that each included \((i,t)\) pair occurs exactly once, that the observed times are in the intended order, and that all fields on a row refer to the same decision. If the design schedules \(K\) decisions for everyone with complete follow-up, there should be \(nK\) rows. With incomplete follow-up, the row count is \(\sum_i\sum_t R_{it}\), not necessarily \(nK\).

What the Pooled Model Receives

For each included row, a pooled model represents the conditional probability of treatment at that decision. Write

$$ p_{it}(\theta)=P(A_{it}=1\mid H_{it},T_{it},R_{it}=1;\theta). $$

Here \(\theta\) denotes the model parameter, and \(T_{it}=t\) records the decision time. The notation states the role of the row variables without prescribing how the history is constructed or how time is encoded. A pooled model estimates its treatment-probability relationship from the stacked rows; the model specification determines which columns and functional forms it uses.

For a binary treatment, the contribution of one observed decision to the conditional likelihood is

$$ q_{it}(\theta) = p_{it}(\theta)^{A_{it}} \{1-p_{it}(\theta)\}^{1-A_{it}}. $$

If \(A_{it}=1\), this is \(p_{it}(\theta)\); if \(A_{it}=0\), it is \(1-p_{it}(\theta)\). This compact expression makes clear why the treatment column must contain the treatment assigned at that row’s time, rather than a person-level summary or a treatment from another period.

Worked Example: Calculating Row Contributions

Suppose two observed decisions have fitted treatment probabilities \(p_{11}=0.70\) and \(p_{12}=0.40\), with recorded treatments \(A_{11}=1\) and \(A_{12}=0\). Their conditional contributions are

$$ q_{11}=0.70^1(1-0.70)^0=0.70, \qquad q_{12}=0.40^0(1-0.40)^1=0.60. $$

The product for these two sequential decisions of the same person is \(0.70\times0.60=0.42\), when the second probability is conditional on the information specified for that second decision. This within-person product is a chain-rule factorization of sequential conditional probabilities; it does not assume the two treatments are independent.

If a second person has a single observed decision with contribution \(0.80\), multiplying the two people’s contributions gives \(0.42\times0.80=0.336\) only under an across-person independence assumption (conditional on modeled covariates, if those are conditioned upon). Without that assumption, the joint probability of the people’s treatment records must be modeled with their dependence included.

Likelihood and Score on the Stacked Data

The distinction between dependence within a person and dependence between people is essential. Within a person, the chain rule expresses a joint treatment-history probability as a product of successive conditional probabilities. Across people, multiplying person-level likelihood contributions requires independent people, conditional on any modeled covariates, or an explicit joint model for their dependence.

Result: Suppose each person’s observed treatment sequence has a correctly specified sequential conditional distribution with factors \(q_{it}(\theta)\), and the \(n\) people are independent conditional on the modeled covariates. Then the conditional likelihood for all observed treatment records is $$ L(\theta)=\prod_{i=1}^{n}\prod_{t:R_{it}=1}q_{it}(\theta), \qquad \ell(\theta)=\sum_{i=1}^{n}\sum_{t:R_{it}=1}\log q_{it}(\theta). $$ The product over times is justified by the within-person chain rule; the product over people is justified by the stated independence assumption.

To verify the result, first fix one person \(i\). The chain rule gives the probability of that person’s observed sequence as a product of the conditional probabilities at each observed decision, with the conditioning information appropriate to that sequence. Under the stated model these factors are \(q_{it}(\theta)\), so the person-level contribution is \(\prod_{t:R_{it}=1}q_{it}(\theta)\). Conditional independence across people makes the joint probability the product of these person-level contributions. Substituting their products and using \(\log\prod x_j=\sum\log x_j\) gives the displayed log-likelihood. No step treats repeated rows from one person as independent unconditional observations. \(\square\)

For a common logistic pooled model, let \(X_{it}\) be the row’s design vector and set \(p_{it}(\theta)=\operatorname{expit}(X_{it}^{\mathsf T}\theta)\), where \(\operatorname{expit}(u)=1/(1+\exp(-u))\). The log-likelihood contribution is

$$ \log q_{it}(\theta) = A_{it}X_{it}^{\mathsf T}\theta - \log\{1+\exp(X_{it}^{\mathsf T}\theta)\}. $$

Differentiating this expression gives \(X_{it}\{A_{it}-p_{it}(\theta)\}\). Therefore the pooled score is

$$ U(\theta)= \sum_{i=1}^{n}\sum_{t:R_{it}=1} X_{it}\{A_{it}-p_{it}(\theta)\}. $$

This follows by differentiating each row contribution and summing the derivatives of the log-likelihood. It shows the computational role of long format: every observed treatment decision contributes a residual, \(A_{it}-p_{it}(\theta)\), multiplied by that row’s design vector. The derivation does not make rows from the same person independent.

Worked Example: An Intercept-Only Pooled Estimate

Consider five observed decision rows with treatments \(1,0,1,1,0\), and fit an intercept-only logistic model, so every row has the same modeled probability \(p\). There are three treated rows and two untreated rows. The likelihood and log-likelihood are

$$ L(p)=p^3(1-p)^2, \qquad \ell(p)=3\log(p)+2\log(1-p). $$

For \(0<p<1\), differentiate:

$$ \frac{d\ell(p)}{dp} = \frac{3}{p}-\frac{2}{1-p} = \frac{3-5p}{p(1-p)}. $$

Setting the numerator to zero gives \(\hat p=3/5=0.60\). The second derivative is \(-3/p^2-2/(1-p)^2\), which is negative throughout \(0<p<1\), so this is the unique maximum. The estimate is the treated proportion among the five rows. If people contribute different numbers of rows, this row-level proportion gives more representation to people with more observed decisions; it is not generally the same as an equally weighted average of person-specific proportions.

Diagnostics, Alternatives, and Limits

Before fitting a pooled model, inspect the stacked records rather than relying only on the original wide file. Useful checks include:

  • Duplicate decisions: confirm that an \((i,t)\) key appears no more than once unless repeated records at the same decision are intentional and modeled.
  • Time alignment: compare the time index on each row with the original treatment column and verify that linked measurements belong to the same occasion.
  • Impossible sequences: check whether rows occur after a person was no longer eligible under the study design, or whether scheduled decisions are missing without an explained reason.
  • Missingness versus no treatment: distinguish an observed \(A_{it}=0\) from an absent or unobserved decision. Coding missing treatment as zero changes the treatment record.
  • Unequal row counts: tabulate rows per person and by time. Unexpected patterns can reveal accidental row loss, duplicated records, or follow-up differences that need substantive attention.

Long format is the natural alternative to fitting a separate model at each time when the goal is to pool decision records; separate-time models instead estimate time-specific models. The earlier material on separate-time and pooled treatment models distinguishes those modeling strategies. Either strategy still requires an appropriately defined treatment record and assumptions suited to the study. Wide format can remain convenient for data collection or descriptive summaries, but it does not directly supply one observation per treatment decision to a pooled row-based model.

A common mistake is to say, “There are six rows, so the sample size for the likelihood is six independent observations.” The rows are six treatment decisions, but multiple decisions may belong to the same person. The within-person likelihood uses conditional factors, while inference must respect the dependence structure and the independent sampling units. If people are independent, person-level contributions can be multiplied as above; standard errors should still account for repeated observations within person rather than treating all rows as independent clusters. If people share clusters or otherwise depend on one another, that dependence must also be addressed. Reshaping alone supplies no such correction.

Finally, a correctly organized table cannot repair a poorly defined observation process. If treatment decisions are absent because of dropout, illness, or another process related to treatment or covariates, simply omitting those rows can change the target distribution. Nor does a long table establish that the chosen history is sufficient for confounding control, that positivity holds, or that the probability model is adequate. Those are separate modeling and causal assumptions, not consequences of the file layout.

Check Your Understanding

Answer each question, then compare with the solutions.

  1. Three people contribute 3, 2, and 1 observed eligible treatment decisions. How many person-period rows should appear, and what identifies each row?
  2. Why is a missing treatment decision not automatically coded as \(A_{it}=0\)?
  3. Two sequential treatment contributions for one person are \(0.8\) and \(0.5\). What is their chain-rule product, and does that product imply unconditional independence?
  4. Five long-format rows contain four treated decisions and one untreated decision. What is the intercept-only pooled maximum-likelihood estimate of \(p\)?

Solutions. (1) There are \(3+2+1=6\) rows. Each is identified by a person-time pair \((i,t)\), with one row per observed eligible decision. (2) A missing decision was not observed as untreated; coding it as zero changes the data and the likelihood contribution. (3) The product is \(0.8\times0.5=0.4\). It is a chain-rule product when the second contribution is conditional on the appropriate information; it does not imply unconditional independence. (4) The intercept-only likelihood is \(p^4(1-p)\), whose maximum is \(\hat p=4/5=0.8\), the treated fraction of the rows.

Clinical Biostats