Announcement

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

  • error when using bsample with weights

    I'm reposting this question because I never got any response

    Hello, I am trying to manually implement the survey design to compute standard errors using the -arhomme- command since svyset is not compatible with it. I am using bsample with the options strata(raestrat) cluster(raehsamp) weight(new_weight), but when I include the weight() option, I receive the error: "estimates post: matrix has missing values." Any advice on how to properly implement the weights would be greatly appreciated. My code is listed below.

    Code:
    ****bootstrap survey design SE*****
    *Step 1, save observed coefficients****
    preserve
    quietly xi: arhomme log_avrg_cost i.inc_d i.endentulism i.race i.age_cat i.male i.education i.veteran i.mothered i.wealth i.smoke_now chronicdisease[pw=new_weight], select(r11dentst = dentalinsurance_w1 endentulism inc_d race age_cat male education veteran mothered wealth smoke_now chronicdisease)  quantiles(0.10, 0.25, 0.50, 0.75, 0.90)  taupoints(29) rhopoints(35)  meshsize(0.5) frank nostderrors  centergrid(-0.20)
    
    matrix beta = e(b)
    global N "e(N)'"
    global Ns "e(sN)'"
    restore
    
    ** step 2 generate program for bootstrap****
    capture program drop arhomme_bootstrap
    program arhomme_bootstrap, eclass
    preserve
    bsample, strata(raestrat) cluster(raehsamp) weight(new_weight)
    
    quietly xi:  arhomme log_avrg_cost i.inc_d i.endentulism i.race i.age_cat i.male i.education i.veteran i.mothered i.wealth i.smoke_now chronicdisease[pw=new_weight], select(r11dentst = dentalinsurance_w1 endentulism inc_d race age_cat male education veteran mothered wealth smoke_now chronicdisease) quantiles(0.10, 0.25, 0.50, 0.75, 0.90)  taupoints(29) rhopoints(35)  meshsize(0.5) frank nostderrors  centergrid(-0.20)
    
    matrix beta_boot = e(b)
    
    forvalue i = 1/119 {
        
        ereturn scalar beta_boot_i' =beta_boot[1, i']
    }
    
    restore
    end  
    
    estimates post: matrix has missing values
    r(504);
    Last edited by Luis Mijares Castaneda; 16 Feb 2025, 13:35.

  • #2
    Debugging this is difficult as we do not have the data to test your command. But I think there is something wrong how you reference the local. It should be:

    Code:
    ereturn scalar beta_boot_`i' =beta_boot[1, `i']
    As you access e(b), you can return the matrix as a whole in eclass, see:

    Code:
    
    cap program drop booter
    program define booter, eclass
    reg mpg weight length foreign
    matrix a = e(b)
    ereturn post a
    end
    
    
    
    bootstrap, seed(123): booter
    estat bootstrap

    Best wishes

    (Stata 16.1 MP)

    Comment


    • #3
      my apologies, here is the dataex

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float log_avrg_cost byte r11dentst float(dentalinsurance_w1 endentulism inc_d race age_cat) byte(male education veteran) float mothered byte wealth float(smoke_now chronicdisease new_weight) byte(raestrat raehsamp)
       5.860786 1 0 0 0 1 3 0 5 0 1 4 0 3  4918.557 40 1
              . 1 1 0 0 1 1 0 5 0 1 4 0 3  6623.596  1 2
       6.478509 1 1 0 0 1 2 0 3 0 0 4 0 0  5815.177  1 2
       7.824446 1 0 0 0 1 2 0 4 0 1 4 1 0  5893.801  1 2
       6.398595 1 1 0 0 1 3 1 5 1 1 4 0 2  6132.698  2 2
       8.699764 1 1 0 0 1 2 0 5 0 0 4 0 0   6568.66  2 2
       5.525453 1 1 0 0 4 3 0 5 0 1 4 0 2  6938.598  2 2
              . 0 1 0 0 3 3 0 1 0 . 2 0 2 2601.5085  1 2
       6.685861 1 1 0 0 3 3 0 1 0 0 1 0 2  3354.929  1 2
       6.398595 1 1 0 0 1 2 0 1 0 1 4 0 1   7329.36  1 1
       7.378384 1 0 0 0 1 4 1 5 1 0 4 0 2  6378.651  1 1
              . 1 1 0 0 3 3 0 1 0 0 1 0 2  8065.042  1 2
              . 0 1 1 0 4 3 1 1 0 0 1 0 3  5591.879  1 2
              . 1 1 1 0 2 3 0 3 0 0 1 0 1  7468.239  2 2
              . 0 0 0 0 2 3 1 1 0 . 1 0 0  6987.121  2 2
       7.151485 1 1 0 0 1 3 0 4 0 1 1 0 1  4896.986  1 2
              . 0 1 1 1 2 3 0 1 0 . 1 0 1  3817.199  1 2
       6.216606 1 1 0 0 1 3 0 4 0 1 4 0 0  5999.094  1 1
       5.993961 1 1 0 0 1 3 1 5 1 1 4 0 0  6925.122  1 1
        8.03948 1 0 0 0 1 3 0 1 0 1 4 0 1  5896.825  1 1
              . 0 0 0 0 1 3 0 3 0 1 1 0 2  8667.065  2 1
       5.303305 1 1 1 0 4 3 0 5 0 0 4 0 2  6781.349  2 2
       5.303305 0 1 0 0 4 3 1 5 1 0 4 0 3  6224.931  2 2
       4.620059 1 1 0 0 1 3 0 3 0 0 1 0 0  6747.077  2 2
              . 0 1 1 0 1 3 1 4 1 . 2 0 1  6543.964  1 2
       7.313887 1 1 0 0 1 2 0 5 0 1 2 0 0  6165.962  1 2
       8.881975 1 0 0 0 1 4 0 5 0 1 4 0 2   3695.68  2 1
       5.993961 1 0 0 0 1 3 1 5 1 1 4 0 2  6698.451  2 1
       7.824446 1 0 0 0 1 3 0 3 0 1 4 0 0  6189.557  2 1
       7.523481 1 1 0 0 1 3 1 5 0 0 4 0 3  6313.635  2 1
       6.803505 1 1 0 0 1 3 0 5 0 1 4 0 1  7654.785  2 1
              . 1 1 0 0 2 3 0 1 0 0 3 0 2 2294.2178  2 1
              . 0 0 0 0 2 3 0 5 0 1 1 0 2 2306.8857  2 1
              . 0 0 1 0 2 3 0 1 0 0 4 0 1  2917.955  2 2
              . 0 0 1 0 2 2 0 3 0 0 1 0 0 2076.2468  1 2
       6.398595 1 1 0 0 2 3 1 3 1 1 3 0 3 3628.3115  1 2
              . 1 1 0 0 2 3 0 3 0 1 3 0 0 1960.0643  1 2
       6.908755 0 0 0 0 2 3 1 3 0 0 2 . 2  6003.062  1 2
       5.786897 1 1 0 0 3 1 0 3 0 1 4 0 0  6675.004  4 1
       4.836282 1 1 0 0 4 3 0 1 0 0 4 0 0  5975.286  4 1
       5.303305 1 1 0 0 4 4 1 5 0 0 4 0 0  6322.484  4 1
       6.908755 1 1 0 0 3 3 1 4 1 1 4 0 1 3176.7256  3 2
       3.314186 1 1 0 0 1 2 0 4 0 1 4 0 0  3072.397  3 2
        7.31422 1 0 0 0 1 2 0 4 0 0 4 0 1  6713.827  3 2
       8.987322 1 1 0 0 2 3 0 5 0 0 1 0 4 3192.3496  4 1
              . 0 1 1 0 4 3 0 4 0 0 2 0 0  3568.704  3 1
       4.912655 1 1 0 1 1 3 1 1 0 1 2 0 1  6398.812  4 2
        5.86221 1 1 0 0 1 3 0 5 0 1 2 0 1  6481.468  4 2
       5.239098 1 1 0 0 1 3 1 3 1 0 4 0 2  6808.061  3 2
              . 0 0 0 0 2 3 0 4 0 0 3 0 3 11825.906  3 2
       6.803505 1 1 0 0 1 4 1 5 1 1 4 0 2  7366.496  4 1
       7.438972 1 0 0 0 3 3 0 3 0 0 3 0 1   3320.87  3 2
              . 0 0 1 0 3 3 1 2 1 0 3 0 1  3323.894  3 2
      4.6151204 0 0 0 0 4 4 1 5 0 0 2 0 1 1206.5812  4 2
      3.9318256 1 0 0 0 4 3 0 5 0 0 2 0 2 1571.9827  4 2
              . 0 0 0 0 2 3 0 1 0 0 1 1 2  5680.608  4 2
       5.351858 1 0 0 0 2 3 0 3 0 0 1 0 1  4061.249  4 2
              . 0 0 0 0 2 3 0 1 0 . 1 0 2   3636.16  4 2
        6.53814 1 0 0 0 1 3 1 4 1 0 1 0 3 3392.4385  4 2
       7.313887 1 1 0 1 4 2 0 1 0 1 2 0 6   3709.96  4 2
       8.101981 1 0 0 0 1 4 1 3 1 1 4 0 4  5359.752  5 2
        6.29803 1 0 0 0 1 3 0 1 0 1 4 0 1  3885.647  5 2
       6.055613 1 1 0 0 1 3 0 5 0 1 3 0 1  4188.258  5 2
              . 0 0 0 0 1 3 0 3 0 1 2 0 2  3567.327  5 2
        7.91972 1 0 0 0 1 4 0 4 0 0 4 0 3  3992.201  6 2
       6.908755 1 1 0 0 1 4 1 5 1 . 4 0 1  3890.897  6 2
       7.003974 1 0 0 0 1 4 1 4 1 1 4 0 1 4450.5884  5 1
       6.763885 1 0 0 0 1 3 0 3 0 0 4 0 4 4880.6187  5 1
              . 1 1 0 0 1 2 0 3 0 1 2 0 1  4865.739  5 1
              . 1 1 0 0 1 3 0 2 0 0 3 0 0   4232.61  6 1
      1.7917595 1 0 0 0 3 3 0 3 0 . 1 0 3 1945.3035  5 2
              . 0 0 1 0 2 3 0 4 0 1 2 0 3  2502.309  6 2
              . 0 0 0 0 1 3 0 1 0 0 1 0 2  4858.299  5 1
       6.685861 1 1 0 0 1 3 1 4 1 1 2 0 1 4108.6255  6 1
       5.463832 1 0 0 0 1 3 1 3 0 . 3 0 2  4545.595  6 1
              . 0 0 0 0 1 3 0 3 0 1 4 0 1 4109.1294  6 2
       4.620059 1 1 0 0 1 4 1 1 1 0 3 0 0 4699.8203  5 2
       6.634634 1 0 0 0 1 3 1 5 1 1 4 0 2  2464.117  5 2
       5.525453 0 1 0 0 1 3 1 5 0 0 4 0 2  3485.679  6 1
       5.860786 1 1 0 0 1 1 0 5 0 1 4 0 0  4411.027  6 1
              . 0 0 1 0 1 3 1 2 0 0 3 0 3  4098.926  6 1
       6.246107 1 0 0 0 3 2 0 1 0 0 3 . 2  4772.491  6 1
              . 0 1 0 0 1 4 1 1 0 0 2 0 3 4599.3877  6 2
       5.673323 1 0 0 0 1 4 0 4 0 0 3 0 0  3694.336  6 2
              . 0 0 1 0 2 3 0 3 0 0 1 0 3  2498.341  5 1
              . 0 0 0 0 2 3 1 2 0 1 1 1 0  5680.104  5 2
              . 0 0 1 1 2 3 1 3 0 1 2 1 0  3492.735  6 2
              . 0 0 0 0 2 3 0 4 0 0 2 0 2  2664.004  5 1
       3.433987 1 1 0 0 2 3 1 4 1 0 3 0 2  1660.687  5 2
      3.9318256 1 1 0 1 2 1 0 4 0 0 3 1 2 1695.4633  5 2
       5.170484 1 0 0 0 2 3 1 1 0 1 2 0 3  4255.165  7 2
              . 0 0 1 0 1 3 1 1 1 0 3 0 4  5537.976  7 1
       7.601402 1 0 0 0 1 3 0 1 0 0 4 0 0  5666.496  7 1
              . 0 0 1 0 2 3 1 1 1 . 2 0 3   4951.05  7 1
              . 0 0 0 0 2 2 0 3 0 1 3 0 1 1885.2877  7 2
              . 0 0 1 0 1 2 0 3 0 . 2 1 2 16301.464  7 1
              . 0 0 1 1 1 3 1 4 0 1 4 0 4  5449.032  7 1
              . 0 0 0 0 1 2 0 3 0 1 4 0 0  5753.079  7 1
        5.01728 1 1 0 0 1 3 0 4 0 1 1 0 3 4752.1553  7 2
              . 0 0 0 0 1 3 0 3 0 0 3 0 4 4227.8853  7 2
      end
      label values r11dentst YESNO
      label def YESNO 0 "0.no", modify
      label def YESNO 1 "1.yes", modify
      label values dentalinsurance_w1 dentalinsurance_w1
      label def dentalinsurance_w1 0 "No", modify
      label def dentalinsurance_w1 1 "Yes", modify
      label values endentulism endentulism
      label def endentulism 0 "No", modify
      label def endentulism 1 "Yes", modify
      label values inc_d inc_d
      label def inc_d 0 "No", modify
      label def inc_d 1 "Yes", modify
      label values race race
      label def race 1 "White", modify
      label def race 2 "Black", modify
      label def race 3 "Hispanic", modify
      label def race 4 "Other", modify
      label values age_cat age_cat
      label def age_cat 1 "50-59", modify
      label def age_cat 2 "60-69", modify
      label def age_cat 3 "70-79", modify
      label def age_cat 4 "80+", modify
      label values male male
      label def male 0 "Female", modify
      label def male 1 "Male", modify
      label values education EDUC
      label def EDUC 1 "1.lt high-school", modify
      label def EDUC 2 "2.ged", modify
      label def EDUC 3 "3.high-school graduate", modify
      label def EDUC 4 "4.some college", modify
      label def EDUC 5 "5.college and above", modify
      label values veteran veteran
      label def veteran 0 "No", modify
      label def veteran 1 "Yes", modify
      label values mothered mothered
      label def mothered 0 "Less than High School", modify
      label def mothered 1 "High School or Higher", modify
      label values smoke_now smoke_now
      label def smoke_now 0 "Non-Smoker", modify
      label def smoke_now 1 "Currently Smokes", modify

      Comment


      • #4
        I think one of the main issues here is that the bsample weighs only accept frequency weights and strata in svy and strata in bsample mean something different. I would argue that bsample is not the main issue here. Rather, arhomme needs to work with these weight aspects as an estimation command. Maybe someone else has some deeper insight on this.
        Best wishes

        (Stata 16.1 MP)

        Comment


        • #5
          I did change from bsample to gsample to incorporate weights, my new code is shown below, I'm just not confident that I'm calculating the SEs properly

          Code:
           
          ****bootstrap survey design SE***** 
          *Step 1, save observed coefficients**** 
          preserve
          quietly xi: arhomme log_avrg_cost i.inc_d i.endentulism i.race i.age_cat i.male i.education i.veteran i.mothered i.wealth i.smoke_now chronicdisease[pw=new_weight], select(r11dentst = dentalinsurance_w1 endentulism inc_d race age_cat male education veteran mothered wealth smoke_now chronicdisease) quantiles(0.10, 0.25, 0.50, 0.75, 0.90)  taupoints(29) rhopoints(35)  meshsize(0.5) frank nostderrors  centergrid(-0.20)
          
          matrix beta = e(b) 
          global N "`e(N)'"
          global Ns "`e(sN)'" 
          restore 
          
          ** step 2 generate program for bootstrap****
          capture program drop arhomme_bootstrap 
          program arhomme_bootstrap, eclass
          preserve
          gsample [w=new_weight], strata(raestrat) cluster(raehsamp)
          
          quietly xi:  arhomme log_avrg_cost i.inc_d i.endentulism i.race i.age_cat i.male i.education i.veteran i.mothered i.wealth i.smoke_now chronicdisease[pw=new_weight], select(r11dentst = dentalinsurance_w1 endentulism inc_d race age_cat male education veteran mothered wealth smoke_now chronicdisease) quantiles(0.10, 0.25, 0.50, 0.75, 0.90)  taupoints(29) rhopoints(35)  meshsize(0.5) frank nostderrors  centergrid(-0.20)
          
          matrix beta_boot = e(b)
          
          forvalue i = 1/119 { 
              
              ereturn scalar beta_boot_`i' =beta_boot[1, `i']
          }
          
          
          restore 
          end 
          
          * step 3: run the bootstrap** 
          
          
          preserve 
          
          simulate b_r11dentst_dental_insurance = beta_boot[1,1] b_r11dentst_endentulism = beta_boot[1,2] b_r11dentst_inc_d = beta_boot[1,3] b_r11dentst_race = beta_boot[1,4] b_r11dentst_age_cat = beta_boot[1,5] ///
          b_r11dentst_male = beta_boot[1,6] b_r11dentst_education = beta_boot[1,7] b_r11dentst_veteran = beta_boot[1,8] b_r11dentst_mothered = beta_boot[1,9] b_r11dentst_wealth = beta_boot[1,10] ///
          b_r11dentst_smoke_now = beta_boot[1,11] b_r11dentst_chronicdisease = beta_boot[1,12] b_r11dentst_cons = beta_boot[1,13] b_q10_cons = beta_boot[1,14] b_q10_inc_d = beta_boot[1,15] ///
          b_q10_endentulism = beta_boot[1,16] b_q10_race_2 = beta_boot[1,17] b_q10_race_3 = beta_boot[1,18] b_q10_race_4 = beta_boot[1,19] b_q10_age_cat_2 = beta_boot[1,20] ///
          b_q10_age_cat_3 = beta_boot[1,21] b_q10_age_cat_4 = beta_boot[1,22] b_q10_male = beta_boot[1,23] b_q10_education_2 = beta_boot[1,24] b_q10_education_3 = beta_boot[1,25] ///
          b_q10_education_4 = beta_boot[1,26] b_q10_education_5 = beta_boot[1,27] b_q10_veteran = beta_boot[1,28] b_q10_mothered = beta_boot[1,29] b_q10_wealth_2 = beta_boot[1,30] ///
          b_q10_wealth_3 = beta_boot[1,31] b_q10_wealth_4 = beta_boot[1,32] b_q10_smoke_now = beta_boot[1,33] b_q10_chronicdisease = beta_boot[1,34] b_q25_cons = beta_boot[1,35] ///
          b_q25_inc_d = beta_boot[1,36] b_q25_endentulism = beta_boot[1,37] b_q25_race_2 = beta_boot[1,38] b_q25_race_3 = beta_boot[1,39] b_q25_race_4 = beta_boot[1,40] ///
          b_q25_age_cat_2 = beta_boot[1,41] b_q25_age_cat_3 = beta_boot[1,42] b_q25_age_cat_4 = beta_boot[1,43] b_q25_male = beta_boot[1,44] b_q25_education_2 = beta_boot[1,45] ///
          b_q25_education_3 = beta_boot[1,46] b_q25_education_4 = beta_boot[1,47] b_q25_education_5 = beta_boot[1,48] b_q25_veteran = beta_boot[1,49] b_q25_mothered = beta_boot[1,50] ///
          b_q25_wealth_2 = beta_boot[1,51] b_q25_wealth_3 = beta_boot[1,52] b_q25_wealth_4 = beta_boot[1,53] b_q25_smoke_now = beta_boot[1,54] b_q25_chronicdisease = beta_boot[1,55] ///
          b_q50_cons = beta_boot[1,56] b_q50_inc_d = beta_boot[1,57] b_q50_endentulism = beta_boot[1,58] b_q50_race_2 = beta_boot[1,59] b_q50_race_3 = beta_boot[1,60] ///
          b_q50_race_4 = beta_boot[1,61] b_q50_age_cat_2 = beta_boot[1,62] b_q50_age_cat_3 = beta_boot[1,63] b_q50_age_cat_4 = beta_boot[1,64] b_q50_male = beta_boot[1,65] ///
          b_q50_education_2 = beta_boot[1,66] b_q50_education_3 = beta_boot[1,67] b_q50_education_4 = beta_boot[1,68] b_q50_education_5 = beta_boot[1,69] b_q50_veteran = beta_boot[1,70] ///
          b_q50_mothered = beta_boot[1,71] b_q50_wealth_2 = beta_boot[1,72] b_q50_wealth_3 = beta_boot[1,73] b_q50_wealth_4 = beta_boot[1,74] b_q50_smoke_now = beta_boot[1,75] ///
          b_q50_chronicdisease = beta_boot[1,76] b_q75_cons = beta_boot[1,77] b_q75_inc_d = beta_boot[1,78] b_q75_endentulism = beta_boot[1,79] b_q75_race_2 = beta_boot[1,80] ///
          b_q75_race_3 = beta_boot[1,81] b_q75_race_4 = beta_boot[1,82] b_q75_age_cat_2 = beta_boot[1,83] b_q75_age_cat_3 = beta_boot[1,84] b_q75_age_cat_4 = beta_boot[1,85] ///
          b_q75_male = beta_boot[1,86] b_q75_education_2 = beta_boot[1,87] b_q75_education_3 = beta_boot[1,88] b_q75_education_4 = beta_boot[1,89] b_q75_education_5 = beta_boot[1,90] ///
          b_q75_veteran = beta_boot[1,91] b_q75_mothered = beta_boot[1,92] b_q75_wealth_2 = beta_boot[1,93] b_q75_wealth_3 = beta_boot[1,94] b_q75_wealth_4 = beta_boot[1,95] ///
          b_q75_smoke_now = beta_boot[1,96] b_q75_chronicdisease = beta_boot[1,97] b_q90_cons = beta_boot[1,98] b_q90_inc_d = beta_boot[1,99] b_q90_endentulism = beta_boot[1,100] ///
          b_q90_race_2 = beta_boot[1,101] b_q90_race_3 = beta_boot[1,102] b_q90_race_4 = beta_boot[1,103] b_q90_age_cat_2 = beta_boot[1,104] b_q90_age_cat_3 = beta_boot[1,105] ///
          b_q90_age_cat_4 = beta_boot[1,106] b_q90_male = beta_boot[1,107] b_q90_education_2 = beta_boot[1,108] b_q90_education_3 = beta_boot[1,109] b_q90_education_4 = beta_boot[1,110] ///
          b_q90_education_5 = beta_boot[1,111] b_q90_veteran = beta_boot[1,112] b_q90_mothered = beta_boot[1,113] b_q90_wealth_2 = beta_boot[1,114] b_q90_wealth_3 = beta_boot[1,115] ///
          b_q90_wealth_4 = beta_boot[1,116] b_q90_smoke_now = beta_boot[1,117] b_q90_chronicdisease = beta_boot[1,118] b_anc_rho = beta_boot[1,119], /// 
          reps(50) seed(123456): arhomme_bootstrap
          
          
          /*
          estimates store  TwoStage_QCR_gsample
          esttab TwoStage_QCR_gsample using "U:\Research\Testa_SDA Project\Dental Costs\results\TwoStage_QCR_gsample_5Rep.rft", ci stat(N)
          */ 
          
          * step 4 estimate the boostrap SE** 
          
          bstat, stat(beta) n(${Ns}) 
          restore

          Comment

          Working...
          X