EconBase
← Back to paper

ddml: Double/debiased machine learning in Stata

Extracted main text — title through conclusion, appendix excluded. This is what our citation measures are computed over, published so the extraction can be checked by eye.

114,100 characters · 36 sections · 75 citation commands

Rendered from LaTeX for readability, not typeset faithfully. Citation keys are highlighted; maths is left as source; figures, tables and equation environments are summarised rather than reproduced; unrecognised commands are greyed out so nothing is silently dropped. Email addresses are removed.

ddml: Double/debiased machine learning in Stata

\graphicspath{{fig/}}

\makeatletter \makeatother

\hypersetup{hidelinks}

\inserttype[st0001]{article} { Achim Ahrens\\ETH Z\"urich \\[email removed] \and Christian B. Hansen\\ University of Chicago\\[email removed] \and Mark E. Schaffer\\ Heriot-Watt University\\Edinburgh, United Kingdom \\ [email removed] \and Thomas Wiemann\\ University of Chicago\\ [email removed] }

abstractWe introduce the package \stcmd{ddml}\xspace for Double/De\-bias\-ed Machine Learning (DDML) in Stata. Estimators of causal parameters for five different econometric models are supported, allowing for flexible estimation of causal effects of endogenous variables in settings with unknown functional forms and/or many exogenous variables. \stcmd{ddml}\xspace is compatible with many existing supervised machine learning programs in Stata. We recommend using DDML in combination with stacking estimation which combines multiple machine learners into a final predictor. We provide Monte Carlo evidence to support our recommendation.

Introduction

Identification of causal effects frequently relies on an unconfoundedness assumption, requiring that treatment or instrument assignment is sufficiently random given observed control covariates. Estimation of causal effects in these settings then involves conditioning on the controls. Unfortunately, estimators of causal effects that are insufficiently flexible to capture the effect of confounds generally do not produce consistent estimates of causal effects even when unconfoundedness holds. For example, blandhol2022tsls highlight that TSLS estimands obtained after controlling linearly for confounds do not generally correspond to weakly causal effects even when instruments are valid conditional on controls. Even in the ideal scenario where theory provides a small number of relevant controls, theory rarely specifies the exact nature of confounding. Thus, applied empirical researchers wishing to exploit unconfoundedness assumptions to learn causal effects face a nonparametric estimation problem.

Traditional nonparametric estimators suffer greatly under the curse of dimensionality and are quickly impractical in the frequently encountered setting with multiple observed covariates.\footnote{For example, the number of coefficients in polynomial series regression with interaction terms increases exponentially in the number of covariates.} These difficulties leave traditional nonparametric estimators essentially inapplicable in the presence of increasingly large and complex data sets, e.g.\ textual confounders as in roberts2020a or digital trace data hangartner2021a. Tools from supervised machine learning have been put forward as alternative estimators. These approaches are often more robust to the curse of dimensionality via the exploitation of regularization assumptions. A prominent example of a machine learning-based causal effects estimator is Post-Double Selection Lasso (PDS-Lasso) of Belloni2014a, which fits auxiliary lasso regressions of the outcome and treatment(s), respectively, against a menu of transformed controls. Under an approximate sparsity assumption, which posits that the DGP can be approximated well by a relatively small number of terms included in the menu, this approach allows for precise treatment effect estimation. The lasso can also be used for approximating optimal instruments Belloni2012. Lasso-based approaches for estimation of causal effects have become a popular strategy in applied econometrics gilchrist2016something,dhar2022reshaping, partially facilitated by the availability of software programs in Stata (\stcmd{pdslasso}\xspace, Ahrens2018; statacorp2019) and R (hdm, Chernozhukov2016b).

Although approximate sparsity is a weaker regularization assumption than assuming a linear functional form that depends on a known low-dimensional set of variables, it may not be suitable in a wide range of applications. For example, giannone2021economic argue that approximate sparsity may provide a poor description in several economic examples. There is thus a potential benefit to expanding the set of regularization assumptions and correspondingly considering a larger set of machine learners including, for example, random forests, gradient boosting, and neural networks. While the theoretical properties of these estimators are an active research topic (see, e.g., Athey2019a, farrell2021deep), machine learning methods are widely adopted in industry and practice for their empirical performance. To facilitate their application for causal inference in common econometric models, Chernozhukov2018 propose Double/Debiased Machine Learning (DDML), which exploits Neyman orthogonality of estimating equations and cross-fitting to formally establish asymptotic normality of estimators of causal parameters under relatively mild convergence rate conditions on nonparametric estimators.

DDML increases the set of machine learners that researchers can leverage for estimation of causal effects. Deciding which learner is most suitable for a particular application is difficult, however, since researchers are rarely certain about the structure of the underlying data generating process. A practical solution is to construct combinations of a diverse set of machine learners using stacking Wolpert1992,Breiman1996a. Stacking is a meta learner given by a weighted sum of individual machine learners (the “base learners”). When the weights corresponding to the base learners are chosen to maximize out-of-sample predictive accuracy, this approach hedges against the risk of relying on any particular poorly suited or ill-tuned machine learner.

In this article, we introduce the Stata package \stcmd{ddml}\xspace, which implements DDML for Stata.\footnote{This article refers to version v1.4.2 of \stcmd{ddml}\xspace.} \stcmd{ddml}\xspace adds to a small number of programs for causal machine learning in Stata (Ahrens2018, statacorp2019, statacorp2021). We briefly summarize the four main features of the program:

enumerate\stcmd{ddml}\xspace supports flexible estimators of causal parameters in five econometric models: (1) the Partially Linear Model, (2) the Interactive Model (for binary treatment), (3) the Partially Linear IV Model, (4) the Flexible Partially Linear IV Model, and (5) the Interactive IV Model (for binary treatment and instrument). • \stcmd{ddml}\xspace supports data-driven combinations of multiple machine learners via stacking by leveraging \stcmd{pystacked}\xspace Ahrens2022, our complementary Stata frontend relying on the Python library scikit-learn scikit-learn,sklearn_api. \stcmd{ddml}\xspace also supports two novel approaches to pairing DDML with stacking introduced in Ahrens2023_applied: short-stacking takes a short-cut by leveraging the cross-fitted predicted values for estimating the stacking weights and pooled stacking enforces common weights across cross-fitting folds. • Aside from \stcmd{pystacked}\xspace, \stcmd{ddml}\xspace can be used in combination with many other existing supervised machine learning programs available in or via Stata. \stcmd{ddml}\xspace has been tested with lassopack Ahrens2019, rforest Schonlau2020, svmachines Guenther2018, and parsnip Huntington2021. Indeed, the requirements for compatibility with \stcmd{ddml}\xspace are minimal: Any eclass program with the Stata-typical “\texttt{reg\,\,y\,\,x}” syntax, support for \texttt{if} conditions and post-estimation \texttt{predict} is compatible with \stcmd{ddml}\xspace. • \stcmd{ddml}\xspace provides flexible multi-line syntax and short one-line syntax. The multi-line syntax offers a wide range of options, guides the user through the DDML algorithm step-by-step, and includes auxiliary programs for storing, loading and displaying additional information. We also provide a complementary one-line version called \stcmd{qddml}\xspace (`quick' \stcmd{ddml}\xspace), which uses a similar syntax as \stcmd{pdslasso}\xspace and \texttt{ivlasso} Ahrens2018.

The article proceeds as follows. Section (ref) outlines DDML for the Partially Linear and Interactive Models under conditional unconfoundedness assumptions. Section (ref) outlines DDML for Instrumental Variables (IV) models. Section (ref) discusses how stacking can be combined with DDML and provides evidence from Monte Carlo simulations illustrating the advantages of DDML with stacking. Section (ref) explains the features, syntax and options of the program. Section (ref) demonstrates the program's usage with two applications.

DDML with Conditional Unconfoundedness

This section discusses DDML for the Partially Linear Model and the Interactive Model in turn. The exposition follows Chernozhukov2018. Both models are special cases of the general causal model

align[align omitted — 60 chars of source]

where $f_0$ is a structural function, $Y$ is the outcome, $D$ is the variable of interest, $\bm{X}$ are observed covariates, and $U$ are all unobserved determinants of $Y$ (i.e., other than $D$ and $\bm{X}$).\footnote{Since in (ref), $(D, \bm{X}, U)$ jointly determine $Y$, the model is also dubbed the “all causes model” (see, e.g., heckman2007econometric). Note that the model can equivalently be put into potential outcome notation with potential outcomes defined as $Y(d)\equiv f_0(d, \bm{X}, U)$.} The key difference between the Partially Linear Model and the Interactive Model is their position in the trade-off between functional form restrictions on $f_0$ and restrictions on the joint distribution of observables $(D,\bm{X})$ and unobservables $U$. For both models, we highlight key parameters of interest, state sufficient identifying assumptions, and outline the corresponding DDML estimator. A random sample $\{(Y_i, D_i, \bm{X}_i)\}_{i=1}^n$ from $(Y, D, \bm{X})$ is considered throughout.

The Partially Linear Model (partial)

The Partially Linear Model imposes the estimation model

align[align omitted — 99 chars of source]

where $\theta_0$ is a fixed unknown parameter. The key feature of the model is that the controls $\bm{X}$ enter through the unknown and potentially nonlinear function $g_0$. Note that $D$ is not restricted to be binary and may be discrete, continuous or mixed. For simplicity, we assume that $D$ is a scalar, although \stcmd{ddml}\xspace allows for multiple treatment variables in the Partially Linear Model.

The parameter of interest is $\theta_0$, the causal effect of $D$ on $Y$.\footnote{The interpretation of $\theta_0$ can be generalized. For example, the results of angrist1999empirical imply that in the general causal model (ref), $\theta_0$ is a positively weighted average of causal effects (e.g., conditional average treatment effects) under stronger identifying assumptions. The basic structure can also be used to obtain valid inference on objects of interest, such as projection coefficients, in the presence of high-dimensional data or nonparametric estimation without requiring a causal interpretation.} The key identifying assumption is given in Assumption (ref).\footnote{Discussions of Partially Linear Model typically show identification under the stronger assumption that $E[U\vert D,\bm{X}]=0$. We differentiate here to highlight differences between the Partially Linear Model and Interactive Model.}

assumption[Conditional Orthogonality] $E[Cov(U, D\vert \bm{X})]= 0$ .

To show identification of $\theta_0$, consider the score

align[align omitted — 150 chars of source]

where $\bm{W} \equiv (Y,D,\bm{X})$, and $\ell$ and $m$ are nuisance functions. Letting $m_0(\bm{X})\equiv E[D\vert \bm{X}] $ and $\ell_0(\bm{X})\equiv E[Y\vert \bm{X}]$, note that

align*[align* omitted — 71 chars of source]

by Assumption (ref). When in addition $E[Var(D\vert \bm{X})] \neq 0$, we get

align[align omitted — 159 chars of source]

Equation (ref) is constructive in that it motivates estimation of $\theta_0$ via a simple two-step procedure: First, estimate the conditional expectation of $Y$ given $\bm{X}$ (i.e., $\ell_0$) and of $D$ given $\bm{X}$ (i.e., $m_0$) using appropriate nonparametric estimators (e.g., machine learners). Second, residualize $Y$ and $D$ by subtracting their respective conditional expectation function (CEF) estimates, and regress the resulting CEF residuals of $Y$ on the CEF residuals of $D$. This approach is fruitful when the estimation error of the first step does not propagate excessively to the second step. DDML leverages two key ingredients to control the impact of the first step estimation error on the second step estimate: 1) second step estimation based on Neyman orthogonal scores and 2) cross-fitting. As shown in Chernozhukov2018, this combination facilitates the use of any nonparametric estimator that converges sufficiently quickly in the first and potentially opens the door for the use of many machine learners.

Neyman orthogonality refers to a property of score functions $\psi$ that ensures local robustness to estimation errors in the first step. Formally, it requires that the Gateaux derivative with respect to the nuisance functions evaluated at the true values is mean-zero. In the context of the partially linear model, this condition is satisfied for the moment condition (ref):

align*[align* omitted — 135 chars of source]

where the derivative is with respect to the scalar $r$ and evaluated at $r=0$. Heuristically, we can see that this condition alleviates the impact of noisy estimation of nuisance functions as local deviations of the nuisance functions away from their true values leave the moment condition unchanged. We refer to Chernozhukov2018 for a detailed discussion but highlight that all score functions discussed in this article are Neyman orthogonal.

Cross-fitting ensures independence between the estimation error from the first step and the regression residual in the second step. To implement cross-fitting, we randomly split the sample into $K$ evenly-sized folds, denoted as $I_1,\ldots, I_K$. For each fold $k$, the conditional expectations $\ell_0$ and $m_0$ are estimated using only observations not in the $k$th fold -- i.e., in $I^c_k\equiv I \setminus I_k$ -- resulting in $\hat{\ell}_{I^c_{k}}$ and $\hat{m}_{I^c_{k}}$, respectively, where the subscript ${I^c_{k}}$ indicates the subsample used for estimation. The out-of-sample predictions for an observation $i$ in the $k$th fold are then computed via $\hat{\ell}_{I^c_{k}}(\bm{X}_i)$ and $\hat{m}_{I^c_{k}}(\bm{X}_i)$. Repeating this procedure for all $K$ folds then allows for computation of the DDML estimator for $\theta_0$:

align[align omitted — 254 chars of source]

where $k_i$ denotes the fold of the $i$th observation.\footnote{We here omit the constant from the estimation stage. Since the residualized outcome and treatment may not be exactly mean-zero in finite samples, \stcmd{ddml}\xspace includes the constant by default in the estimation stage of partially linear models.}

We summarize the DDML algorithm for the Partially Linear Model in Algorithm 1:\footnote{Algorithm 1 corresponds to the `DML2' algorithm in Chernozhukov2018. Chernozhukov2018 recommend `DML2' over the alternative `DML1' algorithm, which fits the final estimator by fold.}

minipage{\linewidth} \begin{sttech}[Algorithm 1. DDML for the Partially Linear Model.] Split the sample $\{(Y_i, D_i, \bm{X}_i)\}_{i=1}^n$ randomly in $K$ folds of approximately equal size. Denote $I_k$ the set of observations included in fold $k$ and $I_k^c$ its complement. \begin{enumerate}[nosep] • For each $k\in\{1,\ldots, K\}$: \begin{enumerate} • Fit a CEF estimator to the sub-sample $I_k^c$ using $Y_i$ as the outcome and $\bm{X}_i$ as predictors. Obtain the out-of-sample predicted values $\hat{\ell}_{I^c_k}(\bm{X}_i)$ for $i\in I_k$. • Fit a CEF estimator to the sub-sample $I_k^c$ using $D_i$ as the outcome and $\bm{X}_i$ as predictors. Obtain the out-of-sample predicted values $\hat{m}_{I^c_k}(\bm{X}_i)$ for $i\in I_k$. \end{enumerate} • Compute (ref). \end{enumerate} \end{sttech}

Chernozhukov2018 give conditions on the joint distribution of the data, in particular on $g_0$ and $m_0$, and properties of the nonparametric estimators used for CEF estimation, such that $\hat{\theta}_n$ is consistent and asymptotically normal. Standard errors are equivalent to the conventional linear regression standard errors of $Y_i-\hat{\ell}_{I^c_{k_i}}(\bm{X}_i)$ on $D_i-\hat{m}_{I^c_{k_i}}(\bm{X}_i)$. ddml computes the DDML estimator for the Partially Linear Model using Stata's regress. All standard errors available for linear regression in Stata are also available in \stcmd{ddml}\xspace, including different heteroskedasticity and cluster-robust standard errors.\footnote{See help regress\#\#vcetype for available options.}

\paragraph{Remark 1: Number of folds.} The number of cross-fitting folds is a necessary tuning choice. Theoretically, any finite value is admissable. Chernozhukov2018 report that four or five folds perform better than only using $K=2$. Based on our simulation experience, we find that more folds tends to lead to better performance as more data is used for estimation of conditional expectation functions, especially when the sample size is small. We believe that more work on setting the number of folds would be useful, but believe that setting $K = 5$ provides is likely a good baseline in many settings.

\paragraph{Remark 2: Cross-fitting repetitions.} DDML relies on randomly splitting the sample into $K$ folds. We recommend running the cross-fitting procedure more than once using different random folds to assess randomness introduced via the sample splitting. \stcmd{ddml}\xspace facilitates this using the rep(integer) options, which automatically estimates the same model multiple times and combines the resulting estimates to obtain the final estimate. By default, \stcmd{ddml}\xspace reports the median over cross-fitting repetitions. \stcmd{ddml}\xspace also supports the average of estimates. Specifically, let $\hat\theta_n^{(r)}$ denote the DDML estimate from the $r$th cross-fit repetition and $\hat{s}_n^{(r)}$ its associated standard error estimate with $r=1,\ldots,R$. The aggregate median point estimate and associated standard error are defined as \[ \breve{\hat{\theta}}_n = {\textrm{median}}{\left(\left(\hat\theta_n^{(r)}\right)_{r=1}^R\right)} \quad\textrm{and}\quad \breve{\hat{s}}_n = \sqrt{\textrm{median}{\left(\left((\hat{s}_n^{(r)})^2 + (\hat\theta_n^{(r)} - \breve{\hat{\theta}}_n)^2\right)_{r=1}^R\right)}}. \] The aggregate mean point estimates and associated standard error are calculated as \[ \bar{\hat{\theta}}_n = \frac{1}{R}\sum_{r=1}^R \hat\theta_n^{(r)} \quad\textrm{and}\quad \bar{\hat{s}}_n = \sqrt{\textrm{hmean}{\left(\left((\hat{s}_n^{(r)})^2 + (\hat\theta_n^{(r)} - \bar{\hat{\theta}}_n)^2\right)_{r=1}^R\right)}}, \] where $\textrm{hmean}{()}$ is the harmonic mean.\footnote{The harmonic mean of $x_1,\ldots,x_n$ is defined as $\textrm{hmean}{(x_1,\ldots,x_n)}=n\left(\sum_{i=1}^n \frac{1}{x_i} \right)^{-1}$. We use the harmonic mean as it is less sensitive to outlier values.}

\paragraph{Remark 3: Cluster-dependence and folds.} Under cluster-dependence, we recommend randomly assigning folds by cluster; see fcluster(varname).

The Interactive Model (interactive)

The Interactive Model is given by

align[align omitted — 91 chars of source]

where $D$ takes values in $\{0,1\}$. The key deviations from the Partially Linear Model are that $D$ must be a scalar binary variable and that $D$ is not required to be additively separable from the controls $\bm{X}$. In this setting, the parameters of interest we consider are

align*[align* omitted — 169 chars of source]

which correspond to the average treatment effect (ATE) and average treatment effect on the treated (ATET), respectively.

Assumptions (ref) and (ref) below are sufficient for identification of the ATE and ATET. Note the conditional mean independence condition stated here is stronger than the conditional orthogonality assumption sufficient for identification of $\theta_0$ in the Partially Linear Model.

assumption[Conditional Mean Independence] $E[U\vert D, \bm{X}]=0$.
assumption[Overlap] $\Pr(D=1 \vert \bm{X})\in (0,1)$ with probability 1.

Under assumptions (ref) and (ref), we have

align*[align* omitted — 110 chars of source]

so that identification of the ATE and ATET immediately follows from their definition.\footnote{In the defined Interactive Model under Assumption (ref), the heterogeneity in treatment effects that the ATE and ATET average over is fully observed since $U$ is additively separable. Under stronger identifying assumptions, the DDML ATE and ATET estimators outlined here also apply to the ATE and ATET in the general causal model (ref) that average over both observed and unobserved heterogeneity. See, e.g., Belloni2017.}

In contrast to Section (ref), second-step estimators are not directly based on the moment conditions used for identification. Additional care is needed to ensure local robustness to first-stage estimation errors (i.e., Neyman orthogonality). In particular, the Neyman orthogonal score for the ATE that Chernozhukov2018 consider is the efficient influence function of Hahn1998

align*[align* omitted — 193 chars of source]

where $\bm{W}\equiv (Y, D, \bm{X})$. Similarly for the ATET,

align*[align* omitted — 171 chars of source]

Importantly, for $g_0(D,\bm{X})\equiv E[Y\vert D, \bm{X}]$, $m_0(\bm{X})\equiv E[D\vert \bm{X}]$, and $p_0 \equiv E[D]$, Assumptions (ref) and (ref) imply

align*[align* omitted — 163 chars of source]

and we also have that the Gateaux derivative of each condition with respect to the nuisance parameters $(g_0,m_0,p_0)$ is zero.

As before, the DDML estimators for the ATE and ATET leverage cross-fitting. The DDML estimators of the ATE and ATET based on $\psi^{\textrm{ATE}}$ and $\psi^{\textrm{ATET}}$ are

align[align omitted — 733 chars of source]

where $\hat{g}_{I^c_{k}}$ and $\hat{m}_{I^c_{k}}$ are cross-fitted estimators for $g_0$ and $m_0$ as defined in Section (ref). Since $D$ is binary, the cross-fitted values $\hat{g}_{I^c_{k}}(1,\bm{X})$ and $\hat{g}_{I^c_{k}}(0,\bm{X})$ are computed by only using treated and untreated observations, respectively. $\hat{p}_{I^c_{k}}$ is a cross-fitted estimator of the unconditional treatment probability.

\stcmd{ddml}\xspace supports heteroskedasticity and cluster-robust standard errors for $\hat{\theta}_n^{\textrm{ATE}}$ and $\hat{\theta}_n^{\textrm{ATET}}$. The algorithm for estimating the ATE and ATET are conceptually similar to Algorithm 1. We delegate the detailed outline to Algorithm A.1 in the Appendix. Mean and median aggregation over cross-fitting repetitions are implemented as outlined in Remark 2.

DDML with Instrumental Variables

This section outlines the Partially Linear IV Model, the Flexible Partially Linear IV Model, and the Interactive IV Model. The discussion is again based on Chernozhukov2018. As in the previous section, each model is a special case of the general causal model (ref). The discussion in this section differs from the preceding section in that identifying assumptions leverage instrumental variables $\bm{Z}$. The two partially linear IV models assume strong additive separability as in (ref), while the Interactive IV Model allows for arbitrary interactions between the treatment $D$ and the controls $\bm{X}$ as in (ref). The Flexible Partially Linear IV Model allows for approximation of optimal instruments\footnote{We only accommodate approximation of optimal instruments under homoskedasticity. The instruments are valid more generally but are not optimal under heteroskedasticity. Obtaining optimal instruments under heteroskedasticity would require estimating conditional variance functions.} as in Belloni2012 and Chernozhukov2015a, but relies on a stronger independence assumption than the Partially Linear IV Model. Throughout this discussion, we consider a random sample $\{(Y_i, D_i, \bm{X}_i,\bm{Z}_i)\}_{i=1}^n$ from $(Y, D, \bm{X}, \bm{Z})$.

Partially Linear IV Model (iv)

The Partially Linear IV Model considers the same functional form restriction on the causal model as the Partially Linear Model in Section (ref). Specifically, the Partially Linear IV Model maintains

align*[align* omitted — 89 chars of source]

where $\theta_0$ is the unknown parameter of interest.\footnote{As in Section (ref), the interpretation of $\theta_0$ can be generalized under stronger identifying assumptions. See angrist2000interpretation.}

The key deviation from the Partially Linear Model is that the identifying assumptions leverage instrumental variables $Z$, instead of directly restricting the dependence of $D$ and $U$. For ease of exposition, we focus on scalar-valued instruments in this section but we emphasize that \stcmd{ddml}\xspace for Partially Linear IV supports multiple instrumental variables and multiple treatment variables.

Assumptions (ref) and (ref) below are sufficient orthogonality and relevance conditions, respectively, for identification of $\theta_0$.

assumption[Conditional IV Orthogonality] $E[Cov(U,Z\vert \bm{X})]= 0$.
assumption[Conditional Linear IV Relevance] $E[Cov(D,Z\vert \bm{X})]\neq 0$.

To show identification, consider the score function

align*[align* omitted — 123 chars of source]

where $\bm{W}\equiv (Y, D, \bm{X}, Z)$. Note that for $\ell_0(\bm{X}) \equiv E[Y\vert \bm{X}]$, $m_0(\bm{X})\equiv E[D\vert \bm{X}]$, and $r_0(\bm{X})\equiv E[Z\vert \bm{X}]$, Assumption (ref) implies $E[\psi(\bm{W}; \theta_0, \ell_0,m_0,r_0)]=0.$ We will also have that the Gateux derivative of $E[\psi(\bm{W}; \theta_0, \ell_0,m_0,r_0)]$ with respect to the nuisance functions $(\ell_0,m_0,r_0)$ will be zero. Rewriting $E[\psi(\bm{W}; \theta_0, \ell_0,m_0,r_0)]=0$ then results in a Wald expression given by

align[align omitted — 189 chars of source]

where Assumption (ref) is used to ensure a non-zero denominator.

The DDML estimator based on Equation (ref) is given by

align[align omitted — 311 chars of source]

where $\hat{\ell}_{I^c_{k}}$, $\hat{m}_{I^c_{k}}$, and $\hat{r}_{I^c_{k}}$ are appropriate cross-fitted CEF estimators.

Standard errors corresponding to $\hat{\theta}_{n}$ are equivalent to the IV standard errors where $Y_i-\hat{\ell}_{I^c_{k_i}}(\bm{X}_i)$ is the outcome, $D_i-\hat{m}_{I^c_{k_i}}(\bm{X}_i)$ is the endogenous variable, and $Z_i-\hat{r}_{I^c_{k_i}}(\bm{X}_i)$ is the instrument. \stcmd{ddml}\xspace supports conventional standard errors available for linear instrumental variable regression in Stata, including heteroskedasticity and cluster-robust standard errors. Mean and median aggregation over cross-fitting repetitions are implemented as outlined in Remark 2. In the case where we have multiple instruments or endogenous regressors, we adjust the algorithm by residualizing each instrument and endogenous variable as above and applying two-stage least squares with the residualized outcome, endogenous variables, and instruments.

Flexible Partially Linear IV Model (fiv)

The Flexible Partially Linear IV Model considers the same parameter of interest as the Partially Linear IV Model. The key difference here is that identification is based on a stronger independence assumption which allows for approximating optimal instruments using nonparametric estimation, including machine learning, akin to Belloni2012 and Chernozhukov2015a. In particular, the Flexible Partially Linear IV Model leverages a conditional mean independence assumption rather than an orthogonality assumption as in Section (ref). As in Section (ref), we state everything in the case of a scalar $D$.

assumption[Conditional IV Mean Independence] $E[U\vert \bm{Z},\bm{X}] = 0$.

Assumption (ref) implies that for any function $\tilde{p}(\bm{Z},\bm{X})$, it holds that

align[align omitted — 202 chars of source]

where $\ell_0(\bm{X})=E[Y\vert \bm{X}]$ and $m_0(\bm{X})=E[D\vert \bm{X}]$. Identification based on (ref) requires that there exists some function $\tilde{p}$ such that

align[align omitted — 113 chars of source]

A sufficient assumption is that $D$ and $\bm{Z}$ are not mean independent conditional on $\bm{X}$. This condition allows setting $\tilde{p}(\bm{Z},\bm{X}) = E[D\vert \bm{Z},\bm{X}]$ which will then satisfy (ref).\footnote{The choice $\tilde{p}(\bm{Z},\bm{X}) = E[D\vert \bm{Z},\bm{X}]$ results in the optimal instrument, in the sense of semi-parametric efficiency, under homoskedasticity.} Assumption (ref) is a consequence of this non-mean independence.

assumption[Conditional IV Relevance] $E[Var(E[D\vert \bm{Z},\bm{X}]\vert \bm{X})] \neq 0.$

Consider now the score function

align*[align* omitted — 141 chars of source]

where $\bm{W}\equiv (Y, D, \bm{X}, \bm{Z})$. Note that for $\ell_0(\bm{X}) \equiv E[Y\vert \bm{X}]$, $m_0(\bm{X})\equiv E[D\vert \bm{X}]$, and $p_0(\bm{Z},\bm{X}) \equiv E[D\vert \bm{Z},\bm{X}]$, Assumption (ref) and the law of iterated expectations imply $E[\psi(\bm{W}; \theta_0, \ell_0, m_0, p_0)]=0$ and the Gateaux differentiability condition holds. Rewriting then results in a Wald expression given by

align[align omitted — 212 chars of source]

where Assumption (ref) ensures a non-zero denominator.

The DDML estimator based on the moment solution (ref) is given by

align[align omitted — 375 chars of source]

where $\hat{\ell}_{I^c_{k}}$, $\hat{m}_{I^c_{k}}$, and $\hat{p}_{I^c_{k}}$ are appropriate cross-fitted CEF estimators.

In simulations, we find that the finite sample performance of the estimator in (ref) improves when the law of iterated expectations applied to $E[p_0(\bm{Z},\bm{X})] = m_0(\bm{X})$ is explicitly approximately enforced in estimation. As a result, we propose an intermediate step to the previously considered two-step DDML algorithm: Rather than estimating the conditional expectation of $D$ given $\bm{X}$ directly, we estimate it by projecting first-step estimates of the conditional expectation of $p_0(\bm{Z},\bm{X})$ onto $\bm{X}$ instead. Algorithm 2 outlines the LIE-compliant DDML algorithm for computation of (ref).

minipage{\linewidth} \begin{sttech}[Algorithm 2. LIE-compliant DDML for the Flexible Partially Linear IV Model.] Split the sample $\{(Y_i, D_i, \bm{X}_i, \bm{Z}_i)\}_{i=1}^n$ randomly in $K$ folds of approximately equal size. Denote $I_k$ the set of observations included in fold $k$ and $I_k^c$ its complement. \begin{enumerate}[nosep] • For each $k\in\{1,\ldots, K\}$: \begin{enumerate} • Fit a CEF estimator to the sub-sample $I_k^c$ using $Y_i$ as the outcome and $\bm{X}_i$ as predictors. Obtain the out-of-sample predicted values $\hat{\ell}_{I^c_k}(\bm{X}_i)$ for $i\in I_k$. • Fit a CEF estimator to the sub-sample $I_k^c$ using $D_i$ as the outcome and $(\bm{Z}_i,\bm{X}_i)$ as predictors. Obtain the out-of-sample predicted values $\hat{p}_{I^c_k}(\bm{Z}_i,\bm{X}_i)$ for $i\in I_k$ and in-sample predicted values $\hat{p}_{I^c_k}(\bm{Z}_i,\bm{X}_i)$ for $i\in I^c_k$. • Fit a CEF estimator to the sub-sample $I_k^c$ using the in-sample predicted values $\hat{p}_{I^c_k}(\bm{Z}_i,\bm{X}_i)$ as the outcome and $X_i$ as predictors. Obtain the out-of-sample predicted values $\hat{m}_{I^c_k}(\bm{X}_i)$ for $i\in I_k.$ \end{enumerate} • Compute (ref). \end{enumerate} \end{sttech}

Standard errors corresponding to $\hat{\theta}_{n}$ in (ref) are the same as in Section (ref) where the instrument is now given by $\hat{p}_{I^c_{k_i}}(\bm{Z}_i, \bm{X}_i)-\hat{m}_{I^c_{k_i}}(\bm{X}_i)$. Mean and median aggregation over cross-fitting repetitions are as outlined in Remark 2.

Interactive IV Model (interactiveiv)

The Interactive IV Model considers the same causal model as in Section (ref); specifically

align*[align* omitted — 74 chars of source]

where $D$ takes values in $\{0,1\}$. The key difference from the Interactive Model is that this section considers identification via a binary instrument $Z$ representing assignment to treatment.

The parameter of interest we target is

align[align omitted — 176 chars of source]

where $p_0(Z,\bm{X})\equiv \Pr(D=1\vert Z, \bm{X})$. Here, $\theta_0$ is a local average treatment effect (LATE). Note that in contrast to the LATE developed in imbens1994identification, we follow the exposition in Chernozhukov2018 where “local” does not strictly refer to compliers but instead observations with a higher propensity score -- i.e., a higher probability of complying.\footnote{Identification of the conventional complier-focused LATE is achieved under stronger conditional independence and monotonicity assumptions not introduced in this paper. Under these stronger assumptions, the DDML LATE estimator outlined here targets the conventionally considered LATE parameter.}

Identification again leverages Assumptions (ref) and (ref) made in the context of the Flexible Partially Linear IV Model. In addition, we assume that the propensity score is weakly monotone with probability one, and that the support of the instrument is independent of the controls.

assumption[Monotonicity] $p_0(1, \bm{X})\geq p_0(0, \bm{X})$ with probability 1.
assumption[IV Overlap] $\Pr(Z=1\vert \bm{X})\in(0,1)$ with probability 1.

Assumptions (ref)-(ref) imply that

align[align omitted — 160 chars of source]

where $\ell_0(Z, \bm{X}) \equiv E[Y\vert Z, \bm{X}]$, verifying identification of the LATE $\theta_0$. Akin to Section (ref), however, estimators of $\theta_0$ should not directly be based on Equation (ref) because the estimating equations implicit in obtaining (ref) do not satisfy Neyman-orthogonality. Hence, a direct estimator of $\theta_0$ obtained by plugging nonparametric estimators in for nuisance functions in (ref) will potentially be highly sensitive to the first step nonparametric estimation error. Rather, we base estimation on the Neyman orthogonal score function

align*[align* omitted — 319 chars of source]

where $\bm{W} \equiv (Y, D, \bm{X}, Z)$ . Note that under Assumptions (ref)-(ref) and for $\ell_0(Z,\bm{X}) \equiv E[Y\vert Z,\bm{X}]$, $p_0(Z,\bm{X}) \equiv E[D|Z,\bm{X}]$, and $r_0(\bm{X})\equiv E[Z\vert \bm{X}]$, we have $E[\psi(\bm{W};\theta_0, \ell_0, p_0, r_0)]=0$ and can verify that its Gateaux derivative with respect to the nuisance functions local to their true values is also zero.

The DDML estimator based on the orthogonal score $\psi$ is then

align[align omitted — 634 chars of source]

where $\hat{\ell}_{I^c_{k}}$, $\hat{p}_{I^c_{k}}$, and $\hat{r}_{I^c_{k}}$ are appropriate cross-fitted CEF estimators. Since $Z$ is binary, the cross-fitted values $\hat{\ell}_{I^c_{k}}(1,\bm{X})$ and $\hat{p}_{I^c_{k}}(1,\bm{X})$, as well as $\hat{\ell}_{I^c_{k}}(0,\bm{X})$ and $\hat{p}_{I^c_{k}}(0,\bm{X})$ are computed by using only assigned and unassigned observations, respectively.

\stcmd{ddml}\xspace supports heteroskedasticity and cluster-robust standard errors for $\hat{\theta}_n$. Mean and median aggregation over cross-fitting repetitions are implemented as outlined in Remark 2.

The choice of machine learner

Chernozhukov2018 show that DDML estimators are asymptotically normal when used in combination with a general class of machine learners satisfying a relatively weak convergence rate requirement for estimating the CEFs. While asymptotic properties of common machine learners remain an highly active research area, recent advances provide convergence rates for special instances of many machine learners, including lasso bickel2009simultaneous, Belloni2012, random forests wager2015adaptive, wager2018, Athey2019a, neural networks schmidt2020nonparametric, farrell2021deep, and boosting luo2016high. It seems likely that many popular learners will fall under the umbrella of suitable learners as theoretical results are further developed. However, we note that currently known asymptotic properties do not cover a wide range of learners, such as very deep and wide neural networks and deep random forests, as they are currently implemented in practice.

The relative robustness of DDML to the first-step learners leads to the question of which machine learner is the most appropriate for a given application. It is ex ante rarely obvious which learner will perform best. Further, rather than restricting ourselves to one learner, we might want to combine several learners into one final learner. This is the idea behind stacking generalization, or simply “stacking”, due to Wolpert1992 and Breiman1996a. Stacking allows one to accommodate a diverse set of base learners with varying tuning and hyper-tuning parameters. It thus provide a convenient framework for combining and identifying suitable learners, thereby reducing the risk of misspecification. Ahrens2023_applied introduce short-stacking which reduces the computational cost of pairing DDML and stacking drastically, as well as pooled stacking which enforces common weights across cross-fitting folds.

We discuss stacking approaches to DDML estimation in Section (ref). Section (ref) demonstrates the performance of DDML in combination with stacking approaches using a simulation.

DDML and stacking

Our discussion of stacking in the context of DDML focuses on the Partially Linear Model in (ref), but we highlight that DDML and stacking can be combined in the same way for all other models supported in \stcmd{ddml}\xspace. Suppose we consider $J$ machine learners, referred to as base learners, to estimate the CEFs $\ell_0(\bm{X}) \equiv E[Y|\bm{X}]$ and $m_0(\bm{X}) \equiv E[D|\bm{X}]$. The set of base learners could, for example, include cross-validated lasso and ridge with alternative sets of predictors, gradient boosted trees with varying tree depth and feed-forward neural nets with varying number of hidden layers and neurons. Generally, we recommend considering a relatively large and diverse set of base learners, and including some learners with alternative tuning parameters.

We randomly split the sample into $K$ cross-fitting folds, denoted as $I_1,\ldots,I_K$. In each cross-fitting step $k$, we define the training sample as $I_k^c\equiv T_k$, comprising all observations excluding the cross-fitting hold-out fold $k$. This training sample is further divided into $V$ cross-validation folds, denoted as $T_{k,1},\ldots,T_{k,V}$. The stacking regressor fits a final learner to the training sample $T_k$ using the cross-validated predicted values of each base learner as inputs. A typical choice for the final learner is constrained least squares (CLS) which restricts the weights to be positive and sum to one. The stacking objective function for estimating $\ell_0(\bm{X})$ using the training sample $T_k$ is then defined as:

align[align omitted — 245 chars of source]

where $w_{k,j}$ are referred to as stacking weights. We use $\hat{\ell}^{(j)}_{T_{k,v(i)}^c}(\bm{X}_i)$ to denote the cross-validated predicted value for observation $i$, which is obtained from fitting learner $j$ on the sub-sample $T_{k,v(i)}^c \equiv T_k \setminus T_{k,v(i)}$, i.e., the sub-sample excluding the fold $v(i)$ into which observation $i$ falls. The stacking predicted values are obtained as $\sum_j \hat{w}_{k,j} \hat{\ell}_k^{(j)}(\bm{X}_i)$ where each learner $j$ is fit on the step-$k$ training sample $T_k$. The objective function for estimating $m_0(\bm{X})$ is defined accordingly.

CLS frequently performs well in practice and facilitates the interpretation of stacking as a weighted average of base learners Hastie2009. It is, however, not the only sensible choice of combining base learners. For example, stacking could instead select the single learner with the lowest quadratic loss, i.e., by imposing the constraint $w_{k,j}\in\{0,1\}$ and $\sum_{k,j}w_{k,j}=1$. We refer to this choice as “single best” and include it in our simulation experiments. We implement stacking for DDML using \stcmd{pystacked}\xspace Ahrens2022.

\paragraph{Pooled stacking.} A variant of stacking specific to DDML is pooled stacking. Standard stacking fits the final learner $K$ separate times, once in each cross-fitting step, yielding $K$ separate sets of stacking weights $\hat{w}_{k,j}$ for the $J$ learners. With DDML pooled stacking, we can impose the additional constraint in (ref) that the weights are the same across all cross-fit folds, $\hat{w}_{k,j}=\hat{w}_{j}, \forall \: k$. By returning a single set of stacking weights, pooled stacking imposes an additional degree of regularization and facilitates interpretation but suffers from the same high computational cost as pairing DDML with (regular) stacking.

\paragraph{Short-stacking.} Stacking and pooled stacking rely on cross-validation. In the context of DDML we can also exploit the cross{\it -fitted} predicted values directly for stacking. That is, we can directly apply CLS to the cross-fitted predicted values for estimating $\ell_0(\bm{X})$ (and similarly $m_0(\bm{X})$): \[ \underset{w_1,\ldots,w_J}{\min} \sum_{i=1}^n \left( Y_i - \sum_{j=1}^J w_j\hat{\ell}^{(j)}_{I_{k(i)}^c}(\bm{X}_i) \right)^2, \qquad \textrm{s.t.}\ w_j\geq 0,\ \sum_{j=1}^J |w_j|=1\] We refer to this form of stacking that utilizes the cross-fitted predicted values as short-stacking as it takes a short-cut. This is to contrast it with regular stacking which estimates the stacking weights for each cross-fitting fold $k$. The main advantage of short-stacking relative to standard stacking is the lower computational cost, because short-stacking does not require the fitting of the $j$ learners on sub-samples to obtain the cross-validated predicted values $\hat{\ell}^{(j)}_{T_{k,v(i)}^c}(\bm{X}_i)$ needed for standard stacking. Furthermore, short-stacking (like pooled stacking) also produces a single set of weights for the entire sample, which facilitates interpretation and implies a higher degree of regularization. A potential disadvantage of short-stacking is that it is more susceptible to over-fitting issues since stacking weights and structural parameters are estimated using the same cross-fitted predicted values. We thus recommend only considering short-stacking in regular settings where the number of candidate learners is small relative to $N$ Ahrens2023_applied. Algorithm A.4 in the Appendix summarizes the short-stacking algorithm for the Partially Linear Model.\footnote{While short-stacking can be applied in a similar fashion to other conditional expectations, a complication arises in the Flexible Partially Linear IV Model where the cross-fitted predicted values of $E[D|\bm{X}]$ depend on $E[D|\bm{X},\bm{Z}]$. We describe the algorithm that accounts for this in the Appendix; see Algorithm A.5.}

Monte Carlo simulation

To illustrate the advantages of DDML with stacking, we generate artificial data based on the Partially Linear Model

align[align omitted — 164 chars of source]

where both $\varepsilon_i$ and $u_i$ are independently drawn from the standard normal distribution. We set the target parameter to $\theta_0=0.5$ and the sample size to either $n=100$ or $n=1000$. The controls $\bm{X}_i$ are drawn from the multivariate normal distribution with $N(0,\bm{\Sigma})$ where $\Sigma_{ij}=(0.5)^{|i-j|}$. The number of controls is set to $p=\dim(\bm{X}_i)=50$, except in DGP 5 where $p=7$. The constants $c_Y$ and $c_D$ are chosen such that the $R^2$ in (ref) and (ref) are approximately equal to 0.5. To induce heteroskedasticity, we set \[\sigma_D\left(\bm{X}_i\right)=\sqrt{\frac{\left(1+g(\bm{X}_i)\right)^2}{\frac{1}{n}\sum_i\left(1+g(\bm{X}_i)\right)^2}} \quad\text { and } \quad \sigma_Y\left(D_i, \bm{X}_i\right)=\sqrt{\frac{\left(1+\theta_0 D_i+g(\bm{X}_i)\right)^2}{\frac{1}{n}\sum_i\left(1+\theta_0 D_i+g(\bm{X}_i)\right)^2}}\] The nuisance function $g(\bm{X}_i)$ is generated using five exemplary DGPs, which cover linear and nonlinear processes with varying degrees of sparsity and varying number of observed covariates: \[

array[array omitted — 717 chars of source]

\] DGP 1 is a linear design involving many negligibly small parameters. While not exactly sparse, the design can be approximated well through a sparse representation. DGP 2 is linear in the parameters and exactly sparse, but includes interactions and second-order polynomials. DGPs 3-5 are also exactly sparse but involve complex nonlinear and interaction effects. DGP 4 and 5 are identical, except that DGP 5 does not add nuisance covariates that are unrelated to $Y$ and $D$.

We consider DDML with the following supervised machine learners for cross-fitting the CEFs:\footnote{All base learners have been implemented using \stcmd{pystacked}\xspace. We use the defaults of \stcmd{pystacked}\xspace for parameter values and settings not mentioned here.}

enumerate[noitemsep,topsep=0pt,itemsep=0pt,parsep=0pt,before=,after=] • Cross-validated lasso & ridge with untransformed base controls • Cross-validated lasso & ridge with 5th-order polynomials of base controls but no interactions (referred to as `Poly 5') • Cross-validated lasso & ridge with second-order polynomials and all first order interaction terms (referred to as `Poly 2 + Inter.') • Random forests (RF) with low regularization: base controls, maximum tree depth of 10, 500 trees and approximately $\sqrt{p}$ features considered at each split • RF with medium regularization: same as 7., but with maximum tree depth of 6 • RF with high regularization: same as 7., but with maximum tree depth of 2 • Gradient boosted trees (GB) with low regularization: base controls, 1000 trees and a learning rate of 0.3. We enable early stopping which uses a 20% validation sample to decide whether to stop the learning algorithm. Learning is terminated after 5 iterations with no meaningful improvement in the mean-squared loss of the validation sample.\footnote{We use a tolerance level of 0.01 to measure improvements.} • GB with medium regularization: same as 10., but with learning rate of 0.1 • GB with high regularization: same as 10., but with learning rate of 0.01 • Feed-forward neural net with base controls and two layers of size 20

We use the above set of learners as base learners for DDML with stacking approaches. Specifically, we estimate DDML using stacking, short-stacking and pooled stacking which we combine with CLS and the single-best learner. We set the number of folds to $K=20$ if $n=100$, and $K=5$ if $n=1000$. That is, we adapt the number of folds $K$ to the total sample size $n$ to ensure that the CEF estimators are trained on sufficiently large training samples.

For comparison, we report results for OLS and PDS-lasso with base controls, PDS-Lasso with Poly 5, PDS-Lasso with Poly 2 + Interactions, and an oracle estimator using the full sample.\footnote{The PDS-Lasso estimators set tuning parameters using the default in \stcmd{pdslasso}\xspace.} The oracle estimator presumes knowledge of the function $g(\bm{X})$, and obtains estimates by regressing $Y$ on the two variables $D$ and $g(\bm{X})$.

We report simulation median absolute bias (MAB) and coverage rates of 95% confidence intervals (CR) for DGPs 1-3 in Table (ref). We delegate results for DGPs 4 and 5, including a brief discussion, to Appendix B. DDML estimators leveraging stacking approaches perform favorably in comparison to individual base learners in terms of bias and coverage. The relative performance of stacking approaches seems to improve as the sample size increases, likely reflecting that the stacking weights are more precisely estimated in larger small samples. For $n=1000$, the bias of stacking with CLS is at least as low as the bias of the best-performing individual learner under DGP 1-2, while only gradient boosting and neural net yield a lower bias than stacking under DGP 3.

Results for coverage are similar with stacking-based estimates being comparable with the best performing feasible estimates and the oracle when $n = 1000.$ With $n = 100$, coverage of confidence intervals for stacking-based estimators are inferior to coverages for a small number of the individual learners but still competitive and superior than most learners. Looking across all results, we see that stacking provides robustness to potentially very bad performance that could be obtained from using a single poorly performing learner.

There are overall little performance differences among the six stacking estimators considered., suggesting that short-stacking has a substantial practical advantage due to its lower computational cost. Ahrens2023_applied report that short-stacking reduces the compute time by a factor of $1/V$ where $V$ is the number of cross-validation folds. There is some evidence that the single-best selector outperforms CLS in very small sample sizes in DGPs 2-3, but not in DGP 1 (and also not in DGPs 4-5, see Table B.1). We suspect that the single-best selector works better in scenarios where there is one base learner that clearly dominates.

The mean-squared prediction errors (MSPE) and the average stacking weights, which we report in Tables B.2 and B.3 in the Appendix, provide further insights into how stacking with CLS functions. CLS assigns large stacking weights to base learners with a low MSPE, which in turn are associated with a low bias. Importantly, stacking assigns zero or close-to-zero weights to poorly specified base learners such as the highly regularized random forest, which in all three DGPs ranks among the individual learners with highest MSPE and highest bias. The robustness to misspecified and ill-chosen machine learners, which could lead to misleading inference, is indeed one of our main motivations for advocating stacking approaches to DDML.

DDML with stacking approaches also compares favorably to conventional full-sample estimators. In the relatively simple linear DGP 1, DDML with stacking performs similarly to OLS and the infeasible oracle estimator---both in terms of bias and coverage---for $n=100$ and $n=1000$. In the more challenging DGPs 2 and 3, the bias of DDML with stacking is substantially lower than the biases of OLS and the PDS-Lasso estimators. While the bias and size distortions of DDML with stacking are still considerable in comparison to the infeasible oracle for $n=100$, they are close to the oracle for $n=1000$. The results overall highlight the flexibility of DDML with stacking to flexibly approximate a wide range of DGPs provided a diverse set of base learners is chosen.

sidewaystable\scriptsize\singlespacing \begin{threeparttable} \caption{Bias and Coverage Rates in the Linear and Nonlinear DGPs} \begin{tabular}{rlcccccccccccccc} \toprule \midrule & & \multicolumn{4}{c}{DGP 1} & & \multicolumn{4}{c}{DGP 2} & & \multicolumn{4}{c}{DGP 3} \\ \cline{3-6} \cline{8-11} \cline{13-16} \\[-6pt] & & \multicolumn{2}{c}{$n=100$} &\multicolumn{2}{c}{$n=1000$} && \multicolumn{2}{c}{$n=100$} &\multicolumn{2}{c}{$n=1000$} && \multicolumn{2}{c}{$n=100$} &\multicolumn{2}{c}{$n=1000$} \\ && MAB & CR & MAB & CR && MAB & CR & MAB & CR && MAB & CR & MAB & CR \\ \midrule \multicolumn{2}{l}{Full sample:}\\ \IfFileExists{Simul/sim_SJ/bias_1.tex}{ \openin\pin@file Simul/sim_SJ/bias_1.tex \setcounter{pinlineno}{1} \@whilenum\value{pinlineno}<1 \do{ \read\pin@file to\pin@line \stepcounter{pinlineno} } \addtocounter{pinlineno}{-1} \let\empty \begingroup \endlinechar\newlinechar \@whilenum\value{pinlineno}<5 \do{ \readline\pin@file to\pin@line \edef{\pin@line} \stepcounter{pinlineno} } \closein\pin@file \expandafter\endgroup \scantokens\expandafter }{ \errmessage{File `Simul/sim_SJ/bias_1.tex' doesn't exist!} } \multicolumn{2}{l}{DDML methods:}\\ \multicolumn{2}{l}{\it Base learners}\\ \IfFileExists{Simul/sim_SJ/bias_1.tex}{ \openin\pin@file Simul/sim_SJ/bias_1.tex \setcounter{pinlineno}{1} \@whilenum\value{pinlineno}<6 \do{ \read\pin@file to\pin@line \stepcounter{pinlineno} } \addtocounter{pinlineno}{-1} \let\empty \begingroup \endlinechar\newlinechar \@whilenum\value{pinlineno}<19 \do{ \readline\pin@file to\pin@line \edef{\pin@line} \stepcounter{pinlineno} } \closein\pin@file \expandafter\endgroup \scantokens\expandafter }{ \errmessage{File `Simul/sim_SJ/bias_1.tex' doesn't exist!} } \multicolumn{2}{l}{\it Meta learners}\\ \IfFileExists{Simul/sim_SJ/bias_1.tex}{ \openin\pin@file Simul/sim_SJ/bias_1.tex \setcounter{pinlineno}{1} \@whilenum\value{pinlineno}<20 \do{ \read\pin@file to\pin@line \stepcounter{pinlineno} } \addtocounter{pinlineno}{-1} \let\empty \begingroup \endlinechar\newlinechar \@whilenum\value{pinlineno}<25 \do{ \readline\pin@file to\pin@line \edef{\pin@line} \stepcounter{pinlineno} } \closein\pin@file \expandafter\endgroup \scantokens\expandafter }{ \errmessage{File `Simul/sim_SJ/bias_1.tex' doesn't exist!} } \midrule \bottomrule \end{tabular} \begin{tablenotes}[para,flushleft] \scriptsize • Notes: The table reports median absolute bias (MAB) and coverage rate of a 95% confidence interval (CR). We employ standard errors robust to heteroskedasticity. For comparison, we report the following full sample estimators: infeasible Oracle, OLS, PDS-Lasso with base and two different expanded sets of covariates. DDML estimators use 20 folds for cross-fitting if $n=100$, and 5 folds if $n=1000$. Meta-learning approaches rely on all listed base learners. Results are based on 1,000 replications. Results for DGPs 4 and 5 can be found in Table B.1 in the Appendix. \end{tablenotes} \end{threeparttable}

The program

In this section, we provide an overview of the \stcmd{ddml}\xspace package. We introduce the syntax and workflow for the main programs in Section (ref). Section (ref) lists the options. Section (ref) covers the simplified one-line program \stcmd{qddml}\xspace. We provide an overview of supported machine learning programs in Section (ref). Finally, Section (ref) adds a note on how to ensure replication with \stcmd{ddml}\xspace.

Syntax: \stcmd{ddml}\xspace

The ddml estimation proceeds in four steps.

Step 1: Initialize \stcmd{ddml}\xspace and select model.

stsyntaxddml init {\it model} \optional{, mname({\it name}) vars({\it varlist}) kfolds({\it integer}) fcluster({\it varname}) foldvar({\it varlist}) reps({\it integer}) tabfold vars({\it varlist}) }

where {\it model} selects between the Partially Linear Model (partial), the Interactive Model (interactive), the Partially Linear IV Model (iv), the Flexible Partially Linear IV Model (fiv), and the Interactive IV Model (interactiveiv). This step creates a persistent Mata object with the name provided by mname(\emph{name}\texttt{)} in which model specifications and estimation results will be stored. The default name is {\it m0}.

At this stage, the user-specified folds for cross-fitting can be set via integer-valued Stata variables (see {\tt foldvar({\it varlist})}). By default, observations are randomly assigned to folds and {\tt kfolds({\it integer})} determines the number of folds (the default is 5). Cluster-randomized fold splitting is supported (see {\tt fcluster({\it varname})}). The user can also select the number of times to fully repeat the cross-fitting procedure (see {\tt rep({\it integer})}).

Step 2: Add supervised machine learners for estimating conditional expectations.

In this second step, we select the machine learning programs for estimating CEFs.

stsyntaxddml {\it cond_exp} \optional{, mname({\it name}) vname({\it varname}) learner({\it name}) vtype({\it string}) predopt({\it string}) } : {\it command} {\it depvar} {\it vars} \optional{, {\it cmdopt} }

where {\it cond_exp} selects the conditional expectation to be estimated by the machine learning program command. At least one learner is required for each conditional expectation. Table (ref) provides an overview of which conditional expectations are required by each model. The program command is a supervised machine learning program such as cvlasso or \stcmd{pystacked}\xspace (see compatible programs in Section (ref)). The options {\it cmdopt} are specific to that program.

table[table omitted — 747 chars of source]

Step 3: Perform cross-fitting.

This step implements the cross-fitting algorithm. Each learner is fit iteratively on training folds and out-of-sample predicted values are obtained. Cross-fitting is the most time-consuming step, as it involves fitting the selected machine learners repeatedly.

stsyntaxddml crossfit \optional{, mname({\it name}) shortstack poolstack nostdstack finalest({\it name}) }

Step 4: Estimate causal effects.

In the last step, we estimate the parameter of interest for all combination of learners added in Step 2.

stsyntaxddml estimate \optional{, mname({\it name}) robust cluster({\it varname}) vce({\it vcetype}) atet trim shortstack poolstack finalest({\it name}) }

To report and post selected results, we can use {\tt ddml estimate} with the {\tt replay} option:

stsyntaxddml estimate \optional{, replay mname({\it name}) spec({\it integer or string}) rep({\it integer or string}) fulltable notable allest }

Utilities

{\tt ddml describe} provides information about the model setup and or results:

stsyntaxddml describe \optional{, mname({\it name}) sample learners crossfit estimates all }

{\tt ddml} stores many internal results on associate arrays, notably the various stacking weights. These can be retrieved using {\tt ddml extract:}

stsyntaxddml extract \optional{{\it objectname} , mname({\it name}) show({\it name}) ename({\it name}) vname({\it varname}) stata keys key1({\it string}) key2({\it string}) key3({\it string}) subkey1({\it string}) subkey2({\it string}) }

{\tt ddml export} saves the estimated conditional expectations and other variables to a CSV file:

stsyntaxddml export {\it filename} \optional{, mname({\it name}) addvars({\it varlist}) }

Options

\paragraph{Step 1 options: Initialization.}

description• name of the DDML model. Allows to run multiple DDML models simultaneously. Defaults to {\it m0}. • number of cross-fitting folds. The default is 5. • cluster identifiers for cluster randomization of folds. • integer variables to specify custom folds (one per cross-fitting repetition). • number of cross-fitting repetitions, i.e., how often the cross-fitting procedure is repeated on randomly generated folds. • prints a table with frequency of observations by fold.

\paragraph{Step 2 options: Adding learners.}

description• name of the dependent variable in the reduced form estimation. This is usually inferred from the {\it command} line but is mandatory for the {\tt fiv} model. • optional name of the variable to be created. • optional variable type of the variable to be created. Defaults to {\it double}. {\it none} can be used to leave the type field blank. (Setting \texttt{vtype(}\emph{{\it none}}\texttt{)} is required when using \stcmd{ddml}\xspace with {\tt rforest}.) • {\tt predict} option to be used to get predicted values. Typical values could be {\tt xb} or {\tt pr}. Default is blank.

\paragraph{Step 3 options: Cross-fitting.}

description• asks for short-stacking to be used. Short-stacking uses the cross-fitted predicted values to obtain a weighted average of several base learners. • asks for pooled stacking to be used. This is available only if {\tt pystacked} has been used for standard stacking in all equations. • is used in conjunction with {\tt pystacked} and short-stacking; it tells {\tt pystacked} to generate the base learner predictions without the computationally-expensive additional step of obtaining the stacking weights. • sets the final estimator for all stacking methods; the default {\it nnls1} is least squares without a constant and with the constraints that weights are non-negative and sum to one. Alternative final estimators include {\it singlebest} (use the minimum MSE base learner), {\it ols} (ordinary least squares) and {\it avg} (unweighted average of all base learners).

\paragraph{Step 4 options: Estimation.}

description• select specification. This can either be the specification number, {\it mse} for minimum-MSE specification (the default) or {\it ss} for short-stacking. • select cross-fitting repetitions. This can either be the cross-fit repetition number, {\it mn} for mean aggregation or {\it md} for median aggregation (the default). See Remark 2 for more information. • report SEs that are robust to the presence of arbitrary heteroskedasticity. • select cluster-robust variance-covariance estimator. • select variance-covariance estimator, e.g.\ \texttt{vce(hc3)} or \texttt{vce(cluster id)}. See \verb+help regress##vcetype+ for available options. • trimming of propensity scores for the Interactive and Interactive IV models. The default is 0.01 (that is, values below 0.01 and above 0.99 are set to 0.01 and 0.99, respectively). • report average treatment effect of the treated (default is ATE). • suppress constant term in the estimation stage (only relevant for partially linear models). • see above under {\tt ddml crossfit}.

Re-fitting the final learner using {\tt ddml estimate} with stacking options is generally very fast because it does not require re-cross-fitting.

Short syntax: \stcmd{qddml}\xspace

The \stcmd{ddml}\xspace package includes the wrapper program \stcmd{qddml}\xspace which provides a one-line syntax for estimating a \stcmd{ddml}\xspace model. The one-line syntax follows the syntax of pdslasso and ivlasso Ahrens2018. The main restriction of \stcmd{qddml}\xspace compared to the more flexible multi-line syntax is that \stcmd{qddml}\xspace only allows for one user-specified machine learner (in addition to regress, which is added by default).

\stcmd{qddml}\xspace has integrated support for \stcmd{pystacked}\xspace, and \stcmd{pystacked}\xspace, is the default learner in all equations. The syntax for \stcmd{qddml}\xspace options differs depending on whether \stcmd{pystacked}\xspace is used as the learner in each equation.

Syntax when used with \stcmd{pystacked}\xspace

stsyntaxqddml \depvar\ {\it treatment_vars} ({\it controls}) ({\it treatment_vars}=excluded_instruments) \optional{ pystacked(string) pystacked_y(string) pystacked_d(string) pystacked_z(string) shortstack stdstack poolstack finalest(name) mname(name) {\it options} }

The \stcmd{pystacked}\xspace option sets the options for all the conditional expectations estimated by \stcmd{pystacked}\xspace; the _y, _d and _z variants control the options sent to the corresponding conditional expectation estimations. Other options are as in \stcmd{ddml}\xspace.

Syntax when used with other learners

stsyntaxqddml \depvar\ {\it treatment_vars} ({\it controls}) ({\it treatment_vars}=excluded_instruments) \optional{ cmd(string) ycmd(string) dcmd(string) zcmd(string) *cmdopt(string) *vtype(name) *predopt(name) {\it options} }

The cmd option sets the options for all the conditional expectations estimated by \stcmd{pystacked}\xspace; the ycmd, dcmd and zcmd variants control the options sent to the corresponding conditional expectation estimations. The cmdopt option can be used either to set the options for all equations, or, by replacing the asterisk with y, d or z, set the options for the corresponding conditional expectation estimation. Other options are as in \stcmd{ddml}\xspace.

Supported machine learning programs

\stcmd{ddml}\xspace is compatible with any supervised ML program in Stata that supports the typical “reg\,\,y\,\,x” syntax, comes with a post-estimation \stcmd{predict} and supports \stcmd{if} statements. We have tested \stcmd{ddml}\xspace with the following programs:

itemize[nosep] • \stcmd{pystacked}\xspace facilitates the stacking of a wide range of machine learners including regularized regression, random forests, support vector machines, gradient boosted trees and feed-forward neural nets using Python's scikit-learn Ahrens2022,scikit-learn,sklearn_api. In addition, \stcmd{pystacked}\xspace can also be used as a front-end to fit individual machine learners. \stcmd{ddml}\xspace has integrated support for \stcmd{pystacked}\xspace and is the recommended default learner. • lassopack implements regularized regression, e.g.\ lasso, ridge, elastic net Ahrens2019. • rforest is a random forest wrapper for WEKA Schonlau2020,frank2009weka. • svmachines allows for the estimation of support vector machines using libsvm chang2011libsvm,Guenther2018. • The program parsnip of the package \texttt{mlrtime} provides access to R's \emph{parsnip} machine learning library through \texttt{rcall} Huntington2021,haghish2019seamless. Using \texttt{parsnip} requires the installation of the supplementary wrapper program \texttt{parsnip2}.\footnote{Available from {\tt\url{https://github.com/aahrens1/parsnip2}.}}

Stata programs that are currently not supported can be added relatively easily using wrapper programs (see parsnip2 for an example).

Inspecting results and replication

In this section we discuss how to ensure replicability when using \stcmd{ddml}\xspace. We also discuss some tools available for tracing replication failures. First, however, we briefly describe how \stcmd{ddml}\xspace stores results.

\stcmd{ddml}\xspace stores estimated conditional expectations in Stata's memory as Stata variables. These variables can be inspected, graphed and summarized in the usual way. Fold ID variables are also stored as Stata variables (by default named \verb+m0_fid_+r, where \verb+m0+ is the default model name and r is the cross-fitting repetition). \stcmd{ddml}\xspace models are stored on Mata structs and using Mata's associative arrays. Specifically, the \stcmd{ddml}\xspace model created by \stcmd{ddml}\xspace init is an mStruct, and information relating to the estimation of conditional expectations are stored in eStructs. Results relating to the overall model estimation are stored in associative arrays that live in the \texttt{mStruct}, and results relating to the estimation of conditional expectations are stored in associative arrays that live in the corresponding \texttt{eStruct}s.

Replication tips:

itemize• Set the Stata seed before {\tt ddml init}. This ensures that the same random fold variable is used for a given data set. • Using the same fold variable alone is usually not sufficient to ensure replication, since many machine learning algorithms involve randomization. That said, note that the fold variable is stored in memory and can be reused for subsequent estimations via the foldvar(varlist) option. • Replication of \stcmd{ddml}\xspace results may require additional steps with some programs that rely on randomization in other software environments, e.g., R or Python. \stcmd{pystacked}\xspace uses a Python seed generated in Stata. Thus, when \stcmd{ddml}\xspace is used with \stcmd{pystacked}\xspace, setting the seed before {\tt ddml init} also guarantees that the same Python seed underlies the stacking estimation. Other programs relying on randomization outside of Stata might not behave in the same way. Thus, when using other programs, check the help files for options to set external random seeds. Try estimating each of the individual learners on the entire sample to see what settings need to be passed to them for their results to replicate. • Beware of changing samples. Fold splits or learner idiosyncracies may mean that sample sizes vary slightly across learners, estimation samples and/or cross-fitting repetitions. • The \stcmd{ddml}\xspace export utility can be used to export the estimated conditional expectations, fold variables and sample indicators to a CSV format file for examination and comparison in other software environments.

Applications

We demonstrate the ddml workflow using two applications. In Section (ref), we apply the DDML estimator to estimate the effect of 401(k) eligibility on financial wealth following poterba1995. We focus on the Partially Linear Model for the sake of brevity, but provide code that demonstrates the use of \stcmd{ddml}\xspace with the Interactive Model, Partially Linear IV Model and Interactive IV Model using the same application in Appendix C. Additional examples can also be found in the help file. Based on berry1995automobile, we show in Section (ref) how to employ \stcmd{ddml}\xspace for the estimation of the Flexible Partially Linear IV Model which allows both for flexibly controlling for confounding factors using high-dimensional function approximation of confounding factors and for estimation of optimal instrumental variables.

401(k) and financial wealth

The data consists of $n=9915$ households from the 1991 SIPP. The application is originally due to poterba1995, but has been revisited by Belloni2017, Chernozhukov2018, and wuthrich2021, among others. Following previous studies, we include the control variables age, income, years of education, family size, as well as indicators for martial status, two-earner status, benefit pension status, IRA participation, and home ownership. The outcome is net financial assets and the treatment is eligibility to enroll for the 401(k) pension plan.

We load the data and define three globals for outcome, treatment and control variables. We then proceed in the four steps outlined in Section (ref).

stlog. use "sipp1991.dta", clear . global Y net_tfa . global X age inc educ fsize marr twoearn db pira hown . global D e401

Step 1: Initialize ddml model.

We initialize the \stcmd{ddml}\xspace model and select the Partially Linear Model in (ref). Before initialization, we set the seed to ensure replication. This should always be done before ddml init, which executes the random fold assignment. In this example, we opt for four folds to ensure the readability of some of the output shown below, although we recommend considering a larger number of folds in practice.

stlog. set seed 123 . ddml init partial, kfolds(4)

Step 2: Add supervised machine learners for estimating conditional expectations.

In this step, we specify which machine learning programs should be used for the estimation of the conditional expectations $E[Y\vert \bm{X}]$ and $E[D\vert \bm{X}]$. For each conditional expectation, at least one learner is required. For illustrative purposes, we consider regress for linear regression, pystacked with the m(lassocv) option for cross-validated lasso (as an example of how to use pystacked to estimate a single learner) and rforest for random forests. When using rforest, we need to add the option \texttt{vtype(none)} since the post-estimation \texttt{predict} command of \texttt{rforest} does not support variable types.

stlog. *** add learners for E[Y|X] . ddml E[Y|X]: reg $Y $X Learner Y1_reg added successfully. . ddml E[Y|X]: pystacked $Y c.($X)\#\#c.($X), type(reg) m(lassocv) Learner Y2_pystacked added successfully. {\smallskip} . ddml E[Y|X], vtype(none): rforest $Y $X, type(reg) Learner Y3_rforest added successfully. {\smallskip} . *** add learners for E[D|X] . ddml E[D|X]: reg $D $X Learner D1_reg added successfully. {\smallskip} . ddml E[D|X]: pystacked $D c.($X)\#\#c.($X), type(reg) m(lassocv) Learner D2_pystacked added successfully. . ddml E[D|X], vtype(none): rforest $D $X, type(reg) Learner D3_rforest added successfully.

The flexible \stcmd{ddml}\xspace syntax allows specification of different sets of covariates for different learners. This flexibility can be useful as, for example, linear learners such as the lasso might perform better if interactions are provided as inputs, whereas tree-based methods such as random forests may detect certain interactions in a data-driven way. Here, we use interactions and second-order polynomials for the cross-validated lasso, but not for the other learners.

This application has only one treatment variable, but \stcmd{ddml}\xspace does support multiple treatment variables. To add a second treatment variable, we would simply add a statement such as ddml E[D|X]: reg D2 \$X where D2 would be the name of the second treatment variable. An example with two treatments is provided in the help file.

The auxiliary ddml sub-command describe allows us to verify that the learners were correctly registered:

stlog. ddml describe Model: partial, crossfit folds k=4, resamples r=1 Mata global (mname): m0 Dependent variable (Y): net_tfa net_tfa learners: Y1_reg Y2_pystacked Y3_rforest D equations (1): e401 e401 learners: D1_reg D2_pystacked D3_rforest

Step 3: Perform cross-fitting.

The third step performs cross-fitting, which is the most time-intensive process. The shortstack option enables the short-stacking algorithm of Section (ref).

stlog. ddml crossfit, shortstack Cross-fitting E[y|X] equation: net_tfa Cross-fitting fold 1 2 3 4 ...completed cross-fitting...completed short-stacking Cross-fitting E[D|X] equation: e401 Cross-fitting fold 1 2 3 4 ...completed cross-fitting...completed short-stacking

Six variables are created and stored in memory which correspond to the six learners specified in the previous step. These variables are called \verb+Y1_reg_1+, \verb+Y2_pystacked_1_1+, \verb+Y3_rforest_1+, \verb+D1_reg_1+, \verb+D2_pystacked_1_1+ and \verb+D3_rforest_1+. Y and D indicate outcome and treatment variable. The index 1 to 3 is a learner counter. reg, pystacked and \texttt{rforest} correspond to the names of the commands used. The \verb+_1+ suffix indicates the cross-fitting repetition. The additional \verb+_1+ in the case of \verb+D2_pystacked_1_1+ indicates the learner number (there is only a single \texttt{pystacked} learner).

After cross-fitting, we can inspect the mean-squared prediction errors by fold and learner:

stlog. ddml desc, crossfit Model: partial, crossfit folds k=4, resamples r=1 Mata global (mname): m0 Dependent variable (Y): net_tfa net_tfa learners: Y1_reg Y2_pystacked Y3_rforest D equations (1): e401 e401 learners: D1_reg D2_pystacked D3_rforest Crossfit results (detail): All By fold: Cond. exp. Learner rep MSE 1 2 3 4 net_tfa shortstack 1 1.5e+09 1.8e+09 1.4e+09 1.4e+09 1.5e+09 e401 shortstack 1 0.18 0.17 0.17 0.18 0.18

Step 4: Estimate causal effects.

In this final step, we obtain the causal effect estimates. Since we requested short-stacking in Step 3, ddml shows the short-stacking result which relies on the cross-fitted values of each base learner. In addition, the specification that corresponds to the minimum-MSE learners is listed at the beginning of the output.

stlog. ddml estimate, robust Model: partial, crossfit folds k=4, resamples r=1 Mata global (mname): m0 Dependent variable (Y): net_tfa net_tfa learners: Y1_reg Y2_pystacked Y3_rforest D equations (1): e401 e401 learners: D1_reg D2_pystacked D3_rforest DDML estimation results: spec r Y learner D learner b SE mse 1 Y2_pystacked D2_pystacked 9788.291 (1339.797) ss 1 [shortstack] [ss] 9748.788 (1331.364) mse = minimum MSE specification for that resample. Shortstack DDML model y-E[y|X] = y-Y_net_tfa_ss_1 Number of obs = 9915 D-E[D|X] = D-D_e401_ss_1 \HLI{13}{\TOPT}\HLI{64} {\VBAR} Robust net_tfa {\VBAR} Coefficient std. err. z P>|z| [95% conf. interval] \HLI{13}{\PLUS}\HLI{64} e401 {\VBAR} 9748.788 1331.364 7.32 0.000 7139.362 12358.21 _cons {\VBAR} 92.48199 534.8262 0.17 0.863 -955.7581 1140.722 \HLI{13}{\BOTT}\HLI{64} Stacking final estimator: nnls1

Since we have specified three learners per conditional expectation, there are in total 9 specifications relying on the base learners (since we can combine \verb+Y1_reg_1+, \verb+Y2_pystacked_1+ and \verb+Y3_rforest_1+ with \verb+D1_reg_1+, \verb+D2_pystacked_1+ and \verb+D3_rforest_1+). To get all results, we add the allcombos option:

stlog. ddml estimate, robust allcombos Model: partial, crossfit folds k=4, resamples r=1 Mata global (mname): m0 Dependent variable (Y): net_tfa net_tfa learners: Y1_reg Y2_pystacked Y3_rforest D equations (1): e401 e401 learners: D1_reg D2_pystacked D3_rforest DDML estimation results: spec r Y learner D learner b SE 1 1 Y1_reg D1_reg 5986.657 (1523.694) 2 1 Y1_reg D2_pystacked 9563.875 (1389.172) 3 1 Y1_reg D3_rforest 8659.141 (1258.982) 4 1 Y2_pystacked D1_reg 9175.519 (1371.065) * 5 1 Y2_pystacked D2_pystacked 9788.291 (1339.797) 6 1 Y2_pystacked D3_rforest 8497.214 (1200.268) 7 1 Y3_rforest D1_reg 9044.071 (1485.073) 8 1 Y3_rforest D2_pystacked 10103.630 (1426.134) 9 1 Y3_rforest D3_rforest 9528.449 (1293.485) ss 1 [shortstack] [ss] 9748.788 (1331.364) * = minimum MSE specification for that resample. Shortstack DDML model y-E[y|X] = y-Y_net_tfa_ss_1 Number of obs = 9915 D-E[D|X] = D-D_e401_ss_1 \HLI{13}{\TOPT}\HLI{64} {\VBAR} Robust net_tfa {\VBAR} Coefficient std. err. z P>|z| [95% conf. interval] \HLI{13}{\PLUS}\HLI{64} e401 {\VBAR} 9748.788 1331.364 7.32 0.000 7139.362 12358.21 _cons {\VBAR} 92.48199 534.8262 0.17 0.863 -955.7581 1140.722 \HLI{13}{\BOTT}\HLI{64} Stacking final estimator: nnls1

We can use the spec(string) option to select among the listed specifications. {\it string} is either the specification number, ss, st or ps, to get the short-stacking, standard stacking or pooled stacking specification, respectively, or \texttt{mse} for the specification corresponding to the minimal MSPE. In the example above, \texttt{spec(1)} reports in full the specification using \texttt{regress} for estimating both $E[Y\vert \bm{X}]$ and $E[D\vert \bm{X}]$. The \texttt{spec(}\emph{string}\texttt{)} option can be provided either in combination with \texttt{allcombos}, or after estimation in combination with the {\tt replay} option, for example:

stlog. ddml estimate, spec(1) replay Model: partial, crossfit folds k=4, resamples r=1 Mata global (mname): m0 Dependent variable (Y): net_tfa net_tfa learners: Y1_reg Y2_pystacked Y3_rforest D equations (1): e401 e401 learners: D1_reg D2_pystacked D3_rforest DDML estimation results: spec r Y learner D learner b SE 1 1 Y2_pystacked D2_pystacked 5986.657 (1523.694) ss 1 [shortstack] [ss] 9748.788 (1331.364) mse = minimum MSE specification for that resample. DDML model, specification 1 y-E[y|X] = y-Y1_reg_1 Number of obs = 9915 D-E[D|X] = D-D1_reg_1 \HLI{13}{\TOPT}\HLI{64} {\VBAR} Robust net_tfa {\VBAR} Coefficient std. err. z P>|z| [95% conf. interval] \HLI{13}{\PLUS}\HLI{64} e401 {\VBAR} 5986.657 1523.694 3.93 0.000 3000.271 8973.042 _cons {\VBAR} 10.74705 561.2911 0.02 0.985 -1089.363 1110.857 \HLI{13}{\BOTT}\HLI{64}

Manual final estimation.

In the background, ddml estimate regresses \verb+Y1_reg_1+ against \verb+D1_reg_1+ with a constant. We can verify this manually:

stlog. gen double Y1_resid = $Y - Y1_reg {\smallskip} . gen double D1_resid = $D - D1_reg . reg Y1_resid D1_resid, robust Linear regression Number of obs = 9,915 F(1, 9913) = 15.44 Prob > F = 0.0001 R-squared = 0.0023 Root MSE = 55891 \HLI{13}{\TOPT}\HLI{64} {\VBAR} Robust Y1_resid {\VBAR} Coefficient std. err. t P>|t| [95% conf. interval] \HLI{13}{\PLUS}\HLI{64} D1_resid {\VBAR} 5986.657 1523.694 3.93 0.000 2999.906 8973.407 _cons {\VBAR} 10.74705 561.2911 0.02 0.985 -1089.498 1110.992 \HLI{13}{\BOTT}\HLI{64}

Manual estimation using { regress} allows the use of { regress}'s postestimation tools.

Stacking

We next demonstrate DDML with stacking. To this end, we exploit the stacking regressor implemented in \stcmd{pystacked}\xspace. \stcmd{pystacked}\xspace allows combining multiple base learners with learner-specific settings and covariates into a final meta learner. The learners are separated by \verb+||+. method(name) selects the learner, xvars(varlist) specifies learner-specific covariates (overwritting the default covariates \verb+$X+) and \texttt{opt(}\emph{string}\texttt{)} passes options to the learners. In this example, we use OLS, cross-validated lasso and ridge, random forests and gradient boosting. We furthermore use parallelization with 5 cores. A detailed explanation of the \stcmd{pystacked}\xspace syntax can be found in Ahrens2022.

stlog. *** add learners for E[Y|X] . ddml E[Y|X]: pystacked $Y $X || /// > method(ols) || /// > m(lassocv) xvars(c.($X)\#\#c.($X)) || /// > m(ridgecv) xvars(c.($X)\#\#c.($X)) || /// > m(rf) pipe(sparse) opt(max_features(5)) || /// > m(gradboost) pipe(sparse) opt(n_estimators(250) learning_rate(0.01)) , /// > njobs(5) Learner Y1_pystacked added successfully. . *** add learners for E[D|X] . ddml E[D|X]: pystacked $D $X || /// > method(ols) || /// > m(lassocv) xvars(c.($X)\#\#c.($X)) || /// > m(ridgecv) xvars(c.($X)\#\#c.($X)) || /// > m(rf) pipe(sparse) opt(max_features(5)) || /// > m(gradboost) pipe(sparse) opt(n_estimators(250) learning_rate(0.01)) , /// > njobs(5) Learner D1_pystacked added successfully.

After cross-fitting, we retrieve the cross-fitted MSPE using ddml extract with show(mse) or examine the stacking weights using stweights:

stlog. qui ddml crossfit . ddml extract, show(stweights) mean stacking weights across folds/resamples for D1_pystacked (e401) final stacking estimator: nnls1 learner mean_weight rep_1 ols 1 .01557419 .01557419 lassocv 2 .10077907 .10077907 ridgecv 3 .43674242 .43674242 rf 4 .02946916 .02946916 gradboost 5 .41743516 .41743516 mean stacking weights across folds/resamples for Y1_pystacked (net_tfa) final stacking estimator: nnls1 learner mean_weight rep_1 ols 1 .09662631 .09662631 lassocv 2 .46475744 .46475744 ridgecv 3 .32388159 .32388159 rf 4 .09392877 .09392877 gradboost 5 .0145518 .0145518

Finally, in the estimation stage, we retrieve the results of DDML with stacking:

stlog. ddml estimate, robust Model: partial, crossfit folds k=4, resamples r=1 Mata global (mname): m0 Dependent variable (Y): net_tfa net_tfa learners: Y1_pystacked D equations (1): e401 e401 learners: D1_pystacked DDML estimation results: spec r Y learner D learner b SE st 1 Y1_pystacked D1_pystacked 9406.385 (1300.170) Stacking DDML model y-E[y|X] = y-Y1_pystacked_1 Number of obs = 9915 D-E[D|X] = D-D1_pystacked_1 \HLI{13}{\TOPT}\HLI{64} {\VBAR} Robust net_tfa {\VBAR} Coefficient std. err. z P>|z| [95% conf. interval] \HLI{13}{\PLUS}\HLI{64} e401 {\VBAR} 9406.385 1300.17 7.23 0.000 6858.099 11954.67 _cons {\VBAR} 199.9921 535.7477 0.37 0.709 -850.0541 1250.038 \HLI{13}{\BOTT}\HLI{64} Stacking final estimator: nnls1

The DDML-specific stacking approaches of short-stacking and pooled stacking can be requested at either the cross-fitting or estimation steps. Re-fitting the final learner at the estimation step allows us to avoid repeating the computationally-intensive cross-fitting. Here, we request short-stacking and pooled stacking but using the single-best base learner.

stlog. ddml estimate, robust shortstack poolstack finalest(singlebest) Model: partial, crossfit folds k=4, resamples r=1 Mata global (mname): m0 Dependent variable (Y): net_tfa net_tfa learners: Y1_pystacked D equations (1): e401 e401 learners: D1_pystacked DDML estimation results: spec r Y learner D learner b SE st 1 Y1_pystacked D1_pystacked 9406.385 (1300.170) ss 1 [shortstack] [ss] 9847.256 (1339.419) ps 1 [poolstack] [ps] 9788.291 (1339.797) Shortstack DDML model y-E[y|X] = y-Y_net_tfa_ss_1 Number of obs = 9915 D-E[D|X] = D-D_e401_ss_1 \HLI{13}{\TOPT}\HLI{64} {\VBAR} Robust net_tfa {\VBAR} Coefficient std. err. z P>|z| [95% conf. interval] \HLI{13}{\PLUS}\HLI{64} e401 {\VBAR} 9847.256 1339.419 7.35 0.000 7222.044 12472.47 _cons {\VBAR} 78.69447 534.6452 0.15 0.883 -969.1909 1126.58 \HLI{13}{\BOTT}\HLI{64} Stacking final estimator: singlebest

Short-stacking is computationally substantially faster than regular stacking (or pooled stacking) because it avoids the cross-validation within cross-fit folds. Below, we disable regular stacking with the nostdstack option in the cross-fitting stage. In this example, where we use parallelization with 5 cores, the run time is only 50.7 seconds DDML with short-stakcing compared to 93.0 seconds for DDML with regular stacking.

stlog. qui ddml crossfit, shortstack nostdstack . ddml estimate, robust Model: partial, crossfit folds k=4, resamples r=1 Mata global (mname): m0 Dependent variable (Y): net_tfa net_tfa learners: Y1_pystacked D equations (1): e401 e401 learners: D1_pystacked DDML estimation results: spec r Y learner D learner b SE ss 1 [shortstack] [ss] 9602.257 (1300.825) Shortstack DDML model y-E[y|X] = y-Y_net_tfa_ss_1 Number of obs = 9915 D-E[D|X] = D-D_e401_ss_1 \HLI{13}{\TOPT}\HLI{64} {\VBAR} Robust net_tfa {\VBAR} Coefficient std. err. z P>|z| [95% conf. interval] \HLI{13}{\PLUS}\HLI{64} e401 {\VBAR} 9602.257 1300.825 7.38 0.000 7052.686 12151.83 _cons {\VBAR} 83.96648 533.9871 0.16 0.875 -962.6289 1130.562 \HLI{13}{\BOTT}\HLI{64} Stacking final estimator:

One-line syntax.

\stcmd{qddml}\xspace provides a simplified and convenient one-line syntax. The main constraint of \stcmd{qddml}\xspace is that it only allows for one user-specified learner. The default learner is pystacked, which by default uses OLS, cross-validated lasso and gradient boosting as default learners. The pystacked base learners as well as non-pystacked commands can be be modified via various options. Below we use qddml with pystacked's default base learners. We omit the output for the sake of brevity.

stlog. qui qddml $Y $D (c.($X)\#\#c.($X)), model(partial) kfolds(4) robust

The market for automobiles

For this demonstration, we follow chernozhukov2015 who estimate a stylized demand model using instrumental variables based on the data from berry1995automobile. The authors of the original study estimate the effect of prices on the market share of automobile models in a given year ($n=2217$). The controls are product characteristics (a constant, air conditioning dummy, horsepower divided by weight, miles per dollar, vehicle size). To account for endogenous prices, berry1995automobile suggest exploiting other products' characteristics as instruments. Following chernozhukov2015, we define the baseline set of instruments as the sum over all other products' characteristics, calculated separately for own-firm and other-firm products, which yields 10 baseline instruments. chernozhukov2015 also construct an augmented set of instruments, including first-order interactions, squared and cubic terms. In the analysis below, we extend chernozhukov2015 by applying DDML with stacking and a diverse set of learners including OLS, lasso, ridge, random forest and gradient boosted trees. We use the augmented set of controls for all base learners and OLS, which we include for reference.

We load and prepare the data:

stlog. use BLP_CHS.dta, clear . global Y y . global D price . global Xbase hpwt air mpd space . global Xaug augX* . global Zbase Zbase* . global Zaug Zaug*

\paragraph{Step 1: Initialize ddml model.}

stlog. set seed 123 . ddml init fiv, kfolds(4) reps(5)

Note that in the ddml init step, we include the option reps(5) which will result in running the full cross-fitting procedure five times, each with a different random split of the data. Replicating the procedure multiple times allows us to gauge the impact of randomness due to the random splitting of the data into subsamples.

\paragraph{Step 2: Add supervised machine learners for estimating conditional expectations.}

Estimation of a fiv model requires us to add learners for $E[Y|\bm{X}]$, $E[D|\bm{X},\bm{Z}]$ and $E[D|\bm{X}]$. Compared to the other models supported by \stcmd{ddml}\xspace, there is one complication that arises because, in order to estimate $E[D|\bm{X}]$, we exploit fitted values of $E[D|\bm{X},\bm{Z}]$ to impose LIE-compliance. Since these fitted values have not yet been generated, we use the placeholder \verb+{D}+ that in the cross-fitting stage will be internally replaced with estimates of $E[D|\bm{X},\bm{Z}]$. We use the learner(string) option to match one learner for $E[D|\bm{X}]$ with a learner for $E[D|\bm{X},\bm{Z}]$, and vname(varname\texttt{)} to indicate the name of the treatment variable.

stlog. *** add learners for E[Y|X] . ddml E[Y|X], learner(Ypystacked): pystacked $Y $Xaug || /// > method(ols) xvars($Xbase) || /// > m(lassocv) || /// > m(ridgecv) || /// > m(rf) opt(n_estimators(200) max_features(None)) || /// > m(rf) opt(n_estimators(200) max_features(10)) || /// > m(rf) opt(n_estimators(200) max_features(5)) || /// > m(gradboost) opt(n_estimators(800) learning_rate(0.01)) || /// > m(gradboost) opt(n_estimators(800) learning_rate(0.1)) || /// > m(gradboost) opt(n_estimators(800) learning_rate(0.3)) , /// > njobs(4) Learner Ypystacked added successfully. {\smallskip} . ddml E[D|X,Z], learner(Dpystacked): pystacked $D $Xaug $Zaug || /// > method(ols) xvars($Xbase $Zbase) || /// > m(lassocv) || /// > m(ridgecv) || /// > m(rf) opt(n_estimators(200) max_features(None)) || /// > m(rf) opt(n_estimators(200) max_features(10)) || /// > m(rf) opt(n_estimators(200) max_features(5)) || /// > m(gradboost) opt(n_estimators(800) learning_rate(0.01)) || /// > m(gradboost) opt(n_estimators(800) learning_rate(0.1)) || /// > m(gradboost) opt(n_estimators(800) learning_rate(0.3)) , /// > njobs(4) Learner Dpystacked added successfully. . ddml E[D|X], mname(m0) learner(Dpystacked) vname($D): /// > pystacked {\lbr}D{\rbr} $Xaug || /// > method(ols) xvars($Xaug) || /// > m(lassocv) || /// > m(ridgecv) || /// > m(rf) opt(n_estimators(200) max_features(None)) || /// > m(rf) opt(n_estimators(200) max_features(10)) || /// > m(rf) opt(n_estimators(200) max_features(5)) || /// > m(gradboost) opt(n_estimators(800) learning_rate(0.01)) || /// > m(gradboost) opt(n_estimators(800) learning_rate(0.1)) || /// > m(gradboost) opt(n_estimators(800) learning_rate(0.3)) , /// > njobs(4) Learner Dpystacked_h added successfully.

\paragraph{Steps 3-4: Perform cross-fitting (output omitted) and estimate causal effects.}

stlog. qui ddml crossfit . ddml estimate, robust Model: fiv, crossfit folds k=4, resamples r=5 Mata global (mname): m0 Dependent variable (Y): y y learners: Ypystacked D equations (1): price price learners: Dpystacked DDML estimation results: spec r Y learner D learner b SE DH learner st 1 Ypystacked Dpystacked -0.108 (0.010) Dpystacked_h st 2 Ypystacked Dpystacked -0.123 (0.010) Dpystacked_h st 3 Ypystacked Dpystacked -0.110 (0.011) Dpystacked_h st 4 Ypystacked Dpystacked -0.127 (0.011) Dpystacked_h st 5 Ypystacked Dpystacked -0.132 (0.012) Dpystacked_h Mean/med Y learner D learner b SE DH learner st mn Ypystacked Dpystacked -0.120 (0.014) Dpystacked_h st md Ypystacked Dpystacked -0.123 (0.015) Dpystacked_h Median over 5 stacking resamples y-E[y|X] = y-Ypystacked Number of obs = 2217 E[D|X,Z] = D-Dpystacked E[D{\caret}|X] = Dpystacked_h Orthogonalized D = D - E[D{\caret}|X]; optimal IV = E[D|X,Z] - E[D{\caret}|X]. \HLI{13}{\TOPT}\HLI{64} {\VBAR} Robust y {\VBAR} Coefficient std. err. z P>|z| [95% conf. interval] \HLI{13}{\PLUS}\HLI{64} price {\VBAR} -.1230112 .0152603 -8.06 0.000 -.1529209 -.0931016 \HLI{13}{\BOTT}\HLI{64} Stacking final estimator: nnls1 Summary over 5 resamples: D eqn mean min p25 p50 p75 max price -0.1199 -0.1318 -0.1271 -0.1230 -0.1101 -0.1075

\paragraph{Manual final estimation.} We can obtain the final estimate manually. To this end, we construct the instrument as $\hat{E}[D|X,Z]-\hat{E}[D|X]$ and the residualized endogenous regressor as $D-\hat{E}[D|X]$. The residualized dependent variable is saved in memory. Here we obtain the estimate from the first cross-fitting replication. We could obtain the estimate for replication $r$ by changing the “_1” to “_$r$”.

stlog. gen double Y1_resid = $Y - Ypystacked_1 {\smallskip} . gen double dtilde = $D - Dpystacked_h_1 . gen double optiv = Dpystacked_1 - Dpystacked_h_1 . ivreg Y1_resid (dtilde=optiv), robust Instrumental variables 2SLS regression Number of obs = 2,217 F(1, 2215) = 110.49 Prob > F = 0.0000 R-squared = 0.0888 Root MSE = .965 \HLI{13}{\TOPT}\HLI{64} {\VBAR} Robust Y1_resid {\VBAR} Coefficient std. err. t P>|t| [95% conf. interval] \HLI{13}{\PLUS}\HLI{64} dtilde {\VBAR} -.1075272 .0102295 -10.51 0.000 -.1275875 -.0874668 _cons {\VBAR} .0064207 .0205008 0.31 0.754 -.0337821 .0466235 \HLI{13}{\BOTT}\HLI{64} Instrumented: dtilde Instruments: optiv

Conclusion

This article introduces the Stata program ddml for Double/Debiased Machine Learning. The program allows for flexible estimation of structural parameters in five econometric models, leveraging a wide range of supervised machine learners. While ddml is compatible with many existing machine learning programs in Stata, it is specifically designed to be used with pystacked, which allows combining several learners into a meta learner via stacking. We see several avenues for extensions: First, ddml primarily focuses on cross-sectional models. Some panel models are readily implementable in ddml, but expanding its capabilities for seamless use with a wide range of panel models would increase its practical relevance. Second, researchers and policymakers are frequently interested in learning treatment effects for specific sub-populations sharing observable characteristics. The estimation of conditional average treatment effects would be a natural extension to the existing ddml program. Third, \texttt{ddml} currently lacks under-identification diagnostics and weak-identification robust inference for instrumental variable regressions, which we hope to add in future releases.

Acknowledgments

We thank users who tested earlier versions of the program. We also thank Jan Ditzen, Ben Jann, Eroll Kuhn, Di Liu and Moritz Marbach for helpful comments, as well as participants at Stata User Conferences in Germany (2021), Italy (2022), Switzerland (2022) and the UK (2023). All remaining errors are our own.

aboutauthorsAchim Ahrens is Post-Doctoral Researcher and Senior Data Scientist at the Public Policy Group and Immigration Policy Lab, ETH Z\"urich. Christian B. Hansen is the Wallace W. Booth Professor of Econometrics and Statistics at the University of Chicago Booth School of Business. Mark E. Schaffer is Professor of Economics in Edinburgh Business School at Heriot-Watt University, Edinburgh, UK, and a Research Fellow at the Institute for the Study of Labour (IZA), Bonn. Thomas Wiemann is an economics PhD student at the University of Chicago.