Announcement

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

  • How to plot two curves in the same Figure by Stata?

    Hello everyone,

    I meet with an issue of plotting by Stata. I could you can help me.

    There is a dependent variable A, an independent variable B, and a moderate variable C (C is also a dummy variable). Now, I need to get the scatterplot and its fitted curve of B*C and A, because C is a dummy variable, I want to get two curves in the same figure for C=1 and C=0 respectively.

    How can I make this plot?

    Thank you for answering, I really appreciate it.

  • #2
    Welcome to the Stata Forum / Statalist.

    Please read the FAQ, particularly on how to share data.

    This is the best way to entail a helpful reply.

    That said, hopefully the example below applies to your needs:

    Code:
    sysuse auto
    scatter mpg price, by(foreign)
    Click image for larger version

Name:	Graph_test.png
Views:	1
Size:	27.4 KB
ID:	1408617



    Edited to add the code and comment:

    For interaction terms, you may try something like:

    Code:
    gen repprice = rep78*price
    twoway( scatter mpg repprice if foreign ==0) (scatter mpg repprice if foreign == 1), by(foreign)
    That said, it seems you need interaction terms plotted in the graph. If the model is, say, a regression analysis, you may use - margins - followed by marginspot.
    Last edited by Marcos Almeida; 31 Aug 2017, 07:49.
    Best regards,

    Marcos

    Comment


    • #3
      regplot can do this. There is no interaction term in this example, but there could be.

      Code:
      . sysuse auto, clear
      (1978 Automobile Data)
      
      . regress mpg weight foreign
      
            Source |       SS           df       MS      Number of obs   =        74
      -------------+----------------------------------   F(2, 71)        =     69.75
             Model |   1619.2877         2  809.643849   Prob > F        =    0.0000
          Residual |  824.171761        71   11.608053   R-squared       =    0.6627
      -------------+----------------------------------   Adj R-squared   =    0.6532
             Total |  2443.45946        73  33.4720474   Root MSE        =    3.4071
      
      ------------------------------------------------------------------------------
               mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
      -------------+----------------------------------------------------------------
            weight |  -.0065879   .0006371   -10.34   0.000    -.0078583   -.0053175
           foreign |  -1.650029   1.075994    -1.53   0.130      -3.7955    .4954422
             _cons |    41.6797   2.165547    19.25   0.000     37.36172    45.99768
      ------------------------------------------------------------------------------
      
      . regplot, sep(foreign) mcolor(orange blue) fitopts(lcolor(orange blue))
      Where does regplot come from?

      Code:
      . search regplot, sj
      
      Search of official help files, FAQs, Examples, SJs, and STBs
      
      SJ-10-1 gr0009_1  . . . .  Software update for model diagnostic graph commands
              . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
              (help anovaplot, indexplot, modeldiag, ofrtplot, ovfplot,
              qfrplot, racplot, rdplot, regplot, rhetplot, rvfplot2,
              rvlrplot, rvpplot2 if installed)
              Q1/10   SJ 10(1):164
              provides new command rbinplot for plotting means or medians
              of residuals by bins; provides new options for smoothing
              using restricted cubic splines; updates anova examples
      
      SJ-4-4  gr0009  . . . . . . . . . . Speaking Stata: Graphing model diagnostics
              . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
              (help anovaplot, indexplot, modeldiag, ofrtplot, ovfplot,
              qfrplot, racplot, rdplot, regplot, rhetplot, rvfplot2,
              rvlrplot, rvpplot2 if installed)
              Q4/04   SJ 4(4):449--475
              plotting diagnostic information calculated from residuals
              and fitted values from regression models with continuous
              responses
      Note: there is another rdplot in existence. Mine was first, but at a wild guess the other one is used more, so in due course I will change the name of mine.

      Comment


      • #4
        Following up on the code Marcos Almeida posted in #2, you can use lfit and qfit to add linear and quadratic fit lines/curves. E.g.,

        Code:
        sysuse auto, clear
        * Linear fit
        scatter mpg price, by(foreign) || lfit mpg price, by(foreign)
        * Quadratic fit
        scatter mpg price, by(foreign) || qfit mpg price, by(foreign)
        * Both linear and quadratic fits
        scatter mpg price, by(foreign) || lfit mpg price, by(foreign)  || qfit mpg price, by(foreign)
        HTH.
        --
        Bruce Weaver
        Email: [email protected]
        Version: Stata/MP 18.5 (Windows)

        Comment


        • #5
          Due to the paucity of information as well as lack of a workable data set, we have three members hazarding a guess.

          As a tentative approach, I'm considering the use of - separate - so as to get the plot.

          Also, perhaps something like this:

          Code:
          sysuse auto
          gen highprice = price >= 6200
          twoway (scatter mpg weight if highprice==0, legend(label(1 "Low Price"))) (scatter mpg weight if highprice ==1, legend(label(2 "High Price"))), by(foreign)
          Click image for larger version

Name:	Graph_test2.png
Views:	1
Size:	30.9 KB
ID:	1408689

          Best regards,

          Marcos

          Comment


          • #6
            Originally posted by Marcos Almeida View Post
            Welcome to the Stata Forum / Statalist.

            Please read the FAQ, particularly on how to share data.

            This is the best way to entail a helpful reply.

            That said, hopefully the example below applies to your needs:

            Code:
            sysuse auto
            scatter mpg price, by(foreign)
            [ATTACH=CONFIG]n1408617[/ATTACH]


            Edited to add the code and comment:

            For interaction terms, you may try something like:

            Code:
            gen repprice = rep78*price
            twoway( scatter mpg repprice if foreign ==0) (scatter mpg repprice if foreign == 1), by(foreign)
            That said, it seems you need interaction terms plotted in the graph. If the model is, say, a regression analysis, you may use - margins - followed by marginspot.
            Thank you very much for your reply, your code is very useful to me. Thanks and best regards.

            Comment


            • #7
              Originally posted by Bruce Weaver View Post
              Following up on the code Marcos Almeida posted in #2, you can use lfit and qfit to add linear and quadratic fit lines/curves. E.g.,

              Code:
              sysuse auto, clear
              * Linear fit
              scatter mpg price, by(foreign) || lfit mpg price, by(foreign)
              * Quadratic fit
              scatter mpg price, by(foreign) || qfit mpg price, by(foreign)
              * Both linear and quadratic fits
              scatter mpg price, by(foreign) || lfit mpg price, by(foreign) || qfit mpg price, by(foreign)
              HTH.
              Thank you sir, I know how to accomplish my task now. Thanks

              Comment


              • #8
                I was wondering if it is possible to put different fits on different graphs. I want to display the linear and quadratic fits for the same data, is it possible to do that other than doing separately?

                Comment


                • #9
                  Sure.

                  Code:
                  sysuse auto , clear
                  twoway scatter mpg weight || lfit mpg weight || qfit mpg weight
                  twoway scatter mpg weight || lfit mpg weight || qfit mpg weight , legend(order(2 "linear" 3 "quadratic") pos(1) ring(0) col(1)) ytitle("`: var label mpg'")
                  See also aaplot from SSC.

                  Comment


                  • #10

                    twoway scatter mpg weight || lfit mpg weight || qfit mpg weight This gives linear and quadratic fits on the same graph, I want them on different graphs. I couldn't make the second one work, I get an "invalid varlist" error Thanks for the quick response by the way.

                    Comment


                    • #11
                      Looking at your original question in #8

                      (A) I was wondering if it is possible to put different fits on different graphs.

                      (B) I want to display the linear and quadratic fits for the same data, is it possible to do that other than doing separately?

                      I thought that you really meant (B), how to present graphs not separately, but in one. If you want them on separate graphs, what's the problem?

                      Code:
                      twoway scatter mpg weight || lfit mpg weight
                      twoway scatter mpg weight || qfit mpg weight
                      Are you asking about graph combine?

                      Code:
                      help graph combine

                      Comment


                      • #12
                        Yes! Thank you so much.

                        Comment

                        Working...
                        X