Announcement

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

  • can you use addplot with stpm2 ?

    Hi I've been using -stpm2- from: https://pclambert.net/software/stpm2/stpm2_timevar/

    Is it possible to have prediction curves for hormon 0 and hormon 1 as a function of age on the same graph **WITH CONFIDENCE INTERVALS <<--- this is my problem ?

    P Lambert's original code
    Code:
    predict t1_age, surv at(hormon 0 pr_1 3.43) ci timevar(t1)  
    
    twoway  (rarea t1_age_lci t1_age_uci age, sort color(blue%25))  /// (line t1_age age, sort lcolor(red)) /// ,ytitle("1 year survival") legend(off) /// ylabel(,angle(h) format(%3.1f)) name(age_t1, replace)
    I've tried the below

    Code:
    predict t5_age, surv at(hormon 0 pr_1 3.43) ci timevar(t5)
    predict t5_age2, surv at(hormon 1 pr_1 3.43) ci timevar(t5)  
    
    twoway  (rarea t5_age_lci t5_age_uci age, sort color(blue%25))  /// (line t5_age age, sort lcolor(red)) || twoway (rarea t5_age2_lci t5_age2_uci age, sort color(blue%25)) (line t5_age2 age, sort lcolor(red)) ,ytitle("5 year survival") legend(off) ylabel(,angle(h) format(%3.1f)) name(age_t5, replace)
    Stata error:
    twoway is not a twoway plot type

    Is there anyone who knows if this possible?

    I know that this works - but it does not plot the confidence intervals for the hormon 1

    Code:
    //No confidence intervals for t5_age2
    
    twoway  (rarea t5_age_lci t5_age_uci age, sort color(blue%25))  ///
    (line t5_age age, sort lcolor(red)) || (line t5_age2 age, sort lcolor(red)) ,ytitle("5 year survival") legend(off) ylabel(, angle(h) format(%3.1f)) name(age_t5, replace)
    
    //Attempt with confidence intervals for t5_age2 
    
    twoway  (rarea t5_age_lci t5_age_uci age, sort color(blue%25))  ///
    (line t5_age age, sort lcolor(red)) || (line t5_age2 age, sort lcolor(red)) twoway  (rarea t5_age2_lci t5_age2_uci age, sort color(blue%25))   ,ytitle("5 year survival") legend(off) ylabel(,angle(h) format(%3.1f)) name(age_t5, replace)
    
    
    //error: twoway is not a twoway plot type
    Last edited by Rose Matthews; 08 Nov 2023, 13:39.

  • #2
    Stata is telling you the problem, but indirectly. The command name twoway should appear just once as the first word. Stata can't make sense of the second or any further occurrence.

    I don't see any relation of the code to your title question.

    There may be other errors, which would be flagged once you fix that one. If you like the () syntax for distinct twoway calls, then I suggest that you avoid the || syntax, and vice versa.

    Comment


    • #3
      Perhaps I may have worded it incorrectly, but to add 2 graphs pls see the code below:

      Code:
        
       predict t5_age, surv at(hormon 0 pr_1 3.43) ci timevar(t5) predict t5_age2, surv at(hormon 1 pr_1 3.43) ci timevar(t5)  twoway (rarea t5_age_uci t5_age_lci age if age<1, sort lcolor(gs14) fcolor(gs14)) ///    (line t5_age age if age<1, sort), || (line t5_age2 age, sort lcolor(red)) (rarea t5_age2_lci t5_age2_uci age, sort color(blue%25)), ytitle("5 year survival") legend(off) ylabel(,angle(h) format(%3.1f)) ///

      Comment


      • #4
        Please post #3 again. (Many of us have been bitten by problems copying code chunks from one part of Statalist to another.)

        Comment


        • #5
          Here's some code.

          Code:
          use https://www.pclambert.net/data/rott2b, clear
          
          stset rf, f(rfi==1) scale(12) exit(time 60)
          rcsgen age, df(3) gen(agercs) center(60)
          stpm2 hormon agercs* pr_1, scale(hazard) df(4) eform
          
          gen t1 = 1 
          predict t1_age, surv at(hormon 0 pr_1 3.43) ci timevar(t1)
          twoway  (rarea t1_age_lci t1_age_uci age, sort color(blue%25))  ///
                           (line t1_age age, sort lcolor(red)) ///
                           ,ytitle("1 year survival") legend(off) ///
                           ylabel(,angle(h) format(%3.1f)) name(age_t1, replace)
                           
          // up to this point I've just copied code from https://pclambert.net/software/stpm2/stpm2_timevar/
          
          // we now want to estimate 1-year survival for both levels of -hormon- and plot it without CIs 
                           
          predict h0_t1_age, surv at(hormon 0 pr_1 3.43) timevar(t1)
          predict h1_t1_age, surv at(hormon 1 pr_1 3.43) timevar(t1)
          
          twoway  (line h0_t1_age age, sort lcolor(red)) ///
                  (line h1_t1_age age, sort lcolor(blue)) ///
                   ,ytitle("1 year survival") ///
                    ylabel(,angle(h) format(%3.1f)) name(age_t1, replace)    ///
                    legend(order( 1 "hormon=0" 2 "hormon=1") ring(0) pos(6) cols(1))
          I don't think using -addplot- is the way to go.

          If I've understood your question, you want to plot the predicted survival as a function of age for both levels of -hormon-.

          The approach I have used is to first predict each of the two quantities you want, and then plot them. Note that h0_t1_age is the same as t1_age; I chose to first reproduce Paul Lambert's code and then address your question.

          The key is recognising that -rarea- is used for plotting confidence bands and -line- is used for plotting a line.

          Comment


          • #6
            Are you the Paul Dickman working with P Lambert ?
            your websites are similar ! what a wonderful function stpm2!

            Your info on your websites in terms of help file and step by step are so easy to understand !

            i’ve solved this question (in this post) and actually made interactive graphs too !

            i wonder if i could get your thoughts on this question - i think this may be against statalist rules
            https://www.statalist.org/forums/for...2-by-p-lambert

            Apologies I should have written p lambert & p dickman !
            pity you only run survival courses once a year in June !

            Comment

            Working...
            X