Announcement

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

  • Juxtaposing graphs

    Hello,

    I have a survival data of participants enrolled on disease management program. I have dissected the data into various groups such as gender (male/female), previous medical history (yes/no), on medication (yes/no), etc. One way to create survival graphs, is by writing sts graph code for every group, for example sts graph, by (gender) OR sts graph, by (previoushistory) etc.

    But this method allows you to have multiple graphs; whereas, I want to present one graph with multiple lines where each line is for different groups i.e. one for males, one for females, one for medication history as yes, and so on.

    When I write the code, for lets say 3 groups: sts graph, by (gender previoushistory medication), Stata creates additional (unwanted groups). For example, one group is gender=0/previoushistory=0/medication=0, another is gender=0/previoushistory=1/medication=0, or gender=0/previoushistory=1/medication=1 and so on.

    Is there a code which can allow me to show only required lines (2 per group)? OR, most importantly, is there a way to juxtapose/overlap different Kaplan Meier graphs in Stata?

    Thanks!

  • #2
    Please help!

    Comment


    • #3
      Hi Preeti,

      I know very little about Kaplan-Meier Survival graphs, but you want 8 lines on the graph right? (One for each of the various combinations of gender, previoushistory, and medication)

      So would it work to combine numbers 1-8 into a single variable (I've called it combo1 below)? Then you could do sts graph by() the new combination variable (pick whichever one you wish).
      Click image for larger version

Name:	Statalist - STS - idea.png
Views:	1
Size:	7.8 KB
ID:	1471182



      So combo1 I made equal to the numeric combinations of gender, history, and medication (I though it would be easier to remember the categories that way). combo1_string was created as a 3-character string (code below). That to me seemed easier, but I didn't know if sts graph, by(var) allowed the by variable to be a string. Hence the numeric solutions as well. I don't know how to get the correct labels at the bottom of the resulting graph (perhaps with label define combo1_l 1 "Male, no prior, no medication" 2 "Male, no prior, yes medication", and so on.

      Code:
      gen combo1_string = string(gender) + string( history) + string( medication)
      sts graph, by (combo1_string)  // if it allows strings.  Otherwise by(combo1) or by(combo2)

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input byte(gender history medication) int combo1 byte combo2 str3 combo1_string
      1 0 0 100 1 "100"
      1 0 1 101 2 "101"
      1 1 0 110 3 "110"
      1 1 1 111 4 "111"
      0 0 0   0 5 "000"
      0 0 1   1 6 "001"
      0 1 0  10 7 "010"
      0 1 1  11 8 "011"
      end
      
      . desc
      
      ---------------------------------------------------------------------------------------------------------------------------------------------
                    storage   display    value
      variable name   type    format     label      variable label
      ---------------------------------------------------------------------------------------------------------------------------------------------
      gender          byte    %8.0g                 Gender
      history         byte    %8.0g                 History
      medication      byte    %8.0g                 Medication
      combo1          int     %8.0g                 Combo1
      combo2          byte    %8.0g                 Combo2
      combo1_string   str3    %9s                  
      --------------------------------
      I hope some of this is helpful!
      Good luck!

      Comment


      • #4
        I don't know why I didn't think of this, but if sts graph allows by() a string variable, you could put the description in the the variable. I've attached it as both a screen shot and a dataex example.

        Click image for larger version

Name:	Statalist - STS - idea2.png
Views:	1
Size:	15.2 KB
ID:	1471185


        Code:
        sts graph, by (combo_string_desc)
        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input byte(gender history medication) int combo1 byte combo2 str3 combo1_string str33 combo_string_desc
        1 0 0 100 1 "100" "Male, no prior, no medication"    
        1 0 1 101 2 "101" "Male, no prior, yes medication"   
        1 1 0 110 3 "110" "Male, yes prior, no medication"   
        1 1 1 111 4 "111" "Male, yes prior, yes medication"  
        0 0 0   0 5 "000" "Female, no prior, no medication"  
        0 0 1   1 6 "001" "Female, no prior, yes medication" 
        0 1 0  10 7 "010" "Female, yes prior, no medication" 
        0 1 1  11 8 "011" "Female, yes prior, yes medication"
        end

        Comment


        • #5
          Hi David, thanks a ton for the help!

          However, we do not have to create groups such as 1 "Male, no prior, no medication" 2 "Male, no prior, yes medication", and so on. Using the code "sts graph, by(gender prior medication)" will automatically create these groups.

          My question is, how to overlay/juxtapose OR create a graph with groups such as 1 "All males" 2 "All females" 3 " yes prior" 4 "no prior" and so on. I do not want to have combination of groups?

          Is there a way to do that?

          Comment


          • #6
            I want a graph with survival lines on:

            1 all male participants (irrespective of their previous history or medication history)
            2 all females participants (irrespective of their previous history or medication history)
            3 all participants with no previous history (irrespective of their gender or medication history)
            4 all participants with yes previous history (irrespective of their gender or medication history)
            5 all participants with no medication (irrespective of their gender or previous history)
            6 all participants with yes medication (irrespective of their gender or previous history)

            Comment


            • #7
              Edited to add: This crossed with your post #6 (so I didn't have the list of the 6 lines that you wanted).

              Unfortunately, I don't know. In looking around a little bit, other posts suggested using the -graph combine- function. Others suggested sts graph, by() addplot(). (BTW, it might be helpful for others coming along later to help if you could post a .png file of the graph you are getting, and if you have an example of what you would like it to be like (that might be harder)

              Below comes from:
              Code:
              sts graph, by(gender) name(gr01, replace)
              sts graph, by(previoushistory ) name(gr02, replace)
              sts graph, by(medication) name(gr03, replace)
              graph combine gr01 gr02 gr03, ycommon xcommon
              Take a look at these other posts that may help point you in the right direction (I searched "kaplan meier graph combine stata" and ""kaplan meier graph overlay stata")In Stata's dropdown menu for Graphics > Survival analysis graphs > Kaplan-Meier survivor function, it has an option for "Add plots." I don't know if that would help you get the graph the way you want it.
              Click image for larger version

Name:	Statalist - STS - addplot.png
Views:	1
Size:	19.7 KB
ID:	1471194



              Sorry I can't be of more help! Hope something here can help point you in the right direction.

              --David
              Last edited by David Benson; 20 Nov 2018, 00:26.

              Comment


              • #8
                Many thanks, David! I do not want to combine graphs. Anyway, I will try some of the option described in the posts. Thanks again!

                Comment


                • #9
                  Hi Preeti,

                  If I understand correctly, the central issue is that you wish to split your data into several subgroups to be plotted simultaneously. sts graph, by() will not work, since it assumes that you wish to split your data once [albeit possibly into sub-sub-groups if more than one variable within by() ].

                  I'm not going to comment on whether what you want to do is a good idea or not, or if some other approach might be better. But here are a couple of ways to achieve what you want (there may be others):


                  Method 1: Duplicate your dataset by the number of different groupings you wish to display. In each duplication, define a single subgroup, setting all other subgroup variables to a dummy value such as "9". Then use sts graph, by() as normal:

                  Code:
                  webuse stan3, clear
                  
                  // first let's group age and waiting time
                  gen byte age50 = age>=50 if !missing(age)
                  label define age50 0 "Aged below 50" 1 "Aged 50 or above"
                  label values age50 age50
                  
                  gen byte wait30 = wait>=30 if !missing(wait)
                  label define wait30 0 "Waited less than 30 days" 1 "Waited 30 days or more"
                  label values wait30 wait30
                  
                  
                  // now let's say we want four pairs of curves:
                  // age, surgery yes/no, transplant yes/no, waiting time
                  // duplicate our dataset that number of times
                  gen byte expand = 1
                  forvalues i=2/4 {
                      expand 2 if expand==1, gen(expand_temp)
                      replace expand = `i' if expand_temp
                      drop expand_temp
                  }
                  
                  
                  // each group only appears in one copy of the dataset; otherwise set to dummy value "9"
                  replace age50 = 9 if expand!=1
                  replace surgery = 9 if expand!=2
                  replace transplant = 9 if expand!=3
                  replace wait30 = 9 if expand!=4
                  
                  
                  // now simply plot a single pair of curves for each copy of the dataset
                  stset stime, f(died)
                  sts graph, by(age50 surgery transplant wait30) ///
                      plot1opts(lstyle(p1)) plot2opts(lstyle(p1) lpattern(dash)) ///
                      plot3opts(lstyle(p2)) plot4opts(lstyle(p2) lpattern(dash)) ///
                      plot5opts(lstyle(p3)) plot6opts(lstyle(p3) lpattern(dash)) ///
                      plot7opts(lstyle(p4)) plot8opts(lstyle(p4) lpattern(dash)) ///
                      legend(order (1 "Aged below 50" 2 "Aged 50 or above" ///
                          3 "Surgery" 4 "No surgery" ///
                          5 "Transplant" 6 "No transplant" ///        
                          7 "Waited less than 30 days" 8 "Waited 30 days or more"))


                  Method 2: Use sts gen to manually calculate the survival rates over time within each subgroup, and plot them manually using twoway connected:

                  Code:
                  webuse stan3, clear
                  
                  // first let's group age and waiting time
                  gen byte age50 = age>=50 if !missing(age)
                  label define age50 0 "Aged below 50" 1 "Aged 50 or above"
                  label values age50 age50
                  
                  gen byte wait30 = wait>=30 if !missing(wait)
                  label define wait30 0 "Waited less than 30 days" 1 "Waited 30 days or more"
                  label values wait30 wait30
                  
                  
                  // use sts gen to manually calculate line data
                  stset stime, f(died)
                  sts gen s_age50 = s, by(age50)
                  sts gen s_surgery = s, by(surgery)
                  sts gen s_transplant = s, by(transplant)
                  sts gen s_wait30 = s, by(wait30)
                  
                  sort stime
                  twoway connected s_age50 stime if age50==0, connect(J) msymbol(none) lstyle(p1) ///
                      || connected s_age50 stime if age50==1, connect(J) msymbol(none) lstyle(p1) lpattern(dash) ///
                      || connected s_surgery stime if surgery==0, connect(J) msymbol(none) lstyle(p2) ///
                      || connected s_surgery stime if surgery==1, connect(J) msymbol(none) lstyle(p2) lpattern(dash) ///
                      || connected s_transplant stime if transplant==0, connect(J) msymbol(none) lstyle(p3) ///
                      || connected s_transplant stime if transplant==1, connect(J) msymbol(none) lstyle(p3) lpattern(dash) ///
                      || connected s_wait30 stime if wait30==0, connect(J) msymbol(none) lstyle(p4) ///
                      || connected s_wait30 stime if wait30==1, connect(J) msymbol(none) lstyle(p4) lpattern(dash) ///
                      legend(order (1 "Aged below 50" 2 "Aged 50 or above" ///
                          3 "Surgery" 4 "No surgery" ///
                          5 "Transplant" 6 "No transplant" ///        
                          7 "Waited less than 30 days" 8 "Waited 30 days or more"))


                  Hope that helps!

                  David.

                  Comment


                  • #10
                    Sorry to add to this -- it looks like David Fisher's excellent code will accomplish what you want. I'm just adding this because this is the screenshot I thought I added to post #7 above. I have no idea where the screenshot in #7 (the regression table with model 6 circled) came from. But I didn't want people to read this topic, see that screenshot and say "That's not an STS graph -- what was he thinking?" (and I can no longer edit post #7).

                    Click image for larger version

Name:	Statalist - STS - addplot.png
Views:	1
Size:	17.4 KB
ID:	1471318

                    Comment


                    • #11
                      David Fisher, both the methods worked perfectly! Sharing the graph created by method #2. THANK YOU SO MUCH!!!
                      Attached Files

                      Comment


                      • #12
                        David Benson, thank you so much for all your help! Apparently, type of plots listed under "create" can only be added using -add plot-, and survival graph is not one it.

                        Comment

                        Working...
                        X