Announcement

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

  • Problems with bootstrapping using own program

    Dear list,

    I am using a very specific method with no ready-made Stata command to calculate a quantity of interest, and I was asked to provide bootstrap confidence interval for the quantity. I learnt that the bootstrap command in Stata can be combined with user-written program. However, I have difficulties using this function properly.

    Below is an example:

    Code:
    capture drop _all
    sysuse auto, clear
    
    program drop _all
    program myboot ,rclass
    
    replace price = price*2
    keep in 1
    /* my calculation eventually produces a data set with one observation and a few variables
    containing the values produced from my calculation
    (e.g., a latent measure of average level of happiness among British men as calculated using
    a complex method).
    I need to calculate the bootstrap CI of the values as recorded in these variables.
    */
    
    sum price, meanonly
    return scalar outcome=r(mean)
    end
    
    bootstrap outcome=r(outcome), reps(100): myboot
    As you can see, it does not work. What I really want to do is as follows, where I no longer use the bootstrap command but instead use bsample:

    Code:
    sysuse auto, clear
    set seed 1
    foreach boot of numlist 1(1)100 {
        preserve
        bsample
        replace price = price*2
        keep in 1
        gen outcome=price
        gen boot=`boot'
        tempfile b`boot'
        save `b`boot''
        restore
    }
    use `b1', clear
    forvalues p=2/100 {
        append using `b`p''
    }
    centile outcome, centile(2.5 50 97.5)
    I want to combine bootstrap with my own program instead of creating a lot of samples using bsample, as bootstrap also provides other useful functions (e.g., calculating different types of standard errors).

    Thank you!

  • #2
    You say in your code that your "calculation eventually produces a data set with one observation." Given that, using bootstrap sampling doesn't make sense. Perhaps you mean something different than what you say above. It occurs to me that perhaps you want to take a bootstrap sample, and then calculate a scalar value from that sample, and return that to -bootstrap-. If so, I think your best bet here is to explain differently and in more detail what you want your -myboot- program to calculated. (I can't understand the intent of what you are doing in your second block of code.)

    All that being said, I don't think your "keep in 1" does something worthwhile, as you don't indicate any steps that ensure that the first observation in your data set is going to be something distinct or meaningful. This makes me think that there's some misunderstanding here, either me about what you want, or you, about what your program -myboot- should be doing.

    Comment


    • #3
      Thank you for your reply, Mike Lacy. I have rephrased my question as follows:

      I want to take a bootstrap sample, and then calculate a scalar value from that sample using a method (no ready-made Stata commands can implement this method so I have to write my own program), and use -bootstrap- to run this program 500 times to obtain a bootstrap confidence interval for that scalar value. Below is an example in which I calculate the range of the variable price in the auto data. It does not work. I believe I must have missed something here. My calculation is more complex than that shown in the example, so I am looking for a more general solution to incorporating user-written program into -bootstrap-when the outcome of the program is a set of scalar values. I am not looking for a shortcut to the issue in this particular example (such as using bsample to do the bootstrapping manually). My program does not use regression so the program does not produce any ereturn values.

      Code:
      capture drop _all
      sysuse auto, clear
      
      program drop _all
      program myboot ,rclass
      
      sum price
      local pricemax=r(max)
      local pricemin=r(min)
      scalar range=`pricemax'-`pricemin'
      end
      
      bootstrap outcome=r(range), reps(100): myboot
      Thank you!

      Comment


      • #4
        That was a good re-phrasing of your question. You've just done one small thing wrong. Your program must use the -return- command to put your "range" scalar value into the r-class return list. That's how it becomes available to -bootstrap-.

        I'd also note that your use of -drop _all- is undesirable. You just need to drop the program you are about to create. Further as a matter of style, I'd recommend that you place the code to define your program before you load any data, although that is a personal preference.

        Try this:

        Code:
        // Define the program
        capture program drop myboot
        program myboot, rclass
        sum price
        local pricemax=r(max)
        local pricemin=r(min)
        // Tell Stata to create an item called r(range) in the r-class return list, and assign a value to it.
        return scalar range=`pricemax'-`pricemin'
        end
        //
        sysuse auto
        bootstrap outcome=r(range), reps(100): myboot
        Note that in this situation, your -return- statement could have been
        Code:
        return scalar range = r(max) - r(min)

        Comment


        • #5
          Thank you, Mike Lacy I really appreciate your help.

          Comment

          Working...
          X