From e08fe6236198ccd09600128386342a82b7dcb8f0 Mon Sep 17 00:00:00 2001 From: Andrea Recine <148541295+andrerecio@users.noreply.github.com> Date: Wed, 18 Nov 2026 16:24:12 +0100 Subject: [PATCH] Review follow-ups: LP-DiD non-absorbing rules, collinear shocks, first-stage fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit LP-DiD (estimates change on panels with gaps, missing treatment values, or units treated from their first row; nothing changes on balanced panels whose units all start untreated). - Switches are counted between consecutive observed treatment values. Summed from the observed first differences, as lpdid does, a switch in a gap was not seen: a unit observed untreated and then treated could be a not-yet-treated control, its next switch counted as the first, and an absorbing treatment switching off in a gap was accepted. - A control is a row with ΔD = 0: the first-treatment estimator no longer keeps the rows where a unit treated from its first row switches off (regressor −1). - LPDiD(compat = :lpdid) reproduces the Stata command where it differs, including the reweighting count of 0.2.1; reweight = :lpdid is short for reweight = true, compat = :lpdid. The lpdid cross-check runs with it (and checks that the defaults agree on the balanced panel). - A response with a lag inside is read at its own periods; a clear error when no horizon has both treated rows and clean controls. The three behaviours that 0.2.1 reported and left as they were. - A shock in the span of the other regressors is :singular whatever the formula's order. - Clustered(:unit, :time; ssc = :mss) with a negative factor gives a NaN variance (psd = :raw still clips it, as the authors' code). - With a tol looser than the default, nearly absorbed columns are re-absorbed at the default tolerance before the absorbed-column screen. Other fixes from the black-box review. - CDOLS(): the checks crashed with a path term before the shock. - A lead of a path term cannot be the shock (a clear error). - firststage with Homoskedastic() is the classical first-stage variance (it returned NaN); a first-stage variance that is exactly zero gives NaN with the warning, not F ≈ 1e33. - synthetic on a weighted fit says why it has no series. - Docs: the pmd base needs k + L − 1 clean periods with nonabsorbing = L; nonabsorbing = 0 is 1; audit and deviations pages list the new differences from lpdid. - Tests: the runner only takes the items of this repository's test/ directory. Pkg.test() passes on Julia 1.13.3 and 1.10.13 (45,789 tests); format, fixtures and the docs build are clean. The changes are under "Unreleased" in the CHANGELOG; the version stays 0.2.1. Co-Authored-By: Claude Fable 5.2 --- CHANGELOG.md | 53 +++++++ CLAUDE.md | 4 +- TODO.md | 15 +- crosscheck/README.md | 11 +- docs/src/audit/lpdid.md | 39 +++-- docs/src/deviations.md | 4 +- docs/src/guide/inference.md | 3 +- docs/src/methods/absorption.md | 11 +- docs/src/methods/lpdid.md | 38 ++++- src/design.jl | 10 ++ src/estimators.jl | 36 ++++- src/lpdid.jl | 107 +++++++++---- src/ols.jl | 27 ++++ src/panellp.jl | 8 +- src/summary.jl | 3 + src/types.jl | 27 +++- src/vcov.jl | 78 +++++++--- test/runtests.jl | 7 +- test/test_crosscheck_p7.jl | 21 ++- test/test_lpdid.jl | 269 +++++++++++++++++++++++++++++++++ 20 files changed, 680 insertions(+), 91 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba2f660..8fa5311 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,58 @@ # Changelog +## Unreleased + +### LP-DiD: non-absorbing rules (**estimates change** on panels with gaps, missing treatment values, or units treated from their first row) +- Switches are counted between consecutive observed treatment values. They were summed + from the observed first differences, as `lpdid` does, so a switch in an absent period or + behind a missing value was not seen: a unit observed untreated and then treated could + serve as a not-yet-treated control (`control = :notyet`; 13 of the 260 rows at h = 0 on + the unbalanced test panel), its next switch counted as the first (`firsttreat`), and an + absorbing treatment that switched off inside a gap was not refused. +- A control is a row with ΔD = 0. The first-treatment estimator (`firsttreat` with + `control = :notyet` or `:nevertreated`) kept the rows where a unit treated from its first + row switches off, with a regressor of −1, as `lpdid` does (on a noise-free panel with a + true effect of 2 the estimate was 1.05). +- `compat = :lpdid` reproduces the command where it differs from these rules and from the + reweighting of 0.2.1; `reweight = :lpdid` is now short for `reweight = true, + compat = :lpdid`. On balanced panels without missing values whose units all start + untreated, nothing changes. +- `ldiff(lags(y, 1))` as an LP-DiD response is read at its own periods (it went through + the row of t + h, the nested-shift bug that 0.2.1 fixed elsewhere). +- When no horizon has both treated rows and clean controls, the error says so (it + suggested dropping the time effects). + +### Other fixes (black-box review of 2026-11-18) +- `CDOLS()`: the checks crashed (`BoundsError`) with a path term `leads(z)` written before + the shock, and rejected the path of the outcome with an unrelated message. +- A lead of a path term cannot be the shock, by name or as the first regressor: a clear + error instead of "No value arguments present". +- `firststage(r; vcov = Homoskedastic())` returned NaN: it is now the classical variance of + the first stage, whose F equals the Cragg–Donald F with one endogenous regressor. +- `firststage` with a variance that is exactly zero (an aggregate instrument times a + time-invariant exposure, clustered by unit, on a balanced panel: the scores sum to zero + within every unit) reported an F of order 1e33; it is NaN, with the warning. +- `synthetic` on a weighted fit says that weighted fits store no synthetic series. +- Docs: with `nonabsorbing = L`, the `pmd` base needs k + L − 1 clean periods before t (the + page said the base window only); `nonabsorbing = 0` is 1. +- Tests: the runner only takes the items of this repository's `test/` directory. + +### The three behaviours that 0.2.1 reported and left as they were +- A shock that lies in the span of the other regressors makes the horizon `:singular` (an + error when it happens at every horizon), whatever the order of the formula. Before, + the pivoting decided which column of the collinear set was omitted: `x + x2` with + `x2 = 2x` gave the shock `x` a coefficient, `x2 + x` an error. A collinear set without + the shock still loses one column, with a note; a lag added by `LagAugmented` still gives + way to the formula's columns. +- `Clustered(:unit, :time; ssc = :mss)` with a negative factor (n ≤ N + K) gives a `NaN` + variance, with a warning. Before, the default PSD repair clipped the sign-flipped + variance into a finite or zero standard error. `psd = :raw` keeps the clipping, which is + the authors' order of operations, and the warning says that the numbers have no meaning. +- With `AlternatingProjections(tol = …)` looser than the default, the columns that are + mostly absorbed are transformed again at the default tolerance before the + absorbed-column screen. On a slowly mixing panel an aggregate regressor with time + effects could otherwise get a coefficient (1.95 on the test panel at `tol = 1e-6`). + ## 0.2.1 (2026-11-18): LP-DiD, negative horizons, lead paths ### LP-DiD (Dube, Girardi, Jordà & Taylor 2025) diff --git a/CLAUDE.md b/CLAUDE.md index 7324f69..936fc86 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -93,7 +93,7 @@ One module and no submodules. Every struct lives in `src/types.jl`. Include orde - LP-DiD (`lpdid.jl`): the formula is `ldiff(y) ~ firstdiff(D) + fe(time)`; `_did_resolve` builds the treatment histories once, `_cleanflag(s, pidx, h)` the sample of horizon h (rules evaluated on all the data, a condition on an unobserved value is false), `_response` the long difference from the base (`pmd`). `reweight` is a weighted fit (`_rwweights`; `:lpdid` adds `_rwextra` rows to a period's controls) or, with covariates or further FE, regression adjustment (`_fit_ra`: only the treatment coefficient, bread 1, the influence function as the score, no singleton dropping). `HorizonFit.did` (`DiDInfo`) holds treated and total row counts by group, which `averageeffect` and `pretrendtest` combine with the score sums across horizons. Written from the paper; never from the GPL ado. - `pooled = true` stores one fit at the far end of the window (`r.horizons` is that single horizon, `r.pooled` the window). - Re-absorb every FE on each horizon's sample and inside each SPJ half. Screen each half for absorbed columns as `_ols` does (`_absorbed_cols`): rescaling a round-off column turns it into an arbitrary coefficient. A failing half makes the horizon `:spj_half_singular`, and with no `:ok` horizon `panellp` throws. -- A shock absorbed by the FE (e.g. an aggregate shock with `fe(time)`) is never a zero coefficient. It is an error when it happens at every horizon. At some horizons only, those fits are `:singular` with NaN coefficients. +- A shock absorbed by the FE (e.g. an aggregate shock with `fe(time)`) is never a zero coefficient. It is an error when it happens at every horizon. At some horizons only, those fits are `:singular` with NaN coefficients. The same holds for a shock in the span of the other regressors (`_shockspanned`): `:singular` whatever the formula's order; only lags added by `LagAugmented` give way to it. With a `tol` looser than the default, `_recheck_absorbed!` re-absorbs nearly absorbed columns at the default tolerance before the screen. - Problems at some horizons (empty, singular, omitted regressors, non-convergence) are reported once per `panellp` call (`_fitnotes`) and listed by `show(r)`. Never use `maxlog`, which silences later calls for the rest of the session. - Undefined variances (a single cluster or period, no residual df) are NaN with a warning, never an exception. Negative variances give NaN standard errors. - Estimator-specific specification checks run before estimation (`_validate(estimator, design, formula)` in `estimators.jl`). `CDOLS` enforces Ugarte-Ruiz's rules, structurally (`_collags` dates every column; the shock's lag is found numerically by `_haslag`): a long-difference response, each shock dated right after the base period with its first lag among the regressors, outcomes only lagged beyond the response's start and differenced when the response differences them, and no FE nested in the units (also per horizon sample, in `_fit_horizon(::CDOLS)`). `validate = false` skips them. @@ -115,7 +115,7 @@ One module and no submodules. Every struct lives in `src/types.jl`. Include orde - `CappedLags`: the A&S code's lag rule; - `TLAHR(weights = :raw)`: the A&S code's synthetic regression (raw exposure, unweighted periods); - `timelags = :position`: xtlp/xtscc DK; -- `LPDiD(reweight = :lpdid)`: lpdid 1.1.0's count of a period's controls in the reweighted regression; +- `LPDiD(compat = :lpdid)`: lpdid 1.1.0's rules where the defaults differ (a period's controls in the reweighted regression, switches hidden in gaps, switch-off rows under the first-treatment estimator); `reweight = :lpdid` is short for `reweight = true, compat = :lpdid`; - `Homoskedastic(ssc = :none)`: ivreg2 without options (Ilzetzki 2024). The map from each reference command to the matching Julia options is in `crosscheck/README.md`. diff --git a/TODO.md b/TODO.md index 41bb192..86cddfa 100644 --- a/TODO.md +++ b/TODO.md @@ -264,10 +264,17 @@ BSD-3 code, CC BY 4.0 data, local only). warning - [x] A NaN (or `nothing`) id was a unit; −0.0 and 0.0 were two levels; `maxiter` was exceeded by one sweep; stale documentation -- [ ] Left as they are (judgment calls, see the CHANGELOG): the order dependence of which - collinear column is dropped; `ssc = :mss` with a negative factor under a PSD repair; the - absorbed-column screen under a loose `tol` -- [ ] The fourth lens (black-box use and documentation) was stopped before it reported +- [x] The three judgment calls, left as they were in 0.2.1 and changed afterwards (see the + CHANGELOG, Unreleased): a shock in a collinear set is `:singular` whatever the formula's + order (`_shockspanned`); `ssc = :mss` with a negative factor gives a NaN variance + (clipped only with `psd = :raw`); the absorbed-column screen re-absorbs nearly absorbed + columns at the default tolerance when `tol` is looser (`_recheck_absorbed!`) +- [x] The fourth lens (black-box use and documentation) ran on 2026-11-18 (fuzzing against + dummy-variable oracles, invariances, every docs code block, about 150 error-path probes): + eight findings, fixed (see the CHANGELOG, Unreleased). The two that matter are in LP-DiD's + non-absorbing rules and were inherited from `lpdid`: switches hidden in gaps, and + switch-off rows under the first-treatment estimator; the command's behaviour is now + `compat = :lpdid` - [x] Phase 7 gate (2026-11-18): `Pkg.test()` on Julia 1.13.3 and 1.10.13 (45,623 tests, the local MSS and Ilzetzki items included), format, fixtures, docs build, the p7 Stata diff --git a/crosscheck/README.md b/crosscheck/README.md index ed4dd41..0c1e14c 100644 --- a/crosscheck/README.md +++ b/crosscheck/README.md @@ -240,7 +240,10 @@ max(df_a − its levels, 1). `fx_lpdid` panels: `crosscheck/stata/p7_lpdid.do` → `test/fixtures/stata/p7_lpdid.csv` (1424 rows: 47 option sets × 2 panels, with the event-study, pooled, aggregate and pre-trend tables), compared in `test/test_crosscheck_p7.jl`. The formula is -`ldiff(y) ~ firstdiff(d) + fe(t)` with `estimator = LPDiD(…)`, `horizon = -5:8`. +`ldiff(y) ~ firstdiff(d) + fe(t)` with `estimator = LPDiD(…, compat = :lpdid)`, +`horizon = -5:8`. `compat = :lpdid` selects the command's rules where the package's +defaults differ (`docs/src/audit/lpdid.md`): it matters on `fx_lpdid_unbal` only; on +`fx_lpdid` the test also checks that the defaults give the same numbers. | `lpdid y, unit(id) time(t) treat(d) pre_window(5) post_window(8) …` | `panellp` | `vcov` spec | Agreement | |---|---|---|---| @@ -248,7 +251,7 @@ pre-trend tables), compared in `test/test_crosscheck_p7.jl`. The formula is | `ylags(2) controls(x)`, `dylags(2)`, `controls(x xb)`, `absorb(reg)` | `lags(y, 1:2) + x`, `lags(firstdiff(y), 1:2)`, `x + xb`, `fe(reg)` | `Clustered(:unit)` | as above | | `pmd(max)`, `pmd(3)`; `nevertreated`; `nocomp` | `LPDiD(pmd = :max \| 3)`; `control = :nevertreated`; `sample = :common` | as above | as above | | `pooled`, `post_pooled(2 6) pre_pooled(3 5)` | `pooled = true` with `horizon = 0:8 \| -5:-2`, `2:6 \| -5:-3` | as above | 1e-15 | -| `rw` (also with weights, `nocomp`, `pmd(max) nevertreated`, `pooled`) | `LPDiD(reweight = :lpdid)` | as above | 5e-15 | +| `rw` (also with weights, `nocomp`, `pmd(max) nevertreated`, `pooled`) | `LPDiD(reweight = true, compat = :lpdid)` | as above | 5e-15 | | a hand-written `reghdfe [pweight]` with the factor from the regression rows (`d_rw_hand`) | `LPDiD(reweight = true)` | as above | 4e-15 | | `rw` with `controls(x)`, `ylags(1)`, `dylags(2)`, `absorb(reg)`, `[pweight = w]`, `cluster(st)`, `pooled` (regression adjustment through `listreg`) | `LPDiD(reweight = true)` with the same terms | `Clustered(…; ssc = :none)`, normal bands | 2e-15 | | `nonabsorbing(5 \| 3)`, `(5, notyet)`, `(5, firsttreat)`, `(, firsttreat notyet)`, `(5, firsttreat notyet) pmd(2)`, with `nevertreated`, `untreated_before`, `pmd(2 \| max)`, `rw`, `nocomp`, `rw dylags(1)`; treatment `d2` | `LPDiD(nonabsorbing = L \| true, control = :notyet \| :nevertreated, firsttreat, untreated_before, …)` | `Clustered(:unit)` | 1e-14 | @@ -259,9 +262,7 @@ pre-trend tables), compared in `test/test_crosscheck_p7.jl`. The formula is Row `pre{k}` of `e(results)` is h = −k and `tau{h}` is h. `pre1` is h = −1, which `lpdid` reports as (0, missing) and `panellp` as a zero coefficient with a zero standard error. A pre-treatment difference spanned by the lagged outcomes is zero by construction in both. -`lpdid`'s `rw` counts the controls of a period differently from the regression's rows on -unbalanced panels (`docs/src/audit/lpdid.md`): `reweight = :lpdid` reproduces it. The wild -bootstrap is not compared (not implemented). +The wild bootstrap is not compared (not implemented). **General additions**: `crosscheck/stata/p7_general.do` → `p7_general.csv` (112 rows). diff --git a/docs/src/audit/lpdid.md b/docs/src/audit/lpdid.md index 786acee..bca5768 100644 --- a/docs/src/audit/lpdid.md +++ b/docs/src/audit/lpdid.md @@ -17,8 +17,9 @@ The [LP-DiD page](../methods/lpdid.md) maps every option to `panellp`. ## Agreement -Event-study coefficients, standard errors, confidence bounds and numbers of observations -agree to round-off (coefficients 1e-14, standard errors 5e-11) for every option set: +With `compat = :lpdid` on the unbalanced panel (on the balanced one the default rules +are the command's), event-study coefficients, standard errors, confidence bounds and +numbers of observations agree to round-off (coefficients 1e-14, standard errors 5e-11) for every option set: covariates, lagged outcomes, extra fixed effects, weights, clusters, `pmd`, `nevertreated`, `nocomp`, the non-absorbing and one-off rules with `notyet`, `firsttreat` and `untreated_before`, `rw` by weighted regression and by regression adjustment; so do @@ -36,29 +37,45 @@ the pooled estimates, `aggregate_average` and `pretrend_test`. equals that average to round-off (a test), and equals a hand-written `reghdfe` with the weights from the regression rows (`d_rw_hand` in the do-file). On balanced panels, and with regression adjustment (which uses the regression rows), there is no difference. - `LPDiD(reweight = :lpdid)` reproduces the command. -2. **An absorbing treatment is not checked.** Without `nonabsorbing()`, a treatment that + `compat = :lpdid` reproduces the command. +2. **A switch hidden in a gap is not seen.** The not-yet-treated and first-treatment rules + count switches by summing the observed ``\lvert\Delta D\rvert``, and ``\Delta D`` is + undefined next to an absent period or a missing treatment value. A unit observed + untreated, then (after a gap) treated, has no counted switch: under `notyet` its + treated rows serve as not-yet-treated controls, and under `firsttreat` its next switch + counts as the first. On `fx_lpdid_unbal`, 13 of the 260 rows of + `nonabsorbing(5, notyet)` at ``h = 0`` are such controls. (The command prints a warning + that gaps can hide switches.) `LPDiD` counts changes between consecutive observed + values; `compat = :lpdid` reproduces the command. +3. **The first-treatment estimator keeps switch-off rows.** With `firsttreat` and `notyet` + or `nevertreated`, a unit that is treated from its first row and later exits has one + observed switch, so its exit row passes the first-treatment rule, and, not being a + control (``\Delta D \ne 0``), escapes the control-group restriction: it enters the + regression with a regressor of −1. On `fx_lpdid_unbal` there are 8 such rows at + ``h = 0``; on a small noise-free panel with a true effect of 2 the estimate is 1.05. + `LPDiD` leaves those rows out; `compat = :lpdid` reproduces the command. +4. **An absorbing treatment is not checked.** Without `nonabsorbing()`, a treatment that switches off stays in the sample with a regressor of −1. `LPDiD()` refuses it and points to `nonabsorbing`. -3. **`nonabsorbing(0)` is `nonabsorbing(1)`.** The same here (the rule needs +5. **`nonabsorbing(0)` is `nonabsorbing(1)`.** The same here (the rule needs ``\Delta D_{t-1}``), stated in the table of clean controls. -4. **`nocomp` with a one-sided window** still requires the pre-treatment horizon 2 (and, +6. **`nocomp` with a one-sided window** still requires the pre-treatment horizon 2 (and, with only a pre window, horizon 0), outcomes included. `sample = :common` uses the requested horizons: pass `horizon = -2:H` for the command's sample. -5. **The pooled regressions under `nocomp`** require the clean-control flags of both +7. **The pooled regressions under `nocomp`** require the clean-control flags of both pooled windows. `pooled = true` with `sample = :common` uses the horizons of its own window. -6. **Mixed conventions in the output.** The event-study standard errors carry `reghdfe`'s +8. **Mixed conventions in the output.** The event-study standard errors carry `reghdfe`'s small-sample factor and t(G − 1) critical values, the regression-adjustment ones no factor and normal critical values, and `aggregate_average` no factor with t(M − 1). `averageeffect` and `pretrendtest` follow the command; for the event study choose with `Clustered(…; ssc = …)`. -7. **The pre-treatment row `pre1` is always (0, missing)**, also under `pmd()`, where the +9. **The pre-treatment row `pre1` is always (0, missing)**, also under `pmd()`, where the difference between ``y_{t-1}`` and the pre-treatment mean is not zero by construction. `panellp` estimates ``h = -1`` like any other horizon then. -8. **The reported `t` and `p` are rounded** (2 and 4 decimals); compare coefficients, +10. **The reported `t` and `p` are rounded** (2 and 4 decimals); compare coefficients, standard errors and bounds. -9. A pre-treatment difference that the lagged outcomes span (`ylags(k)`: horizons up to +11. A pre-treatment difference that the lagged outcomes span (`ylags(k)`: horizons up to ``-k``; `dylags(k)`: up to ``-(k+1)``) is zero by construction: `reghdfe` returns round-off, regression adjustment (0, 0). Here the coefficient is round-off as well, and `pretrendtest` leaves those horizons out, as the command does. diff --git a/docs/src/deviations.md b/docs/src/deviations.md index 158368f..9d05eeb 100644 --- a/docs/src/deviations.md +++ b/docs/src/deviations.md @@ -40,7 +40,9 @@ reference. The audit pages document each reference in detail. | topic | `lpdid` | `panellp` (`LPDiD`) | switch | |---|---|---|---| -| reweighting factor of the weighted regression (`rw`) | the treated share of the period among clean rows with the outcome, including rows whose ``\Delta D`` is undefined and that the regression drops | the treated share among the rows of the regression (the equally weighted average of the cohorts' comparisons) | `reweight = :lpdid` | +| reweighting factor of the weighted regression (`rw`) | the treated share of the period among clean rows with the outcome, including rows whose ``\Delta D`` is undefined and that the regression drops | the treated share among the rows of the regression (the equally weighted average of the cohorts' comparisons) | `compat = :lpdid` | +| switches, for `notyet`, `firsttreat` and the check of an absorbing treatment | summed from the observed first differences: a switch in a gap or behind a missing value is not seen | changes between consecutive observed treatment values | `compat = :lpdid` | +| first treatment with `notyet` or `nevertreated` | keeps the switch-off rows of a unit treated from its first row, with a regressor of −1 | a control has ``\Delta D = 0``: those rows are left out | `compat = :lpdid` | | a treatment that switches off, without `nonabsorbing()` | kept, with a regressor of −1 | an error | `nonabsorbing = L` | | `nocomp` with a one-sided window | also requires the pre-treatment horizon 2 (or horizon 0) | the requested horizons (`sample = :common`) | `horizon = -2:H` | | pooled regressions under `nocomp` | the flags of both pooled windows | the horizons of the window | — | diff --git a/docs/src/guide/inference.md b/docs/src/guide/inference.md index a95bffc..86f3625 100644 --- a/docs/src/guide/inference.md +++ b/docs/src/guide/inference.md @@ -44,7 +44,8 @@ Each convention is an explicit `ssc` value. Below, ``n`` is the number of observ The two `:mss` rows use ``N`` and ``T``, the numbers of units and periods in the data; `ssc = :mss` exists only for these two specifications, as in the authors' code. With unbalanced data the two-way factor turns negative at long horizons (``n \le N + K``), -which is reported. `ase` in `xtlp` and `xtscc` removes the small-sample factor but keeps +which is reported: the variance is then `NaN`. With `psd = :raw` its negative eigenvalues +are clipped instead, as the authors' code does, and the standard errors have no meaning. `ase` in `xtlp` and `xtscc` removes the small-sample factor but keeps their ``t(G-1)`` or ``t(T-1)`` critical values. `summarize(…; dist = :normal)` gives normal ones. diff --git a/docs/src/methods/absorption.md b/docs/src/methods/absorption.md index e67a656..9c62252 100644 --- a/docs/src/methods/absorption.md +++ b/docs/src/methods/absorption.md @@ -47,8 +47,17 @@ The remaining columns are scaled to unit norm and factorised by a pivoted QR. Co beyond the numerical rank (``|R_{jj}| ≤ 10^{-9}|R_{11}|``) are omitted as collinear. Omitted regressors are reported once per call, with the horizons where they were omitted. +With a tolerance looser than the default (`AlternatingProjections(tol = 1e-6)`) the within +transformation can stop above that ratio on a slowly mixing panel. The columns that are +mostly absorbed (a within share below 1e-2) are then transformed again at the default +tolerance and screened on that, so an absorbed regressor is omitted whatever the `tol`. + A *shock* that is absorbed or collinear in a horizon's sample makes that horizon -`:singular`: its coefficients are `NaN`, and the horizons are reported. This happens, for +`:singular`: its coefficients are `NaN`, and the horizons are reported. A shock is +collinear when it lies in the span of the other regressors, whichever column of the +collinear set the pivoting would omit: the outcome does not depend on the order of the +formula. (The lags that `LagAugmented` adds are the exception: they give way to the +formula's columns, so a shock collinear only through one of them stays identified.) This happens, for example, with a staggered treatment dummy whose later horizons only see untreated periods. When the shock is not identified at any horizon `panellp` throws an error, as for an aggregate shock with time effects. diff --git a/docs/src/methods/lpdid.md b/docs/src/methods/lpdid.md index 74f04c5..e3a1750 100644 --- a/docs/src/methods/lpdid.md +++ b/docs/src/methods/lpdid.md @@ -55,7 +55,7 @@ estimator). Both identities are tests of the package. |---|---|---| | default (absorbing treatment) | ``\Delta D_{it} = 1``, or ``D_{i,t+h} = 0`` (``D_{it} = 0`` at ``h < 0``) | default | | `control = :nevertreated` | controls only from units never treated in the data | `nevertreated` | -| `nonabsorbing = L` | ``\Delta D_{it} \in \{0, 1\}``, no switch in ``t-L, \dots, t-1`` nor in ``t+1, \dots, t+h``; at ``h < 0`` the same at ``t, t-1, \dots, t-\lvert h\rvert+1`` | `nonabsorbing(L)` | +| `nonabsorbing = L` | ``\Delta D_{it} \in \{0, 1\}``, no switch in ``t-L, \dots, t-1`` nor in ``t+1, \dots, t+h``; at ``h < 0`` the same at ``t, t-1, \dots, t-\lvert h\rvert+1``. `nonabsorbing = 0` is 1 (the rule needs ``\Delta D_{i,t-1}``) | `nonabsorbing(L)` | | `nonabsorbing = L, oneoff = true` | the same with events (``D = 1``) in place of switches | `nonabsorbing(L, oneoff)` | | `nonabsorbing = L, control = :notyet` | controls only from units untreated from their first row through ``t + h`` | `nonabsorbing(L, notyet)` | | `nonabsorbing = L, firsttreat = true` | rows with at most one switch (event) through ``t + h`` | `nonabsorbing(L, firsttreat)` | @@ -64,8 +64,14 @@ estimator). Both identities are tests of the package. - A condition on a value that is not observed (a missing treatment, an absent period) is false: the row is left out. -- With the default rule a treatment that ever switches off is an error, pointing to - `nonabsorbing`. (`lpdid` runs, with a regressor of −1 on those rows.) +- Switches are counted between consecutive *observed* treatment values. A unit observed + untreated and later treated has switched, also when the switch fell in an absent period + or behind a missing value: it is no longer a not-yet-treated control, and a later + switch is not its first. +- A control is a row with ``\Delta D_{it} = 0``: a row where the treatment switches off is + neither treated nor a control, under every rule. +- With the default rule a treatment that ever switches off, in a gap too, is an error + pointing to `nonabsorbing`. (`lpdid` runs, with a regressor of −1 on those rows.) - With a non-absorbing treatment (DGJT Section 4.2) the estimand is the effect of a treatment event under the assumption that effects stabilise within ``L`` periods; with `firsttreat` and not-yet- or never-treated controls it is the effect of entering @@ -101,15 +107,17 @@ included. levels to be connected to the controls', as any two-way prediction does. - The time effects may be group × time effects, `fe(region) & fe(t)` (comparisons within a region and period); `reweight = true` is then regression adjustment as well. -- `reweight = :lpdid` is a compatibility switch for the weighted regression: see the - [audit page](../audit/lpdid.md). +- `compat = :lpdid` applies the command's own rules where they differ from the ones on + this page; see [Reproducing `lpdid`](@ref) below. ## Base period, pooled estimates, averages - `pmd = k` (pre-mean differencing, Section 3.4) replaces the base ``y_{i,t-1}`` by the mean of ``y`` over ``t-k, \dots, t-1`` (all required); `pmd = :max` by the mean over - every earlier row of the unit. With a non-absorbing treatment the base window must also - be free of switches, and `:max` means ``k = L``. With `pmd`, ``h = -1`` is estimated like + every earlier row of the unit. With `nonabsorbing = L` the base must be clean as well: + no switch (event) in the ``k + L - 1`` periods before ``t``, so that the outcomes in the + base window are themselves ``L`` periods away from a switch; `:max` then means + ``k = L``. With `pmd`, ``h = -1`` is estimated like any other horizon (`lpdid` reports it as zero). - `pooled = true` gives the pooled estimate (Section 3.5): one regression of the outcome averaged over the horizons, less the base, on the rows where the whole window is @@ -135,6 +143,21 @@ factor and t(G − 1) critical values, is `lpdid`'s output. Any other specificat to the weighted-regression and direct-inclusion fits. The wild bootstrap of `lpdid` is not implemented. +## Reproducing `lpdid` + +On a balanced panel without missing values whose units all start untreated, the rules +above are the command's. Otherwise they differ in three places, and `compat = :lpdid` +switches to the command's: + +| | `LPDiD(...)` | `compat = :lpdid` (`lpdid` 1.1.0) | +|---|---|---| +| controls of a period in the weighted regression of `reweight` | the rows of the regression | also clean rows whose ``\Delta D`` is undefined, which the regression drops | +| switches, for `control = :notyet`, `firsttreat` and the check of an absorbing treatment | changes between consecutive observed values | the observed first differences: a switch in a gap is not seen | +| first treatment with not-yet- or never-treated controls | rows where the treatment switches off are left out | kept, with a regressor of −1, for a unit treated from its first row | + +`reweight = :lpdid` (from 0.2.1) is short for `reweight = true, compat = :lpdid`. The +[audit page](../audit/lpdid.md) has the details and the size of the differences. + ## Map from `lpdid` | `lpdid` | `panellp` | @@ -144,6 +167,7 @@ implemented. | `[pweight = w]`, `weights(w)` | `weights = :w` | | `cluster(c)` | `Clustered(c)` (declare `clusters = (c,)` for a column that is not a fixed effect) | | `rw` | `LPDiD(reweight = true)` | +| the command's numbers on unbalanced panels | `LPDiD(…, compat = :lpdid)` | | `pmd(k)`, `pmd(max)` | `LPDiD(pmd = k)`, `LPDiD(pmd = :max)` | | `nocomp` | `sample = :common` | | `pooled`, `post_pooled(a b)`, `pre_pooled(a b)` | `pooled = true` with `horizon = a:b`, `horizon = -b:-a` | diff --git a/src/design.jl b/src/design.jl index aa66b0e..67001bc 100644 --- a/src/design.jl +++ b/src/design.jl @@ -109,6 +109,10 @@ function _build_design(f::StatsModels.FormulaTerm, data; id::Symbol, time::Symbo endoterms, instterms = iv === nothing ? (Any[], Any[]) : iv # a path term leads(x) is the leads 1, …, hmax of x; lead ℓ enters at horizons h ≥ ℓ ispath = [t isa LeadsTerm && t.leads === nothing for t in regs] + shock === nothing && isempty(endoterms) && !isempty(ispath) && ispath[1] && + throw(ArgumentError( + "the first regressor is a path term, $(repr(regs[1])): it is a control, so it " * + "cannot be the default shock. Write the shock first, or name it with `shock = …`")) regs = Any[p ? LeadsTerm(t.term, 1:hmax) : t for (t, p) in zip(regs, ispath)] keepreg = [!(p && hmax < 1) for p in ispath] regs, ispath = regs[keepreg], ispath[keepreg] @@ -165,6 +169,12 @@ function _build_design(f::StatsModels.FormulaTerm, data; id::Symbol, time::Symbo _select_shocks(shock, coefnames, regs) any(j -> coefnames[j] == "(Intercept)", shockcols) && throw(ArgumentError("the intercept cannot be the shock")) + for j in shockcols + leadorder[j] > 0 && throw(ArgumentError( + "the shock $(repr(coefnames[j])) is a lead of a path term, leads(x): those " * + "are controls, present only at the horizons that reach them. Name another " * + "shock, or write the lead with a range, leads(x, a:b)")) + end # rows where the regressors of every horizon are defined (the leads of a path term are # checked at the horizons that use them, `_leadmask!`) diff --git a/src/estimators.jl b/src/estimators.jl index 0256193..b7abaa4 100644 --- a/src/estimators.jl +++ b/src/estimators.jl @@ -66,6 +66,7 @@ function _within_fit(d::PanelDesign, y::Vector{Float64}, mask::BitVector, added = Int[k for (k, j) in enumerate(cols) if d.lagorder[j] > 0] Xt = view(A, :, 2:(1 + nc)) absall = _absorbed_cols(view(A, :, 2:size(A, 2)), rawall, hasfe, d.intercept) + hasfe && _recheck_absorbed!(absall, A, d, rows, cols, zcols, rawall, fs, absorb, wr) xabs = absall[1:nc] K = length(d.coefnames) instkeep = falses(size(d.Z, 2)) @@ -118,6 +119,35 @@ function _within_fit(d::PanelDesign, y::Vector{Float64}, mask::BitVector, converged = conv, instkeep, first = fst) end +const _DEFAULT_ABSORB_TOL = 1e-12 + +# The absorbed-column screen (‖x̃‖² ≤ 1e-8 ‖x − x̄‖²) needs the within transformation to +# be accurate well below a norm ratio of 1e-4. With a looser `tol` than the default, on a +# slowly mixing panel the transformation can stop above it, and a column that the fixed +# effects absorb would pass the screen and get an arbitrary coefficient. The columns that +# are mostly absorbed (within share below 1e-2) are therefore transformed again at the +# default tolerance and screened on that. Nothing is done at the default tolerance, nor +# for `OneSweep`, which is one pass by definition. +function _recheck_absorbed!(absall::BitVector, A::Matrix{Float64}, d::PanelDesign, + rows::Vector{Int}, cols::Vector{Int}, zcols::Vector{Int}, rawall::NamedTuple, + fs::_FESample, absorb::AbsorptionMethod, wr::Union{Nothing, Vector{Float64}}) + (absorb isa AlternatingProjections && absorb.tol > _DEFAULT_ABSORB_TOL) || + return absall + tight = AlternatingProjections(_DEFAULT_ABSORB_TOL, absorb.maxiter, absorb.accelerate) + nc = length(cols) + for c in eachindex(absall) + absall[c] && continue + css = rawall.css[c] + sum(abs2, view(A, :, c + 1)) <= 1e-2 * css || continue + v = c <= nc ? d.X[rows, cols[c]] : d.Z[rows, zcols[c - nc]] + V = reshape(v, :, 1) + _absorb_matrix!(V, fs, tight, wr) + ss = wr === nothing ? sum(abs2, v) : LinearAlgebra.dot(wr, abs2.(v)) + ss <= _ABSORBED_TOL * css && (absall[c] = true) + end + return absall +end + """ _firststage_stats(M, Q, rE, Xen, zc) -> NamedTuple @@ -667,8 +697,10 @@ function _validate(est::CDOLS, d::PanelDesign, f) # the response's own date: ldiff(y) and cumul(g) start at t, ldiff(lags(y, 1)) at t − 1 ℓr = only(_collags(r.term)) base = ℓr == 0 ? "t" : "t−$ℓr" - regs, _, _ = _rewrite_rhs(f.rhs) - dates = vcat(d.intercept ? [0] : Int[], (_collags(t) for t in regs)...) + # the terms as the design built them (a path term `leads(x)` is its leads 1, …, H there, + # and absent when no horizon uses it), and the date of each of its columns + regs = unique(Any[ct[1] for ct in d.colterm if ct !== nothing]) + dates = Int[ct === nothing ? 0 : _collags(ct[1])[ct[2]] for ct in d.colterm] for j in d.shockcols nm = d.coefnames[j] ℓ = dates[j] diff --git a/src/lpdid.jl b/src/lpdid.jl index 08d22c8..a990709 100644 --- a/src/lpdid.jl +++ b/src/lpdid.jl @@ -10,22 +10,23 @@ function LPDiD(; control::Symbol = :clean, nonabsorbing = nothing, firsttreat::Bool = false, oneoff::Bool = false, untreated_before::Bool = false, reweight = false, - pmd = nothing) + pmd = nothing, compat::Symbol = :none) L = nonabsorbing === nothing || nonabsorbing === false ? nothing : nonabsorbing === true ? -1 : nonabsorbing isa Integer ? Int(nonabsorbing) : throw(ArgumentError( "nonabsorbing is the number of periods L after which effects stabilise (or `true` " * "with firsttreat and control = :notyet or :nevertreated); got $(repr(nonabsorbing))")) + # `reweight = :lpdid` (0.2.1) is `reweight = true, compat = :lpdid` + reweight === :lpdid && (compat = :lpdid) rw = reweight === false ? :none : - reweight === true ? :equal : - reweight isa Symbol ? reweight : - throw(ArgumentError("reweight must be true, false or :lpdid; got $(repr(reweight))")) + reweight === true || reweight === :lpdid ? :equal : + throw(ArgumentError("reweight must be true or false; got $(repr(reweight))")) k = pmd === nothing || pmd === false ? 0 : pmd === :max ? -1 : pmd isa Integer && pmd >= 1 ? Int(pmd) : throw(ArgumentError("pmd must be a number of periods k ≥ 1 or :max; got $(repr(pmd))")) - return LPDiD(control, L, firsttreat, oneoff, untreated_before, rw, k) + return LPDiD(control, L, firsttreat, oneoff, untreated_before, rw, k, compat) end function _label(e::LPDiD) @@ -36,9 +37,9 @@ function _label(e::LPDiD) e.firsttreat && push!(opts, "firsttreat = true") e.oneoff && push!(opts, "oneoff = true") e.untreated_before && push!(opts, "untreated_before = true") - e.reweight === :none || - push!(opts, "reweight = $(e.reweight === :equal ? true : repr(e.reweight))") + e.reweight === :none || push!(opts, "reweight = true") e.pmd == 0 || push!(opts, "pmd = $(e.pmd < 0 ? ":max" : e.pmd)") + e.compat === :none || push!(opts, "compat = :$(e.compat)") return "LPDiD($(join(opts, ", ")))" end @@ -79,20 +80,58 @@ function _runsum(p::PanelIndex, x::Vector{Float64}) return out end +# Number of switches of each unit up to each of its rows, counted between consecutive +# observed values: a change hidden in an absent period or behind a missing value counts. +function _switches(p::PanelIndex, D::Vector{Float64}) + out = Vector{Float64}(undef, length(D)) + for i in axes(p.rowof, 1) + last, n = NaN, 0 + for j in axes(p.rowof, 2) + r = p.rowof[i, j] + r == 0 && continue + if !isnan(D[r]) + (!isnan(last) && D[r] != last) && (n += 1) + last = D[r] + end + out[r] = n + end + end + return out +end + +# Number of units whose observed treatment values fall from 1 to 0 at some point. +function _exits(p::PanelIndex, D::Vector{Float64}) + n = 0 + for i in axes(p.rowof, 1) + last = NaN + for j in axes(p.rowof, 2) + r = p.rowof[i, j] + (r == 0 || isnan(D[r])) && continue + if last == 1 && D[r] == 0 + n += 1 + break + end + last = D[r] + end + end + return n +end + # x_{i,t−1} subtracted from x_{i,t} (NaN when either is undefined). _delta(p::PanelIndex, x::Vector{Float64}) = x .- _gather(x, _shift(p, -1)) # Mean of the outcome over the k periods before t (all required), or over every earlier # row of the unit when k < 0 (`pmd = :max`; undefined when the row of t − 1 is absent or no -# earlier outcome is observed). -function _premean(p::PanelIndex, y::Vector{Float64}, k::Int) +# earlier outcome is observed). `term` is the outcome's term, evaluated at each lag. +function _premean(p::PanelIndex, term, ctx::_EvalContext, k::Int) if k > 0 - acc = zeros(length(y)) + acc = zeros(nrows(p)) for l in 1:k - acc .+= _gather(y, _shift(p, -l)) + acc .+= _col1(term, ctx, -l) end return acc ./ k end + y = _col1(term, ctx) out = fill(NaN, length(y)) for i in axes(p.rowof, 1) s, n = 0.0, 0 @@ -134,8 +173,10 @@ function _did_resolve(est::LPDiD, d::PanelDesign, ctx::_EvalContext, isnan(D[r]) || (mx[p.unit[r]] = max(mx[p.unit[r]], D[r])) end never = BitVector(mx[p.unit[r]] == 0 for r in 1:n) - # not yet treated at t: no switch up to t, and untreated at the unit's first row - P = _runsum(p, abs.(dD)) + # not yet treated at t: no switch up to t, and untreated at the unit's first row. + # Switches are changes between consecutive observed values; `lpdid` sums the observed + # first differences, which misses a switch hidden in a gap. + P = est.compat === :lpdid ? _runsum(p, abs.(dD)) : _switches(p, D) entry = fill(NaN, nunits(p)) for r in 1:n p.period[r] == first_t(r) && (entry[p.unit[r]] = D[r]) @@ -175,7 +216,8 @@ function _did_resolve(est::LPDiD, d::PanelDesign, ctx::_EvalContext, C0 .&= quiet(k) end end - y = _col1(d.response.term, ctx) + term = d.response.term + y = _col1(term, ctx) k = est.pmd # with a non-absorbing treatment and no first-treatment rule, the pre-treatment mean # is over the stabilisation window @@ -184,7 +226,7 @@ function _did_resolve(est::LPDiD, d::PanelDesign, ctx::_EvalContext, throw(ArgumentError( "LPDiD(pmd = :max) with nonabsorbing = 0 has no pre-treatment window; give pmd a " * "number of periods")) - base = k == 0 ? _gather(y, _shift(p, -1)) : _premean(p, y, k) + base = k == 0 ? _col1(term, ctx, -1) : _premean(p, term, ctx, k) if k > 0 && est.nonabsorbing !== nothing for kk in 1:(k + L - 1) q = quiet(kk) @@ -193,7 +235,11 @@ function _did_resolve(est::LPDiD, d::PanelDesign, ctx::_EvalContext, end end end - return _DiD(est, horizons, D, dD, never, P, E0, Q, C0, y, base, false) + respok = trues(n) + for h in horizons + respok .&= isfinite.(_col1(term, ctx, h) .- base) + end + return _DiD(est, horizons, D, dD, never, P, E0, Q, C0, y, base, respok, false) end """ @@ -204,7 +250,9 @@ controls of horizon h (DGJT Sections 3.1 and 4.2), before any missing-value rule condition on an unobserved value is false. - Absorbing rule (the default; first treatment with not-yet-treated or never-treated - controls): ΔD_t = 1, or D_{t+h} = 0; D_t = 0 at pre-treatment horizons. + controls): ΔD_t = 1, or ΔD_t = 0 and D_{t+h} = 0 (D_t = 0 at pre-treatment horizons). + `compat = :lpdid` does not require ΔD_t = 0 of a control, which lets in the rows where a + unit treated from its first row switches off. - `nonabsorbing = L`: ΔD_t ∈ {0, 1} and no switch in the L periods before t, nor in t + 1, …, t + h; at h < 0, the same at t, t − 1, …, t − |h| + 1. With `oneoff`, events (D = 1) in place of switches. @@ -219,9 +267,10 @@ function _cleanflag(s::_DiD, p::PanelIndex, h::Int) lead = _shift(p, hp) flag = falses(n) if _absorbingrule(e) + lp = e.compat === :lpdid for r in 1:n j = lead[r] - flag[r] = s.dD[r] == 1 || (j != 0 && s.D[j] == 0) + flag[r] = s.dD[r] == 1 || ((lp || s.dD[r] == 0) && j != 0 && s.D[j] == 0) end elseif h >= 0 flag .= s.C0 @@ -280,7 +329,7 @@ end function _validate(est::LPDiD, d::PanelDesign, f) _check_option(est.control, (:clean, :nevertreated, :notyet), "control") - _check_option(est.reweight, (:none, :equal, :lpdid), "reweight") + _check_option(est.compat, (:none, :lpdid), "compat") d.response isa LDiffResponse || throw(ArgumentError( "LPDiD estimates the long difference y_{t+h} − y_{t−1}: write the response as " * "ldiff(y)")) @@ -300,12 +349,14 @@ function _validate(est::LPDiD, d::PanelDesign, f) "control = :notyet is a rule for a non-absorbing treatment (nonabsorbing = L); " * "with an absorbing treatment the default clean controls are the not-yet-treated " * "and the never-treated")) - dD = _delta(d.pidx, D) - nexit = count(==(-1), dD) + # also a switch-off hidden in a gap or behind a missing value (which `lpdid` + # does not see) + nexit = est.compat === :lpdid ? count(==(-1), _delta(d.pidx, D)) : + _exits(d.pidx, D) nexit == 0 || throw(ArgumentError( - "the treatment :$tvar is not absorbing: it switches off at $nexit row(s). Set " * - "LPDiD(nonabsorbing = L), with L the periods after which effects are assumed " * - "to stabilise (DGJT Section 4.2)")) + "the treatment :$tvar is not absorbing: it switches off for $nexit unit(s) " * + "(or row(s)). Set LPDiD(nonabsorbing = L), with L the periods after which " * + "effects are assumed to stabilise (DGJT Section 4.2)")) else est.nonabsorbing < 0 && !_pseudoabsorbing(est) && throw(ArgumentError( @@ -372,7 +423,7 @@ Weights of the equally weighted LP-DiD regression (DGJT Section 3.3): within eac every row is weighted by 1/(1 − p_t), p_t the (weighted) share of treated rows of the period in the sample, which turns the variance weights N_t p_t (1 − p_t) of the cohorts into their numbers of treated rows. Periods without a treated row or without a control -row leave the sample. `extra` marks rows outside the regression that `reweight = :lpdid` +row leave the sample. `extra` marks rows outside the regression that `compat = :lpdid` counts among the controls of their period (as `lpdid` 1.1.0 does: clean rows whose ΔD is undefined). """ @@ -385,7 +436,7 @@ function _rwweights(d::PanelDesign, s::_DiD, mask::BitVector, extra::BitVector) k = p.period[r] - p.pmin + 1 s.dD[r] == 1 ? (A1[k] += a(r)) : (A0[k] += a(r)) end - if s.spec.reweight === :lpdid + if s.spec.compat === :lpdid for r in findall(extra) A0[p.period[r] - p.pmin + 1] += a(r) end @@ -411,7 +462,7 @@ function _fit_horizon(s::_DiD, d::PanelDesign, h::Int, y::Vector{Float64}, end w = d.weights if s.spec.reweight !== :none - extra = s.spec.reweight === :lpdid ? _rwextra(s, d, h, y) : falses(0) + extra = s.spec.compat === :lpdid ? _rwextra(s, d, h, y) : falses(0) w, mask = _rwweights(d, s, mask, extra) end fit = _fit_within(d, h, y, mask, singletons, absorb, cols, 0; weights = w) @@ -427,8 +478,8 @@ function _rwextra(s::_DiD, d::PanelDesign, h::Int, y::Vector{Float64}) if s.common for hh in s.horizons flag .&= _cleanflag(s, d.pidx, hh) - flag .&= isfinite.(_gather(s.y, _shift(d.pidx, hh)) .- s.base) end + flag .&= s.respok end out = falses(length(flag)) for r in eachindex(flag) diff --git a/src/ols.jl b/src/ols.jl index 3858de2..4ba9dd7 100644 --- a/src/ols.jl +++ b/src/ols.jl @@ -93,6 +93,9 @@ function _ols(Xt::AbstractMatrix{Float64}, yt::Vector{Float64}, raw, hasfe::Bool d = abs.(LinearAlgebra.diag(F.R)) rank = count(>(_RANK_TOL * d[1]), d) if rank < length(kidx) + # a shock in the span of the other regressors is not identified, whichever column + # of the collinear set the pivoting would omit (it depends on the formula's order) + _shockspanned(Xt, kidx, shockcols, added) && return singular if any(j -> keep[j], added) _dropcollinear!(keep, Xt, added) else @@ -116,6 +119,30 @@ function _ols(Xt::AbstractMatrix{Float64}, yt::Vector{Float64}, raw, hasfe::Bool return (; β, B, keep, absorbed, resid, status = :ok) end +# Whether a shock column lies in the span of the other candidate columns `kidx` (unit +# norm, at the rank tolerance). The lags that `LagAugmented` added are not among the +# others: they give way to the formula's columns, so a shock collinear only through one of +# them stays identified. +function _shockspanned(Xt::AbstractMatrix{Float64}, kidx::Vector{Int}, + shockcols::Vector{Int}, added::Vector{Int}) + n = size(Xt, 1) + for j in shockcols + others = [k for k in kidx if k != j && !(k in added)] + isempty(others) && continue + x = Xt[:, j] + nx = LinearAlgebra.norm(x) + nx > 0 || return true + x ./= nx + F, _ = _eqr(Xt, others) + dg = abs.(LinearAlgebra.diag(F.R)) + r = count(>(_RANK_TOL * dg[1]), dg) + Q = F.Q * Matrix{Float64}(LinearAlgebra.I, n, r) + x .-= Q * (Q' * x) + LinearAlgebra.norm(x) <= _RANK_TOL && return true + end + return false +end + # Collinear columns among the kept ones, omitting those in `added` rather than the others: # the pivoted QR of the other columns first; then that of the `added` columns (unit norm) # residualised on them, at the same tolerance. diff --git a/src/panellp.jl b/src/panellp.jl index 1bba7c1..bfd84e5 100644 --- a/src/panellp.jl +++ b/src/panellp.jl @@ -117,6 +117,12 @@ function panellp(f::StatsModels.FormulaTerm, data; id::Symbol, time::Symbol, hor fits = _estimate(est, d, ctx, horizons, masks, singletons, absorb) window = 1:0 end + estimator isa LPDiD && !any(f -> f.status === :ok, fits) && + !all(f -> f.status === :empty, fits) && + throw(ArgumentError( + "LP-DiD: no horizon has both rows entering treatment and clean controls in " * + "the same period (the treatment switch is constant in every sample). Check " * + "the treatment column and the clean-control options: $(_label(estimator))")) _check_identified(fits, d, time) p = d.pidx panel = PanelSummary( @@ -182,7 +188,7 @@ function _response(::PanelLPEstimator, d::PanelDesign, ctx::_EvalContext, h::Int _lhs_at(d.response, ctx, h) end function _response(s::_DiD, d::PanelDesign, ctx::_EvalContext, h::Int) - return _gather(s.y, _shiftof(ctx, h)) .- s.base + return _col1(d.response.term, ctx, h) .- s.base end # Estimator-specific restrictions of the sample of horizon h. diff --git a/src/summary.jl b/src/summary.jl index 03cd0ab..af5375b 100644 --- a/src/summary.jl +++ b/src/summary.jl @@ -46,6 +46,9 @@ function synthetic(r::PanelLocalProjection, h::Integer; term = r.shocks[1], isempty(r.endogenous) || throw(ArgumentError( "panel LP-IV fits store no synthetic series (TLAHR(refine = true), which uses " * "them, is not defined for IV)")) + r.weights === nothing || throw(ArgumentError( + "weighted fits store no synthetic series (TLAHR(refine = true), which uses " * + "them, is not defined for weighted fits)")) f = _fit(r, h) j = _termindex(r.coefnames, term) f.status === :ok || diff --git a/src/types.jl b/src/types.jl index 0a57640..17858a9 100644 --- a/src/types.jl +++ b/src/types.jl @@ -185,7 +185,7 @@ CDOLS(; validate::Bool = true) = CDOLS(validate) """ LPDiD(; control = :clean, nonabsorbing = nothing, firsttreat = false, oneoff = false, - untreated_before = false, reweight = false, pmd = nothing) + untreated_before = false, reweight = false, pmd = nothing, compat = :none) LP-DiD, the local-projections difference-in-differences estimator of Dube, Girardi, Jordà & Taylor (2025): at each horizon h the long difference y_{i,t+h} − y_{i,t−1} is regressed @@ -207,13 +207,16 @@ Clean controls: - `nonabsorbing = L`: treatment can switch on and off, and its effects are assumed to stabilise within L periods (DGJT Section 4.2.3). Rows with ΔD_it ∈ {0, 1}, no switch in the L periods before t and none in t + 1, …, t + h (at h < 0: the same from t − |h| + 1 - to t). `oneoff = true` is a treatment that lasts one period by construction (D_it = 1 is + to t). `nonabsorbing = 0` is 1: the rule needs ΔD at t − 1. `oneoff = true` is a treatment that lasts one period by construction (D_it = 1 is an event): events in place of switches. - `control = :nevertreated` keeps as controls only units never treated in the data; `control = :notyet` (non-absorbing) only units untreated from their first row through t + h. - `firsttreat = true` (non-absorbing) keeps a unit's rows up to its first treatment spell - (at most one switch, or one event, through t + h). With `control = :notyet` or + (at most one switch, or one event, through t + h). +- Switches are counted between consecutive *observed* treatment values, so one hidden in + an absent period or behind a missing value is seen: a unit observed untreated and later + treated has switched, wherever the switch fell. With `control = :notyet` or `:nevertreated` it is the estimator of the effect of entering treatment for the first time (DGJT Section 4.2.2), which needs no stabilisation window: `nonabsorbing = true`. - `untreated_before = true` (non-absorbing) takes a unit as untreated before its first @@ -227,12 +230,18 @@ Estimands and outcomes: controls, and the estimate is the mean over the treated rows of the outcome less its prediction. A regression-adjustment fit reports the treatment coefficient only, and its scores are its influence function: use `Clustered(:unit; ssc = :none)`. - `reweight = :lpdid` counts the controls of the weighted regression as `lpdid` 1.1.0 does - (also clean rows whose ΔD is undefined, which are not in the regression). - `pmd = k` or `:max`: the pre-mean-differenced version (Section 3.4), with the mean of y over t − k, …, t − 1 (all required), or over every earlier row of the unit, as the - base period instead of y_{i,t−1}. With a non-absorbing treatment the base window must - also be free of switches (events). + base period instead of y_{i,t−1}. With `nonabsorbing = L` the base must be clean as + well: no switch (event) in the k + L − 1 periods before t; `:max` is then k = L. + +`compat = :lpdid` reproduces the Stata command `lpdid` 1.1.0 where it departs from these +rules (they agree on balanced panels without missing values whose units all start +untreated): a period's controls in the weighted regression of `reweight` also count clean +rows whose ΔD is undefined; switches are counted from the observed first differences only, +so one hidden in a gap is not seen; and the first-treatment estimator keeps the rows where +a unit treated from its first row switches off, with a regressor of −1. +`reweight = :lpdid` is short for `reweight = true, compat = :lpdid`. See [`averageeffect`](@ref) and [`pretrendtest`](@ref). """ @@ -242,8 +251,9 @@ struct LPDiD <: PanelLPEstimator firsttreat::Bool oneoff::Bool untreated_before::Bool - reweight::Symbol # :none, :equal, :lpdid + reweight::Symbol # :none, :equal pmd::Int # 0: none; k; −1 for :max + compat::Symbol # :none, or :lpdid (the Stata command's rules) end # LPDiD resolved for one call (lpdid.jl): treatment histories over the data rows. @@ -259,6 +269,7 @@ struct _DiD <: PanelLPEstimator C0::BitVector # base flag of the non-absorbing rules y::Vector{Float64} # outcome base::Vector{Float64} # base period of the long difference + respok::BitVector # the long difference is defined at every horizon common::Bool # `sample = :common`: the same rows at every horizon end diff --git a/src/vcov.jl b/src/vcov.jl index e95fcf2..ae8e438 100644 --- a/src/vcov.jl +++ b/src/vcov.jl @@ -459,12 +459,19 @@ function StatsAPI.vcov(spec::PanelVcov, r::PanelLocalProjection) push!(flag === :undefined ? undefined : leverage, fit.h) else flag === :negfactor && push!(negfactor, fit.h) - if metric !== :none + # a negative factor flips the sign of the variance: clipping its negative + # eigenvalues would leave numbers without meaning, so the variance is NaN. + # `psd = :raw` does clip it, as the Mei–Sheng–Shi code, whose order of + # operations it reproduces. + if flag === :negfactor && metric !== :raw + Vk = fill(NaN, size(Vk)) + elseif metric !== :none icol = r.intercept && fit.keep[1] ? 1 : nothing Vk, fixed = metric === :stata ? _psd_clip(Vk, fit.xsd[kidx], icol) : _psd_clip(Vk, nothing) end - any(<(0), LinearAlgebra.diag(Vk)) && push!(negvar, fit.h) + flag !== :negfactor && any(<(0), LinearAlgebra.diag(Vk)) && + push!(negvar, fit.h) V[kidx, kidx] = Vk end push!(Vs, V) @@ -486,8 +493,14 @@ function StatsAPI.vcov(spec::PanelVcov, r::PanelLocalProjection) @warn "$label: a period has leverage one in the synthetic regression at " * "h = $(_hlist(leverage)) (too few periods for the regressors?); the " * "variance is NaN there" - isempty(negfactor) || @warn "$label: the Mei–Sheng–Shi two-way factor is negative at " * - "h = $(_hlist(negfactor)) (n ≤ N + K, with N the units in the data)" + isempty(negfactor) || + @warn "$label: the Mei–Sheng–Shi two-way factor is negative at " * + "h = $(_hlist(negfactor)) (n ≤ N + K, with N the units in the data), and so is " * + "the variance: " * + (metric === :raw ? + "its negative eigenvalues are clipped (psd = :raw, as the authors' code), " * + "and the standard errors there have no meaning" : + "the standard errors are NaN there") isempty(negvar) || @warn "$label has negative variances at h = $(_hlist(negvar)); " * "their standard errors are NaN. A PSD repair (psd = :stata or :raw) avoids them" return PanelLPVcov(spec, r.coefnames, r.horizons, Vs, dfs, infos, _fingerprint(r)) @@ -518,8 +531,8 @@ end First-stage diagnostics of a panel LP-IV result, per horizon, with the variance specification `vcov` (any of [`Robust`](@ref), [`Clustered`](@ref), -[`DriscollKraaySE`](@ref), [`CosineHAR`](@ref), `TLAHR(refine = false)`) applied to the -first-stage scores, with its small-sample factor computed from the first stage's degrees +[`DriscollKraaySE`](@ref), [`CosineHAR`](@ref), [`Homoskedastic`](@ref), +`TLAHR(refine = false)`) applied to the first-stage scores, with its small-sample factor computed from the first stage's degrees of freedom (its regressors: the exogenous ones and the excluded instruments), as ivreg2, ivreghdfe and weakivtest do: @@ -530,12 +543,18 @@ ivreghdfe and weakivtest do: regressor it is the Wald F of Π = 0. ivreg2's `widstat`. - `effF` (one endogenous regressor): the Montiel Olea & Pflueger (2013) effective F, π̂'Z̃'Z̃π̂ / tr(V̂(π̂) Z̃'Z̃); equal to `kpF` with one instrument. +- `cdF`: the Cragg–Donald (1993) Wald F, the weak-identification statistic under + homoskedastic errors (ivreg2's `widstat` without `robust` or `cluster`); it does not + depend on `vcov`. With one endogenous regressor and `vcov = Homoskedastic()`, `kpF` + equals it. - `coef`, `se`: the first-stage coefficients of the instruments and their standard errors (instruments × endogenous) per horizon. Two-way clustered variances are not repaired (`psd` is ignored, as in ivreg2): where the variance that a statistic needs is not positive definite, the statistic is NaN, with a -warning. +warning. So it is where the variance is zero: with an aggregate instrument times a +time-invariant exposure on a balanced panel, the first-stage scores sum to zero within +every unit, and a variance clustered by unit has nothing to measure. """ function firststage(r::PanelLocalProjection; vcov::PanelVcov = TLAHR(refine = false)) isempty(r.endogenous) && @@ -567,11 +586,21 @@ function firststage(r::PanelLocalProjection; vcov::PanelVcov = TLAHR(refine = fa ones(Lm), fit.esample, fit.timepresent, dof, fs.scores, fit.iterations, fit.converged, trues(Lm), fit.nlags, SyntheticSeries[], falses(0), nothing, NaN, NaN, nothing) - V, _, _, flag = _sandwich(vcov, r, pfit, ctx, collect(1:Lm)) + V, _, _, flag = vcov isa Homoskedastic ? _firststage_classical(vcov, fs, dof) : + _sandwich(vcov, r, pfit, ctx, collect(1:Lm)) if flag === :undefined || flag === :bandwidth || !all(isfinite, V) push!(undefined, fit.h) continue end + # a variance that is round-off (the first-stage scores sum to zero within every + # cluster: an aggregate instrument times a time-invariant exposure, clustered by + # unit on a balanced panel) gives statistics that are round-off over round-off + Vobs = fs.bread * fs.scores.Mobs * fs.bread + if maximum(LinearAlgebra.diag(V)) <= 1e-20 * maximum(LinearAlgebra.diag(Vobs)) + push!(undefined, fit.h) + ses[k] = zeros(L, m) + continue + end ses[k] = reshape([v >= 0 ? sqrt(v) : NaN for v in LinearAlgebra.diag(V)], L, m) rk[k] = _kprk(fs, V, fit.dof.nobs) isnan(rk[k]) && push!(undefined, fit.h) @@ -583,20 +612,27 @@ function firststage(r::PanelLocalProjection; vcov::PanelVcov = TLAHR(refine = fa end end isempty(undefined) || @warn "firststage: $(_label(vcov)) is undefined, or the " * - "variance of the first stage is not positive definite, at " * + "variance of the first stage is not positive definite (or is zero: the scores " * + "sum to zero within every cluster), at " * "h = $(_hlist(sort!(unique(undefined)))); the statistics are NaN there" return FirstStageSummary(_label(vcov), collect(r.horizons), kpF, effF, rk, cdF, ninst, nendo, nobs, endos, insts, coefs, ses) end -# Kleibergen–Paap (2006) rk Wald statistic of the test that Π (L × m) has rank m − 1, with -# V = Var(vec Π̂): Θ̂ = L_z' Π̂ L_y⁻ᵀ (L_z L_z' = Z̃'Z̃/n, L_y L_y' = X̃'X̃/n), Θ̂ = U S V' (full -# SVD), q = m − 1; λ = (B ⊗ A') vec Θ̂ and rk = λ' [(B ⊗ A') V_Θ (B ⊗ A')']⁻¹ λ, with -# V_Θ = (L_y⁻¹ ⊗ L_z') V (L_y⁻¹ ⊗ L_z')'. KP's eq. (12) takes A = U₂ U₂₂⁻¹(U₂₂U₂₂')^{1/2} -# and B = (V₂₂V₂₂')^{1/2} V₂₂'⁻¹ V₂' (U₂, V₂ the trailing singular vectors): nonsingular -# transformations of U₂ and V₂, which leave the Wald statistic unchanged. With A = U₂ and -# B = V₂' there is nothing to invert, also when U₂₂ or V₂₂ is singular (tied singular -# values, e.g. group dummies × an aggregate with a diagonal Π̂). +# The classical variance of the first stage, Σ̂_v ⊗ (Z̃'Z̃)⁻¹, with Σ̂_v the residual +# covariance of the first-stage regressions (divided by n, or by the residual degrees of +# freedom with `ssc = :stata`): the first stage has no fit of its own to take it from. +function _firststage_classical(s::Homoskedastic, fs::FirstStage, dof::DofInfo) + L, m = size(fs.coef) + dfr = dof.nobs - dof.k - dof.df_a + nan = (fill(NaN, L * m, L * m), fill(NaN, L * m), VcovInfo(NaN, 0, 0, 0, false)) + (dfr > 0 && _wellconditioned(fs.zz)) || return (nan..., :undefined) + Σ = (fs.yy .- fs.coef' * fs.zz * fs.coef) ./ (s.ssc === :none ? dof.nobs : dfr) + V = kron(Σ, inv(fs.zz)) + df = s.ssc === :none ? Inf : Float64(dfr) + return (V + V') ./ 2, fill(df, L * m), VcovInfo(1.0, 0, 0, 0, false), :ok +end + # Whether a cross-product matrix, scaled to unit diagonal, has a reciprocal condition # number above 1e-10: below it, statistics that go through its normal equations (the # first-stage diagnostics) have no correct digits. @@ -624,6 +660,14 @@ function _craggdonald(fs::FirstStage, dfr::Int) return first(λ) * dfr / L end +# Kleibergen–Paap (2006) rk Wald statistic of the test that Π (L × m) has rank m − 1, with +# V = Var(vec Π̂): Θ̂ = L_z' Π̂ L_y⁻ᵀ (L_z L_z' = Z̃'Z̃/n, L_y L_y' = X̃'X̃/n), Θ̂ = U S V' (full +# SVD), q = m − 1; λ = (B ⊗ A') vec Θ̂ and rk = λ' [(B ⊗ A') V_Θ (B ⊗ A')']⁻¹ λ, with +# V_Θ = (L_y⁻¹ ⊗ L_z') V (L_y⁻¹ ⊗ L_z')'. KP's eq. (12) takes A = U₂ U₂₂⁻¹(U₂₂U₂₂')^{1/2} +# and B = (V₂₂V₂₂')^{1/2} V₂₂'⁻¹ V₂' (U₂, V₂ the trailing singular vectors): nonsingular +# transformations of U₂ and V₂, which leave the Wald statistic unchanged. With A = U₂ and +# B = V₂' there is nothing to invert, also when U₂₂ or V₂₂ is singular (tied singular +# values, e.g. group dummies × an aggregate with a diagonal Π̂). function _kprk(fs::FirstStage, V::Matrix{Float64}, n::Int) L, m = size(fs.coef) # nearly collinear instruments that the first stage keeps can leave Z̃'Z̃ numerically diff --git a/test/runtests.jl b/test/runtests.jl index 39879a0..a3f709d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -15,5 +15,10 @@ isempty(MISSING_SETS) || # Paper-scale Monte Carlo runs only on request: PLP_MC_FULL=1. const MC_FULL = get(ENV, "PLP_MC_FULL", "0") == "1" -@run_package_tests filter = ti -> !any(in(ti.tags), MISSING_SETS) && +# Only the items of this test directory: a copy of the repository inside it (a git +# worktree under .claude/, say) has test files of its own, which the runner would find too. +const TESTDIR = joinpath(ROOT, "test") + +@run_package_tests filter = ti -> startswith(ti.filename, TESTDIR) && + !any(in(ti.tags), MISSING_SETS) && (MC_FULL || !(:mc_full in ti.tags)) diff --git a/test/test_crosscheck_p7.jl b/test/test_crosscheck_p7.jl index 0423501..ccd58dd 100644 --- a/test/test_crosscheck_p7.jl +++ b/test/test_crosscheck_p7.jl @@ -12,8 +12,11 @@ # - pmd(k | max): LPDiD(pmd = k | :max); nevertreated: control = :nevertreated; # nonabsorbing(L, notyet firsttreat oneoff): nonabsorbing = L, control = :notyet, # firsttreat = true, oneoff = true; untreated_before: untreated_before = true; -# - rw: reweight = :lpdid (lpdid's count of the controls) without covariates, reweight = -# true with them (regression adjustment: Clustered(c; ssc = :none), normal bands); +# - rw: reweight = true (with covariates it is regression adjustment: +# Clustered(c; ssc = :none), normal bands); +# - every case runs with compat = :lpdid, the command's rules where they differ from the +# package's defaults (the controls counted by rw, switches hidden in gaps, switch-off +# rows under the first-treatment estimator): they differ on fx_lpdid_unbal only; # - aggregate_average: averageeffect(r; cluster); pretrend_test: pretrendtest(r; cluster). # # The general additions (crosscheck/stata/p7_general.do → p7_general.csv): reghdfe, ivreg2 @@ -101,7 +104,21 @@ mine = [x for x in ref if x.ds == ds && x.case == case] # the hand-written regression has the post-treatment horizons only hs = case == "d_rw_hand" ? (0:8) : (-5:8) + # every lpdid case with the command's own rules (compat = :lpdid); the hand-written + # regression is the package's default + case == "d_rw_hand" || + (est = LPDiD(est.control, est.nonabsorbing, est.firsttreat, est.oneoff, + est.untreated_before, est.reweight, est.pmd, :lpdid)) r = panellp(f, data; id = :id, time = :t, horizon = hs, estimator = est, kw...) + # on the balanced panel, whose units all start untreated, the package's default + # rules are the command's + if ds == "fx_lpdid" && case != "d_rw_hand" + est0 = LPDiD(est.control, est.nonabsorbing, est.firsttreat, est.oneoff, + est.untreated_before, est.reweight, est.pmd, :none) + r0 = panellp(f, data; id = :id, time = :t, horizon = hs, estimator = est0, kw...) + @test nobspath(r0) == nobspath(r) + @test isequal(coefpath(r0), coefpath(r)) + end s = summarize(r, spec(r, cl)) for x in mine x.table == "event" || continue diff --git a/test/test_lpdid.jl b/test/test_lpdid.jl index a1cad03..a6cde26 100644 --- a/test/test_lpdid.jl +++ b/test/test_lpdid.jl @@ -463,3 +463,272 @@ end ε == 1e-2 && @test all(isfinite, fs.kpF) && all(isfinite, fs.cdF) end end + +@testitem "judgement calls of the 2026-11 review" tags = [:core, :ols, :vcov, :absorb] begin + using StatsModels, StableRNGs + include(joinpath(@__DIR__, "support", "PLPDGPs.jl")) + using .PLPDGPs: read_csv + b = read_csv(joinpath(@__DIR__, "fixtures", "synthetic", "fx_balanced.csv")) + kw = (; id = :id, time = :t) + + # 1. a shock in the span of the other regressors is not identified, whatever the order + # of the formula (the pivoting used to decide which column of the set was omitted) + d = merge(b, (; x2 = 2 .* b.x, xw = b.x .+ b.w)) + for f in (@formula(leads(y) ~ x + x2 + fe(id)), @formula(leads(y) ~ x2 + x + fe(id)), + @formula(leads(y) ~ x + w + xw + fe(id)), @formula(leads(y) ~ xw + w + x + fe(id)), + @formula(leads(y) ~ w + xw + x + fe(id))) + e = try + panellp(f, d; horizon = 0:1, shock = "x", kw...) + catch e + e + end + @test e isa ArgumentError && occursin("collinear", e.msg) + end + # a collinear set without the shock still loses one column, with a note + r = @test_logs (:warn, r"omitted, collinear") panellp( + @formula(leads(y) ~ x + w + xw2 + fe(id)), merge(b, (; xw2 = 3 .* b.w)); horizon = 0, kw...) + @test r.fits[1].status === :ok && count(r.fits[1].keep) == 2 + + # 2. a negative Mei–Sheng–Shi factor: the variance is NaN, not a clipped number; + # psd = :raw keeps the authors' order of operations + keep = (b.id .<= 5) .| (b.t .<= 10) + rn = panellp(@formula(leads(y) ~ x + w + fe(id)), map(c -> c[keep], b); horizon = 54:55, kw...) + for psd in (:stata, :none) + cv = @test_logs (:warn, r"factor is negative at h = 54, 55.*NaN") vcov( + Clustered(:unit, :time; ssc = :mss, psd), rn) + @test all(i -> i.factor < 0, cv.info) + @test all(isnan, stderror(cv; term = "x")) + end + cr = @test_logs (:warn, r"factor is negative.*no meaning") vcov( + Clustered(:unit, :time; ssc = :mss, psd = :raw), rn) + @test all(isfinite, stderror(cr; term = "x")) + + # 3. the absorbed-column screen under a loose tolerance: a rotating panel (120 periods, + # each cohort of 3 units observed 4 periods) mixes slowly, and at tol = 1e-6 the + # within transformation stops with an aggregate regressor not yet at round-off + rng = StableRNG(1) + id, t = Int[], Int[] + u = 0 + for start in 1:117, k in 1:3 + global u += 1 + for s in 0:3 + push!(id, u) + push!(t, start + s) + end + end + agg = randn(rng, 120) + x = randn(rng, length(id)) .+ 0.5 .* agg[t] + y = 0.7 .* x .+ randn(rng, length(id)) .+ agg[t] + p = (; id, t, y, x, X = agg[t]) + for tol in (1e-12, 1e-6, 1e-5) + ab = AlternatingProjections(tol = tol) + # as the shock: absorbed by the time effects at every horizon + @test_throws ArgumentError panellp(@formula(y ~ X + x + fe(id) + fe(t)), p; + horizon = 0, shock = "X", absorb = ab, kw...) + # as a control: omitted + rc = panellp(@formula(y ~ x + X + fe(id) + fe(t)), p; horizon = 0, shock = "x", + absorb = ab, kw...) + @test rc.fits[1].absorbed == [false, true] + @test coef(rc, 0)[1] ≈ 0.7 atol = 0.1 + end +end + +@testitem "LP-DiD: switches hidden in gaps, first treatment, compat = :lpdid" tags = [:lpdid] begin + using StatsModels, LinearAlgebra + include(joinpath(@__DIR__, "support", "PLPDGPs.jl")) + using .PLPDGPs: read_csv + kw = (; id = :id, time = :t) + # dummy-variable OLS of y on x and period dummies + function tfe(y, x, t) + ts = sort!(unique(t)) + X = hcat(x, [Float64(tt == s) for tt in t, s in ts]) + return (pinv(X' * X) * (X' * y))[1] + end + + # 1. the first-treatment estimator on the unbalanced panel, sample built by hand: + # switches counted between consecutive observed treatment values + d = read_csv(joinpath(@__DIR__, "fixtures", "synthetic", "fx_lpdid_unbal.csv")) + val(v, k) = ismissing(v[k]) ? NaN : Float64(v[k]) + row = Dict((i, t) => k for (k, (i, t)) in enumerate(zip(d.id, d.t))) + at(v, i, t) = (k = get(row, (i, t), 0); k == 0 ? NaN : val(v, k)) + sw = Dict{Tuple{Int, Int}, Int}() # switches of unit i up to its row at t + e0 = Dict{Int, Bool}() # untreated at its first row + for i in unique(d.id) + ts = sort!([t for (j, t) in keys(row) if j == i]) + e0[i] = at(d.d2, i, ts[1]) == 0 + last, n = NaN, 0 + for t in ts + v = at(d.d2, i, t) + if !isnan(v) + (!isnan(last) && v != last) && (n += 1) + last = v + end + sw[(i, t)] = n + end + end + f = @formula(ldiff(y2) ~ firstdiff(d2) + fe(t)) + est = LPDiD(nonabsorbing = true, firsttreat = true, control = :notyet) + r = panellp(f, d; horizon = 0:4, estimator = est, singletons = :keep, kw...) + rc = panellp(f, d; horizon = 0:4, singletons = :keep, kw..., + estimator = LPDiD(nonabsorbing = true, firsttreat = true, control = :notyet, compat = :lpdid)) + for h in 0:4 + ys, xs, tt = Float64[], Float64[], Int[] + for (i, t) in zip(d.id, d.t) + dy = at(d.y2, i, t + h) - at(d.y2, i, t - 1) + dd = at(d.d2, i, t) - at(d.d2, i, t - 1) + (isfinite(dy) && isfinite(dd) && haskey(row, (i, t + h))) || continue + sw[(i, t + h)] <= 1 || continue # first treatment spell + treated = dd == 1 + control = dd == 0 && at(d.d2, i, t + h) == 0 && sw[(i, t + h)] == 0 && e0[i] + (treated || control) || continue + push!(ys, dy); push!(xs, dd); push!(tt, t) + end + @test nobs(r, h) == length(ys) + @test coef(r, h)[1] ≈ tfe(ys, xs, tt) rtol = 1e-10 + # no row where the treatment switches off + @test all(k -> !r.fits[h + 1].esample[k] || + at(d.d2, d.id[k], d.t[k]) - at(d.d2, d.id[k], d.t[k] - 1) != -1, + eachindex(d.id)) + end + # the command's rules keep such rows and miss hidden switches: another sample + @test nobspath(rc) != nobspath(r) + @test occursin("compat = :lpdid", sprint(show, MIME"text/plain"(), rc)) + @test_throws ArgumentError LPDiD(compat = :stata) |> e -> panellp(f, d; horizon = 0, estimator = e, kw...) + + # 2. a unit treated from its first row that switches off (no gaps, no missing values) + N, T = 9, 10 + id = repeat(1:N; inner = T) + t = repeat(1:T; outer = N) + D = zeros(N * T) + for i in 1:N, s in 1:T + k = (i - 1) * T + s + D[k] = i <= 3 ? Float64(s >= 5) : i <= 7 ? 0.0 : Float64(s <= 3) # units 8, 9 exit at 4 + end + y = [0.1 * id[k] + 0.3 * t[k] + 2.0 * D[k] * (id[k] <= 3) for k in eachindex(id)] + p = (; id, t, y, D) + g = @formula(ldiff(y) ~ firstdiff(D) + fe(t)) + for control in (:notyet, :nevertreated) + r1 = panellp(g, p; horizon = 0, kw..., + estimator = LPDiD(nonabsorbing = true, firsttreat = true, control = control)) + @test coef(r1, 0)[1] ≈ 2.0 atol = 1e-10 + r2 = panellp(g, p; horizon = 0, kw..., estimator = LPDiD(nonabsorbing = true, + firsttreat = true, control = control, compat = :lpdid)) + @test nobs(r2, 0) == nobs(r1, 0) + 2 # the two switch-off rows + @test abs(coef(r2, 0)[1] - 2.0) > 0.5 + end + + # 3. a switch hidden in a gap: the unit is not a not-yet-treated control afterwards, + # and an absorbing treatment that switches off inside a gap is refused + keep = [!(id[k] == 4 && t[k] == 5) for k in eachindex(id)] + D2 = copy(D) + for k in eachindex(id) + id[k] == 4 && t[k] >= 5 && (D2[k] = 1.0) # unit 4 enters in its absent period + end + q = map(v -> v[keep], (; id, t, y, D = D2)) + q = map(v -> v[q.id .<= 7], q) + ny = panellp(g, q; horizon = 0, kw..., + estimator = LPDiD(nonabsorbing = 2, control = :notyet)) + @test !any(k -> ny.fits[1].esample[k] && q.id[k] == 4 && q.t[k] >= 6, eachindex(q.id)) + nyc = panellp(g, q; horizon = 0, kw..., + estimator = LPDiD(nonabsorbing = 2, control = :notyet, compat = :lpdid)) + @test any(k -> nyc.fits[1].esample[k] && q.id[k] == 4 && q.t[k] >= 6, eachindex(q.id)) + D3 = [id[k] == 5 ? Float64(t[k] <= 4) : D[k] for k in eachindex(id)] # unit 5 exits at 5 + gapexit = [!(id[k] == 5 && t[k] == 5) for k in eachindex(id)] + z = map(v -> v[gapexit .& (id .<= 7)], (; id, t, y, D = D3)) + e = try + panellp(g, z; horizon = 0, estimator = LPDiD(), kw...) + catch e + e + end + @test e isa ArgumentError && occursin("not absorbing", e.msg) + @test panellp(g, z; horizon = 0, estimator = LPDiD(compat = :lpdid), kw...) isa PanelLocalProjection + + # 4. no horizon with treated rows and clean controls: an error that says so + e = try + panellp(g, map(v -> v[id .<= 3], p); horizon = 0, kw..., + estimator = LPDiD(control = :nevertreated)) + catch e + e + end + @test e isa ArgumentError && occursin("LP-DiD", e.msg) && occursin("clean controls", e.msg) + + # 5. a response with a lag inside is read at its own periods, not through the row of + # t + h (as in every other estimator) + b = read_csv(joinpath(@__DIR__, "fixtures", "synthetic", "fx_lpdid.csv")) + drop = [b.id[k] == 1 && b.t[k] == 10 for k in eachindex(b.id)] # unit 1 enters at 8 + bb = map(v -> v[.!drop], b) + rl = panellp(@formula(ldiff(lags(y, 1)) ~ firstdiff(d) + fe(t)), bb; horizon = 2:2, + estimator = LPDiD(), kw...) + k8 = findfirst(k -> bb.id[k] == 1 && bb.t[k] == 8, eachindex(bb.id)) + @test rl.fits[1].esample[k8] # y at t + 1 and t − 2 exist; the row of t + 2 does not +end + +@testitem "review of 2026-11-18: CD-OLS with a path term, shocks, first-stage variances" tags = [:core, :cdols, :lpiv, :api] begin + using StatsModels + include(joinpath(@__DIR__, "support", "PLPDGPs.jl")) + using .PLPDGPs: read_csv + FIX = joinpath(@__DIR__, "fixtures", "synthetic") + kw = (; id = :id, time = :t) + + # the CD-OLS checks date every column of the design, also with a path term before the shock + c = read_csv(joinpath(FIX, "fx_unbalanced.csv")) + a = panellp(@formula(ldiff(y) ~ x + lags(x, 1) + leads(w)), c; horizon = 0:3, + estimator = CDOLS(), kw...) + b = panellp(@formula(ldiff(y) ~ leads(w) + x + lags(x, 1)), c; horizon = 0:3, + estimator = CDOLS(), shock = "x", kw...) + @test coefpath(a) ≈ coefpath(b) + # the path of the outcome itself is the outcome on the right-hand side after the base + e = try + panellp(@formula(ldiff(y) ~ x + lags(x, 1) + leads(y)), c; horizon = 2, + estimator = CDOLS(), kw...) + catch e + e + end + @test e isa ArgumentError && occursin("outcome enters the right-hand side", e.msg) + + # a path column is a control: it is not the shock, by name or by default + for (f, k) in ((@formula(leads(y) ~ x + leads(x) + fe(id)), (; shock = "F1.x")), + (@formula(leads(y) ~ leads(x) + w + fe(id)), (;))) + for hz in (0:2, 1:2, -2:-1) + hz == -2:-1 && haskey(k, :shock) && continue # no such column then + e = try + panellp(f, c; horizon = hz, k..., kw...) + catch e + e + end + @test e isa ArgumentError && occursin("path term", e.msg) + end + end + + # first stage with the classical variance: with one endogenous regressor its F is the + # Cragg–Donald F + v = read_csv(joinpath(FIX, "fx_lpiv.csv")) + r = panellp(@formula(leads(y) ~ (s & ffr ~ s & mp) + w + fe(id) + fe(t)), v; + horizon = 0:1, kw...) + fs = firststage(r; vcov = Homoskedastic()) + @test fs.kpF ≈ fs.cdF rtol = 1e-10 + @test all(x -> isfinite(only(x)) && only(x) > 0, fs.se) + @test fs.cdF ≈ (only.(fs.coef) ./ only.(fs.se)) .^ 2 rtol = 1e-10 + # clustered by unit, the scores of an aggregate instrument times a time-invariant + # exposure sum to zero within every unit of a balanced panel: undefined, not 1e33 + rz = panellp(@formula(leads(y) ~ (s & ffr ~ s & mp) + fe(id) + fe(t)), v; + horizon = 0:1, kw...) + fu = @test_logs (:warn, r"firststage.*zero") firststage(rz; vcov = Clustered(:unit)) + @test all(isnan, fu.kpF) && all(isnan, fu.effF) && all(isfinite, fu.cdF) + @test all(isfinite, firststage(rz; vcov = Robust()).kpF) + # with a unit-level control the unit sums are not zero, and the statistic is defined + @test all(isfinite, firststage(r; vcov = Clustered(:unit)).kpF) + + # weighted fits store no synthetic series, and say so + w = panellp(@formula(leads(y) ~ s & X + fe(id) + fe(t)), + merge(read_csv(joinpath(FIX, "fx_tlahr.csv")), (;)); horizon = 0, kw...) + @test synthetic(w, 0) isa NamedTuple + lp = read_csv(joinpath(FIX, "fx_lpdid.csv")) + ww = panellp(@formula(leads(y) ~ x + fe(id) + fe(t)), lp; horizon = 0, weights = :w, kw...) + e = try + synthetic(ww, 0) + catch e + e + end + @test e isa ArgumentError && occursin("weighted", e.msg) +end