Announcement

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

  • smoothing lines

    Dear Stata users,

    I am using Stata 15 and trying to plot changes in sb1 vs years by age-groups (4 different models). This graph that I plotted makes artifacts (not smooth lines). I am wondering how to construct plots with nice smooth lines?

    Here is the code used to draw the graph:
    PHP Code:
    set scheme s1color
    tw  
    (lowess sb1 yrs if age_gr==1sort lcolor(blue)) ///
        
    (lowess sb1 yrs if age_gr==2sort lcolor(orange)) ///
        
    (lowess sb1 yrs if age_gr==3sort lcolor(green)) ///
        
    (lowess sb1 yrs if age_gr==4sort lcolor(cranberry)) /// 
        
    (lowess sb1 yrs if age_gr==5sort lcolor(yellow)) ||, by(modelcol(2compact)  ///
         
    ylab(---1  0 1)  xlab(0 5 10 15 20 25 30 35///
         
    leg(allxtitle("years"ytitle("change isn sb1"///
         
    ylab(, gridyline(0xsize(6ysize(4.5

    Thank you very much
    Oyun
    Click image for larger version

Name:	Statalist graph 2020.png
Views:	2
Size:	234.5 KB
ID:	1542377
    Attached Files

  • #2
    You are using the default settings of -lowess-, which include a bandwidth of 0.8. That works well in typical data sets, but not all of them. It looks like your data are very noisy, so try specifying the -bwidth()- option with some number bigger than 0.8. Picking the right bandwidth is more of an art than a science. You may have to try several different values in order to get the look you want. If you pick a number too big, you will just get something like flat lines for everything. Too small and you see lots of irregularity like you have shown. You'll have to just adjust the bandwidth to get it "just right."

    Note: You will have to specify the -bwidth()- option separately for each -lowess-. It may also turn out that there isn't a single value of -bwidth()- that works well for all of the graphs. So you may have to tinker with each one separately.

    Comment


    • #3
      Dear professor Schechter,

      As always, thank you so much for your prompt and very helpful comments.

      Comment


      • #4
        Naturally @Clyde is right with the answer: if a smoothed curve is not smooth enough, you should try smoothing more. But the bandwidth of lowess can't go above 1, which doesn't mean that curves are smoothed to horizontal constants.

        My guess from the curves and experience with smoothing is that you have rather small samples in each group. When that is the case the supposedly smooth curves often just twist and turn in response to quirks in the data. No smoothing method can do anything else in that circumstance.

        I am a big fan of scatterplot smoothing -- when it works. In this case some kind of mixed model may make more sense, but one informed by the underlying medical science, on which I have no idea.

        Comment


        • #5
          Thank you very much for your reply Dr.Cox.

          Comment

          Working...
          X