Announcement

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

  • How to add lines to scatter graph

    Hello,

    I would like to make twoway graph showing fited value and scatter plots with additional upper and lower lines for fitline that shows proportion of plot points located near to the fit line. What should I add to the following command so that there will be upper and lower lines as shown in the example graph? I do not want to have CIs to the fit line. I just need the two line below and above the CIs

    graph twoway (lfit y x) (scatter y x)






  • #2
    If I got you correct, you want to fit the upper/lower line to a certain proportion of your data. Say the limit for upper line is 75th percentile and lower limit is 25th percentile of your data, your outcome variable is y and x variable in x-axis:

    Code:
    qui su y, det
    loc p25 = r(p25)
    loc p75 = r(p75)
    twoway (scatter y x, mcol(black*.25)) || ///
            (lfit y x, lcol(blue)) || ///
            (lfit y x if y<=`p25',lcol(red*.35)) || ///
            (lfit y x if y >=`p75', lcol(red*.35))
    Click image for larger version

Name:	test.png
Views:	1
Size:	65.9 KB
ID:	1453449
    Roman

    Comment


    • #3
      Hi Roman, Thank you so much

      Yes you got me correct. I want to fit certain portion of my data. As you perfectly did in your example. I followed your codes in my data. Unfortunately, the two fitted lines are short and not parallel to middle fitted value as you can see below. What do you should to modify the codes so that I can get a figure similar to your example?


      Code:
      qui su POOR, det
      loc p25 = r(p25)
      loc p75 = r(p75)
      twoway (scatter POOR HEA_poverty_zone, mcol(black*.25)) || ///
      (lfit POOR HEA_poverty_zone, lcol(blue)) || ///
      (lfit POOR HEA_poverty_zone if POOR<=`p25',lcol(red*.35)) || ///
      (lfit POOR HEA_poverty_zone if POOR >=`p75', lcol(red*.35))




      Attached Files
      Last edited by Anagaw Derseh; 15 Jul 2018, 02:42.

      Comment


      • #4
        Hi Roman, Thank you so much

        Yes you got me correct. I want to fit certain portion of my data. As you perfectly did in your example. I followed your codes in my data. Unfortunately, the two fitted lines are short and not parallel to middle fitted value as you can see below. What do you should to modify the codes so that I can get a figure similar to your example?


        Code:
        qui su POOR, det
        loc p25 = r(p25)
        loc p75 = r(p75)
        twoway (scatter POOR HEA_poverty_zone, mcol(black*.25)) || ///
        (lfit POOR HEA_poverty_zone, lcol(blue)) || ///
        (lfit POOR HEA_poverty_zone if POOR<=`p25',lcol(red*.35)) || ///
        (lfit POOR HEA_poverty_zone if POOR >=`p75', lcol(red*.35))





        Attached Files

        Comment


        • #5
          The prescription in #1 is not precise, so it's not surprising that Roman guessed wrong.

          You got what you asked for, regression lines for subsets of your data. Only exceptionally will they be parallel to the regression line for all the data. twoway lfit won't plot a line extended beyond the range of the data it summarizes.

          I guess you want something more like this. No example data given, so I use another dataset.

          Code:
          clear 
          sysuse auto 
          gen gpm = 1000/mpg 
          regress gpm weight 
          local inter = _b[_cons] 
          local slope = _b[weight]  
          predict res, res 
          su res, detail 
          local p25 = r(p25) 
          local p75 = r(p75) 
          
          set scheme s1color 
          
          twoway scatter gpm weight, ms(Oh) || lfit gpm weight, lc(black) || ///
          function p25 = `inter' + `slope' * x + `p25', ra(weight) lc(orange)  || /// 
          function p75 = `inter' + `slope' * x + `p75', ra(weight) lc(blue)  ///
          legend(order(4 "75% below" 2 "linear fit" 3 "25% below") col(1) ring(0) pos(11)) ///
          ytitle(gallons per 1000 miles) xtitle(weight in lb)
          Click image for larger version

Name:	regressplus.png
Views:	1
Size:	46.4 KB
ID:	1453468




          Comment

          Working...
          X