Announcement

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

  • Bootstrapping across multiple lines of code

    I am trying to do a bootstrap exercise where I run a regression for each bootstrap and then compute a number of further exercises for each bootstrap and then run another regression. My output I would like is a bootstrap estimate of the std error for the last regression. I am struggling to understand the manual, but I think should be a fairly simple task. In other words, is it possible to run a bootstrap program where multiple lines of code are included in the bootstrap rather than just the one line that seems to be in all the manual examples.

    I have mocked up a basic example here using the auto data. I want to run a regression for a subset of data, and then I want to predict the fitted value for the full data, and then run a second regression on those fitted values. (This is only a mock, so I am not interested in other ways to do this simple task. In practice I have another 20 lines of code that I would like to run for each bootstrap).

    How do i get a bootstrap estimate of the second regression, knowing that i need to run a bootstrap for the first 3 lines of code as well.

    Code:
    sysuse auto, clear
    
    bootstrap, reps(5): areg price trunk weight displ if headroom<3, a(foreign)
    predict Xprice_tmp, d
    egen Xprice                                            = mean(Xprice_tmp), by(foreign) 
    reg headroom Xprice
    Cheers,
    Mike

  • #2
    I am not sure what you want to achieve. Is this a nested bootstrap? I assume this is not, this sounds strange. You probably want to write a wrapper program and then bootstrap this program. For example

    Code:
    clear all
    sysuse auto
    
    
    cap program drop test
    program define test, rclass
        areg price trunk weight displ if headroom<3, a(foreign)
        predict Xprice_tmp, d
        egen Xprice = mean(Xprice_tmp), by(foreign)
        reg headroom Xprice
        return scalar Xprice = _b[Xprice]
        drop Xprice_tmp Xprice
    end
    
    
    *Test program*
    test
    return list
    
    
    *Bootstrap*
    bootstrap r(Xprice), reps(100) seed(123) nodots: test
    Best wishes

    Stata 18.0 MP | ORCID | Google Scholar

    Comment


    • #3
      This is exactly what I wanted. Thank you so much.

      Comment


      • #4
        Note that examples 4 and 5 in the PDF documentation of bootstrap discuss this technique of using bootstrap with a user-written program containing multiple commands.

        Comment

        Working...
        X