Skip to content

marginal_effects(): report covariates of the NTN mu design - #53

Merged
davidhbernstein merged 1 commit into
mainfrom
fix/me-mu-design-covariates
Oct 1, 2026
Merged

davidhbernstein merged 1 commit into
mainfrom
fix/me-mu-design-covariates

Conversation

@davidhbernstein

Copy link
Copy Markdown
Owner

Bug

For sfm(model_name = "NTN") fitted with uhet/muhet, marginal_effects() built its covariate list from the σ_u design alone (colnames(Z)[keep], where Z is z_spec$Z). The mu_spec δs were then looked up by name for those covariates only. As a result:

  • uhet = ~ z1, muhet = ~ z2 returned a z1 column and no z2 column, with no warning. Yet z2 moves E[u] through μ.
  • muhet = ~ z alone, where σ_u is intercept-only, stopped with "every column of the variance-determinant design is constant".

Both contradict ?marginal_effects, which says a covariate in one design only "contributes only through that one".

Fix

The chain-rule loop already handled a covariate missing from one design, since .sfa_me_delta_by_name() zero-fills it. It was simply never given the μ-only names. The covariate list for the truncated-normal branch is now the union, by name, of the non-constant columns of both designs. The σ_u δ is zero-filled the same way.

Other paths are unaffected:

  • the scaling path, half-normal and exponential (NHN_Z, NE_Z);
  • psfm (TRE_Z, GTRE_Z), including component = "h".

They build the same nm and d as before. All 160 existing marginal-effects expectations pass unchanged.

Test

test-m4-truncnormal-marginal.R gains "a covariate in the mu design only is reported, not dropped".

  • It fits uhet = ~ z1, muhet = ~ z2.
  • It checks dE_u.dz1, dE_u.dz2 and dVar_u.dz2 at three rows against numDeriv derivatives of the closed-form truncated-normal moments. Each covariate is moved in its own design, independently of the package's helpers, with a 1e-5 relative tolerance.
  • It also checks the μ-only fit.

On main this test fails: z2 is absent and the μ-only call errors. On this branch it passes.

Docs (issue #3)

?marginal_effects gains a table of which fits are supported, their link, and which covariates get a column. The links were checked against sfm(): the NTN het path honours z_link, and psfm uses the variance link. \value now lists the "component" attribute and the sigma_h/E_h/Var_h/dE_h.d<z> names used for component = "h". \usage is unchanged.

Local suite

5239 passed, 0 failed, 4 errors. All 4 errors are plm::pdata.frame(): already a pdata.frame in test-panel-boundary.R and test-gtre-draws.R. They come from the local plm 2.6.3, not this change, and main's CI is green on those files.

🤖 Generated with Claude Code

https://claude.ai/code/session_014oJL17ewiahA8pWbemnqNp


Generated by Claude Code

For a truncated-normal fit with uhet/muhet, the covariate list was taken
from the sigma_u design alone. A covariate only in the mu design was
silently dropped (uhet = ~ z1, muhet = ~ z2 gave no z2 column), and
muhet alone stopped with "every column ... is constant". The chain rule
already handled a covariate in one design only; it was never given the
name. The list is now the union, by name, of the non-constant columns of
both designs, with each delta zero-filled where its design lacks the
column.

The new test checks dE/dz1, dE/dz2 and dVar/dz2 against numerical
derivatives of the closed-form truncated-normal moments.

?marginal_effects gains a summary table of fits, links and reported
covariates, and its \value now lists the "component" attribute and the
_h column names returned for component = "h".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014oJL17ewiahA8pWbemnqNp
@davidhbernstein
davidhbernstein merged commit c94721a into main Oct 1, 2026
5 checks passed
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.

2 participants