Announcement

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

  • Sts graph truncate

    Ciao, I am conduct survival analysis and need to truncate x-axis. The survival time data I have obtained start at a time of 5 but sts graph starts at 0. But there is no data that starts before 5. What do I do?


    Code:
    sts graph
    is the code I use.


    Click image for larger version

Name:	Capture.JPG
Views:	1
Size:	60.4 KB
ID:	1472752

  • #2
    All Stata survival analysis commands begin with analytic time = 0. If time = 5 is the earliest start time in your data, then you have incorrectly used the -stset- command. If all of your observed entities firs become at risk at time = 5, then specify the -origin(time 5)- option in your -stset- command. If different entities begin at different times, then you need to have a variable that identifies the start time for each, let's call it start_time, and you would specify the -origin(time start_time)- option instead.

    Comment


    • #3
      Thanks a bunch. I made those suggestions and that led to the earliest observed entry t I wanted but still at risk from t = 0 and the graph start at 0. I did
      Code:
      stset time, failure(hazard) origin(start_time)
      where minimum of time is 5 and start_time is 5 too.

      Comment


      • #4
        If you want a graph whose horizontal axis begins at 5, not 0, you won't be able to get that out of -sts graph- no matter what you do. I think what you'll need to do is run -sts list- with the -saving()- option and then use that new data set and make your graph with -graph twoway connect-. -sts graph- always starts at 0, which is what is usually wanted for a survival graph.

        Comment


        • #5
          Clyde Schechter But for example what if I am examining student drop out and want to use age and not time as the time structure for stset? Thus x-axis should start at 12

          Comment


          • #6
            Then you can't use -sts graph- for that. The way -sts graph- is set up, the horizontal axis is always the amount of time at risk, and, by definition, that always starts at zero. It may correspond to age 12 or something like that, but -sts graph- neither knows nor cares about that. To get the kind of graph you want, you would have to use -sts list, saving()- and then make your own graph using -graph twoway connect-, as I mentioned in #4.

            Comment


            • #7
              Clyde Schechter Cheers I gave this a try but am not 100% sure on how to go about that. Do you have a link or sample code to do so?

              Comment


              • #8
                Here's an example
                Code:
                clear*
                webuse stan3
                
                stset age, failure(died)
                
                tempfile results
                
                sts list, saving(`results')
                
                use `results', clear
                label var time "Age"
                graph twoway connected survivor time, connect(stairstep) xlabel(8(4)60) msym(i)

                Comment


                • #9
                  Thanks so much actually that is similar to what I tried but as you see the curve still starts before 8 (or 18
                  Click image for larger version

Name:	age18.JPG
Views:	1
Size:	24.5 KB
ID:	1475756
                  in the example) added here

                  Comment


                  • #10
                    Without example data (use -dataex-) and the exact code you used, I can't troubleshoot that for you.

                    Comment


                    • #11
                      Clyde Schechter Sorry about that

                      Code:
                       
                       clear* webuse stan3  stset age, failure(died)  tempfile results  sts list, saving(`results')  use `results', clear label var time "Age" graph twoway connected survivor time, connect(stairstep) xlabel(18(4)60) msym(i)

                      Comment


                      • #12
                        The reason you're getting that result is that the first event in that data set occurs at age 8, not 18. So you're asking for something even more out of line with survival analysis: you want to ignore the events that occur before a certain time period. I suppose you have your reasons.

                        Anyway, you can do this easily enough. Just change the -sts list- command in that code to -sts list if age >= 18, saving(`results') - and then you will get a graph that starts at age 18.

                        There's one thing you need to be clear about. The -xlabel()- option in -graph twoway- can extend the horizontal axis of a graph to include values of x that are not part of the data, but it never restricts the axis to exclude observations. If there are observations you want excluded from the graph, they have to be removed from the data either in the code that creates the data (as I have proposed here), or by using an -if- or -in- condition in -graph twoway-. (The latter approach, however, would give incorrect results in this particular setting.)

                        Comment


                        • #13
                          Originally posted by Clyde Schechter View Post
                          Here's an example
                          Code:
                          clear*
                          webuse stan3
                          
                          stset age, failure(died)
                          
                          tempfile results
                          
                          sts list, saving(`results')
                          
                          use `results', clear
                          label var time "Age"
                          graph twoway connected survivor time, connect(stairstep) xlabel(8(4)60) msym(i)
                          I encountered the same problem of producing the graph while using age as my x-axis. Thanks Clyde Schechter for providing the solution. However, I have multiple groups that I need to draw survival curves on the same graph. I know that in the -sts graph- command you can use the -by- option. But I am not sure how to update the code above correspondingly. Do you know about any way I can do it? Thanks!

                          Comment


                          • #14
                            -graph twoway connected- also allows a -by()- option, although it produces separate panels for each level of the by-group. If you want all the curves on a single panel, then what you have to do is -reshape- the data into wide layout so you have a separate survivor variable for each group, and then list all of those survivor variables before the time variable in the -graph twoway connected- command.

                            If you need more specific guidance, you need to post example data, with the -dataex- command. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

                            Comment


                            • #15
                              Thanks Clyde Schechter! Below is a sample of data that I am using. Each line represents one observation from one single person.
                              'leave' is a dummy variable to indicate whether the person leaves home or not. 'ageleave' is the age of respondent who leaves home, and age 35 is the maximum age to indicate the person have not left home before 35 (max age in the study). The other factors include 'sex' and 'reason' of leaving home.

                              Code:
                              input float(ageleave    leave)    byte    sex    float    reason
                              35 0 1 0
                              35 0 1 0
                              22.2 1 1 1
                              35 0 1 0
                              16.7 1 2 3
                              35 0 1 0
                              18.5 1 2 3
                              16 1 2 4
                              35 0 2 0
                              25.3 1 1 1
                              19.6 1 2 1
                              35 0 1 0
                              30.5 1 1 4
                              21 1 1 3
                              32.1 1 1 3
                              35 0 2 0
                              35 0 2 0
                              Below is the code that I revised from your example to produce the two-way graphs. The graph that I produced has 5 panels with survival curves by 5 different reasons.
                              Code:
                              stset ageleave, failure(leave==1)
                              tempfile results
                              sts list, by(reason) saving(`results')
                              use `results', clear
                              label var time "Age"
                              graph twoway connected survivor time, by(reason) connect(stairstep) xlabel(15(5)35) msym(i)
                              What I hope to produce instead is one graph that contains 5 lines in one panel. You mentioned that I can use -reshape wide- command to change the data layout. However, my data is cross-sectional rather than panel. Is it still doable?
                              Last edited by Stephanie Cheng; 21 Jun 2021, 13:50.

                              Comment

                              Working...
                              X