Announcement

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

  • spell number and duration for survival analysis

    I am doing a survival analysis and need to identify what spell a family is in and how long the spell is at each point in time. Eventually i want to left censor the data so that a family who started in the first period is excluded. i want to only use the first spell for a family (with the exception of families that started in period 1, then i want to use their second spell if they have one) I tried using tsset and tsspell, but it was ignoring the breaks in periods.

    Can you help me with the syntax for this?

    I want to end up with a dataset like this (here is an example of family #7 which has several breaks in periods so they will have several spells and the first spell will be left-censored because we don't know the true duration of their spell because they started in period 1 :
    FamilyID period spell duration leftcen
    7 1 1 1 1
    7 2 1 2 1
    7 3 1 3 1
    7 5 2 1 0
    7 6 2 2 0
    7 7 2 3 0
    7 8 2 4 0
    7 9 2 5 0
    7 10 2 6 0
    7 11 2 7 0
    7 12 2 8 0
    7 13 2 9 0
    7 15 3 1 0
    7 16 3 2 0
    7 17 3 3 0
    7 18 3 4 0
    7 21 4 1 0
    7 22 4 2 0
    7 23 4 3 0
    7 24 4 4 0

    here is a sample of my data:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long FamilyID float period
     1  1
     1  2
     1  3
     1  4
     1  5
     1  6
     1  7
     1  8
     2 16
     2 17
     2 18
     2 19
     2 20
     2 21
     2 22
     2 23
     2 24
     3  1
     3  2
     3  3
     3  4
     4  1
     4  2
     4  3
     4  4
     4  5
     4  6
     4  7
     4  8
     4  9
     4 10
     4 11
     4 12
     4 13
     4 14
     4 15
     4 16
     4 17
     4 18
     4 19
     4 20
     5  3
     6 11
     6 12
     6 13
     6 14
     6 15
     6 16
     6 17
     6 18
     6 19
     6 20
     6 21
     6 22
     6 23
     6 24
     7  1
     7  2
     7  3
     7  4
     7  5
     7  6
     7  7
     7  8
     7  9
     7 10
     7 11
     7 12
     7 13
     7 14
     7 15
     7 16
     7 17
     7 18
     7 21
     7 22
     7 23
     7 24
     8  1
     8  2
     8  3
     8 18
     8 19
     8 20
     8 21
     8 22
     8 23
     8 24
     9 17
    10  1
    10  2
    10  3
    10  4
    10  5
    10  6
    10  7
    10  8
    10  9
    10 10
    11  1
    end
    label values AdultID_num AdultID_num
    label def AdultID_num 1 "AA000L1R", modify
    label def AdultID_num 2 "AA001G1U", modify
    label def AdultID_num 3 "AA001G1X", modify
    label def AdultID_num 4 "AA03597B", modify
    label def AdultID_num 5 "AA07853F", modify
    label def AdultID_num 6 "AA09875B", modify
    label def AdultID_num 7 "AA100W3R", modify
    label def AdultID_num 8 "AA11244G", modify
    label def AdultID_num 9 "AA11632F", modify
    label def AdultID_num 10 "AA12853B", modify
    label def AdultID_num 11 "AA15963E", modify

  • #2
    Some issues with your question:

    1. tsspell is from SSC, as you are asked to explain (FAQ Advice #12).
    2. Your illustration using family id 7 does not correspond to your data example, but that is not crucial.
    3. Why only are spells that start in period 1 left censored? If a family is observed from period X > 1, and we do not have observations for periods < X, then how is that different from families that started at period 1? Perhaps you mean to say that all first spells are left censored.
    4. Is there a minimum duration that a spell should be? Can we have spells of length 1?

    Here is some code that creates your wanted variables:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long FamilyID float period
     1  1
     1  2
     1  3
     1  4
     1  5
     1  6
     1  7
     1  8
     2 16
     2 17
     2 18
     2 19
     2 20
     2 21
     2 22
     2 23
     2 24
     3  1
     3  2
     3  3
     3  4
     4  1
     4  2
     4  3
     4  4
     4  5
     4  6
     4  7
     4  8
     4  9
     4 10
     4 11
     4 12
     4 13
     4 14
     4 15
     4 16
     4 17
     4 18
     4 19
     4 20
     5  3
     6 11
     6 12
     6 13
     6 14
     6 15
     6 16
     6 17
     6 18
     6 19
     6 20
     6 21
     6 22
     6 23
     6 24
     7  1
     7  2
     7  3
     7  4
     7  5
     7  6
     7  7
     7  8
     7  9
     7 10
     7 11
     7 12
     7 13
     7 14
     7 15
     7 16
     7 17
     7 18
     7 21
     7 22
     7 23
     7 24
     8  1
     8  2
     8  3
     8 18
     8 19
     8 20
     8 21
     8 22
     8 23
     8 24
     9 17
    10  1
    10  2
    10  3
    10  4
    10  5
    10  6
    10  7
    10  8
    10  9
    10 10
    11  1
    end
    
    bys Fam (period): gen spell= sum(period-period[_n-1]!=1)
    by Fam: replace spell=spell[_n-1] if missing(spell) & !missing(spell[_n-1])
    bys Fam spell (period): gen duration=_n
    by Fam spell: egen leftcen= max(period==1)
    Res.:

    Code:
    . list, sepby(Fam)
    
         +------------------------------------------------+
         | FamilyID   period   spell   duration   leftcen |
         |------------------------------------------------|
      1. |        1        1       1          1         1 |
      2. |        1        2       1          2         1 |
      3. |        1        3       1          3         1 |
      4. |        1        4       1          4         1 |
      5. |        1        5       1          5         1 |
      6. |        1        6       1          6         1 |
      7. |        1        7       1          7         1 |
      8. |        1        8       1          8         1 |
         |------------------------------------------------|
      9. |        2       16       1          1         0 |
     10. |        2       17       1          2         0 |
     11. |        2       18       1          3         0 |
     12. |        2       19       1          4         0 |
     13. |        2       20       1          5         0 |
     14. |        2       21       1          6         0 |
     15. |        2       22       1          7         0 |
     16. |        2       23       1          8         0 |
     17. |        2       24       1          9         0 |
         |------------------------------------------------|
     18. |        3        1       1          1         1 |
     19. |        3        2       1          2         1 |
     20. |        3        3       1          3         1 |
     21. |        3        4       1          4         1 |
         |------------------------------------------------|
     22. |        4        1       1          1         1 |
     23. |        4        2       1          2         1 |
     24. |        4        3       1          3         1 |
     25. |        4        4       1          4         1 |
     26. |        4        5       1          5         1 |
     27. |        4        6       1          6         1 |
     28. |        4        7       1          7         1 |
     29. |        4        8       1          8         1 |
     30. |        4        9       1          9         1 |
     31. |        4       10       1         10         1 |
     32. |        4       11       1         11         1 |
     33. |        4       12       1         12         1 |
     34. |        4       13       1         13         1 |
     35. |        4       14       1         14         1 |
     36. |        4       15       1         15         1 |
     37. |        4       16       1         16         1 |
     38. |        4       17       1         17         1 |
     39. |        4       18       1         18         1 |
     40. |        4       19       1         19         1 |
     41. |        4       20       1         20         1 |
         |------------------------------------------------|
     42. |        5        3       1          1         0 |
         |------------------------------------------------|
     43. |        6       11       1          1         0 |
     44. |        6       12       1          2         0 |
     45. |        6       13       1          3         0 |
     46. |        6       14       1          4         0 |
     47. |        6       15       1          5         0 |
     48. |        6       16       1          6         0 |
     49. |        6       17       1          7         0 |
     50. |        6       18       1          8         0 |
     51. |        6       19       1          9         0 |
     52. |        6       20       1         10         0 |
     53. |        6       21       1         11         0 |
     54. |        6       22       1         12         0 |
     55. |        6       23       1         13         0 |
     56. |        6       24       1         14         0 |
         |------------------------------------------------|
     57. |        7        1       1          1         1 |
     58. |        7        2       1          2         1 |
     59. |        7        3       1          3         1 |
     60. |        7        4       1          4         1 |
     61. |        7        5       1          5         1 |
     62. |        7        6       1          6         1 |
     63. |        7        7       1          7         1 |
     64. |        7        8       1          8         1 |
     65. |        7        9       1          9         1 |
     66. |        7       10       1         10         1 |
     67. |        7       11       1         11         1 |
     68. |        7       12       1         12         1 |
     69. |        7       13       1         13         1 |
     70. |        7       14       1         14         1 |
     71. |        7       15       1         15         1 |
     72. |        7       16       1         16         1 |
     73. |        7       17       1         17         1 |
     74. |        7       18       1         18         1 |
     75. |        7       21       2          1         0 |
     76. |        7       22       2          2         0 |
     77. |        7       23       2          3         0 |
     78. |        7       24       2          4         0 |
         |------------------------------------------------|
     79. |        8        1       1          1         1 |
     80. |        8        2       1          2         1 |
     81. |        8        3       1          3         1 |
     82. |        8       18       2          1         0 |
     83. |        8       19       2          2         0 |
     84. |        8       20       2          3         0 |
     85. |        8       21       2          4         0 |
     86. |        8       22       2          5         0 |
     87. |        8       23       2          6         0 |
     88. |        8       24       2          7         0 |
         |------------------------------------------------|
     89. |        9       17       1          1         0 |
         |------------------------------------------------|
     90. |       10        1       1          1         1 |
     91. |       10        2       1          2         1 |
     92. |       10        3       1          3         1 |
     93. |       10        4       1          4         1 |
     94. |       10        5       1          5         1 |
     95. |       10        6       1          6         1 |
     96. |       10        7       1          7         1 |
     97. |       10        8       1          8         1 |
     98. |       10        9       1          9         1 |
     99. |       10       10       1         10         1 |
         |------------------------------------------------|
    100. |       11        1       1          1         1 |
         +------------------------------------------------+
    
    .

    Comment


    • #3
      Thank you, Andrew!

      This is exactly what I was trying to do. I wanted to left censor the families that had their first spell beginning in the first period because we do not know how many periods they may have been in the program before we started collecting data, thus the duration of that first spell may not be accurate. So for those families, we want to start with the second spell in the dataset, in that way, we get information on the full duration of each spell.

      There is no minimum spell duration, so a duration of 1 period is ok.

      Comment

      Working...
      X