»  Home »  Resources & support »  FAQs »  Stata 7: Obtaining the correct variance–covariance matrix from the bs routine

## How do I get the correct variance–covariance matrix from the bs routine?

 Title Stata 7: Obtaining the correct variance–covariance matrix from the bs routine Author Allen McDowell, StataCorp

### Question

If I run

        matrix myV=e(V)

after running the bs command, the matrix that is saved is not the variance–covariance matrix for my model (i.e., the diagonal of this matrix does not contain the square of the standard errors of the coefficients that are reported in the output). How can I retrieve the correct variance–covariance matrix?

The matrix that is stored in e(V) after running the bs command is the variance–covariance matrix of the estimated parameters from the last estimation (i.e., the estimation from the last bootstrap sample) and not the variance–covariance matrix of the complete set of bootstrapped parameters. The log below demonstrates the correct method for obtaining the variance–covariance matrix for the complete set of bootstrapped parameters. I have inserted comments along the way to describe what is being done at each step.

First, perform the bootstrap. In this example, we will bootstrap the standard errors of the parameters estimated in a quantile regression.

 . use auto
(1978 Automobile Data)

. set seed 2001

. bs "qreg mpg weight length" "_b[weight] _b[length] _b[_cons]", reps(100)
saving(bsresults)

command:     qreg mpg weight length
statistics:  _b[weight] _b[length] _b[_cons]
(obs=74)

Bootstrap statistics

Variable |   Reps   Observed       Bias   Std. Err.   [95% Conf. Interval]
---------+-------------------------------------------------------------------
bs1 |    100  -.0052288   .0003904   .0022816    -.009756 -.0007015  (N)
|                                           -.0085497   .000165  (P)
|                                           -.0085544  6.54e-10 (BC)
---------+-------------------------------------------------------------------
bs2 |    100   -.003268  -.0167272   .0837841   -.1695139  .1629779  (N)
|                                           -.2194719  .0866159  (P)
|                                           -.2138037   .086627 (BC)
---------+-------------------------------------------------------------------
bs3 |    100   37.27451   1.851762   9.345439    18.73113  55.81789  (N)
|                                            28.43342  62.33828  (P)
|                                            28.43342  62.33828 (BC)
-----------------------------------------------------------------------------
N = normal, P = percentile, BC = bias-corrected


Second, use the saved bootstrap results to create the variance–covariance matrix.

 . use bsresults, clear
(bs: qreg mpg weight length)

. matrix accum VCE = bs1 bs2 bs3, deviations  noconstant
(obs=100)

. matrix VCE = VCE/(100-1)


Please note that the 100 in the denominator of this degrees-of-freedom adjustment represents the number of repetitions in the bootstrap sample.

 . matrix list VCE

symmetric VCE[3,3]
bs1         bs2         bs3
bs1   5.206e-06
bs2   -.0001871   .00701978
bs3   .01971814  -.76965181   87.337225


Third, retrieve the coefficients from an estimation using the full sample.

 . use auto, clear
(1978 Automobile Data)

. quietly qreg mpg weight length

. matrix b = e(b)

. matrix list b

b[1,3]
weight      length       _cons
y1  -.00522876  -.00326798    37.27451


Now, get the column names and row names for the new variance–covariance matrix.

 . matrix colnames VCE = weight length _cons

. matrix rownames VCE = weight length _cons


Finally, tell Stata to use the coefficient vector from the estimation on the full sample, along with the new variance–covariance matrix, and display the results.

 . estimates post b VCE

. estimates display
------------------------------------------------------------------------------
|      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
weight |  -.0052288   .0022816    -2.29   0.022    -.0097007   -.0007568
length |   -.003268   .0837841    -0.04   0.969    -.1674819    .1609459
_cons |   37.27451   9.345439     3.99   0.000     18.95779    55.59123
------------------------------------------------------------------------------