R/wboot.ipw.panel.R

Defines functions wboot.ipw.panel

# Bootrstapped IPW Difference-in-Differences with Panel Data
# 2 periods and 2 groups

wboot.ipw.panel <- function(nn, n, deltaY, D, int.cov, i.weights,
                            trim.level = 0.995){
  #-----------------------------------------------------------------------------
  v <- stats::rexp(n)
  #v <- v / mean(v)
  #weights for the bootstrap
  b.weights <- as.vector(i.weights * v)
  # Propensity score estimation
  # ps.b <- suppressWarnings(stats::glm(D ~ -1 + int.cov, family = "binomial", weights = b.weights)$fitted.values)
  ps.b <- suppressWarnings(fastglm::fastglm(x = int.cov,
                                            y = D,
                                            family = stats::binomial(),
                                            weights =  b.weights,
                                            intercept = FALSE,
                                            method = 3)$fitted.values)
  ps.b <- pmin(ps.b, 1 - 1e-6)
  trim.ps <- (ps.b < 1.01)
  trim.ps[D==0] <- (ps.b[D==0] < trim.level)
  # Compute IPW estimator
  att.b <- mean(b.weights * trim.ps * (D - ps.b * (1 - D)/(1 - ps.b)) * deltaY) / mean(b.weights * D)

  #-----------------------------------------------------------------------------
  return(att.b)
}

Try the DRDID package in your browser

Any scripts or data that you put into this service are public.

DRDID documentation built on June 8, 2025, 10:17 a.m.