Announcement

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

  • overlay rather than combine graphs!

    Dear All, Suppose that I run two regressions as follows: (But note that the -mcp- command is from http://www.stata-journal.com/article...article=gr0056)
    Code:
    webuse grunfeld, clear
    
    xtset company year
    
    // 1
    xtreg invest mvalue c.mvalue#c.mvalue if year < 1945, fe
    mcp mvalue, 
    graph save Graph "1.gph", replace
    // 2
    xtreg invest mvalue c.mvalue#c.mvalue if year >= 1945, fe
    mcp mvalue, 
    graph save Graph "2.gph", replace
    // 1 & 2
    graph combine 1.gph 2.gph, ycom
    The two graphs are combined as
    Click image for larger version

Name:	graph-overlay.png
Views:	1
Size:	48.9 KB
ID:	1407365


    How can I overlay these two graphs into a single graph?
    Ho-Chuan (River) Huang
    Stata 17.0, MP(4)

  • #2
    here is one way:
    Code:
    . webuse grunfeld
    
    . xtset company year
    
    . qui xtreg invest mvalue c.mvalue#c.mvalue if year < 1945, fe
    
    . mcp mvalue, nograph sav(f1)
    
    . qui xtreg invest mvalue c.mvalue#c.mvalue if year >= 1945, fe
    
    . mcp mvalue, nograph sav(f2)
    
    . us f1
    
    . gen byte year=0
    
    . append using f2
    
    . replace year=1 if year==.
    
    . twoway (line _margin mvalue if year==0) (line _margin mvalue if year==1)

    Comment


    • #3
      Check out -addplot- from SSC

      Comment


      • #4
        Dear Rich, Thank you so much. It works just fine.

        Ho-Chuan (River) Huang
        Stata 17.0, MP(4)

        Comment


        • #5
          Dear Andrea, thanks for your suggestion. I will check it out.

          Ho-Chuan (River) Huang
          Stata 17.0, MP(4)

          Comment


          • #6
            Hello Stata users/experts
            I have a similar question, but it is about Multivariable Fractional Polynomial (MFP) regression. I was wondering if you could inform me whether it is possible to overlay (and not combine) two multivariable fractional polynomial plots (2 plots for 2 different groups) into a single graph?
            I used the 'twoway' command, however, I got an error indicating that 'mfp is not a twoway plot type'. Please let me know what you think about this issue or if you are aware of any alternatives.
            Thanks

            Comment


            • #7
              Maybe you can also try the command "bytwoway".
              Code:
              webuse grunfeld,clear
               xtset company year
               qui xtreg invest mvalue c.mvalue#c.mvalue if year < 1945, fe
               mcp mvalue, nograph sav(f3)
               qui xtreg invest mvalue c.mvalue#c.mvalue if year >= 1945, fe
              mcp mvalue, nograph sav(f4)
               us f1
              gen byte year=0
               append using f2
               replace year=1 if year==.
              
              bytwoway line _margin mvalue, by(year) aes(color lpattern)
              Best regards.

              Raymond Zhang
              Stata 17.0,MP

              Comment


              • #8
                Raymond Zhang Thank you, Raymond, for the suggestion. The 'bytwoway' command did not work either. I got the same error saying that 'mfp is not a twoway plot type'.

                Comment


                • #9
                  Originally posted by Arash Sereshki View Post
                  Hello Stata users/experts
                  I have a similar question, but it is about Multivariable Fractional Polynomial (MFP) regression. I was wondering if you could inform me whether it is possible to overlay (and not combine) two multivariable fractional polynomial plots (2 plots for 2 different groups) into a single graph?
                  I used the 'twoway' command, however, I got an error indicating that 'mfp is not a twoway plot type'. Please let me know what you think about this issue or if you are aware of any alternatives.
                  Thanks
                  Maybe you should show your example data and code.
                  Best regards.

                  Raymond Zhang
                  Stata 17.0,MP

                  Comment


                  • #10
                    Raymond Zhang Thank you for following-up.

                    The code for the 'Multivariable Fractional Polynomial models' is : "mfp : regress DV IV1 IV2 IV3 IV4 IV5 if Group==1". DV and IVs stand for the dependent variable and independent variables respectively. The GUI of Stata does not let me run the MFP separately for 2 groups at the same time (e.g., Group==0 and Group==1). It does not have 'by' command as it has for multivariable linear regression.

                    The GUI of Stata lets me get the scatter plot for the significant associations between DV and IVs. For example, if IV2 is significant in the model, the code for the scatter plot of the fractional polynomial fit is: "fracplot IV2 if Group==1" But again, the GUI does not have 'by' command to plot regression lines separately for each group on the same plot (showing 2 regression lines for 2 groups).

                    I can use the same commands for the other group (e.g., Group==0) separately to find out the associations between DV and IVs. But I can not plot the associations between DV and IV2 for both groups (Group==0 and Group==1) on the same scatter plot (e.g., showing two regression lines).

                    Thanks again for spending your time on this issue.

                    Comment


                    • #11
                      Is this what you want?
                      [code]

                      net install bytwoway, from("https://raw.githubusercontent.com/matthieugomez/bytwoway.ado/master/")


                      sysuse auto,clear
                      keep if foreign==0
                      mfp: regress mpg weight displacement
                      fracpred dfit, for(displacement)
                      save 1,replace

                      sysuse auto,clear
                      keep if foreign==1
                      mfp: regress mpg weight displacement
                      fracpred dfit, for(displacement)
                      save 2,replace
                      append using 1

                      bytwoway line dfit displacement ,by(foreign) aes(color lpattern)
                      [code]
                      Attached Files
                      Last edited by Raymond Zhang; 22 Jan 2021, 21:45.
                      Best regards.

                      Raymond Zhang
                      Stata 17.0,MP

                      Comment


                      • #12
                        Maybe you like this graph with CI and scatter.
                        Code:
                        sysuse auto,clear
                         keep if foreign==0
                         mfp: regress mpg weight displacement 
                         fracpred dfit, for(displacement) 
                         fracpred ds, for(displacement) s
                         save 1,replace
                         
                         sysuse auto,clear
                         keep if foreign==1
                         mfp: regress mpg weight displacement 
                         fracpred dfit, for(displacement) 
                         fracpred ds, for(displacement) s
                         save 2,replace
                         append using 1
                         
                         gen low=dfit-1.96*ds
                         gen high=dfit+1.96*ds
                         
                         bytwoway line dfit displacement, by(foreign) aes(color lpattern) scheme(s1mono) legend(off)
                         addplot:rarea low hi displacement if foreign==0 ,color(gs14%60) sort || ///
                                 rarea low hi displacement if foreign==1,color(gs12%60) sort   ||  ///
                                 scatter  mpg displacement ,sort
                        Attached Files
                        Best regards.

                        Raymond Zhang
                        Stata 17.0,MP

                        Comment


                        • #13
                          Another simplier method:
                          Code:
                           sysuse auto,clear
                           mfp: regress mpg weight displacement if foreign==0
                           fracplot displacement 
                           
                           mfp: regress mpg weight displacement if foreign==1
                           fracpred dfit, for(displacement) 
                           fracpred ds, for(displacement) s
                          
                           gen low=dfit-1.96*ds
                           gen high=dfit+1.96*ds
                           
                           addplot:  rarea low hi displacement ,color(gs14%80) sort || ///
                                     line dfit displacement ,color(red)||  ///
                                     scatter  mpg displacement if foreign==1,sort
                          Attached Files
                          Last edited by Raymond Zhang; 23 Jan 2021, 01:27.
                          Best regards.

                          Raymond Zhang
                          Stata 17.0,MP

                          Comment


                          • #14
                            @Rich Goldstein Also,Rich's codes can be reduced to:
                            Code:
                            . webuse grunfeld,clear
                            . xtset company year
                            . qui xtreg invest mvalue c.mvalue#c.mvalue if year < 1945, fe
                            . mcp mvalue
                            . qui xtreg invest mvalue c.mvalue#c.mvalue if year >= 1945, fe
                            . mcp mvalue, nograph sav(f2)
                            . use f2,clear
                            . addplot: line _margin mvalue
                            Last edited by Raymond Zhang; 23 Jan 2021, 01:45.
                            Best regards.

                            Raymond Zhang
                            Stata 17.0,MP

                            Comment


                            • #15
                              Originally posted by Raymond Zhang View Post
                              Another simplier method:
                              Code:
                              sysuse auto,clear
                              mfp: regress mpg weight displacement if foreign==0
                              fracplot displacement
                              
                              mfp: regress mpg weight displacement if foreign==1
                              fracpred dfit, for(displacement)
                              fracpred ds, for(displacement) s
                              
                              gen low=dfit-1.96*ds
                              gen high=dfit+1.96*ds
                              
                              addplot: rarea low hi displacement ,color(gs14%80) sort || ///
                              line dfit displacement ,color(red)|| ///
                              scatter mpg displacement if foreign==1,sort
                              Thank you so much, Raymond Zhang.
                              I could get a plot as you attached. However, there is a small problem regarding the second group (foreign==1). So, for the first group (foreign==0), the plot exactly represents the fracplot for the Dependent variable (e.g., mpg) and the significant independent variable (e.g., displacement). However, for the second group (foregin==1), what you plotted is not a fracplot, it is scatter plot representing a linear association between a dependent variable (e.g., mpg) and an independent variable (e.g., displacement).
                              From the statistical point of view, these two groups have not been plotted using the same method.
                              Again thank you so much for helping me.

                              Comment

                              Working...
                              X