Announcement

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

  • Spaghetti plot with two groups

    Hello,
    I have a dataset with 145 participants (98 in group 1, 27 in group 2). The grouping variable is "rand". They each took at test at baseline (variable = "baseline"), "five_wk" and "three_mo". I would like to create a spaghetti plot to show their scores at each time point, with different colors for the two groups and two thicker lines to represent the means. I appreciate any guidance using Stata/SE 16.0.

  • #2
    There is some ambiguity here about your data structure (wide with 3 variables for each time or long with 3 observations for each person?).

    I get 98 + 27 = 125.

    This may give some ideas.


    Code:
    clear 
    set obs 375 
    set seed 280352 
    gen group = cond(_n <= 294, 1, 2) 
    egen id = seq(), block(3)
    egen time = seq(), to(3)
    label def time 1 baseline 2 "5 weeks" 3 "3 months  "
    label val time time 
    gen whatever = (group == 2) + 10 * rbeta(12, 12) if time == 1 
    bysort id (time): replace whatever = whatever[_n-1] + 3 * (rbeta(12,12) - 0.5) if time >= 2 
    egen mean = mean(whatever), by(group)
    set scheme s1color 
    line  whatever mean time, ytitle(whatever) lc(gs12) by(group, note("")) c(L) xla(1/3, valuelabel) xsc(r(0.8 3.2))
    Click image for larger version

Name:	spaghetti.png
Views:	1
Size:	89.1 KB
ID:	1644783


    This isn't subtle about 5 weeks and 3 months being unequally spaced.

    Comment


    • #3
      Thank you Nick. This is exactly what I'd like to create. Sorry I was vague about the time variable. My data is wide with 3 variables. Is your code assuming wide or long?

      Comment


      • #4
        My code assumes long. So reshape long first. If you want detailed advice on how to do it, please give a data example as explained at https://www.statalist.org/forums/help#stata


        Note that

        Code:
         
         egen mean = mean(whatever), by(group time)
        is another possibility.
        Last edited by Nick Cox; 12 Jan 2022, 10:33.

        Comment


        • #5
          Thank you Nick. I believe your instructions above and in the duplicate post resulted in the following code that worked: line ncpt0 ncpt1 mean0 mean1 time, c(L L L L)lc(red*0.2 blue*0.2 red blue) lw(medium medium medthick medthick) legend(order(3 "group 0" 4 "group 1" - "(means thicker)") pos(3) col(1)) yla(, ang(h)) ytitle(NCPT)

          However the x axis is labeled 1, 1.5, 2, 2.5, do you know how I can change this to 3 markers with "Baseline", "5 weeks", and "3 months"?
          Last edited by Katie Holzer; 12 Jan 2022, 12:53.

          Comment


          • #6
            Please disregard: xlabel(1 "Baseline" 2 "5 Week" 3 "3 Month")

            Comment

            Working...
            X