generating bootstrap standard errors for large num

2019-08-28 20:27发布

问题:

Suppose I have four scalars: call them dea_1 dea_2 dea_3 dea_4. They are output from a program samprogram (not shown here).

Now I use the bootstrap command in Stata with these scalars to get bootstrapped standard errors.

set seed 123
bootstrap dea_1=r(dea_1)dea_2=r(dea_2)dea_3=r(dea_3)dea_4=r(dea_4), reps(100): samprogram

This is fine but in my original program, I calculate 30 scalars, dea_1 dea_2 ... dea_30. Now I want to avoid writing each of these 30 scalars in the bootstrap command and for this purpose I wrote a loop as follows:

set seed 234
forvalues i in 1(1)30{
local k dea_`i'
bootstrap dea_`k'=r(dea_`k'), reps(100): samprogram
}

This works, but gives the output for each scalar one at a time. However, I am looking for code which avoids writing all scalars in the bootstrap command but still gives the output for all at the same time (i.e. like the output from the following command)

set seed 345
bootstrap dea_1=r(dea_1)dea_2=r(dea_2)dea_3=r(dea_3)dea_4=r(dea_4)[omitted]...dea_30=r(dea_30), reps(100): samprogram

Any help in this regard will be highly appreciated.

回答1:

This yields to building-up the contents of a local macro step-by-step.

set seed 123
forval i = 1/30 { 
    local call `call' dea_`i'=r(dea_`i') 
}
bootstrap `call', reps(100) : samprogram 

If need be, blank out the macro beforehand by

local call 

More discussion in http://www.stata-journal.com/sjpdf.html?articlenum=pr0005 [free .pdf]

(LATER) Note that contrary to your assertion the code

set seed 234
forvalues i in 1(1)30{
local k dea_`i'
bootstrap dea_`k'=r(dea_`k'), reps(100): samprogram
}

would not work as intended. First time round, for example, bootstrap would be looking for r(dea_dea_1) and would return missing for every sample. The code for calling bootstrap repeatedly could simply be

set seed 234
forvalues i = 1/30 {
    bootstrap dea_`i'=r(dea_`i'), reps(100): samprogram
}

but that would be a bad idea when you can do what you want in one call.



回答2:

An alternative solution would be to make your program eclass and return the results in the matrix e(b). This allows the shortcut bootstrap _b, reps(100): samprogram. Below is an example. The key points here are that the different scalars are stored in the row vector `b', which is returned by the program as the row vector e(b) whith the command:

ereturn post `b', esample(`touse')

A complete example is here:

clear all

program define sim, eclass
    syntax varlist(numeric) [if] [in], by(varname numeric) 
    marksample touse
    markout `touse' `by'

    local k : word count `varlist'
    tempname b m0
    matrix `b' = J(1,`k',.)

    local i = 1
    foreach var of local varlist {
        sum `var' if `touse' & `by', meanonly
        scalar `m0' = r(mean)
        sum `var' if `touse' & !`by', meanonly
        matrix `b'[1,`i'] = `m0' - r(mean)
        local i = `i' + 1
    }
    ereturn post `b', esample(`touse')

end

sysuse auto
bootstrap _b, reps(100) : sim price mpg length weight trunk, by(foreign)