Announcement

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

  • Smoothing an overlaid graph

    Hello,

    I am trying to generate a smoothed overlaid graph but cannot seem to find whether any code exists for this.

    To generate an un-smoothed graph, I am using:

    twoway line x1 x2 x3 x4 x5 y1

    To generate one smoother graph, I have come across the code:

    twoway lowess x1 y1

    However, this code does not allow multiple x variables to be included.

    Does anyone know whether there is a code that satisfies what I am after, or whether there is a code I could use after generating five smoothed graphs using the twoway lowess command to combine all the graphs onto one axis please?

    Look forward to your responses.

    Kate

  • #2
    You can just add other graphs, as in

    Code:
    twoway lowess x1 y1 || lowess x2 y1 || lowess x3 y1
    In some ways more satisfying is to write code that writes code, as in

    Code:
    * sandbox to play 
    clear
    set obs 100
    set seed 2803
    gen t = _n
    
    forval j = 1/5 {
        gen x`j' = (`j' - 3) * t + rnormal(0,10)
    }
    
    line x? t
    
    * you can kill sheep with incantations, as long as you add enough arsenic (Voltaire)
    * here is the arsenic
    
    local call
    forval j = 1/5 {
        local call `call' lowess x`j' t, bw(0.2) ||
    }
    
    twoway `call'

    Comment


    • #3
      Hi Nick,

      Thanks for your reply!

      The first snippet of code didn't work. I wrote:

      twoway lowess surv1 year || surv2 year || surv3 year || surv4 year || surv5 year

      I got the error message "surv2 is not a twoway plot type"

      However your second piece of code has worked, and as you said, is more satisfying!

      Can I ask, how would I write up the method of the second piece of code please? Are the lines still smoothed using the "lowess" function? I am quite new to programs in Stata so just trying to figure out what is going on there, rather than just copying the code!

      Kind regards,
      Kate

      Comment


      • #4
        I didn't recommend the pattern you followed first. Looking again at #2 and translating to the variables you really have yields

        Code:
        twoway lowess surv1 year || lowess surv2 year || lowess surv3 year || lowess surv4 year || lowess surv5 year
        As the error message implies, you need to specify a plot type after each ||, which was done in #2. lowess needs to be repeated.

        Yes, both first and second patterns I suggest apply the subcommand lowess (not a function) to each y variable as a function of the x variable, the variable y being the first-named and the variable x being the second named.

        The local macro j loops over 1 to 5, so the variable name in


        Code:
         
         local call forval j = 1/5 {     local call `call' lowess surv`j' year ||  }  
         twoway `call'
        loops from surv1 to surv5. Each macro name is substituted by its value. Naturally, you should smooth similar curves in the same way and I know no reason to stick to the default, as the degree of smoothing that works best will depend on the data and your project.

        As you go around the loop the syntax for the graph command is built up step by step before being run. The loop itself is just text manipulation. Nothing graphical is done before the twoway statement.

        FWIW, I abandoned use of lowess some years ago, for at least two reasons. The first is that interpretations and implementations of lowess (or loess, or locfit) vary across the statistical world. The second is that twoway lpoly and lpoly are much more flexible and easier to explain.

        Comment


        • #5
          Thanks Nick! I see my simple error with the first piece of code - apologies.

          I will look into twoway lpoly and lpoly too.

          Thanks,
          Kate

          Comment

          Working...
          X