Announcement

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

  • Restricting y axis range with LOWESS curve fit over scatterplot

    I am constructing scatterplots with LOWESS curves fit over them. I have done this successfully with several groups of data, but one set is giving me problems. The LOWESS curve extends beyond the y range of the scatterplot. I've tried all sorts of y range commands, but nothing seems to work. How do I limit the y range of the LOWESS, or perhaps the trick is to limit the x range, which I've tried as well? I've attached an example of graph that works well and the graph that isn't working. All uses same code, just different data. The lowest value of USD_1996 is 10,000, so I don't understand why the LOWESS is plunging so low.

    Code:
    twoway ///
    (scatter USD_1996 DALY_1996, mlabel(display_name) mlabgap(3.5) msymbol(O) mcolor(blue) mlabcolor(blue) mlabvp(position_label) title("All Continents") msize(small) ytitle("Child Health Aid") yscale(log) ylabel(10000 "< $10,000" 10000000 "$10 Million" 10000000000 "$10 Billion", angle(0)) ysc(r(10000 10000000000)) xsc(r(0330000000)) xlabel(160000000 "160 Million" 320000000 "320 Million" , angle(0)) xtitle("DALY")) ///
    (lowess USD_1996 DALY_1996, ysc(r(1000 100000000)) ylabel(10000 "< $10,000" 10000000 "$10 Million" 10000000000 "$10 Billion", angle(0)) ysc(r(10000 10000000000)) lwidth(thin) lcolor(blue) yscale(log) bwidth(.6))


    Thank you,
    Clay Bavinger
    University of Michigan

  • #2
    1. I only see two icons, no actual graphs in the first post here.
    2. Provide your data sample or reproduce your problem with a publicly available data
    3. Lowess may produce smooth curve going below (or above) the lowest (respectively highest) data point, make sure you don't want to ruin your analysis with such a restriction.

    Code:
    clear
    input x y
    1 2
    2 2
    3 3
    4 3
    9 3
    10 3
    11 2
    12 2
    end
    
    lowess y x,bw(.9) generate(smooth)
    produces:
    Code:
            x   y      smooth  
      1.    1   2   1.8802099  
      2.    2   2    2.299835  
      3.    3   3   2.6341092  
      4.    4   3   2.7161675  
      5.    9   3   2.7161675  
      6.   10   3   2.6341092  
      7.   11   2    2.299835  
      8.   12   2   1.8802099
    (note the values at the tails are smaller than anything in the original data).

    Best, Sergiy Radyakin

    Comment


    • #3
      Thank you Sergiy.

      I have attached two code snips with the graph code and data input. One produces scatterplot and LOWESS for the 1996 data, the other for 2009. 2009 works correctly, and 1996 is giving me the issue described earlier. The data for the two years are in similar ranges, though not exactly the same. I don't really want to do any analysis with the LOWESS, just have the curve fit on the graph.

      Having fiddled with it more, I've now realized that I can also cause the 2009 data (which is the set for which the graph looks correct) to have a similar error if I change the bwidth (to .8, for example). So, perhaps there is some trick regarding bwidth, though it seems the the y range is what I'm really interested in.


      Code:
      clear 
      input USD_2009 DALY_2009
      1671317        1.53e+07
      7.78e+09        6.03e+07
      1057266        4163100
      10000        877100
      3.43e+07        4017400
      3.18e+09        8561900
      10000        98700
      1.57e+09        4.30e+07
      4.03e+09        1.61e+07
      5.30e+08        2.34e+08
      10000        2736900
      1198815        6135300
      68235.5        4279700
      5.96e+08        1.24e+08
      end
      
      
      twoway ///
      (scatter USD_2009 DALY_2009, yscale(log) ylabel(10000 "< $10,000" 10000000 "$10 Million" 10000000000 "$10 Billion", angle(0))) ///
      (lowess USD_2009 DALY_2009, bwidth(.6))


      Code:
      clear
      input USD_1996 DALY_1996
      10000        1.46e+07
      6.15e+09        1.18e+08
      10000        7572900
      10000        985500
      226123.5        3439200
      227257        1.81e+07
      10000        121700
      6233331        6.76e+07
      1.14e+09        2.34e+07
      8.28e+07        2.99e+08
      10000        4083800
      10000        1.02e+07
      10000        5330300
      5.21e+07        2.47e+08
      end
      
      twoway ///
      (scatter USD_1996 DALY_1996, yscale(log) ylabel(10000 "< $10,000" 10000000 "$10 Million" 10000000000 "$10 Billion", angle(0))) ///
      (lowess USD_1996 DALY_1996, bwidth(.6))


      Clay Bavinger

      Comment

      Working...
      X