*! version 31july2023

ddml extract utility: Extracting stored information from ddml associative arrays

The examples below use the partially-linear model
and stacking regression using pystacked.
We also request short-stacking.
The model name is the default name "m0".
For simplicity we use pystacked's default learners and settings.

Preparation and estimation:

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

. ddml E[Y|X]: pystacked net_tfa $X, type(reg)

. ddml E[D|X]: pystacked e401 $X, type(reg)

. ddml crossfit, shortstack

. ddml estimate, robust

show(something) option examples

show option examples: report standard (pystacked) and short-stacked weights.
Standard stacking weights displayed here are mean weights across cross-fit folds.

. ddml extract, show(stweights)

. ddml extract, show(ssweights)

The show option leaves results in r(.) macros.

. mat list r(Y_net_tfa_ss)

. mat list r(D_e401_ss)

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

. ddml extract, show(pystacked)

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

Extract the estimated beta for the short-stack specification ("ss"), 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(ss) key2(2) subkey1(b) subkey2(post)

As above, but store as a Mata object "bmat".
This is done by providing this name after "ddml extract".

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

. mata: bmat

By default, the object is saved as a Mata object.
To save as a Stata macro r(bmat), use the Stata option:

. ddml extract bmat, key1(ss) key2(2) subkey1(b) subkey2(post) stata

. mat list r(bmat)

More examples of the above, relating to specification ss and
various resamples or the mean/median across resamples.
(The list of available results was already displayed above by ddml extract, keys.)

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

. ddml extract, key1(ss) key2(2) subkey1(D_e401_ss_mse_folds) subkey2(matrix)

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

. ddml extract, key1(ss) 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(D1_pystacked) key2(est_main)

. ddml extract, vname(e401) key1(D1_pystacked) 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(D1_pystacked) key2(MSE_folds) key3(1)

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

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

. ddml extract, vname(e401) key1(D1_pystacked) 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(D1_pystacked) key2(MSE) key3(1)

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

Using Mata'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(("ss","2"))"'

. mata: AA_e1_r2.keys()

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