Announcement

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

  • Creating episodes

    Dear All,
    I have an individual level panel data with 108 monthly waves.

    Code:
    
    
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double(ID Month_count) float y
    25316   1         0
    25316   2         0
    25316   3         0
    25316   4         0
    25316   5         0
    25316   6         0
    25316   7         0
    25316   8         0
    25316   9         0
    25316  10         0
    25316  11         0
    25316  12         0
    25316  13        10
    25316  14 4.3396225
    25316  15         5
    25316  16         0
    25316  17         5
    25316  18         5
    25316  19         0
    25316  20         0
    25316  21       200
    25316  22         0
    25316  23       800
    25316  24      1270
    25316  25  846.6666
    25316  26  423.3333
    25316  27       800
    25316  28      1620
    25316  29 1334.4578
    25316  30 1652.1237
    25316  31  1935.835
    25316  32  1455.876
    25316  33  975.9167
    25316  34 1295.9578
    25316  35  770.5695
    25316  36 1045.1812
    25316  37  495.0858
    25316  38         0
    25316  39       800
    25316  40  228.5714
    25316  41         0
    25316  42       950
    25316  43  316.6667
    25316  44         0
    25316  45       950
    25316  46  1597.222
    25316  47 2273.0903
    25316  48   2953.94
    25316  49  3411.617
    25316  50 2878.5515
    25316  51 2345.4863
    25316  52 2912.4214
    25316  53  3405.667
    25316  54  3350.731
    25316  55 3806.3596
    25316  56  4230.675
    25316  57 4133.3105
    25316  58 4551.9956
    25316  59 3848.0786
    25316  60  3144.162
    25316  61  2440.245
    25316  62 1736.3282
    25316  63 1032.4114
    25316  64  328.4945
    25316  65         0
    25316  66         0
    25316  67         0
    25316  68       225
    25316  69       645
    25316  70 528.29785
    25316  71 191.08646
    25316  72         0
    25316  73         0
    25316  74         0
    25316  75         0
    25316  76         0
    25316  77         0
    25316  78         0
    25316  79         0
    25316  80         0
    25316  81         0
    25316  82         0
    25316  83         0
    25316  84         0
    25316  85         0
    25316  86         0
    25316  87         0
    25316  88         0
    25316  89         0
    25316  90         0
    25316  91         0
    25316  92         0
    25316  93         0
    25316  94         0
    25316  95         0
    25316  96         0
    25316  97         0
    25316  98         0
    25316  99         0
    25316 100         0
    end
    For each ID I want to create episodes that includes periods of 3+ months where y>0. For instance, in the example above, in Months 13-15 is the first episode as y>0 in 3 consecutive months. In months 17-18 y>0 but this spell only last 2 months (<3 months) and therefore does not count as an episode. Similarly, in month 21 y>0 but its only 1 month and should not be considered an episode. Month 23-37 is the second episode, 24-64 is third episode, and 68-71 is the fourth. It seems I should use some combination of egen or rangestat but I am struggling to specify a rule that will take care of this. I will be grateful for your guidance.
    Sincerely,
    Sumedha.

  • #2
    I am not on very clear about the rule that defines an episode, but is this doing it?
    Code:
    . gen episode = ((y[_n-2]>0)+ (y[_n-1]>0)+ (y[_n]>0) + (y[_n+1]>0) + (y[_n+2]>0))>2

    Comment


    • #3
      I also am not very clear about the definition, but suggest you look at -tsspell- a user-written command; use -search- or -findit- to locate and download

      Comment


      • #4
        Here I use tsspell from SSC. in two passes, to identify episodes (runs, spell) of positive y and then select only those at least length 3.

        Code:
        tsset ID Month_count
        
        tsspell , pcond(y) spell(spell1) seq(seq1) end(end1)
        
        bysort ID spell1 (Month): egen max = max(seq1)
        
        sort ID Month
        
        tsspell, cond(y > 0 & max >= 3) spell(spell2) seq(seq2) end(end2)
        
        list if spell1, sepby(spell1)
        
             +---------------------------------------------------------------------------------+
             |    ID   Month_~t          y   seq1   spell1   end1   max   seq2   spell2   end2 |
             |---------------------------------------------------------------------------------|
         13. | 25316         13         10      1        1      0     3      1        1      0 |
         14. | 25316         14   4.339622      2        1      0     3      2        1      0 |
         15. | 25316         15          5      3        1      1     3      3        1      1 |
             |---------------------------------------------------------------------------------|
         17. | 25316         17          5      1        2      0     2      0        0      0 |
         18. | 25316         18          5      2        2      1     2      0        0      0 |
             |---------------------------------------------------------------------------------|
         21. | 25316         21        200      1        3      1     1      0        0      0 |
             |---------------------------------------------------------------------------------|
         23. | 25316         23        800      1        4      0    15      1        2      0 |
         24. | 25316         24       1270      2        4      0    15      2        2      0 |
         25. | 25316         25   846.6666      3        4      0    15      3        2      0 |
         26. | 25316         26   423.3333      4        4      0    15      4        2      0 |
         27. | 25316         27        800      5        4      0    15      5        2      0 |
         28. | 25316         28       1620      6        4      0    15      6        2      0 |
         29. | 25316         29   1334.458      7        4      0    15      7        2      0 |
         30. | 25316         30   1652.124      8        4      0    15      8        2      0 |
         31. | 25316         31   1935.835      9        4      0    15      9        2      0 |
         32. | 25316         32   1455.876     10        4      0    15     10        2      0 |
         33. | 25316         33   975.9167     11        4      0    15     11        2      0 |
         34. | 25316         34   1295.958     12        4      0    15     12        2      0 |
         35. | 25316         35   770.5695     13        4      0    15     13        2      0 |
         36. | 25316         36   1045.181     14        4      0    15     14        2      0 |
         37. | 25316         37   495.0858     15        4      1    15     15        2      1 |
             |---------------------------------------------------------------------------------|
         39. | 25316         39        800      1        5      0     2      0        0      0 |
         40. | 25316         40   228.5714      2        5      1     2      0        0      0 |
             |---------------------------------------------------------------------------------|
         42. | 25316         42        950      1        6      0     2      0        0      0 |
         43. | 25316         43   316.6667      2        6      1     2      0        0      0 |
             |---------------------------------------------------------------------------------|
         45. | 25316         45        950      1        7      0    20      1        3      0 |
         46. | 25316         46   1597.222      2        7      0    20      2        3      0 |
         47. | 25316         47    2273.09      3        7      0    20      3        3      0 |
         48. | 25316         48    2953.94      4        7      0    20      4        3      0 |
         49. | 25316         49   3411.617      5        7      0    20      5        3      0 |
         50. | 25316         50   2878.552      6        7      0    20      6        3      0 |
         51. | 25316         51   2345.486      7        7      0    20      7        3      0 |
         52. | 25316         52   2912.421      8        7      0    20      8        3      0 |
         53. | 25316         53   3405.667      9        7      0    20      9        3      0 |
         54. | 25316         54   3350.731     10        7      0    20     10        3      0 |
         55. | 25316         55    3806.36     11        7      0    20     11        3      0 |
         56. | 25316         56   4230.675     12        7      0    20     12        3      0 |
         57. | 25316         57   4133.311     13        7      0    20     13        3      0 |
         58. | 25316         58   4551.996     14        7      0    20     14        3      0 |
         59. | 25316         59   3848.079     15        7      0    20     15        3      0 |
         60. | 25316         60   3144.162     16        7      0    20     16        3      0 |
         61. | 25316         61   2440.245     17        7      0    20     17        3      0 |
         62. | 25316         62   1736.328     18        7      0    20     18        3      0 |
         63. | 25316         63   1032.411     19        7      0    20     19        3      0 |
         64. | 25316         64   328.4945     20        7      1    20     20        3      1 |
             |---------------------------------------------------------------------------------|
         68. | 25316         68        225      1        8      0     4      1        4      0 |
         69. | 25316         69        645      2        8      0     4      2        4      0 |
         70. | 25316         70   528.2979      3        8      0     4      3        4      0 |
         71. | 25316         71   191.0865      4        8      1     4      4        4      1 |
             +---------------------------------------------------------------------------------+

        Comment


        • #5
          Thank you Prof. Goldstein and Prof. Cox. Following Prof. Goldstein's message I explored -tsspell- and wrote the following, which seems to work.

          Code:
          tsspell y, fcond((y>0 & L1.y==0)| (y==0 & L1.y>0)) 
          replace _seq=0 if y==0
          bysort ID _spell: egen ep_length=max(_seq)
          recode ep_length (0/2=0)
          rename (_spell _seq _end) (spell1 seq1 end1)
          tsspell ep_length, fcond(ep_length>0 & spell1~=L1.spell1)  
          gen episode=_spell
          replace episode=0 if ep_length==0
          drop _end _seq end1 _spell
          But, Prof. Cox, your version is so much more elegant. Thank you so much! -tsspell- is a work of art!

          Comment

          Working...
          X