Announcement

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

  • How to Add Two Cumulative Failure Plots Together and Add Legends to Distinguish Them in Survival Analysis Using Stata?

    This is a survival analysis dataset. I want to add two separate cumulative failure graphs together with necessary legends by gender to distinguish them in the resulting graph.

    webuse catheter, clear
    stset time infect
    sts gen s = s
    gen cumulative_failure = 1 - s
    sort _t
    label var cumulative_failure "Cumulative Failure Probability"
    tw(line cumulative_failure time if female==1)
    tw(line cumulative_failure time if female==0)

    I know that the goal can be done if using
    tw(line cumulative_failure time if female==1)(line cumulative_failure time if female==0) . I don't want this.
    Also, I don't want to use
    ltable time infect, failure graph by(female) notable
    because it just generates two separate plots although they are in a same plane.

    Thank you for your help!
    Last edited by smith Jason; 23 Apr 2022, 13:36.

  • #2
    I see what you don't want. I'm not sure I understand what you do want. But perhaps it is this?
    Code:
    reshape wide cummulative_failure, i(patient time) j(female)
    label var cummulative_failure1 "Female"
    label var cummulative_failure0 "Male"
    tw line cummulative_failure? time, sort
    Note: I have followed your variable naming, but the correct spelling is cumulative with just one m.

    Comment


    • #3
      Thank you! It is a typo and corrected now.
      By the way, I don't know if I used the formula below
      sts gen s = s, by(female)
      gen cumulative_failure = 1 - s
      to compute cumulative failure by gender is correct,

      webuse catheter,clear
      stset time infect
      sts gen s = s, by(female)
      gen cumulative_failure = 1 - s
      sort _t
      Then, your Stata code.
      reshape wide cumulative_failure, i(patient time) j(female)
      label var cumulative_failure1 "Female"
      label var cumulative_failure0 "Male"
      tw line cumulative_failure? time, sort

      Thank you!
      Last edited by smith Jason; 23 Apr 2022, 13:45.

      Comment


      • #4
        Yes. I hadn't paid close attention to the creation of the cumulative failure part of your code. But it should be done -by(female)-, as in #3, not overall as in #1.

        By the way, another, simpler approach is
        Code:
        webuse catheter,clear
        stset time infect
        sts graph, by(female) failure
        This gives you the cumulative failure function in a step-graph rather than as a line graph. But if you're OK with that, it's less coding.

        Comment


        • #5
          Thank you!

          Comment

          Working...
          X