0

I am replicating a paper by Clément de Chaisemartin: Two-Way Fixed Effects Estimators with Heterogeneous Treatment Effects.

I face a problem with Stata command bsample - Sampling with replacement.

I cannot understand what bsample, cluster(nr) Stata command is doing in for loop and what its results are.

I need to convert bsample, cluster(nr) Stata code into R.

sample data view

Stata code below:

set seed 1
quietly {
forvalue i=1/200 {
    preserve
    bsample, cluster(nr)
    xtreg lwage d81-d87 union, fe robust
    scalar betafe=_b[union]
    reg diff_lwage diff_union d82-d87, cluster(nr)
    scalar betafd=_b[diff_union]
    did_multiplegt lwage nr year union
    scalar did_m=e(effect_0)
    matrix B=B\(betafe-betafd,betafe-did_m,betafd-did_m)
    restore
}
}
Nick Cox
  • 35,529
  • 6
  • 31
  • 47
  • Documentation of `bsample` is online at https://www.stata.com/manuals/rbsample.pdf regardless of whether any reader knowing R well has Stata installed. Executive summary: it is taking a bootstrap sample. – Nick Cox Dec 12 '22 at 12:19
  • Stack Overflow does not work well as a code translation or writing service. – Nick Cox Dec 12 '22 at 12:22
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Dec 12 '22 at 12:52

1 Answers1

0

The State command bsample, cluster(nr) is doing a clustered bootstrap - it resamples cluster ids and then takes all the observations in each resampled cluster. While there would be lots of ways to do this in R, one is:

ids <- na.omit(unique(data$nr))
for(i in 1:nBootstraps){
  samp <- sample(ids, length(ids), replace=TRUE)
  samps <- lapply(samp, function(x)data[which(nr == x), ])
  bs_data <- do.call(rbind, samps)
  ... other stuff ...
}

Where ... other stuff ... are the other functions executed on the bootstrapped data.

DaveArmstrong
  • 18,377
  • 2
  • 13
  • 25