Announcement

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

  • Repeatedly predicting regression, using bootstrap replicates -- easy way to do it?

    Hello, I want to provide a client with a Stata command that takes some varlist of independent / predictor variables, runs a regression (logit, as it happens) with vce(bootstrap, saving(bootfilename)). Then, I want to loop over the individual bootstrap replicates in bootfilename.dta and predict the dependent variable repeatedly, using those coefficient values. The goal is to propagate uncertainty from the regression forward into other code that uses the replicated predicted values.

    Now, I am mindful that they could in future use this with some unexpected varlist, maybe including interactions. So, I want the plugging of the bootstrap coefficients into the prediction formula to be automatic, without any hard coding. I need to plug a variable in bootfilename.dta like _b_cons, which is labelled "_b[_cons]" into the predict command for the right kind of regression, and have it treated as the _cons parameter.

    I know that bootfilename.dta will contain some metadata telling Stata that the regression was a logit, so I really suspect that there is a simple way to do this, and I just don't know it (yet). I could parse the names but it will be messy if interactions get in there.

    Any suggestions would be most gratefully received!

    Anonymised code example:

    clear all
    set seed 9876
    set obs 1000
    gen sex = rbinomial(1,0.55)
    gen ageband = 1 + rbinomial(2, 0.5)
    gen true_logit = -1 + 0.1*1.sex + 0.5*2.ageband + 0.7*3.ageband
    gen true_risk = 1/(1+exp(-1*true_logit))
    gen outcome = rbinomial(1, true_risk)

    // run model and store bootstrap replicate coefficients
    logit outcome i.sex i.ageband, ///
    vce(bootstrap, dots seed(1234) rep(100) ///
    saving(bootfilename, replace))

    // now how can I loop -predict- over the bootstrap file rows?


  • #2
    logit with -vce(bootstrap)- stores the command needed to create the matrix that corresponds to the saved bootstrap estimates. Assuming that you need linear predictions (transformed or otherwise) and the client will have access to the dataset with the necessary variables, you can do the following. I use erepost from SSC to replace the generated coefficients' matrix.

    Code:
    clear all
    set seed 9876
    set obs 1000
    gen sex = rbinomial(1,0.55)
    gen ageband = 1 + rbinomial(2, 0.5)
    gen true_logit = -1 + 0.1*1.sex + 0.5*2.ageband + 0.7*3.ageband
    gen true_risk = 1/(1+exp(-1*true_logit))
    gen outcome = rbinomial(1, true_risk)
    
    // run model and store bootstrap replicate coefficients
    logit outcome i.sex i.ageband, ///
    vce(bootstrap, dots seed(1234) rep(5) ///
    saving(bootfilename, replace))
    use bootfilename, clear
    gen command = "`e(command)'" in 1
    save bootfilename, replace
    
    
    *LATER ON (LOAD DATASET)
    clear all
    set seed 9876
    set obs 1000
    gen sex = rbinomial(1,0.55)
    gen ageband = 1 + rbinomial(2, 0.5)
    gen true_logit = -1 + 0.1*1.sex + 0.5*2.ageband + 0.7*3.ageband
    gen true_risk = 1/(1+exp(-1*true_logit))
    gen outcome = rbinomial(1, true_risk)
    
    *CREATE PREDICTIONS
    preserve
    use bootfilename, clear
    local reps=_N
    local command= "`=command[1]'"
    drop command
    frame put *, into(bootfilename)
    restore
    
    forval i=1/`reps'{
        frame bootfilename: mkmat * in `i', mat(b)
        qui `command'
        mat colnames b= `:colnames e(b)'
        erepost b=b, rename
        predict pred`i', pr
    }
    frame drop bootfilename
    Res.:

    Code:
    . l in 1/20, sep(0)
    
         +------------------------------------------------------------------------------------------------------+
         | sex   ageband   true_l~t   true_r~k   outcome      pred1      pred2      pred3      pred4      pred5 |
         |------------------------------------------------------------------------------------------------------|
      1. |   0         2        -.5   .3775407         0   .3803289   .3468785   .3619351    .381514   .3809033 |
      2. |   1         3        -.2    .450166         0    .457682   .4585897   .4788925   .4520594    .476576 |
      3. |   0         2        -.5   .3775407         1   .3803289   .3468785   .3619351    .381514   .3809033 |
      4. |   1         3        -.2    .450166         0    .457682   .4585897   .4788925   .4520594    .476576 |
      5. |   1         1        -.9   .2890505         1   .3388704   .3714073   .3645394   .2410152    .336082 |
      6. |   1         2        -.4   .4013124         1   .4002018   .3903545   .4078984   .4176058   .4142892 |
      7. |   1         2        -.4   .4013124         0   .4002018   .3903545   .4078984   .4176058   .4142892 |
      8. |   1         1        -.9   .2890505         1   .3388704   .3714073   .3645394   .2410152    .336082 |
      9. |   1         2        -.4   .4013124         0   .4002018   .3903545   .4078984   .4176058   .4142892 |
     10. |   0         1         -1   .2689414         0   .3204161    .328903   .3208146   .2145626    .305709 |
     11. |   1         2        -.4   .4013124         0   .4002018   .3903545   .4078984   .4176058   .4142892 |
     12. |   1         2        -.4   .4013124         0   .4002018   .3903545   .4078984   .4176058   .4142892 |
     13. |   0         2        -.5   .3775407         0   .3803289   .3468785   .3619351    .381514   .3809033 |
     14. |   0         2        -.5   .3775407         0   .3803289   .3468785   .3619351    .381514   .3809033 |
     15. |   1         1        -.9   .2890505         0   .3388704   .3714073   .3645394   .2410152    .336082 |
     16. |   0         3        -.3   .4255575         1   .4370345    .412658   .4307492   .4151123   .4419582 |
     17. |   0         2        -.5   .3775407         0   .3803289   .3468785   .3619351    .381514   .3809033 |
     18. |   0         3        -.3   .4255575         0   .4370345    .412658   .4307492   .4151123   .4419582 |
     19. |   1         2        -.4   .4013124         0   .4002018   .3903545   .4078984   .4176058   .4142892 |
     20. |   0         1         -1   .2689414         0   .3204161    .328903   .3208146   .2145626    .305709 |
         +------------------------------------------------------------------------------------------------------+
    
    .

    Comment


    • #3
      WOW! That's fantastic, thanks so much.

      Robert

      Comment

      Working...
      X