Announcement

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

  • Using margins plot to create bar graphs

    I'm having trouble getting marginsplot to generate a particular bar graph of predicted values that seems like it shouldn't be that hard. Here's a stylized example:

    sysuse auto
    reg price c.mpg##foreign
    margins ,at(mpg=(20 30 40) foreign=(0 1)) vsquish

    I can get two bar graphs for my three values of mpg (one for foreign=0 and one for foreign=1) to print side-by-side with this:

    marginsplot ,x(mpg) recast(bar) by(foreign)

    But what I really want is two bars for each value of mpg where the left is for foreign=0 and the right is foreign=1. I thought this would work, but it definitely does not:

    marginsplot ,x(mpg) recast(bar) plotopts(over(foreign))

    option over() not allowed

    Any ideas would be much appreciated.

    Thanks,

    Doug
    ---------------
    Douglas McKee
    Associate Chair and Senior Lecturer, Economics
    Lecturer, School of Medicine
    Yale University
    [email protected]
    http://dougmckee.net
    http://teachbetter.co
    http://highvariance.net
    310-266-2438

  • #2
    Underneath the code you are firing up twoway bar with this command. But over() is not an option of twoway bar; it's an option of graph bar.

    I don't know a quick way to do what you want.

    Comment


    • #3
      This might get you a little closer:
      Code:
      reg price c.mpg##foreign
      margins, at(mpg=(20 30 40) foreign=(0 1)) vsquish
      marginsplot,  xdim(mpg foreign) recast(bar) plotopts(barw(.8))
      Alternatively, you can use the -post- option and recover your marginal estimates and graph them natively and not use -marginsplot-.

      Best,
      Alan

      Comment


      • #4
        As Nick says it's not quick.

        Code:
        sysuse auto, clear
        reg price c.mpg##foreign
        margins ,at(mpg=(20 30 40) foreign=(0 1)) vsquish post
        mat margins=e(at),e(b)'
        svmat margins
        rename (margins1-margins4)(tw_mpg tw_foreign0 tw_foreign1 tw_price)
        g xaxis=(sum(tw_mpg[_n-1]!=tw_mpg[_n]))+cond(tw_foreign1==1,0.2,-0.2)
        twoway bar tw_price xaxis if tw_foreign1==0, barw(0.3) || ///
        bar tw_price xaxis if tw_foreign1==1, barw(0.3) ///
        ylab(-1000(1000)8000, notick ang(h)) ///
        xlab(1 "20" 2 "30" 3 "40", notick) ///
        ytitle("Price") xtitle("Mpg") ///
        legend(lab (1 "foreign==0") lab (2 "foreign==1"))​

        Comment

        Working...
        X