Announcement

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

  • To create shock variable

    Hi how do I create a shock variable? Suppose I want to create a variable for each year ( for this variable- vc_body_out)which is an indicator of that variable being above or below a threshold of the standard deviation for all the years considered. Kind of similar to how rainfall shocks are created (sd from a historical mean).
    [/CODE]
    Code:
    input int year byte(State_code_census Dist_code_census) float vc_body_out
    2005 1 1 211
    2006 1 1 187
    2007 1 1 186
    2008 1 1 122
    2009 1 1 116
    2010 1 1 148
    2011 1 1 136
    2012 1 1 135
    2013 1 1 128
    2014 1 1 117
    2015 1 1 126
    2016 1 1 118
    2017 1 1 139
    2018 1 1 125
    2005 1 2 284
    2006 1 2 292
    2007 1 2 231
    2008 1 2 172
    2009 1 2 223
    2010 1 2 286
    2011 1 2 268
    2012 1 2 258
    2013 1 2 258
    2014 1 2 214
    2015 1 2 223
    2016 1 2 160
    2017 1 2 190
    2018 1 2 236
    2005 1 3 361
    2006 1 3 336
    2007 1 3 233
    2008 1 3 205
    2009 1 3 239
    2010 1 3 291
    2011 1 3 236
    2012 1 3 204
    2013 1 3 190
    2014 1 3 150
    2015 1 3 193
    2016 1 3 136
    2017 1 3 196
    2018 1 3 234
    2005 1 4 122
    2006 1 4 100
    2007 1 4  96
    2008 1 4 100
    2009 1 4 109
    2010 1 4  98
    2011 1 4 119
    2012 1 4  93
    2013 1 4 102
    2014 1 4  87
    2015 1 4  95
    2016 1 4 133
    2017 1 4  83
    2018 1 4  96
    2005 1 5 197
    2006 1 5 178
    2007 1 5 156
    2008 1 5  71
    2009 1 5  63
    2010 1 5  70
    2011 1 5  94
    2012 1 5  66
    2013 1 5  70
    2014 1 5  73
    2015 1 5  80
    2016 1 5  84
    2017 1 5 104
    2018 1 5 134
    2005 1 6 264
    2006 1 6 239
    2007 1 6 182
    2008 1 6 196
    2009 1 6 247
    2010 1 6 236
    2011 1 6 254
    2012 1 6 277
    2013 1 6 268
    2014 1 6 227
    2015 1 6 224
    2016 1 6 286
    2017 1 6 204
    2018 1 6 236
    2005 1 7   6
    2006 1 7  13
    2007 1 7  10
    2008 1 7   6
    2009 1 7   6
    2010 1 7   6
    2011 1 7   2
    2012 1 7   5
    2013 1 7   1
    2014 1 7   2
    2015 1 7   5
    2016 1 7   5
    2017 1 7   9
    2018 1 7   6
    2005 1 8   2
    2006 1 8   5
    end
    [/CODE]

  • #2
    Hi Raphael,
    I hope I understood what you need clearly. The code below should give you the variable you are interested into, change sd_threshold to set the threshold of the standard deviation you prefer.
    The code does not consider the fact that your data is longitudinal and not a time series.


    Code:
    local sd_threshold = 1 // change this to have a different threshold of your choice
    
    qui summ vc_body_out
    local mean = r(mean)
    local sd = r(sd)
    
    local low_threshold = r(mean) - r(sd)*`sd_threshold'
    local high_threshold = r(mean) + r(sd)*`sd_threshold' 
    
    
    gen indicator = (vc_body_out > `high_threshold' | vc_body_out < `low_threshold')

    Comment


    • #3
      Thank you very much. I wanted this indicator based on all the years in a particular state and district. What I mean is, the indicator should tell me whether for a particular year the value of vc_body_out, is greater or lesser than the threshold by considering all the values of vc_body_out in the time period considered in that specific locality (a district in a particular state)
      I suppose I have to bysort it by state district. Is the following code correct?
      Code:
       bys State_code_census Dist_code_census: qui summ vc_body_out local mean = r(mean) local sd = r(sd)

      Comment


      • #4
        Now I got it. Sorry for the confusion. I would slightly change the code to make it simpler to understand but you can also create a loop and inside use bysort + if conditions.
        I hope this solves your question.

        Code:
        
        
        local sd_threshold = 1 // change this to have a different threshold of your choice
        
        bysort Dist_code_census: egen mean = mean(vc_body_out)
        bysort Dist_code_census: egen sd = sd(vc_body_out)
        
        gen low_threshold = mean - sd*`sd_threshold'
        gen high_threshold =mean + sd*`sd_threshold' 
        
        
        gen indicator = (vc_body_out > high_threshold | vc_body_out < low_threshold)

        Comment


        • #5
          Thank you so much.
          I just have one additional question if that's alright.
          A) How am to I create lagged and lead variables for this shock indicator?
          A1) Suppose its the year 2011. I want an indicator (in the row of year==2011) to show if there was a shock in
          vc_body_out
          constructed as earlier (sd from a historical mean) in the year 2010.

          A2)Similarily I want to run a falsification excercise to see if future years shocks have an impact on my outcome. So I want to construct an indictaor (in the row of year 2011) to show if there was a shock in
          vc_body_out
          constructed as earlier (sd from a historical mean) in the year 2012. My codes are this , but when I br to see if the lagged and lead is showing up its not right
          Last edited by Raphael George; 23 Sep 2023, 21:53.

          Comment

          Working...
          X