Announcement

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

  • Bsample and Manual implementation of Bootstrap give me very different results. Why is that and am I making some mistake?

    Good morning,

    I am implementing Bootstrap manually as explained in Angquist, Stata tip 92: Manual implementation of permutations and bootstraps. The results are vastly different from the results of bsample. Why is that, and am I making some mistake in my code?

    ***
    ----------------------------------------------------------------------------------------------------------------
    name: <unnamed>
    log: C:\Program Files\Stata15\bootLog.log
    log type: text
    opened on: 29 Aug 2018, 10:47:10

    .
    . cap program drop boot1 boot2

    .
    .
    .
    . program define boot1, rclass
    1. args obs
    2.
    . drop _all
    3.
    . set obs `obs'
    4.
    .
    . * I generate one sample from N(0,1000), then I calculate the mean
    .
    . gen x = 1000*rnormal()
    5.
    . summ x
    6.
    . return sca meanx1 = r(mean)
    7.
    . * Then I draw a Bootstrap sample using Bsample and calculate the mean
    .
    . bsample
    8.
    . summ x
    9.
    . return sca meanxboot1 = r(mean)
    10.
    . end

    .
    .
    .
    . program define boot2, rclass
    1. args obs
    2.
    . drop _all
    3.
    . set obs `obs'
    4.
    . * I generate one sample from N(0,1000), then I calculate the mean
    .
    . gen x = 1000*rnormal()
    5.
    . summ x
    6.
    . return sca meanx2 = r(mean)
    7.
    . * Then I draw a Bootstrap sample manually and calculate the mean
    .
    . generate u=ceil(runiform()*_N) // this generates a uniformly distributed set of integers on 1,2,...,_N
    8. replace x =x[u] // this replaces the original x by the value of x at the random univorm integer on 1,2,...,
    > _N
    9.
    . summ x
    10.
    . return sca meanxboot2 = r(mean)
    11.
    . end

    .
    .
    .
    . simulate meanx1=r(meanx1) meanxboot1=r(meanxboot1), reps(10000) seed(69) saving(boot1temp, replace) nodots: bo
    > ot1 999

    command: boot1 999
    meanx1: r(meanx1)
    meanxboot1: r(meanxboot1)


    .
    .
    .
    .
    . simulate meanx2=r(meanx2) meanxboot2=r(meanxboot2), reps(10000) seed(69) nodots: boot2 999

    command: boot2 999
    meanx2: r(meanx2)
    meanxboot2: r(meanxboot2)


    .
    .
    . merge using boot1temp
    (note: you are using old merge syntax; see [D] merge for new syntax)

    .
    . summ meanx1 meanxboot1 meanx2 meanxboot2

    Variable | Obs Mean Std. Dev. Min Max
    -------------+---------------------------------------------------------
    meanx1 | 10,000 .5236758 31.18312 -131.0225 108.7608
    meanxboot1 | 10,000 .3181329 44.58329 -171.1018 175.1777
    meanx2 | 10,000 .2532531 31.49465 -126.415 116.299
    meanxboot2 | 10,000 .3867981 66.27218 -256.7187 262.4239

    .
    . twoway (kdensity meanx1) (kdensity meanxboot1) (kdensity meanx2) (kdensity meanxboot2)

    .
    . log close
    name: <unnamed>
    log: C:\Program Files\Stata15\bootLog.log
    log type: text
    closed on: 29 Aug 2018, 10:47:30
    ----------------------------------------------------------------------------------------------------------------
    ***


    Note that the distribution of the bsample generated bootstrap mean (meanxboot1, std=44.58, min= -171.10, max= 175.17) is vastly different from the distribution of the manual bootstrap generated bootstrap mean (meanxboot2, std=66.27, min=-256.71, max=262.42). The manually generated bootstrap mean has much more spread out distribution.



Working...
X