Announcement

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

  • Creating an Exposure variable which takes the maximum value of the treatment for past 3 years in my panel data

    I want to create an exposure variable for my Crime and Hurricane panel data, which takes the maximum windspeed faced by each county (identified by fips) in the past three years.

    Ex: HE_{it}^{1-3}, which measures hurricane exposure in county "i" over years "t-1" to "t-3".


    clear
    input float year double(fips Total_Crime vmax_sust)
    1990 1073 50341 2.207145436708933
    1991 1073 43327 1.4298561328562058
    1992 1073 31075 10.059897312223804
    1993 1073 26898 .8603758053600334
    1994 1073 31465 8.13925140831877
    1995 1073 36422 16.330129887041235
    1996 1073 44588 3.8827540761652655
    1997 1073 43983 9.63004820452538
    1998 1073 45008 8.246703646725713
    1999 1073 39536 2.080981290087113
    2000 1073 29515 4.8786239007139995
    2001 1073 29001 9.15131518725165
    2002 1073 30482 10.169097044645484
    2003 1073 29719 9.80479374331239
    2004 1073 30261 15.768005046950822
    2005 1073 25597 13.584126194746071
    2006 1073 31951 3.778665943390192
    2007 1073 32539 4.6328214418942935
    2008 1073 39169 10.282823130991767
    2009 1073 34068 7.089614383510564
    2010 1073 14727 5.784313211847036
    2011 1073 2699 7.849796936304541
    2012 1073 2530 4.2862176915251
    2013 1073 2767 3.295956254852688
    2014 1073 3879 1.738178888320849
    2015 1073 14370 2.7071619816506316
    2016 1073 18438 6.294396513693123
    2017 1073 19216 12.81953628665062
    2018 1073 19547 15.124462157506498
    1990 4001 762 0
    1991 4001 921 0
    1992 4001 798 0
    1993 4001 1198 0
    1994 4001 1233 0
    1995 4001 1464 0
    1996 4001 1418 0
    1997 4001 1382 0
    1998 4001 1475 0
    1999 4001 1307 0
    2000 4001 1465 0
    2001 4001 1158 0
    2002 4001 1520 0
    2003 4001 1581 0
    2004 4001 1029 0
    2005 4001 1285 0
    2006 4001 1332 0
    2007 4001 430 0
    2008 4001 485 0
    2009 4001 638 0
    2010 4001 590 0
    2011 4001 611 0
    2012 4001 692 0
    2013 4001 1173 0
    2014 4001 1357 0
    2015 4001 22649 0
    2016 4001 25226 0
    2017 4001 28367 0
    2018 4001 24743 0
    1990 4003 6999 0
    1991 4003 7236 0
    1992 4003 5873 0
    1993 4003 6525 0
    1994 4003 5058 0
    1995 4003 5569 0
    1996 4003 5540 0
    1997 4003 8208 0
    1998 4003 8703 0
    1999 4003 8318 0
    2000 4003 7895 0
    2001 4003 7583 0
    2002 4003 7168 0
    2003 4003 9093 0
    2004 4003 8760 0
    2005 4003 9036 0
    2006 4003 6760 0
    2007 4003 7967 0
    2008 4003 7547 0
    2009 4003 8474 0
    2010 4003 7079 0
    2011 4003 6799 0
    2012 4003 6646 0
    2013 4003 3061 0
    2014 4003 4310 0
    2015 4003 4025 0
    2016 4003 5275 0
    2017 4003 5573 0
    2018 4003 5773 0
    1990 4005 9355 0
    1991 4005 8792 0
    1992 4005 10235 0
    1993 4005 10252 0
    1994 4005 11981 0
    1995 4005 14021 0
    1996 4005 17256 0
    1997 4005 20066 0
    1998 4005 13834 0
    1999 4005 14061 0
    2000 4005 12022 0
    2001 4005 13319 0
    2002 4005 13514 0
    end
    [/CODE]

  • #2
    I think the simplest way to do this is:
    Code:
    xtset fips year
    gen wanted = max(L1.vmax_sust, L2.vmax_sust, L3.vmax_sust)

    Comment


    • #3
      Thanks Clyde!
      As a continuation of the problem above, I want to create the following damage function. Can you please tell me how?

      Damage = [max(WindSpeed_{it} - 50, 0)^3] / max(Windspeed)^3

      I had tried the code below but it I get "unknown egen function"

      by fips, sort: egen Damage_Sust = ((max(vmax_sust-25.72,0)^3)/(max(vmax_sust)^3))

      Comment


      • #4
        .
        Last edited by Anupam Ghosh; 09 Dec 2023, 17:49.

        Comment


        • #5
          Hi Clyde,

          How can I adjust the code so that instead of capturing the maximum value it gives me the exposure values of the past three years? Thus, the following:

          HE_{it}^{1-3}; which measures windspeeds faced by each county "i" over years "t-1" to "t-3".

          Comment


          • #6
            Note that -egen- cannot do what you want, and the code offered in #2 does not mention -egen-. It uses -gen-. While there are similarities between the two, they are really quite different and their capabilities are, in a sense, complementary. Anyway,this problem is suited to -gen-, not -egen-.
            Code:
            xtset fips year
            gen wanted = (max(L1.vmax_sust, L2.vmax_sust, L3.vmax_sust))^3
            Also note that no -by- is necessary with this, because the use of the L operators following -xtset- will automatically cause the calculations to be done -by fips (year)-, and the -xtset- command itself presorts the data accordingly.

            Comment


            • #7
              Hi Clyde,

              I want to run a loop for a variable say X which takes the value 1 if L`i'.hurricane>25. I am using the following code, but the dummy variable does not get created.

              sort fips year
              xtset fips year

              forvalues i = 1/15{
              gen Hurr_Surface`i' = L`i'.vmax_sust //This chunk works
              }

              forvalues i = 1/3{
              by fips, sort: gen X =1 if max(Hurr_Surface`i')>25
              }

              Comment


              • #8
                a variable say X which takes the value 1 if L`i'.hurricane>25
                I don't understand what this means. What value(s) does i take here? In the code you show, you loop i from 1 to 3, but I cannot tell what your intent is here. Do you want to have X = 1 if all of Hurr_Surface1, Hurr_Surface2, and Hurr_Surface3 are > 25? Or if any of the three is > 25? Or if their average is > 25? Or if 2 of the three are? Please clarify what the intent is here.

                Comment


                • #9
                  Clyde, I want X to return 1 if any of the Hurr_Surface`i'>25.

                  Explanation: I have hurricane information from 1990-2018 in a balanced panel. I want to create three treatment effects H_1-3 (for t-1 to t-3; captures short-term), H_4-10 (for t-4 to t-10; captures medium-term), and H_11-15 (for t-11 to t-15; captures long-term). I have created 15 lag values for Hurr_Surface`i' (these are windspeeds). Now for every county I want to create three separate treatment effects, such that

                  sort fips year
                  xtset fips year

                  forvalues i = 1/15 {
                  gen Hurr_Surface`i' = L`i'.vmax_sust
                  replace Hurr_Surface`i' = 0 if Hurr_Surface`i' == .
                  }


                  forvalues i = 1/3{
                  gen H_1-3 =1 , if max(Hurr_Surface`i')>25
                  }

                  forvalues i = 4/10{
                  gen H_4-10 =1 , if max(Hurr_Surface`i')>25
                  }

                  forvalues i = 11/15{
                  gen H_11-15 =1 , if max(Hurr_Surface`i')>25
                  }

                  Comment


                  • #10
                    #Update

                    Here's a code that works, but my three treatment effects should be mutually exclusive which this is not the case with this code. I get 1's for the same row in two or more variables.

                    drop Hurr_Surface1- Surface_BaseDamage_t11_10

                    forvalues i = 1/20 {
                    gen Hurr_Surface`i' = L`i'.vmax_sust
                    replace Hurr_Surface`i' = 0 if Hurr_Surface`i' == .
                    }

                    by fips year, sort: gen Surface_BaseDamage_t1_3 = cond(max(Hurr_Surface1, Hurr_Surface2, Hurr_Surface3)>25,1,0)

                    by fips year, sort: gen Surface_BaseDamage_t4_10 = cond(max(Hurr_Surface4, Hurr_Surface5, Hurr_Surface6,Hurr_Surface7,Hurr_Surface8,Hurr_Sur face9,Hurr_Surface10)>25,1,0)

                    by fips year, sort: gen Surface_BaseDamage_t11_10 = cond(max(Hurr_Surface11, Hurr_Surface12,Hurr_Surface13,Hurr_Surface14,Hurr_ Surface15,Hurr_Surface16,Hurr_Surface17,Hurr_Surfa ce18,Hurr_Surface19,Hurr_Surface20)>25,1,0)

                    Comment


                    • #11
                      but my three treatment effects should be mutually exclusive which this is not the case with this code. I get 1's for the same row in two or more variables.
                      Well, there is absolutely nothing in the way you have constructed these variables that would cause them to be mutually exclusive. That would only be the case if, by design or by coincidence, the data had it turn out that way. But there is nothing in the design of these variables to support mutual exclusivity.

                      I don't know where your assertion that they should be mutually exclusive comes from. All I can say about it is that either your belief that they should be is wrong, or else the variables you have constructed are not what you want to work with. But that gives me no clue what they should be.

                      It might help if you explained why you say that they should be mutually exclusive. What are these variables supposed to represent, and how are they supposed to relate to your data?

                      Comment

                      Working...
                      X