Announcement

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

  • Creating a margins plot with confidence intervals

    Using the marginsplot command, I'm able to create a graph that looks like this:

    Click image for larger version

Name:	Screen Shot 2020-02-21 at 9.52.36 AM.png
Views:	1
Size:	172.0 KB
ID:	1537738

    However, is there a way in Stata to create a graph that looks like this?


    Click image for larger version

Name:	Screen Shot 2020-02-21 at 9.52.55 AM.png
Views:	1
Size:	280.3 KB
ID:	1537739

  • #2
    -combomarginsplot- a user-written command by Nicholas Winter makes this straightforward.

    Code:
    ssc inst combomarginsplot
    Code:
    sysuse auto,clear
    reg price c.mpg
    margins, at(mpg = (10(5)50)) saving(linear, replace)
    reg price c.mpg##c.mpg
    margins, at(mpg = (10(5)50)) saving(quad, replace)
    combomarginsplot linear quad, /// 
    fileci1opts(recast(rarea) fcolor(538b%50))  fileci2opts(recast(rarea) fcolor(538r%50))
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	109.0 KB
ID:	1537759

    Comment


    • #3
      Thank you so much Scott, this definitely got me closer!

      Attached is what I was able to produce. The only thing I can't figure out now is why I have a solid and dotted line for each? Rather than just the dotted line. Also, how would I go about renaming file 1 and file 2?

      This is the code I'm using:

      xtreg lnTotalNRRR lnOVERHEAD lnTOTAL_PROGRAM_SERVICES lnASSETS_TOTAL_EOY AGE, fe
      margins, at(lnOVERHEAD=(5(1)16)) saving(linear, replace)

      xtreg lnTotalNRRR c.lnOVERHEAD##c.lnOVERHEAD lnTOTAL_PROGRAM_SERVICES lnASSETS_TOTAL_EOY AGE, fe
      margins, at(lnOVERHEAD=(5(1)16)) saving(quad, replace)

      combomarginsplot linear quad, fileci1opts(recast(rarea) fcolor(538b%50)) fileci2opts(recast(rarea) fcolor(538r%50))

      Click image for larger version

Name:	Screen Shot 2020-02-24 at 12.14.12 PM.png
Views:	1
Size:	593.3 KB
ID:	1538198

      Comment


      • #4
        You need to use the -file#opt()- with the lpattern() option. You can rename the legend keys using the -order()- option within -legend()-:

        Code:
        combomarginsplot linear quad, /// 
         fileci1opts(recast(rarea) lp(dash) lc(538b) fcolor(538b%50)) ///
         file1opt(lpattern(dash)) ///
         fileci2opts(recast(rarea) fcolor(538r%50)) /// 
         legend(order(1 "Linear CI" 3 "Linear Fit" 2 "Quadratic CI"  4 "Quadratic Fit") ///
          pos(6) row(2))
        Note, I have my default scheme set as 538w (using Daniel Bischolf's g538schemes package), hence colors as 538r (a shade of red) and 538b (a shade of blue). If you do not have -538schemes- installed then those color options will not work.

        To describe and install:
        Code:
        ssc desc g538schemes
        ssc install g538schemes

        Comment


        • #5
          Excellent, many thanks again Scott!

          Comment

          Working...
          X