Announcement

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

  • limit Kaplan-Meier graph presentation to a certain number at risk

    Dear Statalister,

    My question is regarding the command "sts graph". I am using Stata version 13.1.

    Some background information: I am looking at the difference in the time from HIV infection to an HCV infection over calendar periods. Therefore, I made 5 KM curves illustrated in one graph.

    I was wondering whether I can limit/stop the KM curves until there is a minimum of 10 individuals at risk in the dataset instead of letting the whole curve be plotted in the graph until there are no more individuals at risk. Thus for example, if there are only 9 individuals left at risk after 3 years, I would like the curve not to be shown after this point. Is this possible in Stata? (I know is not possible in R).

    Thank you in advance.

    Best, Daniela.

  • #2
    You can try creating a variable that counts the number of individuals at risk at time t, and then use that variable in an if condition in sts graph command to limit the figure. Something like this:

    bys your-time-variable: egen atriskcount = count(your-id-variable)

    sts graph if atriskcount>=10, your-options

    Comment


    • #3
      Hi Cyrus, Thank you.

      I will try something like that. The option you gave is not really feasible because my-time-variable that comes from the command stset (_t) is continuous, so basically per record I get a number 1 per _t (time variable) at risk as everyone has a different end of follow-up time. But maybe an " if" statement with another command will be the only option to limit my KM. If you have any other ideas, would be happy to try them out.

      Best, Daniela.

      Comment


      • #4
        Deleted because of error. Will repost
        Last edited by Steve Samuels; 01 Jun 2016, 06:21.
        Steve Samuels
        Statistical Consulting
        [email protected]

        Stata 14.2

        Comment


        • #5
          You can compute the number at risk with sts gen and from first principles-count backwards. However you cannot subset sts graph with "if". Stata will think that there are no more at risk, meaning everyone has failed. Therefore the proportion surviving is zero (left-hand graph below). Instead you must reset stset to exit observation when the number at risk drops too low (right-hand graph below).

          Code:
          webuse catheter, clear
          stset time, fail(infect)
          
          /* get at risk count from sts generate */
          sts gen atrisk = n
          count if atrisk>10 // 66
          
          /* First principles: count backwards */
          drop atrisk
          gsort -_t
          gen atrisk = _n
          sort _t
          count if atrisk>10  // 66
          sts graph if  atrisk>10, name(gr01,replace) title("subset with -if-")
          
          /* Get time at which the number at risk is < 10: here it is _t = 201 */
          sum _t if atrisk==9, meanonly
          scalar t9 = r(mean)
          
          /* Reset stset to exit at t9 */
          streset, exit(time scalar(t9))
          sts graph, name(gr02, replace) title("subset with exit()")
          graph combine gr01 gr02, ycommon xcommon
          graph export gsubset.png
          Click image for larger version

Name:	gsubset.png
Views:	1
Size:	20.6 KB
ID:	1343498

          Last edited by Steve Samuels; 01 Jun 2016, 06:50.
          Steve Samuels
          Statistical Consulting
          [email protected]

          Stata 14.2

          Comment


          • #6
            Sorry- Where I used
            Code:
            atrisk>10
            I meant
            Code:
            atrisk>=10
            This error doesn't affect the code after the "get time" comment.
            Last edited by Steve Samuels; 01 Jun 2016, 11:14.
            Steve Samuels
            Statistical Consulting
            [email protected]

            Stata 14.2

            Comment

            Working...
            X