Announcement

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

  • Kaplan-Meier curves by stratified by two variables in the same graph

    I am trying to generate a KM survival curve by genotype (3 strata) for two different drug regimens. Thus I would end up with 3 curves for each drug. I can easily do either, survival by genotype or survival by drug using the code below, but cannot figure out how to include both on the same graph.
    Code:
    sts graph, by(rs1729747) -OR- sts graph, by(drug)
    Is there a way to do this?

  • #2
    Here is an example that illustrates some technique. You need to label the final graph appropriately.

    Code:
    webuse drug2, clear
    gen somevar= cond(_n<=_N/3, 1, cond(_n>(2*_N)/3, 3, 2))
    set scheme s1color
    sts graph, by(drug) saving(gr1, replace)
    sts graph, by(somevar) saving(gr2, replace)
    expand 2, gen(new)
    gen var= cond(new, somevar, drug)
    egen newvar= group(new var), label
    sts graph, by(newvar) saving(gr3, replace)
    gr combine gr1.gph gr2.gph gr3.gph
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	76.6 KB
ID:	1601024

    Last edited by Andrew Musau; 01 Apr 2021, 12:44.

    Comment


    • #3
      Thanks Andrew, pretty nifty! I was able to create the graph with the code you provided, but realized that I didn't articulate what I needed correctly. I actually need the survival by genotype for each drug rather than survival by drug and survival by genotype separately. I figured out that I could use the if statement and get what I need but I cannot figure out how to combine the graphs as you have done because I can no longer use "drug" and "genotype" separately where I have question marks. I can get two separate graphs next to each other but not overlayed on the same plot. Will that work for this situation? Here is the code:
      Code:
      sts graph if drug==0, by(rs1729747) saving(gr1, replace)
      sts graph if drug==1, by(rs1729747) saving(gr2, replace)
      expand 2, gen(new)
      gen var= cond(new, ??, ??)
      egen newvar= group(new var), label
      sts graph, by(newvar) saving(gr3, replace)
      gr combine gr1.gph gr2.gph gr3.gph
      Thanks again, I learned something new.
      Sandi

      Comment


      • #4
        That is much easier. Just use the -group()- function of egen to generate a variable with all the combinations.

        Code:
        egen group= group(drug rs1729747), label
        sts graph, by(group)

        Comment


        • #5
          Much easier! I figured out that I could also do this in a more complicated way and get the same graph (see below), but your suggestion is much cleaner. Thank you so much for your time, I really appreciate it!!

          Code:
          gen newvar = .
          replace newvar =1 if drug==0 & rs1729747 ==0
          replace newvar =2 if drug==0 & rs1729747 ==1
          replace newvar =3 if drug==1 & rs1729747 ==0
          replace newvar =4 if drug==1 & rs1729747 ==1
          sts graph, by(newvar)

          Comment

          Working...
          X