-
Notifications
You must be signed in to change notification settings - Fork 0
/
DML.R
102 lines (69 loc) · 5.44 KB
/
DML.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#
# required packages
#
library(DoubleML)
library(mlr3)
library(mlr3learners)
remotes::install_github("mlr-org/mlr3extralearners")
library(mlr3extralearners)
library(data.table)
#
# Learners for DML
#
ml_g_boost_d1_s0.1<- lrn("regr.gbm", distribution ='gaussian', interaction.depth = 1, shrinkage = 0.1, cv.folds = 10)
ml_m_boost_d1_s0.1<- lrn("regr.gbm", distribution ='gaussian', interaction.depth = 1, shrinkage = 0.1, cv.folds = 10)
ml_g_lasso<- lrn("regr.cv_glmnet", s = "lambda.min", alpha=1, nfolds=10, family='gaussian')
ml_m_lasso<- lrn("regr.cv_glmnet", s = "lambda.min", alpha=1, nfolds=10, family='gaussian')
ml_g_tree_cp0.01<- lrn("regr.rpart", cp=0.01) # cp stands for "complexity parameter" / default value for CV is 10 (xval=10)
ml_m_tree_cp0.01<- lrn("regr.rpart", cp=0.01)
#
# Data sets for DML
#
data_DML<- scale(final_data.w[,c('ln_netcash', 'cash.asset',
'size', 'mtb', 'lev', 'div', 'cfo', 'volcfo_ind', 'nwc', 'acq', 'capex',
'salegro', 'zscore', 'efftax', 'roa', 'volprof_firm', 'netdebt', 'netequ', 'netinv', 'saleppe', 'salepiv', 'overinv', 'volequi_firm', 'volequi_ind', 'neginc', 'forinc', 'dominc',
'rd', 'intang', 'tang', 'taxcost', 'carry', 'mature', 'diversity', 'relation', 'foresale')])
#
# separate causal effect (RD for example)
#
data_lnnetcash_rd<- DoubleMLData$new(data = as.data.table(data_DML), y_col = 'ln_netcash',
x_cols = c('size', 'mtb', 'lev', 'div', 'cfo', 'volcfo_ind', 'nwc', 'acq', 'capex',
'salegro', 'zscore', 'efftax', 'roa', 'volprof_firm', 'netdebt', 'netequ', 'netinv', 'saleppe', 'salepiv', 'overinv', 'volequi_firm', 'volequi_ind', 'neginc', 'forinc', 'dominc'),
d_cols = 'rd')
boost_d1_s0.1_cross4_dml2_lnnetcash_rd<- DoubleMLPLR$new(data=data_lnnetcash_rd, ml_g=ml_g_boost_d1_s0.1, ml_m=ml_m_boost_d1_s0.1, score="partialling out", n_rep=100, n_folds=4, dml_procedure="dml2")
boost_d1_s0.1_cross4_dml2_lnnetcash_rd$fit()
boost_d1_s0.1_cross4_dml2_lnnetcash_rd$summary()
lasso_cross4_dml2_lnnetcash_rd<- DoubleMLPLR$new(data=data_lnnetcash_rd, ml_g=ml_g_lasso, ml_m=ml_m_lasso, score="partialling out", n_rep=100, n_folds=4, dml_procedure="dml2")
lasso_cross4_dml2_lnnetcash_rd$fit()
lasso_cross4_dml2_lnnetcash_rd$summary()
tree_cp0.01_cross4_dml2_lnnetcash_rd<- DoubleMLPLR$new(data=data_lnnetcash_rd, ml_g=ml_g_tree_cp0.01, ml_m=ml_m_tree_cp0.01, score="partialling out", n_rep=100, n_folds=4, dml_procedure="dml2")
tree_cp0.01_cross4_dml2_lnnetcash_rd$fit()
tree_cp0.01_cross4_dml2_lnnetcash_rd$summary()
#
# simultaneous causal effects (AFTER controlling for the effect of the other drivers)
#
data_lnnetcash_simul<- DoubleMLData$new(data = as.data.table(data_DML), use_other_treat_as_covariate=TRUE,
y_col = 'ln_netcash', x_cols = c('size', 'mtb', 'lev', 'div', 'cfo', 'volcfo_ind', 'nwc', 'acq', 'capex',
'salegro', 'zscore', 'efftax', 'roa', 'volprof_firm', 'netdebt', 'netequ', 'netinv', 'saleppe', 'salepiv', 'overinv', 'volequi_firm', 'volequi_ind', 'neginc', 'forinc', 'dominc'),
d_cols = c('rd', 'intang', 'tang', 'taxcost', 'carry', 'mature', 'diversity', 'relation', 'foresale' ))
boost_d1_s0.1_cross4_dml2_lnnetcash_simul<- DoubleMLPLR$new(data=data_lnnetcash_simul, ml_g=ml_g_boost_d1_s0.1, ml_m=ml_m_boost_d1_s0.1, score="partialling out", n_rep=100, n_folds=4, dml_procedure="dml2")
boost_d1_s0.1_cross4_dml2_lnnetcash_simul$fit()
boost_d1_s0.1_cross4_dml2_lnnetcash_simul$summary()
boost_d1_s0.1_cross4_dml2_lnnetcash_simul$bootstrap(method = "normal", n_rep_boot = 1000) # for joint inference
boost_d1_s0.1_cross4_dml2_lnnetcash_simul$p_adjust(method = "romano-wolf") # Romano-Wolf adjusting
round(boost_d1_s0.1_cross4_dml2_lnnetcash_simul$p_adjust(method = "BY"), 3) # Benjamini-Yekutieli adjusting
round(boost_d1_s0.1_cross4_dml2_lnnetcash_simul$p_adjust(method = "bonferroni"), 3) # Bonferroni adjusting
lasso_cross4_dml2_lnnetcash_simul<- DoubleMLPLR$new(data=data_lnnetcash_simul, ml_g=ml_g_lasso, ml_m=ml_m_lasso, score="partialling out", n_rep=100, n_folds=4, dml_procedure="dml2")
lasso_cross4_dml2_lnnetcash_simul$fit()
lasso_cross4_dml2_lnnetcash_simul$summary()
lasso_cross4_dml2_lnnetcash_simul$bootstrap(method = "normal", n_rep_boot = 1000)
lasso_cross4_dml2_lnnetcash_simul$p_adjust(method = "romano-wolf")
round(lasso_cross4_dml2_lnnetcash_simul$p_adjust(method = "BY"), 3)
round(lasso_cross4_dml2_lnnetcash_simul$p_adjust(method = "bonferroni"), 3)
tree_cp0.01_cross4_dml2_lnnetcash_simul<- DoubleMLPLR$new(data=data_lnnetcash_augment_simul, ml_g=ml_g_tree_cp0.01, ml_m=ml_m_tree_cp0.01, score="partialling out", n_rep=100, n_folds=4, dml_procedure="dml2")
tree_cp0.01_cross4_dml2_lnnetcash_simul$fit()
tree_cp0.01_cross4_dml2_lnnetcash_simul$summary()
tree_cp0.01_cross4_dml2_lnnetcash_simul$bootstrap(method = "normal", n_rep_boot = 1000)
tree_cp0.01_cross4_dml2_lnnetcash_simul$p_adjust(method = "romano-wolf")
round(tree_cp0.01_cross4_dml2_lnnetcash_simul$p_adjust(method = "BY"), 3)
round(tree_cp0.01_cross4_dml2_lnnetcash_simul$p_adjust(method = "bonferroni"), 3)