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

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.338952372 -0.007741706 -0.144263002 -0.300624406 -1.530981129
[1] -0.1880867  0.1890037 -0.5082378  0.5862232  0.9135337

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]        [,6]
[1,] -0.052135608 -0.2765185 -0.3187510850 -0.176235088 -0.20929659 -0.09869747
[2,] -0.003536905 -0.1228390 -0.0124599701 -0.004747573 -0.06149508 -0.05073729
[3,] -0.134036304 -0.2040638 -0.0009478777 -0.288607069 -0.11617290 -0.02831464
[4,] -0.252747025 -0.2673046 -0.9504277795 -0.325080132 -0.97279930 -0.58729768
[5,] -1.783749701 -1.6825281 -1.2649074535 -1.765478310 -0.86174649 -0.80383217
           [,7]         [,8]       [,9]         [,10]
[1,] -0.2870376 -0.478427517 -0.4200897 -0.4180466440
[2,] -0.0037998 -0.005213774 -0.0443862 -0.0006321326
[3,] -0.2027650 -0.024742574 -0.2170862 -0.3751204477
[4,] -0.3047724 -0.214491989 -0.3774518 -0.2001010851
[5,] -1.0881676 -1.471911748 -0.5374188 -1.2828579342
            [,1]       [,2]        [,3]        [,4]       [,5]        [,6]
[1,]  0.08424614 -0.3387784 -0.12023345 -0.03028439 -0.2222122 -0.03330886
[2,]  0.10713484 -0.6988656  0.26161837 -0.14199633  0.5445840  0.45244377
[3,] -0.51143117 -0.5516139 -0.04292482 -0.67168779 -0.3744560 -0.25575125
[4,]  0.03809844  0.2254787  0.70055207  0.32729955  0.7193116  0.54242696
[5,]  1.16988856  1.0391009  0.89257838  1.11770674  1.0064036  0.55980336
           [,7]       [,8]       [,9]       [,10]
[1,]  0.4335858 -0.3444328 -0.1764962 -0.22104337
[2,] -0.1222954  0.1520867  0.4078915  0.05607009
[3,] -0.5441532 -0.1822914 -0.6658149 -0.65488831
[4,]  0.2186451  0.2514122  0.4563111  0.46961915
[5,]  0.8407599  0.2711369  0.2300136  1.52747392

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.3378421 
         d 
0.08635891 

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.4094012 0.3294386 0.357899 0.3312258 0.397473 0.3386382 0.3969374
          [,8]      [,9]     [,10]
[1,] 0.3310278 0.3119552 0.3370459
           [,1]       [,2]       [,3]       [,4]      [,5]       [,6]
[1,] 0.09131798 0.08410373 0.07333105 0.08473315 0.0872162 0.08713079
           [,7]       [,8]       [,9]      [,10]
[1,] 0.08537576 0.07949822 0.09847374 0.08848262

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.35555    0.08857   4.014 5.96e-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.35555    0.08857   4.014 5.96e-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.510      0.109    4.68 2.87e-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.7360     0.2015   3.653 0.000259 ***
---
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.36557    0.08855   4.129 3.65e-05 ***
---
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.