Skip to content

Commit

Permalink
adapt the unit tests for the bootstrap algo in accordance with #98 #99
Browse files Browse the repository at this point in the history
  • Loading branch information
MalteKurz committed May 10, 2021
1 parent e6632d6 commit 66a57cb
Show file tree
Hide file tree
Showing 21 changed files with 27 additions and 63 deletions.
41 changes: 9 additions & 32 deletions tests/testthat/helper-01-helper_functions.R
Expand Up @@ -36,40 +36,17 @@ draw_bootstrap_weights = function(bootstrap, n_rep_boot, n_obs) {
}


functional_bootstrap = function(theta, se, psi, psi_a, k, smpls, dml_procedure,
functional_bootstrap = function(theta, se, psi, psi_a, k, smpls,
n_rep_boot, weights) {
if (dml_procedure == "dml1") {
test_ids = smpls$test_ids
boot_coef = matrix(NA, nrow = k, ncol = n_rep_boot)
boot_t_stat = matrix(NA, nrow = k, ncol = n_rep_boot)
ii = 0
for (i_fold in 1:k) {
test_index = test_ids[[i_fold]]

score = psi[test_index]
J = mean(psi_a[test_index])

n_obs_in_fold = length(test_index)
for (i in seq(n_rep_boot)) {
boot_coef[i_fold, i] = mean(weights[i, (ii + 1):(ii + n_obs_in_fold)] * 1 / J * score)
boot_t_stat[i_fold, i] = boot_coef[i_fold, i] / se
}
ii = ii + n_obs_in_fold
}
boot_coef = colMeans(boot_coef)
boot_t_stat = colMeans(boot_t_stat)
} else {
# DML2
score = psi
J = mean(psi_a)
boot_coef = matrix(NA, nrow = 1, ncol = n_rep_boot)
boot_t_stat = matrix(NA, nrow = 1, ncol = n_rep_boot)
for (i in seq(n_rep_boot)) {
boot_coef[1, i] = mean(weights[i, ] * 1 / J * score)
boot_t_stat[1, i] = boot_coef[1, i] / se
}
score = psi
J = mean(psi_a)
boot_coef = matrix(NA, nrow = 1, ncol = n_rep_boot)
boot_t_stat = matrix(NA, nrow = 1, ncol = n_rep_boot)
for (i in seq(n_rep_boot)) {
boot_coef[1, i] = mean(weights[i, ] * 1 / J * score)
boot_t_stat[1, i] = boot_coef[1, i] / se
}

res = list(boot_coef = boot_coef, boot_t_stat = boot_t_stat)
return(res)
}
Expand Down
12 changes: 6 additions & 6 deletions tests/testthat/helper-08-dml_plr.R
Expand Up @@ -292,15 +292,15 @@ var_plr = function(theta, d, u_hat, v_hat, v_hatd, score) {

# Bootstrap Implementation for Partially Linear Regression Model
bootstrap_plr = function(thetas, ses, data, y, d,
n_folds, smpls, all_preds, dml_procedure,
n_folds, smpls, all_preds,
bootstrap, n_rep_boot, score,
n_rep=1) {
for (i_rep in 1:n_rep) {
n = nrow(data)
weights = draw_bootstrap_weights(bootstrap, n_rep_boot, n)
this_res = boot_plr_single_split(thetas[i_rep], ses[i_rep],
data, y, d, n_folds, smpls[[i_rep]],
all_preds[[i_rep]], dml_procedure,
all_preds[[i_rep]],
weights, n_rep_boot, score)
if (i_rep==1) {
boot_res = this_res
Expand All @@ -314,7 +314,7 @@ bootstrap_plr = function(thetas, ses, data, y, d,


boot_plr_multitreat = function(thetas, ses, data, y, d,
n_folds, smpls, all_preds, dml_procedure,
n_folds, smpls, all_preds,
bootstrap, n_rep_boot, score,
n_rep=1) {
n_d = length(d)
Expand All @@ -325,7 +325,7 @@ boot_plr_multitreat = function(thetas, ses, data, y, d,
for (i_d in seq(n_d)) {
this_res = boot_plr_single_split(thetas[[i_rep]][i_d], ses[[i_rep]][i_d],
data, y, d[i_d], n_folds, smpls[[i_rep]],
all_preds[[i_rep]][[i_d]], dml_procedure,
all_preds[[i_rep]][[i_d]],
weights, n_rep_boot, score)
boot_theta[i_d, ] = this_res$boot_coef
boot_t_stat[i_d, ] = this_res$boot_t_stat
Expand All @@ -344,7 +344,7 @@ boot_plr_multitreat = function(thetas, ses, data, y, d,


boot_plr_single_split = function(theta, se, data, y, d,
n_folds, smpl, all_preds, dml_procedure,
n_folds, smpl, all_preds,
weights, n_rep_boot, score) {
residuals = compute_plr_residuals(data, y, d, n_folds,
smpl, all_preds)
Expand All @@ -365,6 +365,6 @@ boot_plr_single_split = function(theta, se, data, y, d,
res = functional_bootstrap(theta, se,
psi, psi_a, n_folds,
smpl,
dml_procedure, n_rep_boot, weights)
n_rep_boot, weights)
return(res)
}
4 changes: 2 additions & 2 deletions tests/testthat/helper-09-dml_plriv.R
Expand Up @@ -180,7 +180,7 @@ var_plriv = function(theta, u_hat, v_hat, w_hat, score) {

# Bootstrap Implementation for Partially Linear Regression Model
bootstrap_plriv = function(theta, se, data, y, d, z, n_folds, smpls,
all_preds, dml_procedure, bootstrap, n_rep_boot,
all_preds, bootstrap, n_rep_boot,
n_rep=1) {
for (i_rep in 1:n_rep) {
residuals = compute_plriv_residuals(data, y, d, z, n_folds,
Expand All @@ -196,7 +196,7 @@ bootstrap_plriv = function(theta, se, data, y, d, z, n_folds, smpls,
weights = draw_bootstrap_weights(bootstrap, n_rep_boot, n)
this_res = functional_bootstrap(theta, se, psi, psi_a, n_folds,
smpls[[i_rep]],
dml_procedure, n_rep_boot, weights)
n_rep_boot, weights)
if (i_rep==1) {
boot_res = this_res
} else {
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/helper-10-dml_irm.R
Expand Up @@ -228,7 +228,7 @@ var_irm = function(theta, g0_hat, g1_hat, u0_hat, u1_hat, d, p_hat, m, y, score)

# Bootstrap Implementation for Interactive Regression Model
bootstrap_irm = function(theta, se, data, y, d, n_folds, smpls, all_preds,
dml_procedure, score, bootstrap, n_rep_boot,
score, bootstrap, n_rep_boot,
n_rep=1, trimming_threshold = 1e-12) {
for (i_rep in 1:n_rep) {
res = extract_irm_residuals(data, y, d, n_folds,
Expand All @@ -255,7 +255,7 @@ bootstrap_irm = function(theta, se, data, y, d, n_folds, smpls, all_preds,
weights = draw_bootstrap_weights(bootstrap, n_rep_boot, n)
this_res = functional_bootstrap(theta, se, psi, psi_a, n_folds,
smpls[[i_rep]],
dml_procedure, n_rep_boot, weights)
n_rep_boot, weights)
if (i_rep==1) {
boot_res = this_res
} else {
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/helper-11-dml_irmiv.R
Expand Up @@ -288,7 +288,7 @@ var_irmiv = function(theta, p_hat, mu0_hat, mu1_hat, m0_hat, m1_hat, d, y, z, sc

# Bootstrap Implementation for Interactive Instrumental Variable Regression Model
bootstrap_irmiv = function(theta, se, data, y, d, z, n_folds, smpls, all_preds,
dml_procedure, score, bootstrap, n_rep_boot,
score, bootstrap, n_rep_boot,
n_rep=1, trimming_threshold = 1e-12) {
for (i_rep in 1:n_rep) {
res = extract_iivm_preds(data, y, d, z, n_folds,
Expand Down Expand Up @@ -318,7 +318,7 @@ bootstrap_irmiv = function(theta, se, data, y, d, z, n_folds, smpls, all_preds,
weights = draw_bootstrap_weights(bootstrap, n_rep_boot, n)
this_res = functional_bootstrap(theta, se, psi, psi_a, n_folds,
smpls[[i_rep]],
dml_procedure, n_rep_boot, weights)
n_rep_boot, weights)
if (i_rep==1) {
boot_res = this_res
} else {
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/helper-13-dml_pliv_partial_x.R
Expand Up @@ -190,7 +190,7 @@ var_pliv_partial_x = function(theta, u_hat, w_hat, r_hat_tilde, score) {


bootstrap_pliv_partial_x = function(theta, se, data, y, d, z, n_folds, smpls,
all_preds, dml_procedure, bootstrap,
all_preds, bootstrap,
n_rep_boot, n_rep=1) {
for (i_rep in 1:n_rep) {
residuals = compute_pliv_partial_x_residuals(data, y, d, z, n_folds,
Expand All @@ -207,7 +207,7 @@ bootstrap_pliv_partial_x = function(theta, se, data, y, d, z, n_folds, smpls,
weights = draw_bootstrap_weights(bootstrap, n_rep_boot, n)
this_res = functional_bootstrap(theta, se, psi, psi_a, n_folds,
smpls[[i_rep]],
dml_procedure, n_rep_boot, weights)
n_rep_boot, weights)
if (i_rep==1) {
boot_res = this_res
} else {
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/helper-14-dml_pliv_partial_z.R
Expand Up @@ -130,7 +130,7 @@ var_pliv_partial_z = function(theta, r_hat, y, d, score) {


bootstrap_pliv_partial_z = function(theta, se, data, y, d, z, n_folds, smpls,
all_preds, dml_procedure, bootstrap,
all_preds, bootstrap,
n_rep_boot, n_rep=1) {
for (i_rep in 1:n_rep) {
residuals = compute_pliv_partial_z_residuals(data, y, d, z, n_folds,
Expand All @@ -147,7 +147,7 @@ bootstrap_pliv_partial_z = function(theta, se, data, y, d, z, n_folds, smpls,
weights = draw_bootstrap_weights(bootstrap, n_rep_boot, n)
this_res = functional_bootstrap(theta, se, psi, psi_a, n_folds,
smpls[[i_rep]],
dml_procedure, n_rep_boot, weights)
n_rep_boot, weights)
if (i_rep==1) {
boot_res = this_res
} else {
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/helper-15-dml_pliv_partial_xz.R
Expand Up @@ -178,7 +178,7 @@ var_pliv_partial_xz = function(theta, u_hat, v_hat, w_hat, score) {


bootstrap_pliv_partial_xz = function(theta, se, data, y, d, z, n_folds, smpls,
all_preds, dml_procedure, bootstrap,
all_preds, bootstrap,
n_rep_boot, n_rep=1) {
for (i_rep in 1:n_rep) {
residuals = compute_pliv_partial_xz_residuals(data, y, d, z, n_folds,
Expand All @@ -195,7 +195,7 @@ bootstrap_pliv_partial_xz = function(theta, se, data, y, d, z, n_folds, smpls,
weights = draw_bootstrap_weights(bootstrap, n_rep_boot, n)
this_res = functional_bootstrap(theta, se, psi, psi_a, n_folds,
smpls[[i_rep]],
dml_procedure, n_rep_boot, weights)
n_rep_boot, weights)
if (i_rep==1) {
boot_res = this_res
} else {
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-double_ml_iivm.R
Expand Up @@ -44,7 +44,6 @@ patrick::with_parameters_test_that("Unit tests for IIVM:",
y = "y", d = "d", z = "z",
n_folds = 5, smpls = iivm_hat$smpls,
all_preds= iivm_hat$all_preds,
dml_procedure = dml_procedure,
score = score,
bootstrap = "normal", n_rep_boot = n_rep_boot)$boot_coef

Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-double_ml_iivm_trim.R
Expand Up @@ -44,7 +44,6 @@ patrick::with_parameters_test_that("Unit tests for IIVM:",
y = "y", d = "d", z = "z",
n_folds = 5, smpls = iivm_hat$smpls,
all_preds= iivm_hat$all_preds,
dml_procedure = dml_procedure,
score = score,
bootstrap = "normal", n_rep_boot = n_rep_boot,
trimming_threshold = trimming_threshold)$boot_coef
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-double_ml_irm.R
Expand Up @@ -44,7 +44,6 @@ patrick::with_parameters_test_that("Unit tests for IRM:",
y = "y", d = "d",
n_folds = 5, smpls = irm_hat$smpls,
all_preds= irm_hat$all_preds,
dml_procedure = dml_procedure,
score = score,
bootstrap = "normal", n_rep_boot = n_rep_boot)$boot_coef

Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-double_ml_irm_trim.R
Expand Up @@ -44,7 +44,6 @@ patrick::with_parameters_test_that("Unit tests for IRM:",
y = "y", d = "d",
n_folds = 5, smpls = irm_hat$smpls,
all_preds= irm_hat$all_preds,
dml_procedure = dml_procedure,
score = score,
bootstrap = "normal", n_rep_boot = n_rep_boot,
trimming_threshold = trimming_threshold)$boot_coef
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-double_ml_pliv.R
Expand Up @@ -41,7 +41,6 @@ patrick::with_parameters_test_that("Unit tests for PLIV:",
y = "y", d = "d", z = "z",
n_folds = 5, smpls = pliv_hat$smpls,
all_preds= pliv_hat$all_preds,
dml_procedure = dml_procedure,
bootstrap = "normal", n_rep_boot = n_rep_boot)$boot_coef

set.seed(i_setting)
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-double_ml_pliv_partial_x.R
Expand Up @@ -35,7 +35,6 @@ patrick::with_parameters_test_that("Unit tests for PLIV.partialX:",
y = "y", d = "d", z = paste0("Z", 1:dim_z),
n_folds = 5, smpls = pliv_hat$smpls,
all_preds= pliv_hat$all_preds,
dml_procedure = dml_procedure,
bootstrap = "normal", n_rep_boot = n_rep_boot)$boot_coef

set.seed(i_setting)
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-double_ml_pliv_partial_xz.R
Expand Up @@ -35,7 +35,6 @@ patrick::with_parameters_test_that("Unit tests for PLIV.partialXZ:",
y = "y", d = "d", z = paste0("Z", 1:dim_z),
n_folds = 5, smpls = pliv_hat$smpls,
all_preds= pliv_hat$all_preds,
dml_procedure = dml_procedure,
bootstrap = "normal", n_rep_boot = n_rep_boot)$boot_coef

set.seed(i_setting)
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-double_ml_pliv_partial_z.R
Expand Up @@ -35,7 +35,6 @@ patrick::with_parameters_test_that("Unit tests for PLIV.partialZ:",
y = "y", d = "d", z = paste0("Z", 1:dim_z),
n_folds = 5, smpls = pliv_hat$smpls,
all_preds= pliv_hat$all_preds,
dml_procedure = dml_procedure,
bootstrap = "normal", n_rep_boot = n_rep_boot)$boot_coef

set.seed(i_setting)
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-double_ml_plr.R
Expand Up @@ -45,7 +45,6 @@ patrick::with_parameters_test_that("Unit tests for PLR:",
y = "y", d = "d",
n_folds = n_folds, smpls = plr_hat$smpls,
all_preds= plr_hat$all_preds,
dml_procedure = dml_procedure,
bootstrap = "normal", n_rep_boot = n_rep_boot,
score = score)$boot_coef

Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-double_ml_plr_classifier.R
Expand Up @@ -47,7 +47,6 @@ patrick::with_parameters_test_that("Unit tests for PLR with classifier for ml_m:
y = "y", d = "d",
n_folds = n_folds, smpls = plr_hat$smpls,
all_preds= plr_hat$all_preds,
dml_procedure = dml_procedure,
bootstrap = "normal", n_rep_boot = n_rep_boot,
score = score)$boot_coef

Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-double_ml_plr_multitreat.R
Expand Up @@ -53,7 +53,6 @@ patrick::with_parameters_test_that("Unit tests for PLR:",
y = "y", d = c("d1", "d2", "d3"),
n_folds = n_folds, smpls = plr_hat$smpls,
all_preds= plr_hat$all_preds,
dml_procedure = dml_procedure,
bootstrap = "normal", n_rep_boot = n_rep_boot,
score = score)$boot_coef

Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-double_ml_plr_rep_cross_fit.R
Expand Up @@ -50,7 +50,6 @@ patrick::with_parameters_test_that("Unit tests for PLR:",
n_folds = n_folds, n_rep = n_rep,
smpls = plr_hat$smpls,
all_preds= plr_hat$all_preds,
dml_procedure = dml_procedure,
bootstrap = "normal", n_rep_boot = n_rep_boot,
score = score)$boot_coef

Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-double_ml_set_samples.R
Expand Up @@ -48,7 +48,6 @@ patrick::with_parameters_test_that("Unit tests for PLR:",
y = "y", d = "d",
n_folds = n_folds, smpls = plr_hat$smpls,
all_preds= plr_hat$all_preds,
dml_procedure = dml_procedure,
bootstrap = "normal", n_rep_boot = n_rep_boot,
score = score)$boot_coef

Expand Down

0 comments on commit 66a57cb

Please sign in to comment.