*! version 30aug2024

Partially-linear model - 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: OLS, cross-validated lasso, and gradient boosting.
NB: The model specification and results will be stored on a Mata object
with the default name "m0".

. 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)

. ddml E[Y|X]: pystacked $Y $X

. ddml E[D|X]: pystacked $D $X

. ddml crossfit

. ddml estimate

Replicate the ddml estimate results for the 1st cross-fit estimation (resample 1) by hand,
using the estimated conditional expectations generated by ddml and pystacked;
"_1" means resample 1.
Compare using ddml estimate, replay.

. cap drop Yresid

. cap drop Dresid

. gen double Yresid = $Y - Y1_pystacked_1

. gen double Dresid = $D - D1_pystacked_1

. regress Yresid Dresid

. ddml estimate, mname(m0) spec(st) rep(1) notable replay