************************
*Author: Xiaoqin Wei *
************************
*This programme is based on STATA and Python and for demonstration only. May contain errors. Python version 3.7 is required and need install machine learning packages on python first*
*environment setting*
ssc install ddml, all replace
ssc install lassopack, all replace
ssc install rforest, all replace
ssc install pystacked, all replace
python search
python set exec C:\Anaconda\python.exe, permanently
python query
python which sklearn
*You can directly install python machine learning "packages" here using "pip"*
************************
*enter python code mode*
************************
python:print("hello world")
python
print("hello world")
end
python:pip install "packages"
************************
* end python code mode*
************************
set seed 1
global Y outcome_variable
global D interested_variable
global X list_of_control_variables
qddml $Y $D (c.($X)##c.($X)), model(partial) cmd(pystacked) cmdopt(method(lassocv)) kfolds(4) noreg robust
qddml $Y $D ($X), model(interactive) cmd(pystacked) cmdopt(method(rf)) kfolds(4) noreg robust
************************
*iv test by using DDML*
************************
global sortvar = "[var lists]"
global keep = "[var lists]"
qddml $Y ($X) (D= iv), model(iv) cmd(pystacked) cmdopt(method(rf)) kfolds(4) noreg vce(cl gvkey) *random forest*
outreg2 using ML.tex, tstat bdec(3) tdec(2) sortvar($sortvar) keep($keep) eqkeep(t) replace
qddml $Y ($X) (D= iv), model(iv) cmd(pystacked) cmdopt(method(gradboost)) kfolds(4) noreg robust *gradboost*
outreg2 using ML.tex, tstat bdec(3) tdec(2) sortvar($sortvar) keep($keep) eqkeep(t) append
qddml $Y ($X) (D= iv), model(iv) cmd(pystacked) cmdopt(method(lassocv)) kfolds(4) noreg robust *lasso*
outreg2 using ML.tex, tstat bdec(3) tdec(2) sortvar($sortvar) keep($keep) eqkeep(t) append
Reference
Double/Debiased Machine Learning – Stata Conference, China, Qiang Chen (2023)