marginal_effects(): report covariates of the NTN mu design - #53
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
For
sfm(model_name = "NTN")fitted withuhet/muhet,marginal_effects()built its covariate list from the σ_u design alone (colnames(Z)[keep], whereZisz_spec$Z). Themu_specδs were then looked up by name for those covariates only. As a result:uhet = ~ z1, muhet = ~ z2returned az1column and noz2column, with no warning. Yet z2 moves E[u] through μ.muhet = ~ zalone, 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:
NHN_Z,NE_Z);psfm(TRE_Z,GTRE_Z), includingcomponent = "h".They build the same
nmanddas before. All 160 existing marginal-effects expectations pass unchanged.Test
test-m4-truncnormal-marginal.Rgains "a covariate in the mu design only is reported, not dropped".uhet = ~ z1, muhet = ~ z2.dE_u.dz1,dE_u.dz2anddVar_u.dz2at three rows againstnumDerivderivatives 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.On main this test fails:
z2is absent and the μ-only call errors. On this branch it passes.Docs (issue #3)
?marginal_effectsgains a table of which fits are supported, their link, and which covariates get a column. The links were checked againstsfm(): the NTN het path honoursz_link, andpsfmuses the variance link.\valuenow lists the"component"attribute and thesigma_h/E_h/Var_h/dE_h.d<z>names used forcomponent = "h".\usageis unchanged.Local suite
5239 passed, 0 failed, 4 errors. All 4 errors are
plm::pdata.frame(): already a pdata.frameintest-panel-boundary.Randtest-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