Skip to content

REF/ENH delta method and nonlinear wald test rebased - #7758

Merged
josef-pkt merged 11 commits into
statsmodels:mainfrom
josef-pkt:REF_delta_method_rebased
Nov 2, 2021
Merged

josef-pkt merged 11 commits into
statsmodels:mainfrom
josef-pkt:REF_delta_method_rebased

Conversation

@josef-pkt

Copy link
Copy Markdown
Member

rebased version of #2296

trivial merge conflict
needed fixes to run and pass unit tests

@josef-pkt

josef-pkt commented Sep 26, 2021 •

Copy link
Copy Markdown
Member Author

name inconsistency
uses grad as method name but api/signature has switched to deriv

this doesn't seem to have method for vectorized t_test, only joint wald test

however summary has the full params_table

e.g. in the unit test case (pdb at end of test_deltacov_margeff )

comparing delta_cov with Poisson margins

(Pdb) nlpm
<statsmodels.stats._delta_method.NonlinearDeltaCov object at 0x0000000F639B7C10>

(Pdb) nlpm.summary()
<class 'statsmodels.iolib.table.SimpleTable'>
(Pdb) print(nlpm.summary())
                             Test for Constraints
==============================================================================
                 coef    std err          z      P>|z|      [0.025      0.975]
------------------------------------------------------------------------------
c0            -0.1410      0.008    -18.250      0.000      -0.156      -0.126
c1            -0.6634      0.028    -23.357      0.000      -0.719      -0.608
c2             0.0947      0.005     19.348      0.000       0.085       0.104
c3            -0.0928      0.004    -21.513      0.000      -0.101      -0.084
c4             0.7295      0.033     22.241      0.000       0.665       0.794
c5             0.0911      0.001     60.976      0.000       0.088       0.094
c6            -0.0339      0.025     -1.366      0.172      -0.083       0.015
c7             0.1451      0.041      3.531      0.000       0.065       0.226
c8             0.5534      0.071      7.840      0.000       0.415       0.692
c9             1.8803      0.035     54.390      0.000       1.813       1.948
==============================================================================
(Pdb) print(marg.summary())
       Poisson Marginal Effects
=====================================
Dep. Variable:                      y
Method:                          dydx
At:                              mean
==============================================================================
                dy/dx    std err          z      P>|z|      [0.025      0.975]
------------------------------------------------------------------------------
x1            -0.1410      0.008    -18.250      0.000      -0.156      -0.126
x2            -0.6634      0.028    -23.357      0.000      -0.719      -0.608
x3             0.0947      0.005     19.348      0.000       0.085       0.104
x4            -0.0928      0.004    -21.513      0.000      -0.101      -0.084
x5             0.7295      0.033     22.241      0.000       0.665       0.794
x6             0.0911      0.001     60.976      0.000       0.088       0.094
x7            -0.0339      0.025     -1.366      0.172      -0.083       0.015
x8             0.1451      0.041      3.531      0.000       0.065       0.226
x9             0.5534      0.071      7.840      0.000       0.415       0.692
==============================================================================

last line in lpm is for constant that margeff does not include

@josef-pkt

Copy link
Copy Markdown
Member Author

needs clearer description about difference between joint and vectorized.
Is it the same in base t_test verss wald_test ?

test cases look good, i.e. it looks like it does what we need.

@josef-pkt
josef-pkt force-pushed the REF_delta_method_rebased branch from 7bac6b2 to 4241fdf Compare September 27, 2021 04:18
@josef-pkt

Copy link
Copy Markdown
Member Author

It looks like I don't have any example scripts from 2015 except for those that look like the basis for the unit test examples.

@josef-pkt

Copy link
Copy Markdown
Member Author

I think I looked at this before, confint for predicted probs in discrete models in Stata

Xu, Jun, and J. Scott Long. "Confidence intervals for predicted outcomes in regression models for categorical outcomes." The Stata Journal 5, no. 4 (2005): 537-559.

@josef-pkt

Copy link
Copy Markdown
Member Author

last commit left a +1 in there that I had added to trigger a test failure
marg.margeff+1

@josef-pkt

josef-pkt commented Sep 28, 2021 •

Copy link
Copy Markdown
Member Author

coordinate usage and unit testing with #7166 #7597. It includes get_prediction (mean only) for discrete, Logit, Poisson
need get_prediction for other which , so they can also be used for unit test cases for delta method

lmstat = np.dot(np.dot(diff.T, np.linalg.inv(v)), diff)
return lmstat, stats.chi2.sf(lmstat, df_constraints)

def se_vectorized(self):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure we need _vectorized postfix.
sounds a bit strange

@josef-pkt

Copy link
Copy Markdown
Member Author

another usecase:
params = (linpred, params_extra) in e.g.
count models with extra params

in multilink models we could have params = (linpred, linpred2)
where linpred and their cov_params can be computed as linear transformation.

but we still need cross-cov_params, i.e. off-diagonal blocks.
We don't have those yet.

When we have analytical derivatives, then we won't gain anything, deriv already uses linear prediction part.
It might be useful when we have derivatives in terms of distribution dargs like (mu, scale) in Het models.

@josef-pkt

Copy link
Copy Markdown
Member Author
  • rows are constraints, columns are for params, params has to be a single parameter vector and f depends on the entire vector.
    (i.e. not for vectorized different parameters)

  • function returns 1 value for each row. each evaluated at the same parms

  • cov is joint covariance of row outcomes

  • se_vectorized compudes standard error for each row, but does not compute covariance across rows.

wald_tests uses cov
vectorized t_test (implicit in conf_int, summary...) treats each row separately

same pattern as Results t_test and wald_test in models.

@josef-pkt
josef-pkt force-pushed the REF_delta_method_rebased branch from 4241fdf to b9b0501 Compare October 31, 2021 20:17
@josef-pkt

Copy link
Copy Markdown
Member Author

style check doesn't like the lambda in a unit test :(
otherwise green

later: we should also support transformation classes, i.e. deriv included in func instead of separate argument
However, method names might not match e.g. for link function we need the inverse function for prediction

@josef-pkt

Copy link
Copy Markdown
Member Author

name _wald_test_nl is a bit misleading.

It's not an analogue to wald_test method. It returns the NonlinearDeltaCov instance.
e.g. calling summary on it prints the vectorized t_test-like table
We don't get a joint Wald test result directly.

I think we want the instance return, but it should have a method name that indicates that.
get_xxx_nl for some xxx ?

We could wald_test_nl and t_test_nl methods that directly return the hypothesis test results.
and get_prediction_nl which however would be the same or essentially the same as the t_test_nl.

difference to standard get_prediction:
predict and get_prediction are always observation specific, i.e. equivalent to t_test.
NonlinearDeltaCov also include the cov and wald_test for the joint distribution across rows, whether they are constraints or observations for prediction.

Stata has test_nl and predict_nl separately.

possible
use _get_nonlinear as name for the current method that returns the instance.
decide on what to add later.

one internal usecase is to support get_prediction, e.g. Prob(y=0) aggregated over obs
This gives us standard errors and conf_int for zero probability.
Although, in Poisson we can use endpoint transformation for conf_int

def prob0(p):
    mu = res_poi.model.predict(p)
    prob = np.exp(-mu).mean()
    return prob

wt = res_poi._wald_test_nl(prob0)
wt, print(wt.summary())

We might need a safeguard in the function that has both vectorized and joint inference if the number of rows is large.
cov has shape (n_rows, n_rows) which could be huge if n_rows = nobs.

@josef-pkt

Copy link
Copy Markdown
Member Author

another usecase of joint: comparing predicted means and margins

2 rows with predictions (e.g. given by link) at two points of exog x1, x2, with f(x1), f(x2)
Test or estimate linear combination of f(x1), f(s2), e.g. change in predicted means f(x2; p) - f(x1; p)
For delta method, d_f / d_params is the same as in margins.
I need to check the details.

The diff f(x2; p) - f(x1; p) is not monotonic wr.t. x, so we cannot use endpoint transformation in confint.

We should have a separate function of univariate in linpred functions, e.g. to support get_prediction in Poisson, Logit, GLM and other models (e.g. symmetric distributions, ...) where mean is separable from dispersion/variance/extra_params (block-diagonal information matrix)
This would be an extension of GLM get_prediction to arbitrary monotonic functions not just the link, and also be a general case for eform (which already has a generic name in our code).
Our function might already do most of this, at least for given distribution of mu. Need to check.

_get_wald_nonlinear (I like this one for the NonlinearDeltaCov version)
_get_wald_link or _get_wald_monotonic

@josef-pkt

Copy link
Copy Markdown
Member Author

If we do GMM, then statistics like probability/expected counts - freq is just an extra moment condition.
We need the same function and derivatives as in nonlinear prediction.

A hypothesis test would be either an overidentifying test or a test on the diff = observed - expected.
(see my GMM notebook). In the case of overidentifying restriction, the params would change because we also try to match the extra moment condition.
Estimating the diff as second parameter is similar to getting inference for ATE in treatment effects model using GMM.

@josef-pkt

Copy link
Copy Markdown
Member Author

all green, in it goes

@josef-pkt
josef-pkt merged commit 83f5501 into statsmodels:main Nov 2, 2021
@josef-pkt josef-pkt added this to the 0.14 milestone Nov 2, 2021
@josef-pkt josef-pkt mentioned this pull request Nov 2, 2021
3 of 17 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant