Announcement

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

  • Weights not allowed - Counting per group/consecutive growth

    Hi all,

    I was trying to do something using a weights/forvalues to achieve the following:

    I have ten countries (var: "country" and "countrycode") with 45 quarters (var: "quarter" ranging from 2010q1 to 2021q1) which I want to assign a variable <booming> if the variable rhp (real house prices) increases for 8 consecutive quarters.

    bysort quarter countrycode: replace rise[n] = rise[_n+1] + 1 if rhp>rhp-1

    The above is not allowed due to weights. But also does not take away the fact that it should also be set back to zero if there is a decline in a quarter.

    So basically, what I want to achieve is for every country a variable that counts the number of consecutive quarters when rhp>L1.rhp and adds one to it if the condition is met and resets to zero if not.

    It should kinda look like this: 1,2,3,4,5,6,7,8,9,10 (all rhp>L1.rhp), 0 (when L1.rhp>rhp), 1,2,3,4 and so forth.

    That way I can simply assign a dummy equal to 0 or 1 in case >8 consecutive quarters of growth per country.

    Do you have any advice?

    Many thanks in advance!
    Matthias
    Last edited by Matthias Keer; 26 Jun 2023, 17:26.

  • #2
    That errror message is misleading. It has nothing to do with weights. The problem is that you used rise[_n] on the left side of the equation. That's a syntax error. You can't use subscripted variables on the left side of the equation. When Stata saw the square brackets there, it got confused and thought you were introducing weights into the equation. But you were not. You should have just written plain -rise-.

    That said, your code isn't going to do what you want anyway. You didn't show example data, so I'm illustrating the approach here with the grunfeld.dta dataset that Stata has on its website. This creates a variable rise that tracks the rising of the variable mvalue in that data set. Adapt to your own data.
    Code:
    webuse grunfeld, clear
    
    by company (year), sort: gen run_num = sum(mvalue < mvalue[_n-1])
    by company run_num (year), sort: gen rise = _n-1
    by company run_num (year): gen byte eight_year_rise = _N > 8

    Comment


    • #3
      Thank you very much for this!

      I will upload data below. I only have a problem here now that the eight_year_rise is suddenly ==1 for every rise>0. Do you have advise?


      Code:
      by countrycode (quarter), sort: gen run_num = sum(rhp < rhp[_n-1])
      
      by countrycode run_num (quarter), sort: gen rise = _n-1
      
      by countrycode run_num (quarter): gen byte eight_year_rise = _N > 8

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input str11 country float quarter double rhp float(run_num rise) byte eight_year_rise
      "Austria" 200 83.1094212806023  1  0 0
      "Austria" 201 85.4368338291622  1  1 0
      "Austria" 202 86.8500887376125  1  2 0
      "Austria" 203  87.367155817539  1  3 0
      "Austria" 204 86.4092398350208  2  0 1
      "Austria" 205 87.6010289934449  2  1 1
      "Austria" 206 88.6135619723579  2  2 1
      "Austria" 207 90.4001901326113  2  3 1
      "Austria" 208 91.3353992114357  2  4 1
      "Austria" 209 91.6432725911421  2  5 1
      "Austria" 210 93.0600474043081  2  6 1
      "Austria" 211 94.1020980875646  2  7 1
      "Austria" 212 94.4293462802468  2  8 1
      "Austria" 213 94.8378632988666  2  9 1
      "Austria" 214 96.5917022511876  2 10 1
      "Austria" 215 95.4501624348175  3  0 0
      "Austria" 216 96.4685392982422  3  1 0
      "Austria" 217 96.8929797774766  3  2 0
      "Austria" 218 95.3298611319885  4  0 0
      "Austria" 219  98.181413560537  4  1 0
      "Austria" 220 94.7241947553419  5  0 1
      "Austria" 221  99.038245897078  5  1 1
      "Austria" 222 103.034083802617  5  2 1
      "Austria" 223 103.203475544963  5  3 1
      "Austria" 224 105.826693772654  5  4 1
      "Austria" 225 106.527492830978  5  5 1
      "Austria" 226 107.447141526736  5  6 1
      "Austria" 227 108.155705417181  5  7 1
      "Austria" 228 109.040245321501  5  8 1
      "Austria" 229 110.375465074213  5  9 1
      "Austria" 230 110.866030986058  5 10 1
      "Austria" 231 112.486471002237  5 11 1
      "Austria" 232 112.831496636389  5 12 1
      "Austria" 233 112.742613207034  6  0 1
      "Austria" 234 113.546732764114  6  1 1
      "Austria" 235 115.391262152843  6  2 1
      "Austria" 236 115.620052736446  6  3 1
      "Austria" 237 117.738834407154  6  4 1
      "Austria" 238 118.355874088765  6  5 1
      "Austria" 239  120.99787722738  6  6 1
      "Austria" 240 121.845105142664  6  7 1
      "Austria" 241 124.439709284745  6  8 1
      "Austria" 242 126.688471199246  6  9 1
      "Austria" 243 128.512019934767  6 10 1
      "Austria" 244 132.153997728371  6 11 1
      "Belgium" 200 98.5195438410046  1  0 0
      "Belgium" 201 98.5926498657623  1  1 0
      "Belgium" 202 99.2423811612501  1  2 0
      "Belgium" 203 100.083002929065  1  3 0
      "Belgium" 204  99.848218483059  2  0 0
      "Belgium" 205 99.8928209221033  2  1 0
      "Belgium" 206 100.496225441834  2  2 0
      "Belgium" 207 100.711580034409  2  3 0
      "Belgium" 208  100.77200524109  2  4 0
      "Belgium" 209 100.651063629817  3  0 0
      "Belgium" 210 100.374965268808  4  0 0
      "Belgium" 211 100.013855162098  5  0 0
      "Belgium" 212  100.94173829669  5  1 0
      "Belgium" 213 100.740289766213  6  0 0
      "Belgium" 214  100.44814291608  7  0 0
      "Belgium" 215 100.116412606178  8  0 0
      "Belgium" 216 98.4521890272818  9  0 0
      "Belgium" 217 99.0627928783629  9  1 0
      "Belgium" 218 98.9206368539884 10  0 0
      "Belgium" 219 100.614544867351 10  1 0
      "Belgium" 220 98.8512520317192 11  0 0
      "Belgium" 221 99.7805829108069 11  1 0
      "Belgium" 222 100.567715102402 11  2 0
      "Belgium" 223 100.800449955072 11  3 0
      "Belgium" 224 100.427820772299 12  0 0
      "Belgium" 225 100.695310938857 12  1 0
      "Belgium" 226 101.361768366867 12  2 0
      "Belgium" 227 101.925773981508 12  3 0
      "Belgium" 228 102.599212511077 12  4 0
      "Belgium" 229 101.879266851455 13  0 0
      "Belgium" 230 103.222206981487 13  1 0
      "Belgium" 231 103.539146798904 13  2 0
      "Belgium" 232 103.500193488222 14  0 0
      "Belgium" 233 104.020207354948 14  1 0
      "Belgium" 234 103.583840503686 15  0 0
      "Belgium" 235 103.605488286424 15  1 0
      "Belgium" 236 105.009086357434 15  2 0
      "Belgium" 237 105.160135001934 15  3 0
      "Belgium" 238 107.045191990213 15  4 0
      "Belgium" 239 107.924740957136 15  5 0
      "Belgium" 240 107.545936517849 16  0 0
      "Belgium" 241 109.121294294577 16  1 0
      "Belgium" 242 109.436949085197 16  2 0
      "Belgium" 243 113.243336734188 16  3 0
      "Belgium" 244 114.066126211855 16  4 0
      "Germany" 200 89.0132765700468  1  0 0
      "Germany" 201 89.1565483630681  1  1 0
      "Germany" 202 89.4768036541353  1  2 0
      "Germany" 203 88.4256423307189  2  0 0
      "Germany" 204 90.5370675731289  2  1 0
      "Germany" 205 90.3748709146334  3  0 0
      "Germany" 206 90.5167412287687  3  1 0
      "Germany" 207 90.6467131186003  3  2 0
      "Germany" 208 91.1610488743082  3  3 0
      "Germany" 209 91.4734371711585  3  4 0
      end
      format %tq quarter

      Comment


      • #4
        I only have a problem here now that the eight_year_rise is suddenly ==1 for every rise>0.
        No you don't. Look at your data. You only have eight_year_rise = 1 when there is a run of more than eight years of increasing values of rhp. The short rising streaks have eight_year_rise = 0. It's exactly what you asked for. If you had something else in mind, please explain what you need.

        Comment

        Working...
        X