Announcement

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

  • Preparing data for competing risks

    Hi everyone
    I have four variables: Event 1 (1: yes, 0: no), time to event 1 (years); event 2 (1: yes, 0: no), time to event 2 (years)

    I would like to do competing risks (compete variable event 2), so I have to create one new variable "events" (1: event 1, 2: compete risk has occured) before using stcrreg
    Could you please help me with the appropiate syntaxis to make it?

    Thank you

  • #2
    Jose:
    see Example #1 under -stcrreg- in Stata .pdf manual and related dataset.
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      In that example, they have a variable called "failtype" which includes both events (1= local relapse), (2=distant relapse). But, in my example, I have two different variables (bleeding: yes or no), (death: yes or no), with a following time for bleeding and a following time for death. I want to study bleeding, and use death as a competing variable.
      I assume that I have to create a new variable called "failtype", but I dont know how to deal with the following time. I dont know how to create this syntaxis. Thanks

      Comment


      • #4
        Jose, your intuition to create a new variable indicating the type of failure (or censoring) along with a new time variable is correct. In your case, you death is a competing interest to bleeding. I'll describe the method in words, and it should be easily translated to code.

        For each person, you need to determine the first(chronological event. From there, you can make a new indicator for the competing risk event. If the first event is bleeding, then the indicator of your new event takes a value of 1 (say) to indicate bleeding, or 2 (say) if it's death, or 0 for censoring (i.e., no outcomes occurred).

        Here's a sketch of pseudo-code that may help point you in a useful direction.

        Code:
        gen t_event = min(t_bleed, t_died)
        gen event = .
        replace event = 1 if mi(event_type) & t_event==t_bleed & bleed==1  /* people who had bleeding */
        replace event = 2 if mi(event_type) & t_event==t_died & died==1   /* people who died */
        replace event = 0 if mi(event_type) & max(bleed, died)==0    /* censoring: no event occurred and time should be the same for both events */

        Comment


        • #5
          Thanks Leonardo, but there is something in that syntaxis that does not work. I simplified it, but I dont know if it will be right.

          gen t_event = min(Timetobleeding, Timetodeath)
          gen event = .
          replace event = 1 if t_event==Timetobleeding /* people who had bleeding */
          replace event = 2 if t_event==Timetodeath /* people who died */
          replace event = 0 if Bleeding==0 & Death== 0 /* censoring: no event occurred and time should be the same for both events */

          Thanks again, and sorry

          Comment


          • #6
            I don't think you should have changed the syntax (aside from variable names) in the code above. I also can't help you when you say that "it didn't work", because I don't know if the code doesn't execute as intended, or the model doesn't run or what. At this point, you should provide a data example using -dataex- and describe what exactly didn't work, including the exact code you ran and the exact output Stata gave back.

            Comment


            • #7

              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input double(SM Seg_SM Muerte) float Timetodeath
              0   4.120547945205479 1 4.1177278
              0  5.2684931506849315 1  5.264887
              0  5.7972602739726025 0         .
              0  3.4438356164383563 1 3.4414785
              0  .37534246575342467 1  .3750856
              0   .9424657534246575 1  .9418207
              0   5.594520547945206 0         .
              1   2.136986301369863 1  4.714579
              0  2.8876712328767122 1  2.885695
              0   3.197260273972603 1  3.195072
              0  1.2904109589041095 1 1.2895278
              1  2.6301369863013697 1 3.9890485
              0   5.041095890410959 0         .
              1  1.0082191780821919 1 1.0075291
              1   .9561643835616438 1  .9555099
              0   5.528767123287671 0         .
              0   2.095890410958904 1  2.094456
              0   5.824657534246575 0         .
              1   2.336986301369863 1    5.1718
              0   5.652054794520548 0         .
              0  1.0164383561643835 1 1.0157427
              0   2.956164383561644 1  2.954141
              1 .005479452054794521 1 .26009583
              0   5.632876712328767 0         .
              0  .06301369863013699 0         .
              0  1.2136986301369863 1  1.212868
              1  1.6821917808219178 1 1.6810404
              1   2.863013698630137 1  2.861054
              0   2.536986301369863 1   2.53525
              0   4.986301369863014 0         .
              0   5.884931506849315 0         .
              0   4.027397260273973 1 4.0246406
              0   3.361643835616438 1  3.359343
              0  5.2164383561643835 0         .
              0   5.506849315068493 0         .
              0   5.923287671232877 0         .
              0  3.9863013698630136 1  3.983573
              0   2.989041095890411 1  2.986995
              0                 4.6 1 4.5968513
              0   5.865753424657535 0         .
              1   1.126027397260274 1 2.8145106
              0  1.0136986301369864 1 1.0130048
              0   4.750684931506849 0         .
              0   5.101369863013699 0         .
              0   5.638356164383562 0         .
              0   5.476712328767123 0         .
              0  2.3315068493150686 1  2.329911
              0  1.9863013698630136 1  1.984942
              0   5.153424657534247 0         .
              0  5.2164383561643835 0         .
              0   5.441095890410959 0         .
              1   4.980821917808219 0         .
              0   5.304109589041096 0         .
              0   5.336986301369863 0         .
              0   .9945205479452055 1  .9938399
              0   .9013698630136986 1  .9007529
              0  2.2383561643835614 1  2.236824
              0   .6246575342465753 1    .62423
              0   5.586301369863014 0         .
              0   5.076712328767123 0         .
              0  2.2356164383561645 1 2.2340863
              0    5.47945205479452 0         .
              0   5.315068493150685 0         .
              1   1.284931506849315 1  1.284052
              0   5.641095890410959 0         .
              0   5.126027397260274 0         .
              0   5.273972602739726 0         .
              0    5.67945205479452 0         .
              0   5.868493150684931 0         .
              0   4.945205479452055 0         .
              0  3.9178082191780823 1 3.9151266
              0   3.747945205479452 1   3.74538
              0   5.082191780821918 0         .
              0    5.67945205479452 0         .
              0   5.389041095890411 0         .
              0   4.986301369863014 0         .
              0   5.065753424657534 0         .
              0   5.260273972602739 0         .
              0   5.082191780821918 1  5.078713
              0   5.843835616438356 0         .
              0   .5287671232876713 1  .5284052
              0   5.635616438356164 0         .
              1  3.3095890410958906 1  3.893224
              0  1.2465753424657535 1  1.245722
              0   5.956164383561644 0         .
              0                  .6 1  .5995893
              0   4.358904109589041 0         .
              0  1.8986301369863015 1 1.8973306
              0   4.175342465753425 1 4.1724844
              1   .7561643835616438 1  .7556468
              0   4.468493150684932 1 4.4654346
              0   5.315068493150685 0         .
              0    5.36986301369863 0         .
              0   .5232876712328767 1  .5229295
              0   3.243835616438356 1  3.241615
              0  1.2712328767123289 1 1.2703627
              0   5.789041095890411 0         .
              1   4.454794520547945 1  4.862423
              1    .410958904109589 1  3.247091
              0  2.6575342465753424 1  2.655715
              end
              label values Muerte Muerte
              label def Muerte 0 "No", modify
              label def Muerte 1 "Si", modify
              gen t_event = min(Seg_SM, Timetodeath) gen event = . replace event = 1 if mi(event_type) & t_event==Seg_SM & SM==1 /* people who had bleeding */ replace event = 2 if mi(event_type) & t_event==TimetoDeath & Muerte==1 /* people who died */ replace event = 0 if mi(event_type) & max(SM, Muerte)==0 /* censoring: no event occurred and time should be the same for both events */

              Comment


              • #8
                This seems to put you on the right path.

                Code:
                * Example generated by -dataex-. To install: ssc install dataex
                clear
                input double(SM Seg_SM Muerte) float Timetodeath
                0   4.120547945205479 1 4.1177278
                0  5.2684931506849315 1  5.264887
                0  5.7972602739726025 0         .
                0  3.4438356164383563 1 3.4414785
                0  .37534246575342467 1  .3750856
                0   .9424657534246575 1  .9418207
                0   5.594520547945206 0         .
                1   2.136986301369863 1  4.714579
                0  2.8876712328767122 1  2.885695
                0   3.197260273972603 1  3.195072
                0  1.2904109589041095 1 1.2895278
                1  2.6301369863013697 1 3.9890485
                0   5.041095890410959 0         .
                1  1.0082191780821919 1 1.0075291
                1   .9561643835616438 1  .9555099
                0   5.528767123287671 0         .
                0   2.095890410958904 1  2.094456
                0   5.824657534246575 0         .
                1   2.336986301369863 1    5.1718
                0   5.652054794520548 0         .
                0  1.0164383561643835 1 1.0157427
                0   2.956164383561644 1  2.954141
                1 .005479452054794521 1 .26009583
                0   5.632876712328767 0         .
                0  .06301369863013699 0         .
                0  1.2136986301369863 1  1.212868
                1  1.6821917808219178 1 1.6810404
                1   2.863013698630137 1  2.861054
                0   2.536986301369863 1   2.53525
                0   4.986301369863014 0         .
                0   5.884931506849315 0         .
                0   4.027397260273973 1 4.0246406
                0   3.361643835616438 1  3.359343
                0  5.2164383561643835 0         .
                0   5.506849315068493 0         .
                0   5.923287671232877 0         .
                0  3.9863013698630136 1  3.983573
                0   2.989041095890411 1  2.986995
                0                 4.6 1 4.5968513
                0   5.865753424657535 0         .
                1   1.126027397260274 1 2.8145106
                0  1.0136986301369864 1 1.0130048
                0   4.750684931506849 0         .
                0   5.101369863013699 0         .
                0   5.638356164383562 0         .
                0   5.476712328767123 0         .
                0  2.3315068493150686 1  2.329911
                0  1.9863013698630136 1  1.984942
                0   5.153424657534247 0         .
                0  5.2164383561643835 0         .
                0   5.441095890410959 0         .
                1   4.980821917808219 0         .
                0   5.304109589041096 0         .
                0   5.336986301369863 0         .
                0   .9945205479452055 1  .9938399
                0   .9013698630136986 1  .9007529
                0  2.2383561643835614 1  2.236824
                0   .6246575342465753 1    .62423
                0   5.586301369863014 0         .
                0   5.076712328767123 0         .
                0  2.2356164383561645 1 2.2340863
                0    5.47945205479452 0         .
                0   5.315068493150685 0         .
                1   1.284931506849315 1  1.284052
                0   5.641095890410959 0         .
                0   5.126027397260274 0         .
                0   5.273972602739726 0         .
                0    5.67945205479452 0         .
                0   5.868493150684931 0         .
                0   4.945205479452055 0         .
                0  3.9178082191780823 1 3.9151266
                0   3.747945205479452 1   3.74538
                0   5.082191780821918 0         .
                0    5.67945205479452 0         .
                0   5.389041095890411 0         .
                0   4.986301369863014 0         .
                0   5.065753424657534 0         .
                0   5.260273972602739 0         .
                0   5.082191780821918 1  5.078713
                0   5.843835616438356 0         .
                0   .5287671232876713 1  .5284052
                0   5.635616438356164 0         .
                1  3.3095890410958906 1  3.893224
                0  1.2465753424657535 1  1.245722
                0   5.956164383561644 0         .
                0                  .6 1  .5995893
                0   4.358904109589041 0         .
                0  1.8986301369863015 1 1.8973306
                0   4.175342465753425 1 4.1724844
                1   .7561643835616438 1  .7556468
                0   4.468493150684932 1 4.4654346
                0   5.315068493150685 0         .
                0    5.36986301369863 0         .
                0   .5232876712328767 1  .5229295
                0   3.243835616438356 1  3.241615
                0  1.2712328767123289 1 1.2703627
                0   5.789041095890411 0         .
                1   4.454794520547945 1  4.862423
                1    .410958904109589 1  3.247091
                0  2.6575342465753424 1  2.655715
                end
                
                recast float Seg_SM, force
                
                gen float t_event = min(Seg_SM, Timetodeath)
                gen byte event = .
                replace event = 1 if t_event==Seg_SM /* people who had bleeding */
                replace event = 2 if t_event==Timetodeath /* people who died */
                replace event = 0 if SM==0 & Muerte== 0 /* censoring: no event occurred and time should be the same for both events */
                
                stset t_event , fail(event==1) /* stset with bleeding as event of interest */
                stcrreg t_event , compete(event==2) /* treat death as competing event to bleeding */

                Comment


                • #9
                  In case number 8, death (muerte)=1 and SM (bleeding)=1, with this syntaxis, offers event= .
                  Then, probably there is a mistake in the syntaxis. Thanks again for the help.

                  Comment


                  • #10
                    Sorry, I had made some errors in my code, but the general idea is the same. Note that no events are coded as missing. You also appear to have some minor differences in recorded precision betwen Seg_SM and Timetodeath (double vs float) which may cause you some errors in correctly choosing the right event. You will need to go back to your code to investigate these differences, and I avoid the problem in this example by recasting the double to float type.

                    Code:
                    recast float Seg_SM, force
                    
                    gen float t_event = min(Seg_SM, Timetodeath)
                    gen byte event = .
                    replace event = 0 if SM==0 & Muerte== 0 /* censoring: no event occurred and time should be the same for both events */
                    replace event = 1 if mi(event) & t_event==Seg_SM /* people who had bleeding */
                    replace event = 2 if mi(event) & t_event==Timetodeath /* people who died */

                    Comment


                    • #11
                      Thank you!

                      Comment

                      Working...
                      X