Announcement

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

  • How to identify when a treatment switches between 0 and 1 multiple time in the sample period

    Dear All !
    I have a panel of states. In my data set, I have a dummy variable (treat) such that:
    treat = 1, if state i and j adopted the policy at timet
    = 0 Othewise
    treat is a post-treatment dummy which equal 1 when the policy was first adopted by two states and thereafter; and 0 for all the years before the adoption.
    However, for some dyadic state-pairs, the treat dummy is not stable, i.e., the two states are treated (treat=1) in one-period, untreated in other period (treat=0) and then again treated (treat=1) in some other period. I need to check for the state-pairs where such multiple (more than 1) switches in the treatment occur. I am posting the data example below, in the example the treat switches between 0 and 1 multiple time in the sample period within each dyadic pair. How can I identify such state-pairs where the treat switches between 0 and 1 more than once. Checking by scrolling would be a tedious exercise in this large panel data of mine.
    Any help would be highly appreciated ! Nick Cox Carlo Lazzaro


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str2(state_i state_j) int year byte treat
    "AB" "JK" 1970 0
    "AB" "JK" 1971 0
    "AB" "JK" 1972 0
    "AB" "JK" 1973 0
    "AB" "JK" 1974 1
    "AB" "JK" 1975 1
    "AB" "JK" 1976 1
    "AB" "JK" 1977 1
    "AB" "JK" 1978 1
    "AB" "JK" 1979 0
    "AB" "JK" 1980 0
    "AB" "JK" 1981 0
    "AB" "JK" 1982 0
    "AB" "JK" 1983 0
    "AB" "JK" 1984 0
    "AB" "JK" 1985 0
    "AB" "JK" 1986 1
    "AB" "JK" 1987 1
    "AB" "JK" 1988 1
    "AB" "JK" 1989 1
    "AB" "JK" 1990 1
    "AB" "TM" 1975 1
    "AB" "TM" 1976 1
    "AB" "TM" 1977 1
    "AB" "TM" 1978 0
    "AB" "TM" 1979 0
    "AB" "TM" 1980 0
    "AB" "TM" 1981 1
    "AB" "TM" 1982 1
    "AB" "TM" 1983 0
    "AB" "TM" 1984 0
    "AB" "TM" 1985 0
    "AB" "TM" 1986 1
    "AB" "TM" 1987 1
    "AB" "TM" 1988 1
    end

  • #2
    Use tsspell from SSC to count spells that are either zero or one. Then you're looking for pairs with 3 or more such spells.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str2(state_i state_j) int year byte treat
    "AB" "JK" 1970 0
    "AB" "JK" 1971 0
    "AB" "JK" 1972 0
    "AB" "JK" 1973 0
    "AB" "JK" 1974 1
    "AB" "JK" 1975 1
    "AB" "JK" 1976 1
    "AB" "JK" 1977 1
    "AB" "JK" 1978 1
    "AB" "JK" 1979 0
    "AB" "JK" 1980 0
    "AB" "JK" 1981 0
    "AB" "JK" 1982 0
    "AB" "JK" 1983 0
    "AB" "JK" 1984 0
    "AB" "JK" 1985 0
    "AB" "JK" 1986 1
    "AB" "JK" 1987 1
    "AB" "JK" 1988 1
    "AB" "JK" 1989 1
    "AB" "JK" 1990 1
    "AB" "TM" 1975 1
    "AB" "TM" 1976 1
    "AB" "TM" 1977 1
    "AB" "TM" 1978 0
    "AB" "TM" 1979 0
    "AB" "TM" 1980 0
    "AB" "TM" 1981 1
    "AB" "TM" 1982 1
    "AB" "TM" 1983 0
    "AB" "TM" 1984 0
    "AB" "TM" 1985 0
    "AB" "TM" 1986 1
    "AB" "TM" 1987 1
    "AB" "TM" 1988 1
    end
    
    egen id = group(state_*), label
    tsset id year
    tsspell treat
    
    egen nspells = max(_spell), by(id)
    
    tabdisp id, c(nspells)
    ----------------------
    group(sta |
    te_i      |
    state_j)  |    nspells
    ----------+-----------
        AB JK |          4
        AB TM |          5
    ----------------------
    But you don't really need tsspell here, although it may be convenient. You could just count spells by

    Code:
    bysort state_* (year) : gen counter = sum(treat != treat[_n-1]) 
    by state_* : replace counter = counter[_N]
    Last edited by Nick Cox; 06 Feb 2023, 02:11.

    Comment


    • #3
      Thanks Nick Cox !
      The last lines of codes which you sent worked fine. However, there is one issue, not necessarily with the codes but with the data in my case.
      The 'treat' dummy has missing values for some years; the counter counts the missing values as a switch. For example;
      Code:
      if treat = 0, from t1-t5;
              =  missing, from t6-t10
              = 0, from t11-t15
              = 1, from t16-tn
      In this case the counter reports the number of switches in the treatment as 4. If missing observations are replaced by zero, in that case counter gives the correct number of switches in the treatment as 2.

      regards,
      (Ridwan)

      Comment


      • #4
        A somewhat errant question, but if you are sure the missing values in your dataset are zeros, then why are they missing values? You might include a line to correct this issue, or to assign probability-based values, based on treatment before and after the missing values. Again, this comment is probably outside the scope of your question.

        Comment


        • #5
          I agree with Eric Makela in #4.

          There are various solutions depending on what you want to do with missings.

          One is to ignore them in counting spells, another is to compare being 1 with not being 1, and so on.

          I am not clear which way you are leaning, but given a clear signal, code suggestions may follow.

          Comment


          • #6
            Thanks Nick Cox and Eric Makela, that is correct, the missing values must be dealt with sufficient care. My interpretation of replacing them with zero may be related to theory, provided some assumptions hold.
            If I want to ignore the missing values in counting spells, how should i modiify the code? i.e., in the case below, i should get number of spells as 2 not 4
            Code:
            if treat = 0, from t1-t5;
                        = missing, from t6-t10
                        = 0, from t11-t15
                         = 1, from t16-tn
            Thanks,
            (Ridwan)

            Comment


            • #7
              Code:
              clear 
              set obs 25
              gen id = 1 
              gen time = _n 
              gen treat = 0 if time <= 5 | inrange(time, 11, 15)
              replace treat = . if inrange(time, 6, 10) 
              replace treat = 1 if time > 15 
              tsset id time 
              
              gen which = treat == 1 
              tsspell which
              
              list, sepby(which)
              
                   +--------------------------------------------------+
                   | id   time   treat   which   _spell   _seq   _end |
                   |--------------------------------------------------|
                1. |  1      1       0       0        1      1      0 |
                2. |  1      2       0       0        1      2      0 |
                3. |  1      3       0       0        1      3      0 |
                4. |  1      4       0       0        1      4      0 |
                5. |  1      5       0       0        1      5      0 |
                6. |  1      6       .       0        1      6      0 |
                7. |  1      7       .       0        1      7      0 |
                8. |  1      8       .       0        1      8      0 |
                9. |  1      9       .       0        1      9      0 |
               10. |  1     10       .       0        1     10      0 |
               11. |  1     11       0       0        1     11      0 |
               12. |  1     12       0       0        1     12      0 |
               13. |  1     13       0       0        1     13      0 |
               14. |  1     14       0       0        1     14      0 |
               15. |  1     15       0       0        1     15      1 |
                   |--------------------------------------------------|
               16. |  1     16       1       1        2      1      0 |
               17. |  1     17       1       1        2      2      0 |
               18. |  1     18       1       1        2      3      0 |
               19. |  1     19       1       1        2      4      0 |
               20. |  1     20       1       1        2      5      0 |
               21. |  1     21       1       1        2      6      0 |
               22. |  1     22       1       1        2      7      0 |
               23. |  1     23       1       1        2      8      0 |
               24. |  1     24       1       1        2      9      0 |
               25. |  1     25       1       1        2     10      1 |
                   +--------------------------------------------------+

              Comment


              • #8
                Thank you very much, Nick Cox; this was very helpful.

                Comment

                Working...
                X