Announcement

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

  • #16
    The graph looks fairly silly for your example data, but I think the principle is about right.

    Code:
    clear 
    input id days SCIM group
    1 34 85.9 2
    1 90 94.9 2
    1 146 95.4 2
    2 33 99.2 2
    2 97 100 2
    3 27 90.4 1
    3 76 100 1
    3 111 100 1
    4 38 62.7 2
    4 84 86.9 2
    4 165 96.7 2
    5 20 75.9 1
    5 98 97.8 1
    5 112 98.3 1
    end 
    
    generate months = round(days/30, 0.5) 
    egen mean = mean(SCIM), by(group months) 
    egen SD = sd(SCIM), by(group months)
    gen upper = mean + SD 
    gen lower = mean - SD 
    separate mean, by(group) veryshortlabel
    local mv `r(varlist)'
    
    egen gtag = tag(group months)
    
    sort id months
    
    twoway connected SCIM months if group == 1, lc(red) ms(Oh) mc(red) lw(thin) c(L) ///
    || connected SCIM months if group == 2, lc(blue) mc(blue) ms(+) lw(thin) c(L)    ///
    || line `mv' months if gtag, lc(red blue) lw(thick ..) xla(1/5)  sort                 ///
    || rcap lower upper months if gtag & group == 1, lc(red) ///
    || rcap lower upper months if gtag & group == 2, lc(blue) ///
    legend(order(1 "group 1" 2 "group 2" 3 "mean group 1" 4 "mean group 2")) sort name(G1, replace)

    Comment


    • #17
      Thanks for continuing the thread!
      I'm trying to follow the two examples but they don't run for me, giving
      too few variables specified
      r(102);
      and i am inclined to thing the third || line of the graph is the issue because, if omitted the rest runs, and I do know r(varlist) is different than a macro in some aspects.
      Suggestions?
      Thanks!!!

      Comment


      • #18
        The code Nick offered in #16 runs without errors on my setup. As you yourself have noticed, the issue you are encountering appears to have something to do with macros.

        The problem, I'm pretty sure, is that you are running the code incorrectly. The line -local mv `r(varlist)'- defines the local macro mv. A key thing to remember when running Stata code is that if you stop the execution of code at any point, any local macros that were in existence at that time are lost. So if you try to run this code line by line or in small chunks, and if you have any interruption of the code between the definition of local macro mv and its reference in the third || line of the graph command, then local macro mv will cease to exist and that will create precisely the error you are encountering.

        So the solution to your problem is to run the code correctly: run it all from beginning to end without interruption and it will be fine.

        Never run code with interruptions between the time a local macro is first created and the last time it is used in the code.

        Comment


        • #19
          See https://journals.sagepub.com/doi/pdf...36867X20931028 for more on Clyde's warning about code containing local macros.

          Comment

          Working...
          X