Announcement

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

  • How to print multiple linear regression on the same scatterplot

    Good morning everyone, for a research that I'm performing I'm trying to Plot multiple regression line on the same scatterplot for a Panel dataset, like the one here attached.
    Click image for larger version

Name:	scatterplot with multiple linear regression on it.png
Views:	1
Size:	59.1 KB
ID:	1641097

    Do someone knows how to create these useful representations?
    Thanks in advance for the advices.

  • #2
    something like this work?
    Code:
    twoway scatter tcost output if airline==1 || lfit tcost output if airline==1 || scatter tcost output if airline==2 || lfit tcost output if airline==2
    you'll have to gussy it up with colors (mcolor, lcolor, etc..)

    Comment


    • #3
      Just to flag that the title is misleading here. A multiple regression is one with many predictors. Your problem appears to be showing several regressions with one predictor on the same plot.

      Comment


      • #4
        Extending#2, you are after something like this (fictitious data):

        Code:
        //Fictitious data example:
        
         li in 1/6, noobs clean
        
                   al         tc         ind  
            Airline 1   14.52905    .1517767  
            Airline 1   14.67648   -.0266089  
            Airline 1   14.38981    .4835709  
            Airline 1   15.14451    .3083085  
            Airline 1   14.12012    .2327023  
            Airline 1   14.28547    .5754296
        
        
        //Graph:
        
        #delimit ;
        twoway (scatter tc ind if al==1, msym(o) mcol(maroon)) ||
               (lfit tc ind if al ==1, lcol(maroon) lpatt(solid) lw(medthick)) ||
               (scatter tc ind if al==2, msym(oh) mcol(green)) ||
               (lfit tc ind if al ==2, lcol(green) lpatt(solid) lw(medthick)) ||
               (scatter tc ind if al==3, msym(t) mcol(yellow)) ||
               (lfit tc ind if al ==3, lcol(yellow) lpatt(solid) lw(medthick)) ||
               (scatter tc ind if al==4, msym(t) mcol(blue)) ||
               (lfit tc ind if al==4, lcol(blue) lpatt(solid) lw(medthick)) ||
               (lfit tc ind, lcol(black) lw(thick)),
               xscale(range(-2.5 1))
              yti("Total Cost", col(black) size(medium))
              legend(off)
               text(10 -.5 "Regression Line", col(black) si(small))
               text(14 0.5 "Airline 1", col(black) si(small))
               text(14 -0.5 "Airline 2", col(black) si(small))
               text(12 -1 "Airline 3", col(black) si(small))
               text(6 -1.5 "Airline 4", col(black) si(small))
               note("{it:Source: Fictitous data}")
        ;
        #delimit cr
        Click image for larger version

Name:	image_25131.png
Views:	1
Size:	317.3 KB
ID:	1641231
        Last edited by Roman Mostazir; 16 Dec 2021, 09:37. Reason: Added data example.
        Roman

        Comment


        • #5
          Originally posted by Nick Cox View Post
          Just to flag that the title is misleading here. A multiple regression is one with many predictors. Your problem appears to be showing several regressions with one predictor on the same plot.
          Sorry Nick Cox, you are right I should write it better. I was meaning to printi different linear regressions on the same graph. I will change it.

          Comment


          • #6
            Originally posted by George Ford View Post
            something like this work?
            Code:
            twoway scatter tcost output if airline==1 || lfit tcost output if airline==1 || scatter tcost output if airline==2 || lfit tcost output if airline==2
            you'll have to gussy it up with colors (mcolor, lcolor, etc..)
            George Ford, thank you for your reply. I will try to set it up. I'm a novice in stata so I'm still experimenting!

            Comment


            • #7
              Originally posted by Roman Mostazir View Post
              Extending#2, you are after something like this (fictitious data):

              Code:
              //Fictitious data example:
              
              li in 1/6, noobs clean
              
              al tc ind
              Airline 1 14.52905 .1517767
              Airline 1 14.67648 -.0266089
              Airline 1 14.38981 .4835709
              Airline 1 15.14451 .3083085
              Airline 1 14.12012 .2327023
              Airline 1 14.28547 .5754296
              
              
              //Graph:
              
              #delimit ;
              twoway (scatter tc ind if al==1, msym(o) mcol(maroon)) ||
              (lfit tc ind if al ==1, lcol(maroon) lpatt(solid) lw(medthick)) ||
              (scatter tc ind if al==2, msym(oh) mcol(green)) ||
              (lfit tc ind if al ==2, lcol(green) lpatt(solid) lw(medthick)) ||
              (scatter tc ind if al==3, msym(t) mcol(yellow)) ||
              (lfit tc ind if al ==3, lcol(yellow) lpatt(solid) lw(medthick)) ||
              (scatter tc ind if al==4, msym(t) mcol(blue)) ||
              (lfit tc ind if al==4, lcol(blue) lpatt(solid) lw(medthick)) ||
              (lfit tc ind, lcol(black) lw(thick)),
              xscale(range(-2.5 1))
              yti("Total Cost", col(black) size(medium))
              legend(off)
              text(10 -.5 "Regression Line", col(black) si(small))
              text(14 0.5 "Airline 1", col(black) si(small))
              text(14 -0.5 "Airline 2", col(black) si(small))
              text(12 -1 "Airline 3", col(black) si(small))
              text(6 -1.5 "Airline 4", col(black) si(small))
              note("{it:Source: Fictitous data}")
              ;
              #delimit cr
              [ATTACH=CONFIG]n1641231[/ATTACH]
              Thank you Roman Mostazir, I will try to implement the solutions proposed in this thred.

              Comment

              Working...
              X