Skip to content

Comments

Fix iplot parsing for IV sunab terms#633

Open
kmfrick wants to merge 1 commit intolrberge:masterfrom
kmfrick:master
Open

Fix iplot parsing for IV sunab terms#633
kmfrick wants to merge 1 commit intolrberge:masterfrom
kmfrick:master

Conversation

@kmfrick
Copy link

@kmfrick kmfrick commented Feb 13, 2026

iplot() errors on valid IV models that use sunab(..., no_agg = TRUE) in an interacted endogenous term, even though the coefficient names are valid and plottable.

On the econometrics side, you may argue that Sun & Abraham do not cover fuzzy designs, so this is working as intended as we don't know what the Wald-DiD estimator yields under staggered treatments (do we? maybe under homogeneous effects but staggered treatments?). But then I think it should at least output a clearer error like the "without a doubt, your model is misspecified" when you have only variables that are colinear with the FEs. I may do that if you want.

Repro

library(data.table)
library(fixest)

set.seed(20260213L)
n_id <- 400L
never_cohort <- 100L

dt <- CJ(id = 1:n_id, rel_time = -2:2)
dt[, high_growth := as.integer(id <= n_id / 2L)]
dt[, cohort := fifelse(high_growth == 1L, 0L, never_cohort)]
dt[, x := rnorm(.N)]
dt[, id_fe := rnorm(n_id)[id]]
dt[, y := 0.08 * x * (rel_time >= 0L) * high_growth + id_fe + rnorm(.N, sd = 0.25)]

iv_model <- feols(
  y ~ 1 |
    x:sunab(cohort, rel_time, ref.p = -1, ref.c = never_cohort, no_agg = TRUE) ~
    sunab(cohort, rel_time, ref.p = -1, ref.c = never_cohort, no_agg = TRUE),
  data = dt
)

iplot(iv_model)

Actual behavior

iplot() throws:

In iplot(), no valid variable created with i() found...

Expected behavior

iplot() should plot the IV sunab coefficient

Root cause

In IV stage 2, iplot() used broad fit_ prefix stripping, which would fail to recover the correct i()/sunab term names.

I think there was an issue in the past with this (which maybe I reported myself? not sure), so I was extra cautious in stripping only actual endogenous variables that were fitted in the first stage.

Fix

  • Restrict renaming to known fitted endogenous names via object$iv_endo_names_fit.
  • Strip only the fitted-endo interaction prefix (fit_<endo>:) where appropriate.
  • Keep a fallback path for legacy objects without iv_endo_names_fit.

Tests

Added regression coverage in tests/fixest_tests.R:

  1. IV + sunab(..., no_agg = TRUE) interacted fitted regressor now works with iplot().
  2. IV with an exogenous i() variable named with fit_ prefix (e.g. fit_user) still works, preventing over-matching regressions.

Verification

  • Targeted test chunk (Interact) passes.
  • Repro script now reports IV iplot: OK.

Disclaimer

I noticed the error while writing code for a WP and thought it'd be pretty easy to fix. I had Codex write the correction, and it just changed the regex, but I was suspicious so I probed it a bit more and found the issue with variables that start with "fit_", so I had it make the code robust to that as well. Tests pass, I have experience with R and the code smells okay to me, but let me know if you want me to check more thoroughly something specific.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant