*! version 21jan2023

help ddml extract v1.2

Title

ddml extract Stata extract utility for Double Debiased Machine Learning

Please check the examples provided at the end of the help file.

Syntax

ddml extract [ object_name , mname(name) show(display_item) detail ename(name) vname(varname)
stata keys key1(string) key2(string) key3(string) subkey1(string) subkey2(string) ]

display_item can be mse, n or pystacked.
ddml stores many internal results on associative arrays.
These can be retrieved using the different key options.

Options

main options

mname(name)
Name of the DDML model; a Mata object. Defaults to m0.

vname(name)
Name of a Y, D or Z variable corresponding to a DDML equation.

ename(name)
Name of a DDML equation struct; a Mata object.
Use with crossfit or with a DDML eStruct that has been separately extracted.

stata
Saves extracted objects as Stata r(.) macros (default is to leave as Mata objects).

show options

show(pystacked)
Extracts pystacked weights and learner MSEs.
The MSEs are cross-validation MSEs and correspond to the predictions used to obtain the stacking weights;
see help pystacked.

detail
(show(pystacked) only) Extract detailed pystacked weights and learner MSEs by cross-fit fold.

show(shortstack)
Extracts shortstack weights.

show(mse)
Extracts OOS MSEs by crossfitting fold.

show(n)
Extracts sample size by crossfitting fold.

key options

keys
List all keys on the relevant associative array.

key1(string)
Associative array key #1.

key2(string)
Associative array key #2.

key3(string)
Associative array key #3.

subkey1(string)
Associative array subkey #1.

subkey2(string)
Associative array subkey #2.

Examples

The examples below use the partially linear model and stacking regression using pystacked.
The model name is the default name "m0".

Preparation:

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

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

. set seed 42

. ddml init partial, kfolds(3) reps(2)

Add supervised machine learners for estimating conditional expectations:

. global rflow max_features(5) min_samples_leaf(1) max_samples(.7)

. global rfhigh max_features(5) min_samples_leaf(10) max_samples(.7)

. ddml E[Y|X], learner(Y_m1): pystacked net_tfa $X || method(ols) || method(lassocv) || method(ridgecv) || method(rf) opt($rflow) || method(rf) opt($rfhigh), type(reg)

. ddml E[D|X], learner(D_m1): pystacked e401 $X || method(ols) || method(lassocv) || method(ridgecv) || method(rf) opt($rflow) || method(rf) opt($rfhigh), type(reg)

Cross-fitting and estimation.

. ddml crossfit

. ddml estimate, robust

show option examples

show option examples: examine the learner weights and MSEs reported by pystacked, MSEs by fold, and sample sizes by fold.

. ddml extract, show(pystacked)

. ddml extract, show(mse)

. ddml extract, show(n)

show option leaves results in r(.) macros.

. ddml extract, show(pystacked)

. mat list r(Y_m1_w)

. ddml extract, show(mse)

. mat list r(e401_mse)

List keys examples

List keys of associative arrays used in model m0.
Associative array m0.eqnAA is an "equation AA" and has one key,
which is is the name of the variable for which conditional expectations are estimated.
Associative array m0.estAA is an "estimation AA" and has two keys.
The objects stored on this AA are either estimation results,
AAs that have sets of estimation results, or objects with information about the estimations.

. ddml extract, keys

List keys relating to equation for D variable, e401.
Keys for two associative arrays are reported.
Associative array e401.lrnAA is a "learner AA" and has two keys; it stores e.g. an estimation specification.
Associative array e401.resAA is a "results AA" and has three keys; it stores e.g. estimation results.

. ddml extract, keys vname(e401)

Working with model estimation results

Display matrix of betas for across resamplings and specifications.

. ddml extract, key1(bmat) key2(all)

Extract matrix of SEs for across resamplings and specifications.
Store it as a Mata object called semat.

. ddml extract semat, key1(semat) key2(all)

. mata: semat

As above, but store it as a Stata r(.) macro r(semat).

. ddml extract semat, key1(semat) key2(all) stata

. mat list r(semat)

Extract the estimated beta for specification 1, resample 2.
Provide the keys for the AA with the results for the specification and resampling,
and the subkeys for this AA to obtain the posted beta.

. ddml extract, key1(1) key2(2) subkey1(b) subkey2(post)

More examples of the above, relating to specification 1 and
various resamples or the mean/median across resamples.

. ddml extract, key1(1) key2(1) subkey1(D_m1_mse) subkey2(scalar)

. ddml extract, key1(1) key2(2) subkey1(D_m1_mse) subkey2(scalar)

. ddml extract, key1(1) key2(mn) subkey1(V) subkey2(post)

. ddml extract, key1(1) key2(md) subkey1(title) subkey2(local)

Working with equation estimation results

Display information stored on learner AA e401.lrnAA
about the specification of conditional expectations for variable e401.

. ddml extract, vname(e401) key1(D_m1) key2(est_main)

. ddml extract, vname(e401) key1(D_m1) key2(stack_base_est)

Display information stored on results AA e401.resAA
about the estimation results for resamplings 1 and 2.

. ddml extract, vname(e401) key1(D_m1) key2(MSE_folds) key3(1)

. ddml extract, vname(e401) key1(D_m1) key2(MSE_folds) key3(2)

. ddml extract, vname(e401) key1(D_m1) key2(stack_weights) key3(1)

. ddml extract, vname(e401) key1(D_m1) key2(stack_weights) key3(2)

Working directly with an equation associative array

Extract the associative AA for the estimation of conditional expectations for variable e401.
Store it as a Mata object called AA_e401.
Note: the crossfit command returns an equation associative array,
so this step is unnecessary when using this command.

. ddml extract AA_e401, vname(e401)

. mata: AA_e401

Examples of working with this equation associative array.
Note that the ename option must be used.

. ddml extract, ename(AA_e401) key1(D_m1) key2(MSE) key3(1)

. ddml extract, ename(AA_e401) key1(D_m1) key2(MSE) key3(2)

Using Mtata's associative array commands

If preferred, Mata's associative array commands can be used directly.
Note that all keys are strings.

. mata: m0.estAA.keys()

. `"mata: AA_e1_r2 = (m0.estAA).get(("1","2"))"'

. mata: AA_e1_r2.keys()

. `"mata: AA_e1_r2.get(("b","post"))"'

Authors

Achim Ahrens, Public Policy Group, ETH Zurich, Switzerland

achim.ahrens@gess.ethz.ch

Christian B. Hansen, University of Chicago, USA

Christian.Hansen@chicagobooth.edu

Mark E Schaffer, Heriot-Watt University, UK

m.e.schaffer@hw.ac.uk

Thomas Wiemann, University of Chicago, USA

wiemann@uchicago.edu

Also see (if installed)

Help: ddml, crossfit, pystacked.