Announcement

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

  • Coefplot for marginal effects

    Hi all,

    I'm trying to create a plot, similar to Stata's `coefplot`, but for marginal effects. I have a bunch of categorical variables, which look great on a coefplot, but I can't get the x axis to list marginal effects. In the same way, I can get margins to produce marginal effects, but I can't get marginsplot to produce all of the output. Some sample data is listed below of what I'm trying to do.

    Code:
    sysuse auto, clear
    recode gear_ratio (2/2.50=1) (2.51/3=2) (3/4=3)
    reg price  i.foreign i.gear_ratio
    est store car1
    Here I created a model and stored the estimates.

    Code:
    coefplot(car1)
    coefplot works well, but the axis can't show marginal effects.

    Code:
    margins foreign gear
    marginsplot,recast(scatter)
    margins produces the marginal effects, but can't plot the results. Any way to get the best of both?

    Any help would be appreciated!

  • #2
    Dear Ivan,

    Actually, this is not complicated, as is explained by Ben Jann, the author of coefplot here.
    With your example you need this:
    Code:
    sysuse auto, clear
    recode gear_ratio (2/2.50=1) (2.51/3=2) (3/4=3)
    reg price  i.foreign i.gear_ratio
    margins foreign gear , post
    coefplot, xline(0) xtitle(Average marginal effects)
    and that will produce this visualization:


    Note that the 'trick' is to have margins post its result, as explained by Ben Jann: 'It is essential to specify the post option with margins so that it posts its results in e() from where coefplot collects the results to be displayed. If you do not specify the post option, then margins leaves e() unchanged and coefplot uses the raw coefficients from the ... model that still reside in e()..'.
    Note that coefplot has a lot more to offer.
    Here you can find Ben Jann's 2014 presentation A new command for plotting regression coefficients and other estimates.
    More documentation and examples can be found at the coefplot support page, including his The Stata Journal 2014 paper, which can also be downloaded from here.
    Click image for larger version

Name:	Tutorial_coefplot_marginal_effects.png
Views:	1
Size:	32.8 KB
ID:	1498616

    Happy plotting!
    Last edited by ericmelse; 16 May 2019, 07:18. Reason: Corrected typing error
    http://publicationslist.org/eric.melse

    Comment


    • #3
      Hi Eric, thanks for your help. I stumbled on Jann's 2013 paper, but hadn't seen the `post trick`. This is great, thanks again.
      Last edited by Ivan Privalko; 16 May 2019, 07:24.

      Comment

      Working...
      X