Announcement

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

  • Generate Leads and Lags

    I would like to create leads and lags to control for the last two years before a pollical leader came to power and the first two years after they were out of power. Since I am using a Two Part Model I would like to create these as variables instead of using L2. or F2. I am using Stata 15.1.

    I would like to do this within a loop so that a '1' is placed in the 2 years before n_power1. However, this isn't applicable to all leaders, because some leaders were in power before the analysis timeframe of 2000-2011. The dataset below is a subset of data that includes 2 political leaders.

    I have tried several iterations that have not worked, most recently:
    Code:
    gen lag_birth=1 if n2_power1<n2_power1[_n-2]
    Any suggestions are appreciated.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int n_year float(n_birth leader_id n2_power1 n2_power2)
    2000 1 1 1981 2011
    2001 1 1 1981 2011
    2002 1 1 1981 2011
    2003 1 1 1981 2011
    2004 1 1 1981 2011
    2005 1 1 1981 2011
    2006 1 1 1981 2011
    2007 1 1 1981 2011
    2008 1 1 1981 2011
    2009 1 1 1981 2011
    2010 1 1 1981 2011
    2011 1 1 1981 2011
    2000 0 9 2009 2011
    2001 0 9 2009 2011
    2002 0 9 2009 2011
    2003 0 9 2009 2011
    2004 0 9 2009 2011
    2005 0 9 2009 2011
    2006 0 9 2009 2011
    2007 0 9 2009 2011
    2008 0 9 2009 2011
    2009 1 9 2009 2011
    end




  • #2
    I'm not sure I fully understand what you want, but it may be something like this:
    Code:
    bysort leader_id (n_year): gen before = inrange(n_year, n2_power1 - 2, n2_power1 - 1)
    bysort leader_id (n_year): gen after = inrange(n_year, n2_power2 + 1, n2_power2 + 2)

    Comment


    • #3
      Yes. That works. Thank you.

      Comment


      • #4
        I'm following up on my previous post. I was mistaken and the previous code didn't work. I am trying to create leads/lags for the 2 years before and 2 years after a leader was in political power. I have posted additional data below. The data set is a year region (ADM1) panel and therefore not all years/regions have a leader. Thanks in advance for any suggestions

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input int n_year float(leader_id n2_power1 n2_power2) str43 n_NAME_1 str32 n_NAME_0
        2000 70 1984 2005 "Adrar Mauritania"   "Mauritania"
        2001 70 1984 2005 "Adrar Mauritania"   "Mauritania"
        2002 70 1984 2005 "Adrar Mauritania"   "Mauritania"
        2003 70 1984 2005 "Adrar Mauritania"   "Mauritania"
        2004 70 1984 2005 "Adrar Mauritania"   "Mauritania"
        2005 70 1984 2005 "Adrar Mauritania"   "Mauritania"
        2006 70 1984 2005 "Adrar Mauritania"   "Mauritania"
        2007 70 1984 2005 "Adrar Mauritania"   "Mauritania"
        2008 70 1984 2005 "Adrar Mauritania"   "Mauritania"
        2009 70 1984 2005 "Adrar Mauritania"   "Mauritania"
        2010 70 1984 2005 "Adrar Mauritania"   "Mauritania"
        2011 70 1984 2005 "Adrar Mauritania"   "Mauritania"
        2000 74 2011 2011 "Al Jabal Al Akhdar" "Libya"    
        2001 74 2011 2011 "Al Jabal Al Akhdar" "Libya"    
        2002 74 2011 2011 "Al Jabal Al Akhdar" "Libya"    
        2003 74 2011 2011 "Al Jabal Al Akhdar" "Libya"    
        2004 74 2011 2011 "Al Jabal Al Akhdar" "Libya"    
        2005 74 2011 2011 "Al Jabal Al Akhdar" "Libya"    
        2006 74 2011 2011 "Al Jabal Al Akhdar" "Libya"    
        2007 74 2011 2011 "Al Jabal Al Akhdar" "Libya"    
        2008 74 2011 2011 "Al Jabal Al Akhdar" "Libya"    
        2009 74 2011 2011 "Al Jabal Al Akhdar" "Libya"    
        2010 74 2011 2011 "Al Jabal Al Akhdar" "Libya"    
        2011 74 2011 2011 "Al Jabal Al Akhdar" "Libya"    
        2000  1 1981 2011 "Al Minufiyah"       "Egypt"    
        2001  1 1981 2011 "Al Minufiyah"       "Egypt"    
        2002  1 1981 2011 "Al Minufiyah"       "Egypt"    
        2003  1 1981 2011 "Al Minufiyah"       "Egypt"    
        2004  1 1981 2011 "Al Minufiyah"       "Egypt"    
        2005  1 1981 2011 "Al Minufiyah"       "Egypt"    
        2006  1 1981 2011 "Al Minufiyah"       "Egypt"    
        2007  1 1981 2011 "Al Minufiyah"       "Egypt"    
        2008  1 1981 2011 "Al Minufiyah"       "Egypt"    
        2009  1 1981 2011 "Al Minufiyah"       "Egypt"    
        2010  1 1981 2011 "Al Minufiyah"       "Egypt"    
        2011  1 1981 2011 "Al Minufiyah"       "Egypt"    
        2000 43 2011 2011 "Al Qahirah 2"       "Egypt"    
        2001 43 2011 2011 "Al Qahirah 2"       "Egypt"    
        2002 43 2011 2011 "Al Qahirah 2"       "Egypt"    
        2003 43 2011 2011 "Al Qahirah 2"       "Egypt"    
        2004 43 2011 2011 "Al Qahirah 2"       "Egypt"    
        2005 43 2011 2011 "Al Qahirah 2"       "Egypt"    
        2006 43 2011 2011 "Al Qahirah 2"       "Egypt"    
        2007 43 2011 2011 "Al Qahirah 2"       "Egypt"    
        2008 43 2011 2011 "Al Qahirah 2"       "Egypt"    
        2009 43 2011 2011 "Al Qahirah 2"       "Egypt"    
        2010 43 2011 2011 "Al Qahirah 2"       "Egypt"    
        2011 43 2011 2011 "Al Qahirah 2"       "Egypt"    
        2000 71 1994 2011 "Amajyepfo"          "Rwanda"   
        2001 71 1994 2011 "Amajyepfo"          "Rwanda"   
        2002 71 1994 2011 "Amajyepfo"          "Rwanda"   
        2003 71 1994 2011 "Amajyepfo"          "Rwanda"   
        2004 71 1994 2011 "Amajyepfo"          "Rwanda"   
        2005 71 1994 2011 "Amajyepfo"          "Rwanda"   
        2006 71 1994 2011 "Amajyepfo"          "Rwanda"   
        2007 71 1994 2011 "Amajyepfo"          "Rwanda"   
        2008 71 1994 2011 "Amajyepfo"          "Rwanda"   
        2009 71 1994 2011 "Amajyepfo"          "Rwanda"   
        2010 71 1994 2011 "Amajyepfo"          "Rwanda"   
        2011 71 1994 2011 "Amajyepfo"          "Rwanda"   
        2000 27 1993 2011 "Anseba"             "Eritrea"  
        2001 27 1993 2011 "Anseba"             "Eritrea"  
        2002 27 1993 2011 "Anseba"             "Eritrea"  
        2003 27 1993 2011 "Anseba"             "Eritrea"  
        2004 27 1993 2011 "Anseba"             "Eritrea"  
        2005 27 1993 2011 "Anseba"             "Eritrea"  
        2006 27 1993 2011 "Anseba"             "Eritrea"  
        2007 27 1993 2011 "Anseba"             "Eritrea"  
        2008 27 1993 2011 "Anseba"             "Eritrea"  
        2009 27 1993 2011 "Anseba"             "Eritrea"  
        2010 27 1993 2011 "Anseba"             "Eritrea"  
        2011 27 1993 2011 "Anseba"             "Eritrea"  
        2000 34 2002 2008 "Antananarivo"       "Madagascar"
        2001 34 2002 2008 "Antananarivo"       "Madagascar"
        2002 34 2002 2008 "Antananarivo"       "Madagascar"
        2003 34 2002 2008 "Antananarivo"       "Madagascar"
        2004 34 2002 2008 "Antananarivo"       "Madagascar"
        2005 34 2002 2008 "Antananarivo"       "Madagascar"
        2006 34 2002 2008 "Antananarivo"       "Madagascar"
        2007 34 2002 2008 "Antananarivo"       "Madagascar"
        2008 34 2002 2008 "Antananarivo"       "Madagascar"
        2009 81 2009 2011 "Antananarivo"       "Madagascar"
        2010 81 2009 2011 "Antananarivo"       "Madagascar"
        2011 81 2009 2011 "Antananarivo"       "Madagascar"
        2000 57 2001 2009 "Ashanti"            "Ghana"    
        2001 57 2001 2009 "Ashanti"            "Ghana"    
        2002 57 2001 2009 "Ashanti"            "Ghana"    
        2003 57 2001 2009 "Ashanti"            "Ghana"    
        2004 57 2001 2009 "Ashanti"            "Ghana"    
        2005 57 2001 2009 "Ashanti"            "Ghana"    
        2006 57 2001 2009 "Ashanti"            "Ghana"    
        2007 57 2001 2009 "Ashanti"            "Ghana"    
        2008 57 2001 2009 "Ashanti"            "Ghana"    
        2009 57 2001 2009 "Ashanti"            "Ghana"    
        2010 57 2001 2009 "Ashanti"            "Ghana"    
        2011 57 2001 2009 "Ashanti"            "Ghana"    
        2000 24 1996 2006 "Atakora"            "Benin"    
        2001 24 1996 2006 "Atakora"            "Benin"    
        2002 24 1996 2006 "Atakora"            "Benin"    
        2003 24 1996 2006 "Atakora"            "Benin"    
        end

        Comment


        • #5
          I'm still guessing, but maybe:
          Code:
          bysort n_NAME_1 (n_year): gen leader_change_flag = n2_power1 != n2_power1[_n-1] & _n > 1
          bysort n_NAME_1 (n_year): gen before = leader_change_flag[_n+1] == 1 | leader_change_flag[_n+2] == 1
          bysort n_NAME_1 (n_year): gen after = leader_change_flag == 1 | leader_change_flag[_n-1] == 1
          This assumes that you have no year gaps in your data, which holds for your data example at least.

          If this is not what you wanted I think you will increase your chances of getting a helpful reply if you post an example of how you want your desired variables to look, using a small part of your example data.

          Comment


          • #6
            Thank you. This isn't quite it so I followed your suggestion and manually created the intended outcome below. I am trying to create a variable that controls for the 2 years before and 2 years after a leader is in political power. I do have a balanced 2000-2014 panel.

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input float(leader_id before after) int n_year float(n2_power1 n2_power2) str36 n_NAME_1 float n_birth int ADM1_ID
             1 0 0 2000 1984 2005 "Adrar Mauritania" 1 438
             1 0 0 2001 1984 2005 "Adrar Mauritania" 1 438
             1 0 0 2002 1984 2005 "Adrar Mauritania" 1 438
             1 0 0 2003 1984 2005 "Adrar Mauritania" 1 438
             1 0 0 2004 1984 2005 "Adrar Mauritania" 1 438
             1 0 0 2005 1984 2005 "Adrar Mauritania" 1 438
             . 0 1 2006    .    . "Adrar Mauritania" 0 438
             . 0 1 2007    .    . "Adrar Mauritania" 0 438
             . 0 0 2008    .    . "Adrar Mauritania" 0 438
             . 0 0 2009    .    . "Adrar Mauritania" 0 438
             . 0 0 2010    .    . "Adrar Mauritania" 0 438
             . 0 0 2011    .    . "Adrar Mauritania" 0 438
             . 0 0 2012    .    . "Adrar Mauritania" 0 438
             . 0 0 2013    .    . "Adrar Mauritania" 0 438
             . 0 0 2014    .    . "Adrar Mauritania" 0 438
             2 0 0 2000 1981 2011 "Al Minufiyah"     1 222
             2 0 0 2001 1981 2011 "Al Minufiyah"     1 222
             2 0 0 2002 1981 2011 "Al Minufiyah"     1 222
             2 0 0 2003 1981 2011 "Al Minufiyah"     1 222
             2 0 0 2004 1981 2011 "Al Minufiyah"     1 222
             2 0 0 2005 1981 2011 "Al Minufiyah"     1 222
             2 0 0 2006 1981 2011 "Al Minufiyah"     1 222
             2 0 0 2007 1981 2011 "Al Minufiyah"     1 222
             2 0 0 2008 1981 2011 "Al Minufiyah"     1 222
             2 0 0 2009 1981 2011 "Al Minufiyah"     1 222
             2 0 0 2010 1981 2011 "Al Minufiyah"     1 222
             2 0 0 2011 1981 2011 "Al Minufiyah"     1 222
             . 0 1 2012    .    . "Al Minufiyah"     0 222
             . 0 1 2013    .    . "Al Minufiyah"     0 222
             . 0 0 2014    .    . "Al Minufiyah"     1 222
             . 0 0 2000    .    . "Al Minya"         0 223
             . 0 0 2001    .    . "Al Minya"         0 223
             . 0 0 2002    .    . "Al Minya"         0 223
             . 0 0 2003    .    . "Al Minya"         0 223
             . 0 0 2004    .    . "Al Minya"         0 223
             . 0 0 2005    .    . "Al Minya"         0 223
             . 0 0 2006    .    . "Al Minya"         0 223
             . 0 0 2007    .    . "Al Minya"         0 223
             . 0 0 2008    .    . "Al Minya"         0 223
             . 0 0 2009    .    . "Al Minya"         0 223
             . 1 0 2010    .    . "Al Minya"         0 223
             . 1 0 2011    .    . "Al Minya"         0 223
            35 0 0 2012 2012 2013 "Al Minya"         1 223
            35 0 0 2013 2012 2013 "Al Minya"         1 223
             . 0 1 2014    .    . "Al Minya"         0 223
             . 0 0 2000    .    . "Al Qahirah 2"     0 225
             . 0 0 2001    .    . "Al Qahirah 2"     0 225
             . 0 0 2002    .    . "Al Qahirah 2"     0 225
             . 0 0 2003    .    . "Al Qahirah 2"     0 225
             . 0 0 2004    .    . "Al Qahirah 2"     0 225
             . 0 0 2005    .    . "Al Qahirah 2"     0 225
             . 0 0 2006    .    . "Al Qahirah 2"     0 225
             . 0 0 2007    .    . "Al Qahirah 2"     0 225
             . 0 0 2008    .    . "Al Qahirah 2"     0 225
             . 1 0 2009    .    . "Al Qahirah 2"     0 225
             . 1 0 2010    .    . "Al Qahirah 2"     0 225
             4 0 0 2011 2011 2012 "Al Qahirah 2"     1 225
             4 0 0 2012 2011 2012 "Al Qahirah 2"     1 225
             . 0 1 2013    .    . "Al Qahirah 2"     0 225
             . 0 1 2014    .    . "Al Qahirah 2"     0 225
            end
            ------------------ copy up to and including the previous line ------------------

            Comment


            • #7
              Your "leader_id" variable is not very useful as it does not correspond entirely to the variable "n_NAME_1". You will need to encode this variable to get a valid numerical identifier. Some work is needed prior to creating your variable.

              Code:
              bys n_NAME_1 (n2_power1): replace n2_power1 =n2_power1[1]
              bys n_NAME_1 (n2_power2): replace n2_power2 =n2_power2[1]
              bys n_NAME_1 (n_year): gen Before= n_year==n2_power1- 1|n_year==n2_power1- 2
              bys n_NAME_1 (n_year): gen After= n_year==n2_power2+ 1|n_year==n2_power2+ 2
              EDIT: This assumes that a leader has only one stint in power (continuous period). If you have leaders with more than one stint (e.g., Berlusconi in Italy), then you need to amend the code.
              Last edited by Andrew Musau; 09 Mar 2020, 09:06.

              Comment


              • #8
                While Andrew's skills and knowledge far exceed mine, I believe he has misread n_NAME_1 as being the name of the leader instead of the name of the region. Therefore the code above works only if there is only one leader in a region.

                This should work when there are more leaders:
                Code:
                gen Before = 0
                gen After = 0
                levelsof n_NAME_1, local(regions)
                foreach region of local regions {
                    levelsof n2_power1 if n_NAME_1 == "`region'" , local(fyears)
                    levelsof n2_power2 if n_NAME_1 == "`region'", local(lyears)
                    foreach fyear of local fyears {
                        replace Before = 1 if inrange(n_year, `fyear' - 2, `fyear' - 1) & n_NAME_1 == "`region'"
                    }
                    foreach lyear of local lyears {
                        replace After = 1 if inrange(n_year, `lyear' + 1, `lyear' + 2) & n_NAME_1 == "`region'"
                    }
                }

                Comment


                • #9
                  You are correct Wouter Wakker about me misreading the data, but this is very questionable.

                  While Andrew's skills and knowledge far exceed mine

                  Comment


                  • #10
                    Thank you both. I appreciate the input.

                    The dataset is a ADM1 (regional level)/year panel and the leader_id corresponds to the birth region of the current leader at that year. Therefore the leader_id is not continuous because there isn't a continuous leader from each region. Using the USA current political leader (Trump) as an example, NYC would get a 1 for 2017, 2018, 2019, 2020, but would have no leader_id 2019-2017 because Obama was born in Honolulu (which would get the leader_id for those years).

                    Andrew's code works for most of the sample, because very few leaders are from overlapping regions (continuing with my example above...back to back leaders from NYC). However, there are 6 African leaders successively from the same region and then Andrew's code isn't successful as Wouter points out.

                    I tried implementing Wouter's code and I get an error r(198) invalid 'Jazirah' the problem I think is the way I'm referencing my macros and that the code doesn't like regions with two names like Al Jazirah or Eastern Cameroon?

                    Comment


                    • #11
                      I don't quite understand why you get that error as it works for your data example which also contains region names with spaces. Anyway, substituting the region name for the region code should be a quick solution to the problem.
                      Code:
                      gen Before = 0
                      gen After = 0
                      levelsof ADM1_ID, local(regions)
                      foreach region of local regions {
                          levelsof n2_power1 if ADM1_ID == `region' , local(fyears)
                          levelsof n2_power2 if ADM1_ID == `region', local(lyears)
                          foreach fyear of local fyears {
                              replace Before = 1 if inrange(n_year, `fyear' - 2, `fyear' - 1) & ADM1_ID == `region'
                          }
                          foreach lyear of local lyears {
                              replace After = 1 if inrange(n_year, `lyear' + 1, `lyear' + 2) & ADM1_ID == `region'
                          }
                      }

                      Comment


                      • #12
                        Thank you. I now have the variable created that I need and have improved my understanding of Stata in the process. Much appreciated.

                        Comment

                        Working...
                        X