 Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: Bootstrap not reporting observed coefficient as the mean of expression

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: Bootstrap not reporting observed coefficient as the mean of expression Date Tue, 19 Apr 2011 16:35:56 +0100

```It's trivial by comparison, and not even a problem, but note that

program define myderivative, rclass
version 11
tempname derivative2
quietly regress mpg weight weight2 gear foreign
capture drop derivative2
gen derivative2 =_b[weight] + 2*weight*_b[weight]
quietly summarize derivative2, meanonly
scalar `derivative2' = r(mean)
return scalar derivative2 = `derivative2'
end

can be slimmed down. You take a saved result, put it into a scalar,
and then save it as another scalar. But the relay race is not needed.
You can do it directly.

program define myderivative, rclass
version 11
quietly regress mpg weight weight2 gear foreign
capture drop derivative2
gen derivative2 =_b[weight] + 2*weight*_b[weight]
summarize derivative2, meanonly
return scalar derivative2 = r(mean)
end

-summarize, meanonly- is always done quietly.

On Tue, Apr 19, 2011 at 4:19 PM, Nick Cox <njcoxstata@gmail.com> wrote:
> The problem I think is that you are expecting something that
> -bootstrap- will not and cannot do. You are supplying an expression
> that defines a variable after -regress-, but each such expression fed
> to -bootstrap- should define a constant.
>
> Consider
>
> . regress mpg weight weight2 gear foreign
>
>      Source |       SS       df       MS              Number of obs =      74
> -------------+------------------------------           F(  4,    69) =   39.78
>       Model |   1704.4337     4  426.108425           Prob > F      =  0.0000
>    Residual |  739.025759    69  10.7105182           R-squared     =  0.6975
> -------------+------------------------------           Adj R-squared =  0.6800
>       Total |  2443.45946    73  33.4720474           Root MSE      =  3.2727
>
> ------------------------------------------------------------------------------
>         mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
> -------------+----------------------------------------------------------------
>      weight |   -.016404   .0039597    -4.14   0.000    -.0243033   -.0085047
>     weight2 |   1.65e-06   6.25e-07     2.64   0.010     4.04e-07    2.90e-06
>  gear_ratio |   1.772766    1.48421     1.19   0.236    -1.188151    4.733683
>     foreign |  -2.919893   1.214474    -2.40   0.019    -5.342702   -.4970839
>       _cons |   50.31328   8.083446     6.22   0.000     34.18724    66.43931
> ------------------------------------------------------------------------------
>
> . di (_b[weight] + 2*weight*_b[weight]),
> -96.143738
>
> . di (_b[weight] + 2*weight*_b[weight]),
> -96.143738
>
> After -regress- _b[weight] is defined, otherwise this would not work.
> However, -weight- will in this context always be interpreted as
> -weight-, the value in the first observation, which gives the
> results you found.
>
> Compare
>
> FAQ     . . . . . . . . . . . . . . . . . . . . .  if command vs. if qualifier
>        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  J. Wernow
>        6/00    I have an if or while command in my program that
>                only seems to evaluate the first observation,
>                what's going on?
>                http://www.stata.com/support/faqs/lang/ifqualifier.html
>
> for a different but also similar problem.
>
> Nick
>
> On Tue, Apr 19, 2011 at 2:52 PM, Jaime Ruiz-Tagle
> <jaimeruiztagle@gmail.com> wrote:
>> Hi all,
>>
>> I found a problem in bootstrap, which is not reporting the "mean" of
>> an expression as the "observed coefficient".
>>
>> Here comes an example where bootstrap gives the correct "observed
>> coefficient" only when invoked through a user defined program.
>>
>> Hopefully somebody can help me since I'm very puzzled.
>>
>> Thanks,
>>
>> Jaime.
>>
>>
>> // //////////////////////////////////////////
>> use http://www.stata-press.com/data/r11/auto, clear
>>
>> gen weight2=weight^2
>>
>> regress mpg weight weight2 gear foreign
>>
>> gen derivative1 = (_b[weight] + 2*weight*_b[weight])
>> sum derivative1, detail
>>
>> // Using bootstrap with expression:
>> bootstrap derivative = (_b[weight] + 2*weight*_b[weight]), reps(100)
>> seed(1): regress mpg weight weight2 gear foreign
>>
>> // The "observed coefficient" is reported as -96.14374, but the mean
>> is -99.07872
>>
>> // Now using a user defined program that does the same as expression above
>>
>> capture pr drop myderivative
>> program define myderivative, rclass
>>        version 11
>>        tempname derivative2
>>        quietly regress mpg weight weight2 gear foreign
>>        capture drop derivative2
>>        gen derivative2 =_b[weight] + 2*weight*_b[weight]
>>        quietly summarize derivative2, meanonly
>>        scalar `derivative2' = r(mean)
>>        return scalar derivative2 = `derivative2'
>> end
>>
>> bootstrap r(derivative2), reps(100) seed(1): myderivative
>>
>> // The "observed coefficient" is now reported as equal to the mean = -99.07872
>>
>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```