Created
May 11, 2022 19:50
-
-
Save erikcs/bfb262c2bb78e97d63499195a0797208 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
true = mean(pmax(0.5 * rnorm(1e5), 0)) | |
r = replicate(100, { | |
n <- 2000 | |
p <- 5 | |
X <- matrix(rnorm(n * p), n, p) | |
Z <- runif(n, -4, 4) | |
cutoff <- 0 | |
W <- as.numeric(Z >= cutoff) | |
tau <- pmax(0.5 * X[, 1], 0) | |
Y <- tau * W + 1 / (1 + exp(2 * Z)) + 0.2 * rnorm(n) | |
# Compute the Imbens-Kalyanaraman MSE-optimal bandwidth for a local linear regression. | |
if (require("rdd", quietly = TRUE)) { | |
bandwidth <- IKbandwidth(Z, Y, cutoff) | |
} else { | |
bandwidth <- 1.1 | |
} | |
# Compute kernel weights corresponding to a triangular kernel. | |
dist <- abs((Z - cutoff) / bandwidth) | |
sample.weights <- (1 - dist) * (dist <= 1) / bandwidth | |
lmf <- lm_forest(X, Y, cbind(W, Z), sample.weights = sample.weights, gradient.weights = c(1, 0)) | |
tau.hat <- predict(lmf)$predictions[, 1, ] | |
mean(tau.hat) | |
}) | |
hist(r, breaks = 10) | |
abline(v = true, col = 'red') | |
summary(r) | |
# Min. 1st Qu. Median Mean 3rd Qu. Max. | |
# 0.06025 0.14157 0.17896 0.17591 0.20802 0.31772 | |
true | |
# [1] 0.2005563 | |
Author
erikcs
commented
May 11, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment