Announcement

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

  • Graphing status over time

    Hi All,

    I suspect I can't do what I am trying to do but thought it best to ask before I give up.

    I am trying to track peoples status over time. Each person has date1 (their starting date), date2 (an event) and date3 (their stop date) end (a cut off date).
    I want to create a longitudinal plot that shows how much data we have before the "cut off".

    For some people, they will leave before the "cut off", for some they will leave after and I would like to colour code the time before and after the "cut off" accordingly.

    I have attached some example code for what I have tried so far.

    EXAMPLE CODE 1
    Visually, the shape is what I am looking for, however rather than have each line a different colour, I would want the following:

    between date1 & date2 (green)
    between date2 & date3 (yellow)
    unless anything after the "cut off" (red)

    EXAMPLE CODE 2
    To achieve the above I have reformatted the time points so that they can be reliably colour coded [(time1-time2(green), time2-time3(yellow), time3-time4(red)], however I am unsure where to go from here!

    Any help is appreciated!

    Best wishes,
    Cydney


    Code:
    /* EXAMPLE PART 1 */
    clear
    set obs 500
    
    gen date1 = floor((mdy(12,31,2006)-mdy(12,1,2000)+1)*runiform() + mdy(12,1,2000))
    gen date2 = date1 + floor(runiform()*365)
    gen date3 = date1 + 365
    
    gen end = td(01jun2005)
    
    sort date1
    gen N = _n
    format date1 date2 date3 end %td
    
    preserve
    
    reshape long date, i(N) j(time)
    
    xtline N, i(N) t(date) overlay legend(off) addplot(line N end)
    
    
    /* EXAMPLE PART 2 */
    
    restore
    
    gen time1 = date1
    
    gen time2 = .
    replace time2 = date2 if date2 < end
    replace time2 = end if date2 > end
    
    gen time3 = .
    replace time3 = date3 if date3 < end
    replace time3 = end if date3 > end
    
    gen time4 = .
    replace time4 = date3 if date3 < end
    replace time4 = date3 if date3 > end
    
    format time* %td




  • #2
    You worked hard to explain this but I may not be alone in not being able to digest it easily. We need please a data example that while simplified is still realistic. It would be fine to have utterly invented dates, as may be essential for your project.

    Red and green colour codes together are challenging to many people. nei.nih.gov/learn-about-eye-health/eye-conditions-and-diseases/color-blindness/types-color-vision-deficiency

    Comment


    • #3
      Hi Nick,

      The above example is realistic to the situation other than the fact not everyone will have the event, so for some the data is more simplified.

      The above graph is to show loss of data from stopping a study early. The graph is for internal use, which is why I haven't given more consideration to colour blindness, but the colours themselves are not what is important it is to show completeness of data.

      If someone has the event before the "cut off", we have complete data, if someone finishes the year before the cut off we have complete data. We just want to visually see the time lost for each person, accounting for the fact they may have had the event before the cut off, but not complete a full year follow up.

      I hope that clarification helps.

      Best wishes,
      Cydney

      Comment


      • #4
        I will do what I can -- possibly much later.

        Comment


        • #5
          My bad: I was expecting to see a large chunk of data as an example and foolishly didn't see the simulation commands.

          Unless I have missed something, the principles are as easily discussed with 50 observations as well as with 500, and I've set a seed explicitly.

          The answer I have is the last line of code in this block.

          But I am obstinate in warning against mixing red and green, even if you know that none of your colleagues are challenged that way.

          Code:
          clear
          set obs 50 
          set seed 31459 
          
          gen date1 = floor((mdy(12,31,2006)-mdy(12,1,2000)+1)*runiform() + mdy(12,1,2000))
          gen date2 = date1 + floor(runiform()*365)
          gen date3 = date1 + 365
          
          gen end = td(01jun2005)
          
          sort date1
          gen N = _n
          format date1 date2 date3 end %td
          
          preserve
          
          reshape long date, i(N) j(time)
          
          xtline N, i(N) t(date) overlay legend(off) addplot(line N end)
          
          
          /* EXAMPLE PART 2 */
          
          restore
          
          gen time1 = date1
          
          gen time2 = .
          replace time2 = date2 if date2 < end
          replace time2 = end if date2 > end
          
          gen time3 = .
          replace time3 = date3 if date3 < end
          replace time3 = end if date3 > end
          
          gen time4 = .
          replace time4 = date3 if date3 < end
          replace time4 = date3 if date3 > end
          
          format time* %td
          
          twoway rspike time1 time2 N, horizontal lc(stc1) || rspike time2 time3 N, horizontal lc(stc2) || rspike time3 time4 N, horizontal lc(stc3)
          As details, I note that

          Code:
          gen time1 = date1
          
          gen time2 = .
          replace time2 = date2 if date2 < end
          replace time2 = end if date2 > end
          
          gen time3 = .
          replace time3 = date3 if date3 < end
          replace time3 = end if date3 > end
          
          gen time4 = .
          replace time4 = date3 if date3 < end
          replace time4 = date3 if date3 > end
          can be rewritten

          Code:
          gen time1 = date1
          
          gen time2 = .min(date2, end) 
          
          gen time3 = .min(date3, end) 
          
          gen time4 = .date3
          Note > and < both exclude == (equality) so the above makes a guess about date2 == end and so on.

          Comment

          Working...
          X