Bootstrap example

Consider the first example of decomposition in the vignette("decr_intro", package = "decr"):

# load the package
library(decr)

# load the invented_wages dataset, which is used in the examples
data(invented_wages)

# Establishment of common support between two groups, based on
# the distributions of their characteristics (variables);
# computes counterfactual weights (w_BA and w_AB), that can be used
# to balance the joint distribution of characteristics of one group
# to that of the other group
r01 <- reweight_strata_all2(
  data = invented_wages,
  treatment = "gender", 
  variables = c("sector", "education"),
  y = "wage", 
  weights = "sample_weights")

# nopodec_mean: estimates all the elements to perform a decomposition
# of the difference of the average of an outcome variable (y)
# between the two groups in four components (see later...)
s01 <- nopodec_mean(r01)
s01


# nopodec: decomposition of the observed difference between averages 
# of y of group A and B in 4 components, as in Nopo (2008)
n01_AB <- nopodec(s01, counterfactual = "AB")
n01_AB

These are point estimates of the components of the decomposition of the difference of average wages of men and women.

In order to estimate confidence intervals as well, there is the boot_nopodec_mean function, which takes the same arguments of reweight_strata_all2 (data, treatment, variables, y, weights) and nopodec (counterfactual) plus two additional arguments:

In short, boot_nopodec_mean performs a nonparametric bootstrap, stratified by the strata defined by the variables used for each group, and returns an object of class boot (from package boot). Here below an example with R = 50 bootstrap replicates:

b01 <- boot_nopodec_mean(
  data = invented_wages, 
  treatment = "gender", 
  variables = c("sector", "education"), 
  y = "wage", 
  weights = "sample_weights", 
  R = 50, 
  counterfactual = "AB"
  )

b01

b01 is an object of class boot. The function boot.ci from the boot package can be used to estimate confidence intervals of the decomposition components:

boot::boot.ci(b01, type = "perc")
boot::boot.ci(b01, type = "perc", index = 2)
boot::boot.ci(b01, type = "perc", index = 3)
boot::boot.ci(b01, type = "perc", index = 4)
boot::boot.ci(b01, type = "perc", index = 5)
b01$t0

Using parallel computation

Since bootstrap can take quite long time, parallel computation can be done by using the parallel and ncpus arguments in the boot_nopodec_mean function (these are added to the ... argument). See the documentation of ?boot::boot for more details.

Here an example using 4 CPUs:

b01 <- boot_nopodec_mean(
  data = invented_wages, 
  treatment = "gender", 
  variables = c("sector", "education"), 
  y = "wage", 
  weights = "sample_weights", 
  R = 500, 
  counterfactual = "AB",
  parallel = "snow",
  ncpus = 4
  )


gibonet/decr documentation built on Jan. 5, 2024, 7:26 a.m.