# 9. Sample-splitting, cross-fitting and repeated cross-fitting¶

Sample-splitting and the application of cross-fitting is a central part of double/debiased machine learning (DML). For all DML models DoubleMLPLR, DoubleMLPLIV, DoubleMLIRM, and DoubleMLIIVM, the specification is done via the parameters n_folds and n_rep. Advanced resampling techniques can be obtained via the boolean parameters draw_sample_splitting and apply_cross_fitting as well as the methods draw_sample_splitting() and set_sample_splitting().

As an example we consider a partially linear regression model (PLR) implemented in DoubleMLPLR.

In [1]: import doubleml as dml

In [2]: import numpy as np

In [3]: from doubleml.datasets import make_plr_CCDDHNR2018

In [4]: from sklearn.ensemble import RandomForestRegressor

In [5]: from sklearn.base import clone

In [6]: learner = RandomForestRegressor(n_estimators=100, max_features=20, max_depth=5, min_samples_leaf=2)

In [7]: ml_g = clone(learner)

In [8]: ml_m = clone(learner)

In [9]: np.random.seed(1234)

In [10]: obj_dml_data = make_plr_CCDDHNR2018(alpha=0.5, n_obs=100)

library(DoubleML)
library(mlr3)
lgr::get_logger("mlr3")$set_threshold("warn") library(mlr3learners) library(data.table) learner = lrn("regr.ranger", num.trees = 100, mtry = 20, min.node.size = 2, max.depth = 5) ml_g = learner ml_m = learner data = make_plr_CCDDHNR2018(alpha=0.5, n_obs=100, return_type = "data.table") obj_dml_data = DoubleMLData$new(data,
y_col = "y",
d_cols = "d")


## 9.1. Cross-fitting with $$K$$ folds¶

The default setting is n_folds = 5 and n_rep = 1, i.e., $$K=5$$ folds and no repeated cross-fitting.

In [11]: dml_plr_obj = dml.DoubleMLPLR(obj_dml_data, ml_g, ml_m, n_folds = 5, n_rep = 1)

In [12]: print(dml_plr_obj.n_folds)
5

In [13]: print(dml_plr_obj.n_rep)
1

dml_plr_obj = DoubleMLPLR$new(obj_dml_data, ml_g, ml_m, n_folds = 5, n_rep = 1) print(dml_plr_obj$n_folds)
print(dml_plr_obj$n_rep)  [1] 5  [1] 1  During the initialization of a DML model like DoubleMLPLR a $$K$$-fold random partition $$(I_k)_{k=1}^{K}$$ of observation indices is generated. The $$K$$-fold random partition is stored in the smpls attribute of the DML model object. In [14]: print(dml_plr_obj.smpls) [[(array([ 0, 2, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 19, 21, 22, 23, 24, 25, 27, 28, 29, 31, 32, 34, 35, 36, 37, 38, 40, 44, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 58, 59, 60, 61, 62, 63, 64, 65, 67, 68, 69, 70, 71, 72, 74, 75, 76, 77, 79, 80, 81, 82, 84, 85, 86, 88, 89, 90, 91, 92, 93, 94, 95, 97, 98, 99]), array([ 1, 5, 18, 20, 26, 30, 33, 39, 41, 42, 43, 45, 56, 57, 66, 73, 78, 83, 87, 96])), (array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 26, 29, 30, 31, 32, 33, 34, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 48, 53, 54, 55, 56, 57, 58, 60, 61, 62, 63, 65, 66, 69, 70, 71, 72, 73, 77, 78, 79, 80, 81, 82, 83, 84, 85, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]), array([ 9, 23, 24, 25, 27, 28, 35, 47, 49, 50, 51, 52, 59, 64, 67, 68, 74, 75, 76, 86])), (array([ 0, 1, 2, 3, 5, 6, 7, 9, 10, 11, 12, 14, 16, 17, 18, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 33, 35, 38, 39, 40, 41, 42, 43, 44, 45, 47, 48, 49, 50, 51, 52, 53, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 71, 73, 74, 75, 76, 77, 78, 79, 80, 81, 83, 84, 86, 87, 88, 89, 90, 91, 92, 96, 98, 99]), array([ 4, 8, 13, 15, 19, 32, 34, 36, 37, 46, 54, 55, 70, 72, 82, 85, 93, 94, 95, 97])), (array([ 0, 1, 3, 4, 5, 6, 8, 9, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 39, 41, 42, 43, 44, 45, 46, 47, 49, 50, 51, 52, 53, 54, 55, 56, 57, 59, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 82, 83, 85, 86, 87, 88, 89, 93, 94, 95, 96, 97, 98]), array([ 2, 7, 10, 11, 12, 16, 38, 40, 48, 58, 60, 61, 62, 63, 81, 84, 90, 91, 92, 99])), (array([ 1, 2, 4, 5, 7, 8, 9, 10, 11, 12, 13, 15, 16, 18, 19, 20, 23, 24, 25, 26, 27, 28, 30, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 45, 46, 47, 48, 49, 50, 51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68, 70, 72, 73, 74, 75, 76, 78, 81, 82, 83, 84, 85, 86, 87, 90, 91, 92, 93, 94, 95, 96, 97, 99]), array([ 0, 3, 6, 14, 17, 21, 22, 29, 31, 44, 53, 65, 69, 71, 77, 79, 80, 88, 89, 98]))]]  dml_plr_obj$smpls

1. $train_ids 1. 9 2. 23 3. 31 4. 33 5. 38 6. 39 7. 41 8. 47 9. 48 10. 52 11. 57 12. 58 13. 61 14. 62 15. 65 16. 69 17. 72 18. 73 19. 90 20. 92 21. 13 22. 14 23. 16 24. 18 25. 20 26. 24 27. 34 28. 37 29. 40 30. 42 31. 51 32. 66 33. 86 34. 91 35. 93 36. 94 37. 95 38. 96 39. 98 40. 99 41. 5 42. 10 43. 12 44. 25 45. 28 46. 30 47. 32 48. 35 49. 49 50. 50 51. 55 52. 59 53. 60 54. 71 55. 75 56. 80 57. 82 58. 85 59. 97 60. 100 61. 6 62. 11 63. 15 64. 17 65. 21 66. 26 67. 27 68. 36 69. 44 70. 45 71. 46 72. 54 73. 64 74. 70 75. 74 76. 79 77. 83 78. 84 79. 87 80. 88 1. 1 2. 2 3. 3 4. 4 5. 7 6. 8 7. 19 8. 22 9. 29 10. 43 11. 53 12. 56 13. 63 14. 67 15. 68 16. 76 17. 77 18. 78 19. 81 20. 89 21. 13 22. 14 23. 16 24. 18 25. 20 26. 24 27. 34 28. 37 29. 40 30. 42 31. 51 32. 66 33. 86 34. 91 35. 93 36. 94 37. 95 38. 96 39. 98 40. 99 41. 5 42. 10 43. 12 44. 25 45. 28 46. 30 47. 32 48. 35 49. 49 50. 50 51. 55 52. 59 53. 60 54. 71 55. 75 56. 80 57. 82 58. 85 59. 97 60. 100 61. 6 62. 11 63. 15 64. 17 65. 21 66. 26 67. 27 68. 36 69. 44 70. 45 71. 46 72. 54 73. 64 74. 70 75. 74 76. 79 77. 83 78. 84 79. 87 80. 88 1. 1 2. 2 3. 3 4. 4 5. 7 6. 8 7. 19 8. 22 9. 29 10. 43 11. 53 12. 56 13. 63 14. 67 15. 68 16. 76 17. 77 18. 78 19. 81 20. 89 21. 9 22. 23 23. 31 24. 33 25. 38 26. 39 27. 41 28. 47 29. 48 30. 52 31. 57 32. 58 33. 61 34. 62 35. 65 36. 69 37. 72 38. 73 39. 90 40. 92 41. 5 42. 10 43. 12 44. 25 45. 28 46. 30 47. 32 48. 35 49. 49 50. 50 51. 55 52. 59 53. 60 54. 71 55. 75 56. 80 57. 82 58. 85 59. 97 60. 100 61. 6 62. 11 63. 15 64. 17 65. 21 66. 26 67. 27 68. 36 69. 44 70. 45 71. 46 72. 54 73. 64 74. 70 75. 74 76. 79 77. 83 78. 84 79. 87 80. 88 1. 1 2. 2 3. 3 4. 4 5. 7 6. 8 7. 19 8. 22 9. 29 10. 43 11. 53 12. 56 13. 63 14. 67 15. 68 16. 76 17. 77 18. 78 19. 81 20. 89 21. 9 22. 23 23. 31 24. 33 25. 38 26. 39 27. 41 28. 47 29. 48 30. 52 31. 57 32. 58 33. 61 34. 62 35. 65 36. 69 37. 72 38. 73 39. 90 40. 92 41. 13 42. 14 43. 16 44. 18 45. 20 46. 24 47. 34 48. 37 49. 40 50. 42 51. 51 52. 66 53. 86 54. 91 55. 93 56. 94 57. 95 58. 96 59. 98 60. 99 61. 6 62. 11 63. 15 64. 17 65. 21 66. 26 67. 27 68. 36 69. 44 70. 45 71. 46 72. 54 73. 64 74. 70 75. 74 76. 79 77. 83 78. 84 79. 87 80. 88 1. 1 2. 2 3. 3 4. 4 5. 7 6. 8 7. 19 8. 22 9. 29 10. 43 11. 53 12. 56 13. 63 14. 67 15. 68 16. 76 17. 77 18. 78 19. 81 20. 89 21. 9 22. 23 23. 31 24. 33 25. 38 26. 39 27. 41 28. 47 29. 48 30. 52 31. 57 32. 58 33. 61 34. 62 35. 65 36. 69 37. 72 38. 73 39. 90 40. 92 41. 13 42. 14 43. 16 44. 18 45. 20 46. 24 47. 34 48. 37 49. 40 50. 42 51. 51 52. 66 53. 86 54. 91 55. 93 56. 94 57. 95 58. 96 59. 98 60. 99 61. 5 62. 10 63. 12 64. 25 65. 28 66. 30 67. 32 68. 35 69. 49 70. 50 71. 55 72. 59 73. 60 74. 71 75. 75 76. 80 77. 82 78. 85 79. 97 80. 100$test_ids
1. 1
2. 2
3. 3
4. 4
5. 7
6. 8
7. 19
8. 22
9. 29
10. 43
11. 53
12. 56
13. 63
14. 67
15. 68
16. 76
17. 77
18. 78
19. 81
20. 89
1. 9
2. 23
3. 31
4. 33
5. 38
6. 39
7. 41
8. 47
9. 48
10. 52
11. 57
12. 58
13. 61
14. 62
15. 65
16. 69
17. 72
18. 73
19. 90
20. 92
1. 13
2. 14
3. 16
4. 18
5. 20
6. 24
7. 34
8. 37
9. 40
10. 42
11. 51
12. 66
13. 86
14. 91
15. 93
16. 94
17. 95
18. 96
19. 98
20. 99
1. 5
2. 10
3. 12
4. 25
5. 28
6. 30
7. 32
8. 35
9. 49
10. 50
11. 55
12. 59
13. 60
14. 71
15. 75
16. 80
17. 82
18. 85
19. 97
20. 100
1. 6
2. 11
3. 15
4. 17
5. 21
6. 26
7. 27
8. 36
9. 44
10. 45
11. 46
12. 54
13. 64
14. 70
15. 74
16. 79
17. 83
18. 84
19. 87
20. 88

For each $$k \in [K] = \lbrace 1, \ldots, K]$$ the nuisance ML estimator

$\hat{\eta}_{0,k} = \hat{\eta}_{0,k}\big((W_i)_{i\not\in I_k}\big)$

is based on the observations of all other $$k-1$$ folds. The values of the two score function components $$\psi_a(W_i; \hat{\eta}_0)$$ and $$\psi_b(W_i; \hat{\eta}_0))$$ for each observation index $$i \in I_k$$ are computed and stored in the attributes psi_a and psi_b.

In [15]: dml_plr_obj.fit();

In [16]: print(dml_plr_obj.psi_a[:5])
[[[-2.77031254]]

[[-2.87771887]]

[[-0.08013837]]

[[-0.36003344]]

[[-2.62438715]]]

In [17]: print(dml_plr_obj.psi_b[:5])
[[[ 0.08694518]]

[[-1.06665591]]

[[-0.1202984 ]]

[[ 0.4134775 ]]

[[ 1.11809605]]]

dml_plr_obj$fit() print(dml_plr_obj$psi_a[1:5, ,1])
print(dml_plr_obj$psi_b[1:5, ,1])  [1] -2.17881340 -0.10436072 -0.04075182 -0.03435173 -0.02733278  [1] 0.29979608 0.35057020 -0.11045260 0.06616442 0.02703347  ## 9.2. Repeated cross-fitting with $$K$$ folds and $$M$$ repetition¶ Repeated cross-fitting is obtained by choosing a value $$M>1$$ for the number of repetition n_rep. It results in $$M$$ random $$K$$-fold partitions being drawn. In [18]: dml_plr_obj = dml.DoubleMLPLR(obj_dml_data, ml_g, ml_m, n_folds = 5, n_rep = 10) In [19]: print(dml_plr_obj.n_folds) 5 In [20]: print(dml_plr_obj.n_rep) 10  dml_plr_obj = DoubleMLPLR$new(obj_dml_data, ml_g, ml_m, n_folds = 5, n_rep = 10)
print(dml_plr_obj$n_folds) print(dml_plr_obj$n_rep)

[1] 5

[1] 10


For each of the $$M$$ partitions, the nuisance ML models are estimated and score functions computed as described in Cross-fitting with K folds. The resulting values of the score functions are stored in 3-dimensional arrays psi_a and psi_b, where the row index corresponds the observation index $$i \in [N] = \lbrace 1, \ldots, N\rbrace$$ and the column index to the partition $$m \in [M] = \lbrace 1, \ldots, M\rbrace$$. The third dimension refers to the treatment variable and becomes non-singleton in case of multiple treatment variables.

In [21]: dml_plr_obj.fit();

In [22]: print(dml_plr_obj.psi_a[:5, :, 0])
[[-2.98432359 -3.06480151 -3.85921198 -2.96874769 -3.05657146 -3.2140234
-3.93301812 -2.72412326 -3.92721123 -4.88704139]
[-2.83359228 -2.53108066 -2.03009132 -2.07915489 -3.3994495  -2.36751096
-2.49584278 -2.14283539 -1.98361135 -2.07559537]
[-0.23465747 -0.19072988 -0.09287264 -0.18618573 -0.04664579 -0.10008165
-0.17123038 -0.4765105  -0.29798259 -0.57459766]
[-0.04854801 -0.12693729 -0.11709589 -0.13362169 -0.34118416 -0.1356885
-0.21936282 -0.61791403 -0.01701573 -0.00959174]
[-2.94228402 -4.09970808 -1.74962533 -2.91622689 -1.46372629 -2.85263354
-4.71027078 -1.79069144 -3.53482783 -2.95084544]]

In [23]: print(dml_plr_obj.psi_b[:5, :, 0])
[[ 0.05194436  0.36904565  0.7322728   0.18000647 -0.08155766  0.25451579
-0.15635892 -0.00700313  0.10084129  0.35967313]
[-0.37844833 -1.3861506  -0.51353049 -0.80280278 -0.35055124 -0.52875372
-0.72369879 -0.94641422 -0.7244148  -0.87413061]
[-0.23876905 -0.17447994 -0.11406848 -0.21012099 -0.17211746 -0.14038296
-0.12573943 -0.31596746 -0.09332741 -0.22672197]
[ 0.15449116  0.19571928  0.23704881  0.3477631   0.41909963  0.19310959
0.22392664  0.52443597  0.07921596  0.08061351]
[ 0.61359257  1.3563012   0.18112505  0.45243241  0.38899264  0.77911744
1.01224811  0.87656413  0.92262984  0.50719376]]

dml_plr_obj$fit() print(dml_plr_obj$psi_a[1:5, ,1])
print(dml_plr_objpsi_b[1:5, ,1])   [,1] [,2] [,3] [,4] [,5] [1,] -2.22981857 -0.46808617 -2.755234216 -1.825923275 -1.89804565 [2,] -0.04104147 -0.02305530 -0.570789836 -0.006249416 -0.02682531 [3,] -0.08049487 -0.03678065 -0.120305707 -0.049680713 -0.04415433 [4,] -0.03663248 -0.08159447 -0.022860770 -0.013043853 -0.05693658 [5,] -0.07382425 -0.09741097 -0.006706541 -0.050114390 -0.27840468 [,6] [,7] [,8] [,9] [,10] [1,] -2.1788640340 -1.272754733 -1.835190162 -2.08524693 -2.350458229 [2,] -0.1006151259 -0.014823380 -0.008394862 -0.06235396 -0.123385870 [3,] -0.0962544530 -0.005503975 -0.042136939 -0.01102389 -0.031493424 [4,] -0.1052499931 -0.170550255 -0.038271946 -0.05336468 -0.186434423 [5,] -0.0001392361 -0.087416051 -0.042500310 -0.03557325 -0.001468312   [,1] [,2] [,3] [,4] [,5] [,6] [1,] 0.87176722 0.187147361 0.456539315 0.23958587 0.62844448 0.324530639 [2,] 0.22651886 0.135360199 0.552830203 -0.11001615 -0.22481263 0.267104835 [3,] 0.03609403 0.041686183 -0.007188568 0.03958668 0.19003052 0.172150179 [4,] 0.05589536 0.112623132 0.062136033 0.03502392 0.05953427 0.136527038 [5,] -0.10396942 -0.009758583 0.020697555 0.03903063 0.02008020 0.001518781 [,7] [,8] [,9] [,10] [1,] 0.28820134 0.44407125 0.3359611744 3.437839e-01 [2,] 0.11091204 0.06976972 0.2262368278 2.596166e-01 [3,] 0.02341542 -0.02642588 -0.0009712849 -3.675447e-02 [4,] 0.16004383 0.08693315 0.0299177232 2.206324e-01 [5,] 0.10366772 0.06395096 -0.0139343643 7.365003e-05  We estimate the causal parameter $$\tilde{\theta}_{0,m}$$ for each of the $$M$$ partitions with a DML algorithm as described in Double machine learning algorithms. Standard errors are obtained as described in Variance estimation and confidence intervals for a causal parameter of interest. The aggregation of the estimates of the causal parameter and its standard errors is done using the median \begin{align}\begin{aligned}\tilde{\theta}_{0} &= \text{Median}\big((\tilde{\theta}_{0,m})_{m \in [M]}\big),\\\hat{\sigma} &= \sqrt{\text{Median}\big((\hat{\sigma}_m^2 + (\tilde{\theta}_{0,m} - \tilde{\theta}_{0})^2)_{m \in [M]}\big)}.\end{aligned}\end{align} The estimate of the causal parameter $$\tilde{\theta}_{0}$$ is stored in the coef attribute and the asymptotic standard error $$\hat{\sigma}/\sqrt{N}$$ in se. In [24]: print(dml_plr_obj.coef) [0.50003654] In [25]: print(dml_plr_obj.se) [0.0759755]  print(dml_plr_objcoef)
print(dml_plr_obj$se)   d 0.3899064   d 0.09602633  The parameter estimates $$(\tilde{\theta}_{0,m})_{m \in [M]}$$ and asymptotic standard errors $$(\hat{\sigma}_m/\sqrt{N})_{m \in [M]}$$ for each of the $$M$$ partitions are stored in the attributes _all_coef and _all_se, respectively. In [26]: print(dml_plr_obj._all_coef) [[0.49050748 0.47162414 0.47976884 0.50956561 0.51360033 0.54163618 0.5278666 0.46204646 0.51533086 0.47119733]] In [27]: print(dml_plr_obj._all_se) [[0.075171 0.07658685 0.07032188 0.08584866 0.07904228 0.07626231 0.07552195 0.07687109 0.07377529 0.07145054]]  print(dml_plr_obj$all_coef)
print(dml_plr_obj$all_se)   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 0.4110265 0.3933149 0.3824268 0.3627756 0.3325636 0.4313377 0.4041967 [,8] [,9] [,10] [1,] 0.4563844 0.3864978 0.3480674   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 0.09040816 0.09439002 0.09881701 0.1014575 0.09730979 0.1026725 0.09454096 [,8] [,9] [,10] [1,] 0.09961637 0.09416537 0.08987986  ## 9.3. Externally provide a sample splitting / partition¶ All DML models allow a partition to be provided externally via the method set_sample_splitting(). In Python we can for example use the K-Folds cross-validator of sklearn KFold in order to generate a sample splitting and provide it to the DML model object. Note that by setting draw_sample_splitting = False one can prevent that a partition is drawn during initialization of the DML model object. The following calls are equivalent. In the first sample code, we use the standard interface and draw the sample-splitting with $$K=4$$ folds during initialization of the DoubleMLPLR object. In [28]: np.random.seed(314) In [29]: dml_plr_obj_internal = dml.DoubleMLPLR(obj_dml_data, ml_g, ml_m, n_folds = 4) In [30]: print(dml_plr_obj_internal.fit().summary) coef std err t P>|t| 2.5 % 97.5 % d 0.502792 0.086164 5.835291 5.369687e-09 0.333913 0.67167  set.seed(314) dml_plr_obj_internal = DoubleMLPLR$new(obj_dml_data, ml_g, ml_m, n_folds = 4)
dml_plr_obj_internal$fit() dml_plr_obj_internal$summary()

Estimates and significance testing of the effect of target variables
Estimate. Std. Error t value Pr(>|t|)
d   0.38766    0.09221   4.204 2.62e-05 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1



In the second sample code, we use the K-Folds cross-validator of sklearn KFold and set the partition via the set_sample_splitting() method.

In [31]: dml_plr_obj_external = dml.DoubleMLPLR(obj_dml_data, ml_g, ml_m, draw_sample_splitting = False)

In [32]: from sklearn.model_selection import KFold

In [33]: np.random.seed(314)

In [34]: kf = KFold(n_splits=4, shuffle=True)

In [35]: smpls = [(train, test) for train, test in kf.split(obj_dml_data.x)]

In [36]: dml_plr_obj_external.set_sample_splitting(smpls);

In [37]: print(dml_plr_obj_external.fit().summary)
coef   std err         t         P>|t|     2.5 %   97.5 %
d  0.502792  0.086164  5.835291  5.369687e-09  0.333913  0.67167

dml_plr_obj_external = DoubleMLPLR$new(obj_dml_data, ml_g, ml_m, draw_sample_splitting = FALSE) set.seed(314) # set up a task and cross-validation resampling scheme in mlr3 my_task = Task$new("help task", "regr", data)
my_sampling = rsmp("cv", folds = 4)$instantiate(my_task) train_ids = lapply(1:4, function(x) my_sampling$train_set(x))
test_ids = lapply(1:4, function(x) my_sampling$test_set(x)) smpls = list(list(train_ids = train_ids, test_ids = test_ids)) dml_plr_obj_external$set_sample_splitting(smpls)
dml_plr_obj_external$fit() dml_plr_obj_external$summary()

Estimates and significance testing of the effect of target variables
Estimate. Std. Error t value Pr(>|t|)
d   0.38766    0.09221   4.204 2.62e-05 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1



## 9.4. Sample-splitting without cross-fitting¶

The boolean flag apply_cross_fitting allows to estimate DML models without applying cross-fitting. It results in randomly splitting the sample into two parts. The first half of the data is used for the estimation of the nuisance ML models and the second half for estimating the causal parameter. Note that cross-fitting performs well empirically and is recommended to remove bias induced by overfitting, see also Sample splitting to remove bias induced by overfitting.

In [38]: np.random.seed(314)

In [39]: dml_plr_obj_external = dml.DoubleMLPLR(obj_dml_data, ml_g, ml_m,
....:                                        n_folds = 2, apply_cross_fitting = False)
....:

In [40]: print(dml_plr_obj_external.fit().summary)
coef   std err         t     P>|t|     2.5 %    97.5 %
d  0.553448  0.121353  4.560651  0.000005  0.315601  0.791296

dml_plr_obj_external = DoubleMLPLR$new(obj_dml_data, ml_g, ml_m, n_folds = 2, apply_cross_fitting = FALSE) dml_plr_obj_external$fit()
dml_plr_obj_external$summary()  Estimates and significance testing of the effect of target variables Estimate. Std. Error t value Pr(>|t|) d 0.6524 0.1335 4.888 1.02e-06 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1  Note, that in order to split data unevenly into train and test sets the interface to externally set the sample splitting via set_sample_splitting() needs to be applied, like for example: In [41]: np.random.seed(314) In [42]: dml_plr_obj_external = dml.DoubleMLPLR(obj_dml_data, ml_g, ml_m, ....: n_folds = 2, apply_cross_fitting = False, draw_sample_splitting = False) ....: In [43]: from sklearn.model_selection import train_test_split In [44]: smpls = train_test_split(np.arange(obj_dml_data.n_obs), train_size=0.8) In [45]: dml_plr_obj_external.set_sample_splitting(tuple(smpls)); In [46]: print(dml_plr_obj_external.fit().summary) coef std err t P>|t| 2.5 % 97.5 % d 0.473033 0.134977 3.504531 0.000457 0.208482 0.737584  dml_plr_obj_external = DoubleMLPLR$new(obj_dml_data, ml_g, ml_m,
n_folds = 2, apply_cross_fitting = FALSE,
draw_sample_splitting = FALSE)

set.seed(314)
# set up a task and cross-validation resampling scheme in mlr3
my_task = Task$new("help task", "regr", data) my_sampling = rsmp("holdout", ratio = 0.8)$instantiate(my_task)

train_ids = list(my_sampling$train_set(1)) test_ids = list(my_sampling$test_set(1))
smpls = list(list(train_ids = train_ids, test_ids = test_ids))

dml_plr_obj_external$set_sample_splitting(smpls) dml_plr_obj_external$fit()
dml_plr_obj_external$summary()  Estimates and significance testing of the effect of target variables Estimate. Std. Error t value Pr(>|t|) d 0.1956 0.1833 1.067 0.286  ## 9.5. Estimate DML models without sample-splitting¶ The implementation of the DML models allows the estimation without sample splitting, i.e., all observations are used for learning the nuisance models as well as for the estimation of the causal parameter. Note that this approach usually results in a bias and is therefore not recommended without appropriate theoretical justification, see also Sample splitting to remove bias induced by overfitting. In [47]: np.random.seed(314) In [48]: dml_plr_no_split = dml.DoubleMLPLR(obj_dml_data, ml_g, ml_m, ....: n_folds = 1, apply_cross_fitting = False) ....: In [49]: print(dml_plr_obj_external.fit().summary) coef std err t P>|t| 2.5 % 97.5 % d 0.514558 0.123173 4.177519 0.000029 0.273143 0.755973  dml_plr_no_split = DoubleMLPLR$new(obj_dml_data, ml_g, ml_m,
n_folds = 1, apply_cross_fitting = FALSE)

set.seed(314)
dml_plr_no_split$fit() dml_plr_no_split$summary()

Estimates and significance testing of the effect of target variables
Estimate. Std. Error t value Pr(>|t|)
d    0.4101     0.1071    3.83 0.000128 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1