Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Different at() variables and combomarginsplot

    Hi all,
    I received the following query via email; I'm posting it and my reply in case they are helpful to someone else some day.

    I'm currently working with logistic regresion models. I'd like to use combomarginsplot to graph both the unadjusted and covariate-adjusted average marginal effects of a variable in an interaction term.

    Here is the syntax I used to compute the unadjusted average effects for a given racial/ethnic group:

    Code:
    logit spending_police_decrease RACE##c.log_viocrime_zipZ##c.bsZ  if party3==1   [pweight=weight_post], cluster(inputstate_post)
    margins , dydx(bsZ) at(RACE=(1) log_viocrime_zipZ =(-3(1)3)) saving(whdem_defund_bsZxzip)
    And here is the syntax used to compute the covariate-adjusted average effects:

    Code:
    logit spending_police_decrease RACE##c.log_viocrime_zipZ##c.bsZ i.ideo7 i.party7 age_post i.educ5 i.faminc_new i.region9_post i.urbancity i.marital numchild18 ownhome i.immstat  if party3==1   [pweight=weight_post], cluster(inputstate_post)
    margins , dydx(bsZ) at(RACE=(1) log_viocrime_zipZ=(-3(1)3) ideo7=(2) party7=(1) age_post=(52) faminc_new=(7) educ5=(3) region9_post=(5) urbancity=(2)  marital=(1) numchild18=(0) ownhome=(1) immstat=(5)) saving(whdem_defund_bsZxzip_kitchsink)
    I then tried...

    Code:
    combomarginsplot whdem_defund_bsZxzip whdem_defund_bsZxzip_kitchsink
    ...which returned a 'file 2 _u_at_vars don't match file 1' error.

    What am I doing wrong? Does combomarginsplot lack the ability to plot baseline and covariate-fixed estimates of average effects? (should I send you sample data?)

  • #2

    The basic issue is that the file that -margins- saves contains a lot of metadata about the variables included in the margins, including the list of variables in the at() option. -combomarginsplot- does a lot of work to synchronize those things, and it is not smart enough to deal with cases where there are different or additional at() variables across the various margins files.

    One option would be to leave all those other variables out of the second call to margins (but leave them in the model). This gives you the average marginal effect across the observed values (distinct from the marginal effect at the average, given the nonlinear model.) Arguably, that’s the most comparable to the first call to margins, as implicitly you are doing the same thing in the first margins call. That is, as it stands, your comparison involves two differences in the graphs: they are based on different models, and they are based on different values of the covariates (which are implicitly at their means in the first, and are set to specific values in the second). Whether that matters to you is for you to decide.

    A second option is a work-around, which is to (temporarily) set those other covariates literally to the values you want, and then run margins. In the toy example below that generates the same margins and standard errors (as it should), though you might want to confirm. See the example below:

    Code:
    clear *
    sysuse auto
     
    * create a dichotomous variable
    gen hirep = rep78>=4
     
    * simple model
    logit foreign hirep##c.mpg
    margins, dydx(mpg) at(hirep=1 mpg=(15(5)45)) saving(m_basic, replace)
     
    * more complex model
    logit foreign hirep##c.mpg weight length gear
     
    * this is option 1
    margins, dydx(mpg) at(hirep=1 mpg=(15(5)45)) saving(m_sink_asobserved, replace)
     
    * this is what you want
    margins, dydx(mpg) at(hirep=1 mpg=(15(5)45) weight=3500 length=150 gear=3.25) saving(m_sink, replace)
     
    ** this is the workaround – be sure to preserve!!!
    preserve
    replace weight=3500 if !missing(weight)
    replace length=150 if !missing(length)
    replace gear=3.25 if !missing(gear)
    * note actual margins are the same as above
    margins, dydx(mpg) at(hirep=1 mpg=(15(5)45)) saving(m_sink_asobserved2, replace)
    restore
     
    * This is option 1
    combomarginsplot m_basic m_sink_asobserved, name(option1, replace)
     
    * This is what you want
    combomarginsplot m_basic m_sink_asobserved2, name(option2, replace)
    I believe I can update -combomarginsplot- to deal with at least some cases like this, but it will be a while before I can get to that.

    Best,
    Nick


    Comment

    Working...
    X