Announcement

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

  • lags and leads for panel data, where an individual does an action multiple times during observational period

    I have an issue of not accurately reflecting the lag/lead variables if an individual migrates multiple times during the observational period.
    I am using stata 15.0

    pid = ID number of the individual, wave = time, M0 = If an individual migrated in that wave

    I have made dummy variables for 3 lags and 3 leads for the act of migration, this is the code that I used.

    Code:
    tsset pid wave
    gen p = wave if M0 ==1
    bys pid: egen time = max(p)
    gen timelength = time - wave
    
    gen lead1=1 if timelength ==1
    replace lead1=0 if lead1 != 1
    gen lead2=1 if timelength == 2
    replace lead2=0 if lead2 != 1
    gen lead3=1 if timelength == 3
    replace lead3=0 if lead3 != 1
    gen lead4=1 if timelength == 4
    replace lead4=0 if lead4 != 1
    gen lead5=1 if timelength == 5
    replace lead5=0 if lead5 != 1
    gen lead6=1 if timelength == 2
    replace lead6=0 if lead6 != 1
    gen lead7=1 if timelength == 2
    replace lead7=0 if lead7 != 1
    
    
    gen lag1= 1 if timelength == -1
    replace lag1 = 0 if lag1 != 1
    gen lag2= 1 if timelength == -2
    replace lag2 = 0 if lag2 != 1
    gen lag3= 1 if timelength == -3
    replace lag3 = 0 if lag3 != 1
    gen lag4= 1 if timelength == -4
    replace lag4 = 0 if lag4 != 1
    gen lag5= 1 if timelength == -5
    replace lag5 = 0 if lag5 != 1

    This is it working as intended:
    HTML Code:
         +-----------------------------------------------------------------------------------------+
         |      pid   wave   M0   p   time   timele~h   lead1   lead2   lead3   lag1   lag2   lag3 |
         |-----------------------------------------------------------------------------------------|
    131. | 10029133     12    0   .      .          .       0       0       0      0      0      0 |
    132. | 10040331      6    0   .      7          1       1       0       0      0      0      0 |
    133. | 10040331      7    1   7      7          0       0       0       0      0      0      0 |
    134. | 10040331      8    0   .      7         -1       0       0       0      1      0      0 |
    135. | 10040331      9    0   .      7         -2       0       0       0      0      1      0 |
         |-----------------------------------------------------------------------------------------|
    136. | 10040331     10    0   .      7         -3       0       0       0      0      0      1 |
    137. | 10040331     11    0   .      7         -4       0       0       0      0      0      0 |
    138. | 10040331     12    0   .      7         -5       0       0       0      0      0      0 |
    139. | 10040331     13    0   .      7         -6       0       0       0      0      0      0 |
    140. | 10040331     14    0   .      7         -7       0       0       0      0      0      0 |
         |-----------------------------------------------------------------------------------------|
    141. | 10040331     15    0   .      7         -8       0       0       0      0      0      0 |
    142. | 10040331     16    0   .      7         -9       0       0       0      0      0      0 |
    143. | 10040331     17    0   .      7        -10       0       0       0      0      0      0 |
    144. | 10040331     18    0   .      7        -11       0       0       0      0      0      0 |
    145. | 10040366      6    0   .      .          .       0       0       0      0      0      0 |
         |-----------------------------------------------------------------------------------------|
    146. | 10040366      7    0   .      .          .       0       0       0      0      0      0 |
    147. | 10040366      8    0   .      .          .       0       0       0      0      0      0 |
    148. | 10040366     10    0   .      .          .       0       0       0      0      0      0 |
    149. | 10040366     11    0   .      .          .       0       0       0      0      0      0 |
       


    But when there are multiple acts of migration, it only takes account of the last migration of the individual
    HTML Code:
         +------------------------------------------------------------------------------------------+
         |      pid   wave   M0    p   time   timele~h   lead1   lead2   lead3   lag1   lag2   lag3 |
         |------------------------------------------------------------------------------------------|
    210. | 10048243      6    0    .     15          9       0       0       0      0      0      0 |
    211. | 10048243      7    0    .     15          8       0       0       0      0      0      0 |
    212. | 10048243      8    0    .     15          7       0       0       0      0      0      0 |
    213. | 10048243     10    0    .     15          5       0       0       0      0      0      0 |
    214. | 10048243     11    0    .     15          4       0       0       0      0      0      0 |
         |------------------------------------------------------------------------------------------|
    215. | 10048243     12    0    .     15          3       0       0       1      0      0      0 |
    216. | 10048243     13    1   13     15          2       0       1       0      0      0      0 |
    217. | 10048243     14    0    .     15          1       1       0       0      0      0      0 |
    218. | 10048243     15    1   15     15          0       0       0       0      0      0      0 |
    219. | 10048243     16    0    .     15         -1       0       0       0      1      0      0 |
         |------------------------------------------------------------------------------------------|
    220. | 10048243     17    0    .     15         -2       0       0       0      0      1      0 |
    221. | 10048243     18    0    .     15         -3       0       0       0      0      0      1 |
    Is there a way to change the priority or should my code change in general?

  • #2
    This is a sequel to https://www.statalist.org/forums/for...ced-panel-data which immediately raises some suggestions.

    1. You should cross-reference relevant previous threads, in fact continue them if there is a progression.

    2. Clyde advised you to look at

    Code:
    help tsvarlist
    to understand lag operators and that really was good advice.

    3. The request to provide readable data examples using dataex still stands. Permanently. No-one can use your data example here without editing hackery which is easily avoided if you just use dataex as requested.

    ​​That said, t​​o understand your code here, I started to rewrite it. This block can be cut in two, as true or false conditions are automatically 1 if true and 0 if false.

    Code:
    gen lead1=1 if timelength ==1
    replace lead1=0 if lead1 != 1
    gen lead2=1 if timelength == 2
    replace lead2=0 if lead2 != 1
    gen lead3=1 if timelength == 3
    replace lead3=0 if lead3 != 1
    gen lead4=1 if timelength == 4
    replace lead4=0 if lead4 != 1
    gen lead5=1 if timelength == 5
    replace lead5=0 if lead5 != 1
    gen lead6=1 if timelength == 2
    replace lead6=0 if lead6 != 1
    gen lead7=1 if timelength == 2
    replace lead7=0 if lead7 != 1
    becomes

    Code:
    gen lead1= timelength == 1
    gen lead2= timelength == 2
    gen lead3= timelength == 3
    gen lead4= timelength == 4
    gen lead5= timelength == 5
    gen lead6= timelength == 2
    gen lead7= timelength == 2
    Now I don't understand what's going on. Shouldn't that be 1 2 3 4 5 6 7? Either it's a bug you should fix, or there's some special logic you need to explain.

    Comment


    • #3
      I think this is in the right format:

      The code working as intended version:
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input long pid float(wave M0 p time timelength lead1 lead2 lead3 lag1 lag2 lag3)
      10029133 12 0 . .   . 0 0 0 0 0 0
      10040331  6 0 . 7   1 1 0 0 0 0 0
      10040331  7 1 7 7   0 0 0 0 0 0 0
      10040331  8 0 . 7  -1 0 0 0 1 0 0
      10040331  9 0 . 7  -2 0 0 0 0 1 0
      10040331 10 0 . 7  -3 0 0 0 0 0 1
      10040331 11 0 . 7  -4 0 0 0 0 0 0
      10040331 12 0 . 7  -5 0 0 0 0 0 0
      10040331 13 0 . 7  -6 0 0 0 0 0 0
      10040331 14 0 . 7  -7 0 0 0 0 0 0
      10040331 15 0 . 7  -8 0 0 0 0 0 0
      10040331 16 0 . 7  -9 0 0 0 0 0 0
      10040331 17 0 . 7 -10 0 0 0 0 0 0
      10040331 18 0 . 7 -11 0 0 0 0 0 0
      10040366  6 0 . .   . 0 0 0 0 0 0
      10040366  7 0 . .   . 0 0 0 0 0 0
      10040366  8 0 . .   . 0 0 0 0 0 0
      10040366 10 0 . .   . 0 0 0 0 0 0
      10040366 11 0 . .   . 0 0 0 0 0 0
      end
      And the code showing the multiple observation for an individual:

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input long pid float(wave M0 p time timelength lead1 lead2 lead3 lag1 lag2 lag3)
      10048243  6 0  . 15  9 0 0 0 0 0 0
      10048243  7 0  . 15  8 0 0 0 0 0 0
      10048243  8 0  . 15  7 0 0 0 0 0 0
      10048243 10 0  . 15  5 0 0 0 0 0 0
      10048243 11 0  . 15  4 0 0 0 0 0 0
      10048243 12 0  . 15  3 0 0 1 0 0 0
      10048243 13 1 13 15  2 0 1 0 0 0 0
      10048243 14 0  . 15  1 1 0 0 0 0 0
      10048243 15 1 15 15  0 0 0 0 0 0 0
      10048243 16 0  . 15 -1 0 0 0 1 0 0
      10048243 17 0  . 15 -2 0 0 0 0 1 0
      10048243 18 0  . 15 -3 0 0 0 0 0 1
      end

      Comment


      • #4
        Sorry, but I remain confused on what you are asking.

        The previous thread cited in #2 made standard suggestions about creating leading and lagged variables using lead and lag operators after a tsset

        Either you want something extra, it seems, or you want to approach it differently, perhaps. I still don't know.

        You have provided a brief data example in requested form, but otherwise you haven't answered any of the questions in #2.

        Ignoring the singleton panel 10029133 this takes your example panels and shows you can create shifted versions of M0 for p(revious) and n(ext) waves.

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input long pid float(wave M0 p)
        10040331  6 0  .
        10040331  7 1  7
        10040331  8 0  .
        10040331  9 0  .
        10040331 10 0  .
        10040331 11 0  .
        10040331 12 0  .
        10040331 13 0  .
        10040331 14 0  .
        10040331 15 0  .
        10040331 16 0  .
        10040331 17 0  .
        10040331 18 0  .
        10040366  6 0  .
        10040366  7 0  .
        10040366  8 0  .
        10040366 10 0  .
        10040366 11 0  .
        10048243  6 0  .
        10048243  7 0  .
        10048243  8 0  .
        10048243 10 0  .
        10048243 11 0  .
        10048243 12 0  .
        10048243 13 1 13
        10048243 14 0  .
        10048243 15 1 15
        10048243 16 0  .
        10048243 17 0  .
        10048243 18 0  .
        end
        
         tsset pid wave 
         
        forval j = 3(-1)1 { 
              gen pM0`j' = L`j'.M0 
         }
         
        forval j = 1/3 { 
             gen nM0`j' = F`j'.M0 
        } 
        
         list , sepby(pid) 
        
             +---------------------------------------------------------------------+
             |      pid   wave   M0    p   pM03   pM02   pM01   nM01   nM02   nM03 |
             |---------------------------------------------------------------------|
          1. | 10040331      6    0    .      .      .      .      1      0      0 |
          2. | 10040331      7    1    7      .      .      0      0      0      0 |
          3. | 10040331      8    0    .      .      0      1      0      0      0 |
          4. | 10040331      9    0    .      0      1      0      0      0      0 |
          5. | 10040331     10    0    .      1      0      0      0      0      0 |
          6. | 10040331     11    0    .      0      0      0      0      0      0 |
          7. | 10040331     12    0    .      0      0      0      0      0      0 |
          8. | 10040331     13    0    .      0      0      0      0      0      0 |
          9. | 10040331     14    0    .      0      0      0      0      0      0 |
         10. | 10040331     15    0    .      0      0      0      0      0      0 |
         11. | 10040331     16    0    .      0      0      0      0      0      . |
         12. | 10040331     17    0    .      0      0      0      0      .      . |
         13. | 10040331     18    0    .      0      0      0      .      .      . |
             |---------------------------------------------------------------------|
         14. | 10040366      6    0    .      .      .      .      0      0      . |
         15. | 10040366      7    0    .      .      .      0      0      .      0 |
         16. | 10040366      8    0    .      .      0      0      .      0      0 |
         17. | 10040366     10    0    .      0      0      .      0      .      . |
         18. | 10040366     11    0    .      0      .      0      .      .      . |
             |---------------------------------------------------------------------|
         19. | 10048243      6    0    .      .      .      .      0      0      . |
         20. | 10048243      7    0    .      .      .      0      0      .      0 |
         21. | 10048243      8    0    .      .      0      0      .      0      0 |
         22. | 10048243     10    0    .      0      0      .      0      0      1 |
         23. | 10048243     11    0    .      0      .      0      0      1      0 |
         24. | 10048243     12    0    .      .      0      0      1      0      1 |
         25. | 10048243     13    1   13      0      0      0      0      1      0 |
         26. | 10048243     14    0    .      0      0      1      1      0      0 |
         27. | 10048243     15    1   15      0      1      0      0      0      0 |
         28. | 10048243     16    0    .      1      0      1      0      0      . |
         29. | 10048243     17    0    .      0      1      0      0      .      . |
         30. | 10048243     18    0    .      1      0      0      .      .      . |
             +---------------------------------------------------------------------+
        If that isn't what you want, I think you'll need to explain much more, but I will just back off if I can't follow you. Naturally, others may be able to help.

        Comment


        • #5
          Thank you, that has solved the issue

          Comment

          Working...
          X