Announcement

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

  • graph: sepscatter overlay with marginsplot (including fit lines)

    Hi, I've been reading through the forums and help files but I'm stuck. I want to overlay sepscatter and marginsplot plots on the same plot. What's the best way to overlay two plots one of which is sepscatter ? I tried marginsplot(...), addplot(sepscatter...) but sepscatter was not recognized as a valid twoway graph. I tried graph combine after creating them separately, but got separate plots.

    I could probably get the fit lines using sepscatter , but then the fit lines wouldn't account for the covariates specified in the regression which preceded marginsplot .

    Thanks for any advice.
    Last edited by Cameron Brick; 07 May 2015, 18:47.

  • #2
    If it helps to clarify, this is the basic kind of plot I'm trying to generate, but the addplot(scatter(...)) doesn't change the marker styles by condition, and I can't figure out how to do that without sepscatter . Yes, I'll also use jitter: just learned about that.
    Last edited by Cameron Brick; 07 May 2015, 18:52.

    Comment


    • #3
      That's correct. sepscatter (SSC) is not a twoway type so addplot() won't do what you want in that way. But you should be able to closer just by calling up scatter directly, possibly after separate.

      Comment


      • #4
        Hi Nick, I'm not following. How would scatter help me to create fit lines adjusted for covariates like marginsplot, and even if it did where would I add it to the following command? sepscatter v1 v2, separate(cond)

        Alternately, I just need a way to distinguish between markers by condition in scatter, if that's possible.
        Last edited by Cameron Brick; 07 May 2015, 18:55.

        Comment


        • #5
          I think Nick has something like this in mind:

          Code:
          sysuse auto, clear
          reg price c.mpg##i.foreign
          separate price, by(foreign) gen(DD)
          margins foreign, at(mpg = (10(5)40))
          marginsplot, addplot(scatter DD* mpg)

          Comment


          • #6
            Oh wow, that works great. Thanks Nick and Dimitriy. The legend doesn't display both symbol types and labels, but I may be able to add that manually.

            Comment


            • #7
              First off, with small qualifications the role of addplot() is basically to add something extra; it can do little to change what else is being graphed. The adjustment stuff is entirely distinct and must be done with the margins machinery.

              Second, if you look carefully at the code for sepscatter you will see that its basic trick is to apply separate and then twoway. You can do that for yourself, but you need to create the separate variables with separate before you do any of these graphics and then plot them with addplot() afterwards. See e.g. http://www.stata-journal.com/sjpdf.html?articlenum=gr0023 for the main idea done by hand.

              Comment


              • #8
                Thanks Nick. I succeeded with your directions.

                However, the legend doesn't create a key for the second condition. Is it possible to add a legend key? I managed to find a manual graph editing work-around, if not.

                Comment


                • #9
                  There's something tricky with the legend that I can't seem to figure out.

                  This hacks it together by labeling the separated price variables with the car origin.

                  Code:
                  set more off    
                  sysuse auto, clear
                  
                  replace price = price + 10000 if foreign == 0
                  separate price, by(foreign) gen(DD)
                  labvarch DD*, after( == )
                  
                  reg price c.mpg##i.foreign
                  margins foreign, at(mpg = (10(5)40)) 
                  marginsplot, addplot(scatter DD1 mpg, ms(oh) mc(maroon) || scatter DD0 mpg, ms(dh) mc(navy)) recast(line) recastci(rline)

                  Comment


                  • #10
                    Code:
                      
                    separate price, by(foreign) gen(DD)
                    labvarch DD*, after( == )
                    makes use of labvarch (SSC), but you can get there directly with

                    Code:
                    separate price, by(foreign) gen(DD) veryshortlabel

                    Comment


                    • #11
                      Code:
                      separate price, by(foreign) gen(DD) veryshortlabel
                      does not show a label for the second scatter marker (+). Apologies if I'm doing this wrong.

                      Comment


                      • #12
                        Separate just creates creates the dummy variables and labels them. It will not fix your legend. You need to show us the marginsplot syntax to figure out what is wrong.

                        Combining Nick's suggestion with my code:

                        Code:
                        set more off    
                        sysuse auto, clear
                        
                        replace price = price + 10000 if foreign == 0
                        separate price, by(foreign) gen(DD) veryshortlabel
                        
                        reg price c.mpg##i.foreign
                        margins foreign, at(mpg = (10(5)40)) 
                        marginsplot, addplot(scatter DD0 mpg, ms(dh) mc(navy) || scatter DD1 mpg, ms(oh) mc(maroon)) recast(line) recastci(rline)
                        Last edited by Dimitriy V. Masterov; 08 May 2015, 12:51.

                        Comment


                        • #13
                          That code works perfectly. Thank you for writing it out. I learned several things from picking apart the syntax.

                          Comment


                          • #14
                            I have a similar request. I currently have a marginsplot following nbreg and margins;
                            Code:
                            quietly nbreg totabund meandepth i.period, dispersion(mean) exposure(sweptarea) noomitted vsquish noemptycells nolog
                            margins period, at(meandepth=(.8(.5)5) )
                            marginsplot, recast(line) recastci(rarea)  ///
                                            addplot(scatter totabund meandepth)    ///
                                            legend(pos(2) ring(0) col(1))   ///
                                            name(mplot02, replace)
                            I want to have the added scatter separated by period. I tried modifying the above code. But I was unable to make it work.

                            Comment


                            • #15
                              I apologize. I thought I had read the documentation ... but I not carefully or thoroughly enough. The following accomplishes exactly what I want and is essentially what is posted above.

                              Code:
                              separate totabund, by(period)
                              quietly nbreg totabund meandepth i.period, dispersion(mean) exposure(sweptarea) noomitted vsquish noemptycells nolog
                              margins, at(meandepth=(.8(.5)5) ) over(period)
                              marginsplot, addplot(scatter totabund1 meandepth || scatter totabund2 meandepth) ///
                                          recast(line) recastci(rarea) legend(pos(2) ring(0) col(1))

                              Comment

                              Working...
                              X