Bookmark and Share

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


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: bootstrap weighted mean


From   Joseph Coveney <[email protected]>
To   [email protected]
Subject   Re: st: bootstrap weighted mean
Date   Wed, 2 Oct 2013 16:32:14 +0900

Shuaizhang Feng wrote:

I was trying to bootstrap standard errors for the mean of two
variables. When I do so for simple means, i use the following ado
file.

program myratio, rclass
         summarize `1'
          local a1 = r(mean)
          summarize `2'
          local a2 = r(mean)
 return scalar ratio = `a1'/`a2'
end

and then do the following:

.bootstrap r(ratio), reps(100) saving(cratio, replace): myratio u1 u2, detail

and this gives the results as expected.

however, when i wanted to do ratio of weighted means, i modified the
ado file as follows:

program myratio_w, rclass
      summarize `1'[iw=`3']
          local a1 = r(mean)
      summarize `2'[iw=`3']
          local a2 = r(mean)
 return scalar ratio = `a1'/`a2'
end


and then use:

bootstrap r(ratio), reps(100) saving(cratio, replace): myratio_w u1 u2
weight, detail

then stata does not work properly and gives me the following message:

invalid syntax
an error occurred when bootstrap executed myratio_w
r(198);


I am wondering if anyone from the list can help me out. the difference
between the two ado files seem small Thanks in advance.

--------------------------------------------------------------------------------

The return code is pretty nonspecific.  You can try a couple of
things.  First, turn on tracing:  -set trace on-.  Then, try running
the called command (-myratio_w-) by itself to see where the code
balks.  The message from -bootstrap- implies that the called program
is the problem, but if you don't see any problem running it alone,
then leave tracing on and run the -bootstrap- command in its entirety.

By the way, what's with the -detail- option?  I don't see that option
defined for either your nonweighted command -myratio- or for the
weighted command -myratio_w-.  Neither should have worked . . .

As an alternative, you can always have importance weights as an option
using Stata's syntax (-help syntax-) and that way have a single more
general command.  You'll need to use the -force- option for
-bootstrap- to use importance weights in the called command, but
that's your responsibility, anyway.

Joseph Coveney

. version 13.0

.
. clear *

. set more off

.
. program define myratio, rclass
  1.         version 13.0
  2.         syntax varlist(min=2 max=2 numeric) [iweight]
  3.
.         local weight = cond("`weight'" == "", "", "[`weight'`exp']")
  4.
.         gettoken left right : varlist
  5.
.         summarize `left' `weight', meanonly
  6.         tempname top
  7.         scalar define `top' = r(mean)
  8.
.         summarize `right' `weight', meanonly
  9.         return scalar ratio = `top' / r(mean)
 10. end

.
. set seed `=date("2013-10-02", "YMD")'

. sysuse auto, clear
(1978 Automobile Data)

.
. bootstrap ratio = r(ratio), reps(100) nodots nowarn: myratio weight length

Bootstrap results                               Number of obs      =        74
                                                Replications       =       100

      command:  myratio weight length
        ratio:  r(ratio)

------------------------------------------------------------------------------
             |   Observed   Bootstrap                         Normal-based
             |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
       ratio |   16.06673    .275616    58.29   0.000     15.52653    16.60693
------------------------------------------------------------------------------

.
. generate double iweight = runiform()

. bootstrap ratio = r(ratio), reps(100) nodots nowarn force: ///
>         myratio weight length [iw=iweight]

Bootstrap results                               Number of obs      =        36
                                                Replications       =       100

      command:  myratio weight length [iweight= iweight]
        ratio:  r(ratio)

------------------------------------------------------------------------------
             |   Observed   Bootstrap                         Normal-based
             |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
       ratio |   15.81428   .3546588    44.59   0.000     15.11916    16.50939
------------------------------------------------------------------------------

.
. exit

end of do-file
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index