Announcement

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

  • Loop over imputed data.

    Hello everyone,

    I work with an imputed panel dataset on a household level. What you see here is an excerpt of my data:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double(weight _1_net_wealth _2_net_wealth _3_net_wealth _4_net_wealth _5_net_wealth) float ranknw1 byte pct_percent1
    6030.4927 598500 598500 598500 598500 598500 1198  9
    1539.1941 598000 598000 598000 598000 598000 1199  9
    209.76678 596500 596500 596500 596500 596500 1200  9
    1907.0204 595400 564400 581600 565300 588070 1201  9
    433.08447 595300 589000 605000 601000 667150 1202  9
    9238.9199 595000 595000 595000 595000 595000 1203  9
    8029.6475 595000 595000 595000 595000 595000 1204  9
    356.36459 592000 803500 592000 600000 612000 1205 10
    6368.6235 591600 598200 623600 591600 592500 1206 10
     296.9743 591600 591600 591600 591600 591600 1207 10
    625.55798 590900 590900 590900 590900 590900 1208 10
    266.18207 590000 590000 590000 590000 590000 1209 10
    365.48203 589200 589200 589200 589200 589200 1210 10
    1012.8746 588700 588700 588700 588700 588700 1211 10
    end
    where
    _1_net_wealth ... _5_net_wealth are the imputed net wealth variables of each household
    ranknw1 ranks the households based on _1_net_wealth in descending order
    pct_percent1 shows the distribution of percentiles based on _1_net_wealth
    weight are the weights each household observation represents
    My data ranges over 100 percentiles and is sorted by _1_net_wealth in descending order. However the ranks and percentile are in ascending order, meaning that the observation with the highest value of _1_net_wealth represents the first rank within the first percentile of the distribution.

    The following code I would like to apply to the percentiles 1-10, for all five imputations:

    Code:
    // create ranks and percentiles
    egen ranknw1 = rank(-_1_net_wealth), unique
    xtile pct_percent1 = -_1_net_wealth [weight = weight], nquantiles(100)
    gsort ranknw1
    
    // create CCDF
    keep if pct_percent1 == 10
    bysort pct_percent (ranknw1) : gen ni = sum(weight)
    egen nall = total(weight)
    gen ccdf1 = ni/nall
    gen log_ccdf1 = log(ccdf1)
    gen log_nw1 = log(_1_net_wealth)
    
    // estimate pareto alpha using OLS
    reg ccdf1 log_nw1

    To my question/problem:
    How do I create a loop that applies the code above for all 10 percentiles and for all 5 imputations?

    Thanks a lot and sorry for the long post
    Moritz
    Last edited by Moritz Huth; 04 Mar 2023, 10:40.

  • #2
    I don't follow but my guess is that one place to start is
    Code:
    help mi xeq
    and, if you don't yet know about -foreach-, another place is
    Code:
    h foreach

    Comment


    • #3
      Thank you, Rich. I checked with mi xeq and foreach before, but I was not able to solve the problem on my own, hence the post...

      Comment

      Working...
      X