*! version 30aug2024

ddml export utility - Basic example with pystacked

Load the data, define global macros, set the seed and initialize the model.
Use 2-fold cross-fitting with two repetitions (resamples)
Use pystacked's default learners as the supervised learners.
We explicitly name the model to be estimated as m_sip1991;
this is the name of the Mata global containing the model details.
The default behavior of ddml is to prefix the created sample and fold indicators,
but we specify the prefix option with ddml init
so that all created variables including the estimated conditional expectations are prefixed.

. use https://github.com/aahrens1/ddml/raw/master/data/sipp1991.dta, clear

. global Y net_tfa

. global D e401

. global X tw age inc fsize educ db marr twoearn pira hown

. set seed 42

. ddml init partial, kfolds(2) reps(2) mname(m_sip1991) prefix

. ddml E[Y|X], mname(m_sip1991): pystacked $Y $X

. ddml E[D|X], mname(m_sip1991): pystacked $D $X

. ddml crossfit, mname(m_sip1991)

. ddml estimate, mname(m_sip1991)

It will often be a good idea to include an ID variable that identifies the observation number.
This dataset doesn't include an ID variable, so we create one.

. gen long m_sip1991_id = _n

To include the ID variable with everything else, we use the addvars(.) option.
The data will be exported to a CSV file called "m_ddml_sip1991.csv".

. ddml export using m_ddml_sip1991.csv, mname(m_sip1991) replace addvars(m_sip1991_id)

Lists of saved ddml estimated conditional expectations with and without resample numbers,
saved in r(.) macros:

. `"di "`r(vreplist)'""'

. `"di "`r(vlist)'""'