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

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_elements['psi_a'][:5])
[[[-1.78711285e+00]]

 [[-1.41918406e+00]]

 [[-1.95062986e-02]]

 [[-5.41989983e-04]]

 [[-5.91438767e+00]]]

In [17]: print(dml_plr_obj.psi_elements['psi_b'][:5])
[[[-0.38818693]]

 [[-1.14000073]]

 [[ 0.06692492]]

 [[ 0.00778625]]

 [[ 3.58241568]]]
dml_plr_obj$fit()
print(dml_plr_obj$psi_a[1:5, ,1])
print(dml_plr_obj$psi_b[1:5, ,1])
[1] -0.25243713 -0.04280508 -0.52851705 -0.01976017 -2.56568923
[1] 1.2065198 0.2325812 0.5499107 0.2048327 2.5427837

9.2. Repeated cross-fitting with K folds and M repetitions#

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_l, 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_l, 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_elements['psi_a'][:5, :, 0])
[[-1.80696592e+00 -1.41525168e+00 -2.19509680e+00 -1.95401167e+00
  -9.11199615e-01 -1.46722576e+00 -1.77289874e+00 -1.40127723e+00
  -1.69840389e+00 -1.56387280e+00]
 [-2.76702611e+00 -2.56390147e+00 -1.64476745e+00 -1.52343523e+00
  -2.50398782e+00 -1.49596416e+00 -1.21669513e+00 -2.19374710e+00
  -1.82666866e+00 -2.50093148e+00]
 [-4.38973512e-02 -1.01916030e-01 -4.23690345e-03 -1.51243406e-03
  -3.19073905e-02 -1.94309994e-04 -4.68562150e-02 -8.22507006e-02
  -2.03920960e-02 -5.99549118e-02]
 [-4.92881435e-03 -3.43231359e-03 -7.36566025e-03 -3.21257396e-02
  -2.72875815e-02 -3.36557195e-04 -1.49270769e-02 -9.33335939e-02
  -9.09879814e-04 -1.77401500e-05]
 [-4.07564554e+00 -4.12223182e+00 -5.79792890e+00 -4.88125046e+00
  -5.10637173e+00 -6.12980769e+00 -4.77227783e+00 -5.63245862e+00
  -3.84930915e+00 -5.41798768e+00]]

In [23]: print(dml_plr_obj.psi_elements['psi_b'][:5, :, 0])
[[-2.88988263e-01 -2.65557405e-01 -7.79458848e-02 -4.18888149e-01
  -4.13137893e-01 -3.94441007e-01 -3.39010121e-02 -1.74461783e-01
  -2.85911521e-01 -3.36231307e-01]
 [-9.79338596e-01 -1.44563945e+00 -1.35620768e+00 -1.44713577e+00
  -1.16539906e+00 -1.07544271e+00 -8.08602774e-01 -1.47966100e+00
  -1.08181827e+00 -9.22272803e-01]
 [ 3.30031116e-02  5.04682310e-02 -1.69562150e-02  1.27951256e-02
  -5.69140475e-02  1.72155839e-03  9.32950022e-02  1.12196389e-02
   8.90963122e-02  3.13034980e-02]
 [-1.80714504e-02 -1.98505871e-02  4.07727773e-02  7.39621961e-02
   9.95372559e-02 -4.59307502e-03 -4.86424193e-02  1.57592948e-01
  -1.68554404e-02  1.99571372e-03]
 [ 1.54517706e+00  1.76444177e+00  1.87674597e+00  1.76419024e+00
   2.02079162e+00  2.87290240e+00  1.59199423e+00  2.23751359e+00
   7.73764317e-01  3.18678094e+00]]
dml_plr_obj$fit()
print(dml_plr_obj$psi_a[1:5, ,1])
print(dml_plr_obj$psi_b[1:5, ,1])
            [,1]        [,2]         [,3]         [,4]        [,5]
[1,] -0.04428814 -0.05508317 -0.002493442 -0.136127203 -0.00421565
[2,] -0.12690650 -0.09452911 -0.068486599 -0.000161511 -0.23351535
[3,] -1.26722807 -1.00013820 -0.202054715 -0.580690543 -0.53034890
[4,] -0.67213892 -1.09625749 -0.411136407 -0.012312469 -0.29302490
[5,] -3.63835855 -3.54466575 -4.134382295 -3.735132494 -3.33149500
              [,6]        [,7]        [,8]          [,9]        [,10]
[1,] -0.0009839771 -0.15353897 -0.05892282 -0.0006155274 -0.006336175
[2,] -0.0217186218 -0.00138898 -0.04220894 -0.0770119124 -0.072879404
[3,] -1.0020330084 -0.08815223 -0.59271114 -1.0737230406 -0.484243416
[4,] -0.0089949952 -0.56204492 -0.39609677 -0.3537355071 -0.445318179
[5,] -4.8209769625 -2.85213750 -3.04124838 -2.0664251154 -3.516121684
           [,1]       [,2]       [,3]        [,4]       [,5]        [,6]
[1,]  0.4853692 -0.4762173 -0.1067423 -0.81046327 -0.1608982 -0.06922606
[2,]  0.2159134  0.3504270  0.2401407  0.01129944  0.6589084  0.09398583
[3,]  0.5674384  0.1825040  0.1489112  0.43523205  0.4327693  0.06377038
[4,] -0.6801775 -0.8328230 -0.5659162 -0.16321225 -0.5539996 -0.09461554
[5,]  2.7592087  2.5715910  2.3968987  2.70109193  2.4846891  3.28068121
            [,7]       [,8]        [,9]       [,10]
[1,] -0.76619887 -0.4958420 -0.05748309  0.19036640
[2,] -0.03156700  0.1862020  0.27966099  0.27700288
[3,]  0.12997804  0.4173246  0.68737976  0.09823336
[4,] -0.05475295 -0.5062379 -0.62712156 -0.83584962
[5,]  2.21427819  2.5823616  2.06085261  1.95394347

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. 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.45957837]

In [25]: print(dml_plr_obj.se)
[0.08005229]
print(dml_plr_obj$coef)
print(dml_plr_obj$se)
        d 
0.3172656 
         d 
0.08802924 

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.45467447 0.4065173  0.46709481 0.47857478 0.41093655 0.47759584
  0.46618738 0.38990574 0.44890536 0.46448227]]

In [27]: print(dml_plr_obj._all_se)
[[0.07978296 0.07919896 0.08031571 0.08664208 0.08154161 0.07691847
  0.07436521 0.08091581 0.08333617 0.07685043]]
print(dml_plr_obj$all_coef)
print(dml_plr_obj$all_se)
          [,1]      [,2]      [,3]      [,4]      [,5]     [,6]      [,7]
[1,] 0.3530617 0.2578488 0.3344707 0.3494256 0.2656647 0.352565 0.3225051
          [,8]     [,9]     [,10]
[1,] 0.2833634 0.312026 0.2929164
           [,1]       [,2]       [,3]       [,4]       [,5]       [,6]
[1,] 0.09255228 0.07441789 0.08990459 0.08574508 0.08636259 0.08008678
           [,7]      [,8]      [,9]      [,10]
[1,] 0.09135493 0.0894521 0.0912666 0.08558476

In python, the confidence intervals and p-values are based on the doubleml.DoubleMLFramework object. This class provides methods such as confint, bootstrap or p_adjust. For different repetitions, the computations are done seperately and combined via the median (as based on Chernozhukov et al., 2018).

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_l, 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.424127  0.082297  5.153639  2.554793e-07  0.262829  0.585426
set.seed(314)
dml_plr_obj_internal = DoubleMLPLR$new(obj_dml_data, ml_l, 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.35738    0.08506   4.201 2.65e-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_l, 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.424127  0.082297  5.153639  2.554793e-07  0.262829  0.585426
dml_plr_obj_external = DoubleMLPLR$new(obj_dml_data, ml_l, 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.35738    0.08506   4.201 2.65e-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.

Note

The flag apply_cross_fitting is deprecated for the python package. To avoid cross-fitting, please use the option to set external predictions.

dml_plr_obj_external = DoubleMLPLR$new(obj_dml_data, ml_l, 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.42669    0.09331   4.573 4.81e-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:

dml_plr_obj_external = DoubleMLPLR$new(obj_dml_data, ml_l, 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.5678     0.2461   2.307   0.0211 *
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1


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.

Note

The flag apply_cross_fitting is deprecated for the python package. To avoid cross-fitting, please use the option to set external predictions. Additionally, the number of folds n_folds is expected to be at least 2.

dml_plr_no_split = DoubleMLPLR$new(obj_dml_data, ml_l, 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.26522    0.07456   3.557 0.000375 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1


9.6. References#

  • Chernozhukov, Victor and Demirer, Mert and Duflo, Esther and Fernández-Val, Iván (2018), Generic Machine Learning Inference on Heterogeneous Treatment Effects in Randomized Experiments, with an Application to Immunization in India, National Bureau of Economic Research, doi: 10.3386/w24678.