Announcement

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

  • Count Number of Swithces before and after Consecituve Observations

    I have a panel and would like to count how often a unit changes a variable from 0 to 1 or 1 to 0 after being the same for a certain number of months before the switch and after the switch. Let's say for three months. Later I will want to be flexible about it and change it. Here is an example how the data looks like:
    id month switch
    1 1 0
    1 2 0
    1 3 1
    1 4 0
    1 5 0
    1 6 0
    1 7 1
    1 8 1
    1 9 1
    1 10 0
    1 11 0
    1 12 0
    In this case there are two switches. One from 0 to 1 and one from 1 to 0.

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(id month switch)
    1  1 0
    1  2 0
    1  3 1
    1  4 0
    1  5 0
    1  6 0
    1  7 1
    1  8 1
    1  9 1
    1 10 0
    1 11 0
    1 12 0
    end
    
    
    by id (month), sort: gen spell = sum(switch != switch[_n-1])
    by id spell (month), sort: gen duration = _N
    by id (month), sort: egen wanted = ///
        total(spell != spell[_n-1] & duration[_n-1] >= 3 & _n != 1)

    Comment


    • #3
      Clyde thanks a lot! I just have two more questions about how I would adjust your code if

      1. I wanted to have the number of months to be different before and after the switch. Let's say 3 before and 4 after the switch?

      2. I wanted to know if the switch is going from 0 to 1 or from 1 to 0?

      Comment


      • #4
        Here is some technique. You can work from first principles, as Clyde Schechter showed clearly, or you can use helper commands, or do both.

        Here tsspell is from SSC and the principles were explained in https://journals.sagepub.com/doi/pdf...867X0700700209

        The reason tsspell is not mentioned in that paper is mundane. The paper was already quite long and overdue and so I stopped short of adding sections on the new command. The imagined sequel never got written, and a good reason for that is the help file should be sufficient for anyone experimenting to see what happens. Or so I hope.


        Code:
        clear 
        input byte(id month switch)
        1  1 0
        1  2 0
        1  3 1
        1  4 0
        1  5 0
        1  6 0
        1  7 1
        1  8 1
        1  9 1
        1 10 0
        1 11 0
        1 12 0
        end
        
        tsset id month 
        
        tsspell switch 
        
        list, sepby(id _spell)
        
        bysort id _spell (_seq) : egen length  = max(_seq) 
        
        gen length_prev = cond(_seq == 1, length[_n-1], .) 
        gen length_post = cond(_seq[_n+1] == 1, length[_n+1], .) 
        
        egen spell_length_prev = max(length_prev), by(id _spell)
        egen spell_length_post = max(length_post), by(id _spell)
        
        egen switch01 = total(cond(_seq ==1, switch[_n-1] == 0, .)), by(id)
        
        egen switch10 = total(cond(_seq ==1, switch[_n-1] == 1, .)), by(id)
        
        list, sepby(id _spell) 
        
             +-----------------------------------------------------------------------------------------------------------------------+
             | id   month   switch   _spell   _seq   _end   length   length~v   length~t   spell_~v   spell_~t   switch01   switch10 |
             |-----------------------------------------------------------------------------------------------------------------------|
          1. |  1       1        0        1      1      0        2          .          .          .          1          2          2 |
          2. |  1       2        0        1      2      1        2          .          1          .          1          2          2 |
             |-----------------------------------------------------------------------------------------------------------------------|
          3. |  1       3        1        2      1      1        1          2          3          2          3          2          2 |
             |-----------------------------------------------------------------------------------------------------------------------|
          4. |  1       4        0        3      1      0        3          1          .          1          3          2          2 |
          5. |  1       5        0        3      2      0        3          .          .          1          3          2          2 |
          6. |  1       6        0        3      3      1        3          .          3          1          3          2          2 |
             |-----------------------------------------------------------------------------------------------------------------------|
          7. |  1       7        1        4      1      0        3          3          .          3          3          2          2 |
          8. |  1       8        1        4      2      0        3          .          .          3          3          2          2 |
          9. |  1       9        1        4      3      1        3          .          3          3          3          2          2 |
             |-----------------------------------------------------------------------------------------------------------------------|
         10. |  1      10        0        5      1      0        3          3          .          3          .          2          2 |
         11. |  1      11        0        5      2      0        3          .          .          3          .          2          2 |
         12. |  1      12        0        5      3      1        3          .          .          3          .          2          2 |
             +-----------------------------------------------------------------------------------------------------------------------+
        In words:

        Spells here are just defined as runs of the same value.

        _spell counts spells for each identifier

        _seq counts observations for each spell.

        So, the length of each spell is the last value of _seq for that spell.

        To get the length of the previous spell, we take the first observation in a spell and look at the previous observation.

        To get the length of the following spell, we take the last observation in a spell and look at the next observation.

        And so on. A boundary problem is that nothing precedes the first spell. Note that a reference to varname[0] -- whether implicit or explicit -- is not an error but returns missing.

        Comment


        • #5
          Re #3: for these additional complications, I think you are better off using Nick Cox's approach than mine. I could spell out modifications to the code in #2, but they are sufficiently lengthy that I think the use of -tsspell- is warranted to "hide" that complexity.

          Comment

          Working...
          X