Skip to content

Commit 2822673

Browse files
committed
Merge branch 'master' of github.com:DoubleML/doubleml-for-r into 0.3.X
2 parents a08d25b + 2220df3 commit 2822673

11 files changed

+59
-58
lines changed

R/double_ml.R

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ DoubleML = R6Class("DoubleML",
690690
message("fit() not yet called.")
691691
} else {
692692
k = length(self$coef)
693-
table = matrix(NA, ncol = 4, nrow = k)
693+
table = matrix(NA_real_, ncol = 4, nrow = k)
694694
rownames(table) = names(self$coef)
695695
colnames(table) = c("Estimate.", "Std. Error", "t value", "Pr(>|t|)")
696696
table[, 1] = self$coef
@@ -700,9 +700,8 @@ DoubleML = R6Class("DoubleML",
700700
private$summary_table = table
701701

702702
if (length(k)) {
703-
print(paste(
704-
"Estimates and significance testing of the",
705-
"effect of target variables"))
703+
cat("Estimates and significance testing of the",
704+
"effect of target variables\n")
706705
res = as.matrix(printCoefmat(private$summary_table,
707706
digits = digits,
708707
P.values = TRUE,
@@ -755,9 +754,8 @@ DoubleML = R6Class("DoubleML",
755754
a = c(a, 1 - a)
756755
pct = format.perc(a, 3)
757756
fac = qnorm(a)
758-
ci = array(NA_real_, dim = c(length(parm), 2L), dimnames = list(
759-
parm,
760-
pct))
757+
ci = array(NA_real_, dim = c(length(parm), 2L),
758+
dimnames = list(parm, pct))
761759
ci[] = self$coef[parm] + self$se[parm] %o% fac
762760
}
763761

@@ -766,7 +764,8 @@ DoubleML = R6Class("DoubleML",
766764
a = (1 - level)
767765
ab = c(a / 2, 1 - a / 2)
768766
pct = format.perc(ab, 3)
769-
ci = array(NA, dim = c(length(parm), 2L), dimnames = list(parm, pct))
767+
ci = array(NA_real_, dim = c(length(parm), 2L),
768+
dimnames = list(parm, pct))
770769

771770
if (all(is.na(self$boot_coef))) {
772771
stop(paste(
@@ -970,8 +969,8 @@ DoubleML = R6Class("DoubleML",
970969
t_stat_ = NULL,
971970
tuning_res_ = NULL,
972971
n_rep_boot = NULL,
973-
i_rep = NA,
974-
i_treat = NA,
972+
i_rep = NA_integer_,
973+
i_treat = NA_integer_,
975974
fold_specific_params = NULL,
976975
summary_table = NULL,
977976
learner_class = list(),
@@ -1168,47 +1167,49 @@ DoubleML = R6Class("DoubleML",
11681167
},
11691168
initialize_arrays = function() {
11701169

1171-
private$psi_ = array(NA, dim = c(
1170+
private$psi_ = array(NA_real_, dim = c(
11721171
self$data$n_obs, self$n_rep,
11731172
self$data$n_treat))
1174-
private$psi_a_ = array(NA, dim = c(
1173+
private$psi_a_ = array(NA_real_, dim = c(
11751174
self$data$n_obs, self$n_rep,
11761175
self$data$n_treat))
1177-
private$psi_b_ = array(NA, dim = c(
1176+
private$psi_b_ = array(NA_real_, dim = c(
11781177
self$data$n_obs, self$n_rep,
11791178
self$data$n_treat))
11801179

1181-
private$coef_ = array(NA, dim = c(self$data$n_treat))
1182-
private$se_ = array(NA, dim = c(self$data$n_treat))
1180+
private$coef_ = array(NA_real_, dim = c(self$data$n_treat))
1181+
private$se_ = array(NA_real_, dim = c(self$data$n_treat))
11831182

1184-
private$all_coef_ = array(NA, dim = c(self$data$n_treat, self$n_rep))
1185-
private$all_se_ = array(NA, dim = c(self$data$n_treat, self$n_rep))
1183+
private$all_coef_ = array(NA_real_,
1184+
dim = c(self$data$n_treat, self$n_rep))
1185+
private$all_se_ = array(NA_real_,
1186+
dim = c(self$data$n_treat, self$n_rep))
11861187

11871188
if (self$dml_procedure == "dml1") {
11881189
if (self$apply_cross_fitting) {
1189-
private$all_dml1_coef_ = array(NA, dim = c(
1190+
private$all_dml1_coef_ = array(NA_real_, dim = c(
11901191
self$data$n_treat, self$n_rep,
11911192
self$n_folds))
11921193
} else {
1193-
private$all_dml1_coef_ = array(NA, dim = c(
1194+
private$all_dml1_coef_ = array(NA_real_, dim = c(
11941195
self$data$n_treat, self$n_rep,
11951196
1))
11961197
}
11971198
}
11981199
},
11991200
initialize_boot_arrays = function(n_rep_boot) {
12001201
private$n_rep_boot = n_rep_boot
1201-
private$boot_coef_ = array(NA, dim = c(
1202+
private$boot_coef_ = array(NA_real_, dim = c(
12021203
self$data$n_treat,
12031204
n_rep_boot * self$n_rep))
1204-
private$boot_t_stat_ = array(NA, dim = c(
1205+
private$boot_t_stat_ = array(NA_real_, dim = c(
12051206
self$data$n_treat,
12061207
n_rep_boot * self$n_rep))
12071208
},
12081209
initialize_predictions = function() {
12091210
private$predictions_ = sapply(self$params_names(),
12101211
function(key) {
1211-
array(NA, dim = c(
1212+
array(NA_real_, dim = c(
12121213
self$data$n_obs, self$n_rep,
12131214
self$data$n_treat))
12141215
},
@@ -1243,7 +1244,7 @@ DoubleML = R6Class("DoubleML",
12431244
if (dml_procedure == "dml1") {
12441245
# Note that length(test_ids) is only not equal to self.n_folds
12451246
# if self$apply_cross_fitting ==False
1246-
thetas = rep(NA, length(test_ids))
1247+
thetas = rep(NA_real_, length(test_ids))
12471248
for (i_fold in seq_len(length(test_ids))) {
12481249
test_index = test_ids[[i_fold]]
12491250
thetas[i_fold] = private$orth_est(inds = test_index)

R/helper.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ extract_prediction = function(obj_resampling, learner_class, n_obs,
173173
1:n_iters,
174174
function(x) as.data.table(obj_resampling$predictions("train")[[x]]))
175175
for (i_iter in 1:n_iters) {
176-
preds_vec = as.numeric(rep(NA, n_obs))
176+
preds_vec = rep(NA_real_, n_obs)
177177
f_hat = f_hat_list[[i_iter]]
178178
preds_vec[f_hat[[ind_name]]] = f_hat[[resp_name]]
179179
preds[[i_iter]] = preds_vec
@@ -189,7 +189,7 @@ extract_prediction = function(obj_resampling, learner_class, n_obs,
189189
}
190190
}
191191
} else {
192-
preds = as.numeric(rep(NA, n_obs))
192+
preds = rep(NA_real_, n_obs)
193193
if (testR6(obj_resampling, classes = "ResampleResult")) obj_resampling = list(obj_resampling)
194194
n_obj_rsmp = length(obj_resampling)
195195
for (i_obj_rsmp in 1:n_obj_rsmp) {

tests/testthat/helper-01-helper_functions.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ functional_bootstrap = function(theta, se, psi, psi_a, k, smpls,
4040
n_rep_boot, weights) {
4141
score = psi
4242
J = mean(psi_a)
43-
boot_coef = matrix(NA, nrow = 1, ncol = n_rep_boot)
44-
boot_t_stat = matrix(NA, nrow = 1, ncol = n_rep_boot)
43+
boot_coef = matrix(NA_real_, nrow = 1, ncol = n_rep_boot)
44+
boot_t_stat = matrix(NA_real_, nrow = 1, ncol = n_rep_boot)
4545
for (i in seq(n_rep_boot)) {
4646
boot_coef[1, i] = mean(weights[i, ] * 1 / J * score)
4747
boot_t_stat[1, i] = boot_coef[1, i] / se

tests/testthat/helper-08-dml_plr.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ dml_plr = function(data, y, d,
99
smpls = lapply(1:n_rep, function(x) sample_splitting(n_folds, data))
1010
}
1111

12-
all_thetas = all_ses = rep(NA, n_rep)
12+
all_thetas = all_ses = rep(NA_real_, n_rep)
1313
all_preds = list()
1414

1515
for (i_rep in 1:n_rep) {
@@ -66,7 +66,7 @@ dml_plr_multitreat = function(data, y, d,
6666

6767
for (i_rep in 1:n_rep) {
6868
this_smpl = smpls[[i_rep]]
69-
thetas_this_rep = ses_this_rep = rep(NA, n_d)
69+
thetas_this_rep = ses_this_rep = rep(NA_real_, n_d)
7070
all_preds_this_rep = list()
7171

7272
for (i_d in seq(n_d)) {
@@ -97,7 +97,7 @@ dml_plr_multitreat = function(data, y, d,
9797

9898
}
9999

100-
theta = se = t = pval = rep(NA, n_d)
100+
theta = se = t = pval = rep(NA_real_, n_d)
101101
if (length(this_smpl$train_ids) > 1) {
102102
n = nrow(data)
103103
} else {
@@ -145,7 +145,7 @@ fit_plr_single_split = function(data, y, d,
145145

146146
# DML 1
147147
if (dml_procedure == "dml1") {
148-
thetas = rep(NA, n_folds)
148+
thetas = rep(NA_real_, n_folds)
149149
for (i in 1:n_folds) {
150150
test_index = test_ids[[i]]
151151

@@ -249,7 +249,7 @@ compute_plr_residuals = function(data, y, d, n_folds, smpls, all_preds) {
249249
D = data[, d]
250250
Y = data[, y]
251251

252-
v_hat = u_hat = w_hat = rep(NA, n)
252+
v_hat = u_hat = w_hat = rep(NA_real_, n)
253253

254254
for (i in 1:n_folds) {
255255
test_index = test_ids[[i]]
@@ -268,7 +268,7 @@ compute_plr_residuals = function(data, y, d, n_folds, smpls, all_preds) {
268268

269269
# Orthogonalized Estimation of Coefficient in PLR
270270
orth_plr_dml = function(u_hat, v_hat, v_hatd, score) {
271-
theta = NA
271+
theta = NA_real_
272272

273273
if (score == "partialling out") {
274274
res_fit = stats::lm(u_hat ~ 0 + v_hat)
@@ -335,7 +335,7 @@ boot_plr_multitreat = function(thetas, ses, data, y, d,
335335
for (i_rep in 1:n_rep) {
336336
n = nrow(data)
337337
weights = draw_bootstrap_weights(bootstrap, n_rep_boot, n)
338-
boot_theta = boot_t_stat = matrix(NA, nrow = n_d, ncol = n_rep_boot)
338+
boot_theta = boot_t_stat = matrix(NA_real_, nrow = n_d, ncol = n_rep_boot)
339339
for (i_d in seq(n_d)) {
340340
this_res = boot_plr_single_split(thetas[[i_rep]][i_d], ses[[i_rep]][i_d],
341341
data, y, d[i_d], n_folds, smpls[[i_rep]],

tests/testthat/helper-09-dml_pliv.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ dml_pliv = function(data, y, d, z,
99
smpls = lapply(1:n_rep, function(x) sample_splitting(n_folds, data))
1010
}
1111

12-
all_thetas = all_ses = rep(NA, n_rep)
12+
all_thetas = all_ses = rep(NA_real_, n_rep)
1313
all_preds = list()
1414

1515
for (i_rep in 1:n_rep) {
@@ -30,7 +30,7 @@ dml_pliv = function(data, y, d, z,
3030

3131
# DML 1
3232
if (dml_procedure == "dml1") {
33-
thetas = vars = rep(NA, n_folds)
33+
thetas = vars = rep(NA_real_, n_folds)
3434
for (i in 1:n_folds) {
3535
test_index = test_ids[[i]]
3636
orth_est = orth_pliv_dml(
@@ -149,7 +149,7 @@ compute_pliv_residuals = function(data, y, d, z, n_folds, smpls, all_preds) {
149149
Y = data[, y]
150150
Z = data[, z]
151151

152-
v_hat = u_hat = w_hat = rep(NA, n)
152+
v_hat = u_hat = w_hat = rep(NA_real_, n)
153153

154154
for (i in 1:n_folds) {
155155
test_index = test_ids[[i]]

tests/testthat/helper-10-dml_irm.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ dml_irm = function(data, y, d,
1010
smpls = lapply(1:n_rep, function(x) sample_splitting(n_folds, data))
1111
}
1212

13-
all_thetas = all_ses = rep(NA, n_rep)
13+
all_thetas = all_ses = rep(NA_real_, n_rep)
1414
all_preds = list()
1515

1616
for (i_rep in 1:n_rep) {
@@ -36,7 +36,7 @@ dml_irm = function(data, y, d,
3636

3737
# DML 1
3838
if (dml_procedure == "dml1") {
39-
thetas = vars = rep(NA, n_folds)
39+
thetas = vars = rep(NA_real_, n_folds)
4040

4141
for (i in 1:n_folds) {
4242
test_index = test_ids[[i]]
@@ -184,7 +184,7 @@ extract_irm_residuals = function(data, y, d, n_folds, smpls, all_preds, score,
184184
D = data[, d]
185185
Y = data[, y]
186186

187-
g0_hat = g1_hat = u0_hat = u1_hat = m_hat = p_hat = rep(NA, n)
187+
g0_hat = g1_hat = u0_hat = u1_hat = m_hat = p_hat = rep(NA_real_, n)
188188

189189
for (i in 1:n_folds) {
190190
test_index = test_ids[[i]]

tests/testthat/helper-11-dml_iivm.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ dml_irmiv = function(data, y, d, z,
1212
smpls = lapply(1:n_rep, function(x) sample_splitting(n_folds, data))
1313
}
1414

15-
all_thetas = all_ses = rep(NA, n_rep)
15+
all_thetas = all_ses = rep(NA_real_, n_rep)
1616
all_preds = list()
1717

1818
for (i_rep in 1:n_rep) {
@@ -39,7 +39,7 @@ dml_irmiv = function(data, y, d, z,
3939

4040
# DML 1
4141
if (dml_procedure == "dml1") {
42-
thetas = vars = rep(NA, n_folds)
42+
thetas = vars = rep(NA_real_, n_folds)
4343
for (i in 1:n_folds) {
4444
test_index = test_ids[[i]]
4545
orth_est = orth_irmiv_dml(
@@ -257,7 +257,7 @@ extract_iivm_preds = function(data, y, d, z, n_folds, smpls, all_preds,
257257
D = data[, d]
258258
Y = data[, y]
259259
Z = data[, z]
260-
m_hat = g0_hat = g1_hat = r0_hat = r1_hat = rep(NA, n)
260+
m_hat = g0_hat = g1_hat = r0_hat = r1_hat = rep(NA_real_, n)
261261

262262
for (i in 1:n_folds) {
263263
test_index = test_ids[[i]]
@@ -279,7 +279,7 @@ extract_iivm_preds = function(data, y, d, z, n_folds, smpls, all_preds,
279279

280280
# Orthogonalized Estimation of Coefficient in irm
281281
orth_irmiv_dml = function(m_hat, g0_hat, g1_hat, r0_hat, r1_hat, d, y, z, score) {
282-
theta = NA
282+
theta = NA_real_
283283

284284
if (score == "LATE" | score == "partialling out") {
285285
theta = 1 / mean(r1_hat - r0_hat + z * (d - r1_hat) / m_hat - ((1 - z) * (d - r0_hat) / (1 - m_hat))) *

tests/testthat/helper-12-p_adjust.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ p_adjust.DML = function(x, method = "RW", ...) {
6767
# v = x$residuals$v
6868
# ev = e * v
6969
# Ev2 = colMeans(v^2)
70-
# Omegahat = matrix(NA, ncol = k, nrow = k)
70+
# Omegahat = matrix(NA_real_, ncol = k, nrow = k)
7171
# for (j in 1:k) {
7272
# for (l in 1:k) {
7373
# Omegahat[j, l] = Omegahat[l, j] = 1/(Ev2[j] * Ev2[l]) * mean(ev[, j] * ev[, l])
7474
# }
7575
# }
7676
# se = sqrt(diag(Omegahat))
7777
#
78-
# Beta_i = matrix(NA, ncol = k, nrow = B)
78+
# Beta_i = matrix(NA_real_, ncol = k, nrow = B)
7979
# for (i in 1:B) {
8080
# Beta_i[i, ] = MASS::mvrnorm(mu = rep(0, k), Sigma = Omegahat/n)
8181
# }

tests/testthat/helper-13-dml_pliv_partial_x.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ dml_pliv_partial_x = function(data, y, d, z,
99
smpls = lapply(1:n_rep, function(x) sample_splitting(n_folds, data))
1010
}
1111

12-
all_thetas = all_ses = rep(NA, n_rep)
12+
all_thetas = all_ses = rep(NA_real_, n_rep)
1313
all_preds = list()
1414

1515
for (i_rep in 1:n_rep) {
@@ -29,7 +29,7 @@ dml_pliv_partial_x = function(data, y, d, z,
2929

3030
# DML 1
3131
if (dml_procedure == "dml1") {
32-
thetas = vars = rep(NA, n_folds)
32+
thetas = vars = rep(NA_real_, n_folds)
3333
for (i in 1:n_folds) {
3434
test_index = this_smpl$test_ids[[i]]
3535
orth_est = orth_pliv_partial_x_dml(
@@ -129,8 +129,8 @@ fit_nuisance_pliv_partial_x = function(data, y, d, z,
129129
r_hat_list = lapply(r_r$predictions(), function(x) x$response)
130130

131131
n = nrow(data)
132-
r_hat_array = rep(NA, n)
133-
m_hat_array = matrix(NA, nrow = n, ncol = n_z)
132+
r_hat_array = rep(NA_real_, n)
133+
m_hat_array = matrix(NA_real_, nrow = n, ncol = n_z)
134134

135135
for (i_fold in seq_len(length(test_ids))) {
136136
test_index = test_ids[[i_fold]]
@@ -164,7 +164,7 @@ compute_pliv_partial_x_residuals = function(data, y, d, z, n_folds, smpls,
164164
D = data[, d]
165165
Y = data[, y]
166166

167-
u_hat = w_hat = rep(NA, n)
167+
u_hat = w_hat = rep(NA_real_, n)
168168

169169
for (i in 1:n_folds) {
170170
test_index = test_ids[[i]]

tests/testthat/helper-14-dml_pliv_partial_z.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ dml_pliv_partial_z = function(data, y, d, z,
88
smpls = lapply(1:n_rep, function(x) sample_splitting(n_folds, data))
99
}
1010

11-
all_thetas = all_ses = rep(NA, n_rep)
11+
all_thetas = all_ses = rep(NA_real_, n_rep)
1212
all_preds = list()
1313

1414
for (i_rep in 1:n_rep) {
@@ -28,7 +28,7 @@ dml_pliv_partial_z = function(data, y, d, z,
2828

2929
# DML 1
3030
if (dml_procedure == "dml1") {
31-
thetas = vars = rep(NA, n_folds)
31+
thetas = vars = rep(NA_real_, n_folds)
3232
for (i in 1:n_folds) {
3333
test_index = this_smpl$test_ids[[i]]
3434
orth_est = orth_pliv_partial_z_dml(
@@ -110,7 +110,7 @@ compute_pliv_partial_z_residuals = function(data, y, d, z, n_folds, smpls,
110110

111111
r_hat_list = all_preds$r_hat_list
112112
n = nrow(data)
113-
r_hat = rep(NA, n)
113+
r_hat = rep(NA_real_, n)
114114

115115
for (i in 1:n_folds) {
116116
test_index = test_ids[[i]]

0 commit comments

Comments
 (0)