Skip to content

SUMM/Review Margins #2154

Open
Open

Description

@josef-pkt

(DiscreteMargins is one of the main parts of traditional models regression/discrete/genmod/robust that I didn't go through completely in details.)

I was trying to figure out how some basic things are calculated in Discretemargins, but it's rather confusing. It needs an outline of the main elements. My plan was to see how it can be extended and how much overlap there is with extended prediction.

Note: one main difference of margins to predict is in the dimension. A full problem is has 2d effects/prediction (nobs, k_params/k_effects) which would imply a 3d summary table. Margins with at='all' would be this case, but is not fully implemented.
To get to 2d tables: Margins reduced over nobs, prediction looks only at univariate effect/predicted value.

for future

  • can we extend method? Stata also handles predict inside Margins
    different dy, ey, dx, ex are just rescalings, predict requires different effect function
  • where do we need to add other mean functions? extension to GLM with link function, in discrete models link or mean function is hard coded
  • what's the relationship to average treatment effect? Wooldridge, Stata has separate manual for treatment effect that I barely looked at.
  • code sharing with other parts (predict_at ?), general pattern, refactoring (DiscreteMargins is it's own results class and uses _reset cache).
  • robustness (to misspecification): is using sandwich robust cov_params enough?
  • extension to terms, i.e. multi-column exog
  • Stata has hypothesis tests that compares margins, I don't know much about this yet.

Outline

  • (1) data/exog handling
  • (2) calculating effects and distribution (covariance) of effects, defining effects and derivatives for delta method
  • (3) results and user access to them

to (2)

  • _effects_at calculates average effect for overall, why does it select first row for at other than overall and all? Isn't it an error if it has more than one row?

to be continued, ...

Issues

(needs more cross-links to other issues)
#930 Margins for GLM, GEE has margins but maybe untested
#745 Margins for multi-column exog
#2150 related SUMMary issue for prediction


issues while reading code

  • unit test coverage, AFAICS only Logit is tested, I'm using Poisson as an example to play with
  • open TODOs in comments
  • transform in atexog as in predict? in _get_margeff_exog if atexog is not None
  • const_idx is now available model.data
  • from the results I don't see how to get the information about which columns were treated as dummy (I haven't tried count exog).
  • for cross-checking or reuse it would be useful to have access to the exog that were used in calculating the results
  • naming: in margeff_cov_params: derivative argument is misleading since it uses the effect function and not the derivative of the effects function, but it takes jacobian if array.
  • There is currently no analytical derivative of effects function possible/implemented
  • DRYing model._derivative_exog is model specific, but all have the same boiler plate of ey ex transformation/scaling

reusable code

  • exog at or atexog handling: _get_margeff_exog should be reusable by a predict_at (Kerby plot function), we can also add transform/patsy handling from predict (which I will already need in different places for predict) to margins atexog handling.
  • detection of dummy and count exog: eventually we need a pattern/convention for this, nonparametric product kernel an MICE use also the exog type
  • delta method doesn't work for 'all', needs vectorized version. see ENH: delta method #2155

Activity

  1. josef-pkt commented on Dec 24, 2014

    @josef-pkt
    MemberAuthor

    aside: Stata has the params table for exp(params) separately from the margins. (an experimental extra in #2151)
    It cannot be calculated using Margins, not even for Poisson or models with log link, AFAICS

    more general: do we get results (value, se, confint) for other nonlinear functions like odds-ratio?

  2. josef-pkt commented on Dec 13, 2016

    @josef-pkt
    MemberAuthor

    reference to R, and plots

    related to binary or categorical explanatory variables, predictive margin
    http://stackoverflow.com/questions/41123980/plot-contrast-of-a-linear-model-in-python
    https://cran.r-project.org/web/packages/effects/index.html
    and several articles by John Fox and co-authors
    Fox, John. 1987. “Effect Displays for Generalized Linear Models.” Sociological Methodology 17: 347–61. doi:10.2307/271037.
    ———. 2003. “Effect Displays in R for Generalised Linear Models.” Journal of Statistical Software 8 (15). doi:10.18637/jss.v008.i15.
    Fox, John, and Jangman Hong. 2009. “Effect Displays in R for Multinomial and Proportional-Odds Logit Models: Extensions to the Effects Package.” Journal of Statistical Software 32 (1). doi:10.18637/jss.v032.i01.

    I briefly read through the 1987 article:
    The emphasis is on interaction terms and displaying 2 or three effects at the same time. However, the focus in on the linear prediction, where other explanatory variables are fixed at the mean or another relevant point, i.e. effect at the average, not average effect over the distribution of other exog. "gender = 0.5)
    Also, Fox looks at the highest terms based on principle of marginality, i.e. interaction effect + main effect + constant as the prediction function for a multi-column variable, and similar for polynomials.

  3. josef-pkt commented on Dec 14, 2016

    @josef-pkt
    MemberAuthor

    I think the answer to the stackoverflow question is #2023 (not merged, plot was just linear points, IIRC)
    The predict method #2172 was merged.

    I don't remember what the status of these predict/margin functionality is. We don't have anything that handles categorical variables with more than 2 levels and interaction terms, as in John Fox and R effects library, AFAIR.

  4. josef-pkt commented on Apr 21, 2018

    @josef-pkt
    MemberAuthor

    Margin class is not used. DiscreteMargin doesn't subclass Margin.

  5. josef-pkt commented on Dec 4, 2018

    @josef-pkt
    MemberAuthor

    (I didn't see a better issue to add this link)

    course in categorical data analysis with large section on margins
    https://www3.nd.edu/~rwilliam/xsoc73994/index.html

    main reference that I used for categorical endog data is Agresti's textbook.
    but AFAICS from a quick look, Williams has more on categorical exog.

  6. josef-pkt commented on Apr 10, 2020

    @josef-pkt
    MemberAuthor
  7. josef-pkt commented on Sep 14, 2020

    @josef-pkt
    MemberAuthor

    #6939 fixed problem with extra params in negbin, #6763

    still needs unit tests and more review of margins for models with extra params, negbin, NBP, GBP
    (IIRC, margins are not available for zero inflated count models)

  8. added
    topic-post_estim
    on Jun 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    comp-basecomp-discretedesigntopic-post_estim

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions