| Title: | Double/Debiased Machine Learning |
|---|---|
| Description: | Estimate common causal parameters using double/debiased machine learning as proposed by Chernozhukov et al. (2018) <doi:10.1111/ectj.12097>. 'ddml' simplifies estimation based on (short-)stacking as discussed in Ahrens et al. (2024) <doi:10.1002/jae.3103>, which leverages multiple base learners to increase robustness to the underlying data generating process. |
| Authors: | Achim Ahrens [aut], Christian B Hansen [aut], Mark E Schaffer [aut], Thomas Wiemann [aut, cre] |
| Maintainer: | Thomas Wiemann <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.9.0 |
| Built: | 2026-05-16 09:05:05 UTC |
| Source: | https://github.com/thomaswiemann/ddml |
Random subsample from the data of Angrist & Evans (1998).
AE98AE98
A data frame with 5,000 rows and 13 variables.
Indicator equal to 1 if the mother is employed.
Number of weeks of employment.
Hours worked per week.
Indicator equal to 1 if the mother has more than 2 kids.
Indicator equal to 1 if the first two children are of the same sex.
Age in years.
Age in years at birth of the first child.
Indicator equal to 1 if the mother is black.
Indicator equal to 1 if the mother is Hispanic.
Indicator equal to 1 if the mother is neither black nor Hispanic.
Years of education.
Indicator equal to 1 if the first child is male.
Indicator equal to 1 if the second child is male.
Angrist J, Evans W (1998). "Children and Their Parents' Labor Supply: Evidence from Exogenous Variation in Family Size." American Economic Review, 88(3), 450-477.
Returns a named list of single-ensemble ddml
objects. Each element retains all S3 methods
(summary, tidy, glance,
confint, vcov).
## S3 method for class 'ddml' as.list(x, ...)## S3 method for class 'ddml' as.list(x, ...)
x |
An object inheriting from class |
... |
Currently unused. |
A named list of length nfit.
y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace")] fit = ddml_plm(y, D, X, learners = list( list(what = ols), list(what = mdl_glmnet)), ensemble_type = c("nnls", "singlebest"), sample_folds = 2, silent = TRUE) as.list(fit)y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace")] fit = ddml_plm(y, D, X, learners = list( list(what = ols), list(what = mdl_glmnet)), ensemble_type = c("nnls", "singlebest"), sample_folds = 2, silent = TRUE) as.list(fit)
Returns a named list of single-ensemble
ddml_rep objects.
## S3 method for class 'ddml_rep' as.list(x, ...)## S3 method for class 'ddml_rep' as.list(x, ...)
x |
A |
... |
Currently unused. |
A named list of ddml_rep objects.
y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace")] reps = ddml_replicate(ddml_plm, y = y, D = D, X = X, learners = list(what = ols), resamples = 3, sample_folds = 2, silent = TRUE) as.list(reps)y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace")] reps = ddml_replicate(ddml_plm, y = y, D = D, X = X, learners = list(what = ols), resamples = 3, sample_folds = 2, silent = TRUE) as.list(reps)
Returns a named list of single-fit ral objects,
one per column of coefficients. This is the
primary mechanism for passing multi-ensemble results
to modelsummary.
## S3 method for class 'ral' as.list(x, ...)## S3 method for class 'ral' as.list(x, ...)
x |
An object inheriting from class |
... |
Currently unused. |
A named list of ral objects, each with
nfit = 1.
Returns a named list of single-fit ral_rep
objects. Each element aggregates across resamples
for a single ensemble type.
## S3 method for class 'ral_rep' as.list(x, ...)## S3 method for class 'ral_rep' as.list(x, ...)
x |
An object inheriting from class |
... |
Currently unused. |
A named list of ral_rep objects.
Extract Coefficients from a RAL Object
## S3 method for class 'ral' coef(object, ...)## S3 method for class 'ral' coef(object, ...)
object |
An object inheriting from class |
... |
Currently unused. |
Named vector (single fit) or matrix (multiple fits).
Extract Aggregated Coefficients
## S3 method for class 'ral_rep' coef(object, aggregation = c("median", "mean", "spectral"), ...)## S3 method for class 'ral_rep' coef(object, aggregation = c("median", "mean", "spectral"), ...)
object |
An object inheriting from class
|
aggregation |
Character string: |
... |
Currently unused. |
Named vector (single fit) or matrix (multiple).
Computes confidence intervals for one or more parameters.
## S3 method for class 'ral' confint( object, parm = NULL, level = 0.95, fit_idx = 1, type = "HC1", uniform = FALSE, bootstraps = 999L, ... )## S3 method for class 'ral' confint( object, parm = NULL, level = 0.95, fit_idx = 1, type = "HC1", uniform = FALSE, bootstraps = 999L, ... )
object |
An object inheriting from class |
parm |
A specification of which parameters are to be given confidence intervals, either a vector of numbers or a vector of names. If missing, all parameters are considered. |
level |
Confidence level. Default 0.95. |
fit_idx |
Integer index of the fit. Defaults to 1. |
type |
Character. HC type. Default |
uniform |
Logical. If |
bootstraps |
Integer number of bootstrap draws.
Only used when |
... |
Currently unused. |
A matrix with columns for lower and upper bounds.
When uniform = TRUE, the attribute
"crit_val" contains the critical value.
Chernozhukov V, Chetverikov D, Kato K (2013). "Gaussian approximations and multiplier bootstrap for maxima of sums of high-dimensional random vectors." Annals of Statistics, 41(6), 2786-2819.
Confidence Intervals for RAL Rep Objects
## S3 method for class 'ral_rep' confint( object, parm = NULL, level = 0.95, fit_idx = 1, aggregation = c("median", "mean", "spectral"), type = "HC1", uniform = FALSE, bootstraps = 999L, ... )## S3 method for class 'ral_rep' confint( object, parm = NULL, level = 0.95, fit_idx = 1, aggregation = c("median", "mean", "spectral"), type = "HC1", uniform = FALSE, bootstraps = 999L, ... )
object |
An object inheriting from class
|
parm |
Parameter specification (names or indices). |
level |
Confidence level. Default 0.95. |
fit_idx |
Integer index of the fit. Defaults to 1. |
aggregation |
Character string. Aggregation rule. |
type |
Character. HC type. Default |
uniform |
Logical. Uniform bands via multiplier
bootstrap? Default |
bootstraps |
Integer. Bootstrap draws. Default 999. |
... |
Currently unused. |
A matrix with columns for lower and upper bounds.
When uniform = TRUE, the attribute
"crit_val" contains the aggregated critical
value.
Chernozhukov V, Chetverikov D, Kato K (2013). "Gaussian approximations and multiplier bootstrap for maxima of sums of high-dimensional random vectors." Annals of Statistics, 41(6), 2786-2819.
Cross-fitted predictions using stacking.
crosspred( y, X, learners, sample_folds = 10, ensemble_type = "average", cv_folds = 10, custom_ensemble_weights = NULL, cluster_variable = seq_along(y), subsamples = NULL, cv_subsamples = NULL, cv_subsamples_list = NULL, silent = FALSE, auxiliary_X = NULL, parallel = NULL )crosspred( y, X, learners, sample_folds = 10, ensemble_type = "average", cv_folds = 10, custom_ensemble_weights = NULL, cluster_variable = seq_along(y), subsamples = NULL, cv_subsamples = NULL, cv_subsamples_list = NULL, silent = FALSE, auxiliary_X = NULL, parallel = NULL )
y |
The outcome variable. |
X |
A (sparse) matrix of predictive variables. |
learners |
Omission of the |
sample_folds |
Number of cross-fitting folds. |
ensemble_type |
Ensemble method to combine base learners into final estimate of the conditional expectation functions. Possible values are:
Multiple ensemble types may be passed as a vector of strings. |
cv_folds |
Number of folds used for cross-validation. |
custom_ensemble_weights |
A numerical matrix with
user-specified ensemble weights. Each column corresponds to a
custom ensemble specification, each row corresponds to a base
learner in |
cluster_variable |
A vector of cluster indices. |
subsamples |
List of vectors with sample indices for cross-fitting. |
cv_subsamples |
List of lists, each corresponding to a subsample containing vectors with subsample indices for cross-validation. |
cv_subsamples_list |
Deprecated; use |
silent |
Boolean to silence estimation updates. |
auxiliary_X |
An optional list of matrices of length
|
parallel |
An optional named list with parallel processing
options. When
|
crosspred implements the cross-fitting step of the
Double/Debiased Machine Learning procedure combined with
stacking. It produces the cross-fitted nuisance estimates
used in the Neyman orthogonal scores of
all ddml_* estimators.
Let be an -fold partition of
, and denote the training set for fold
by
.
Given base learners, the procedure operates on each
cross-fitting fold in three steps:
Step 1 (Stacking weights).
Run -fold cross-validation on
(via crossval) to estimate the MSPE of each
base learner, and solve for fold-specific stacking weights
.
Step 2 (Fit).
Fit each base learner on the full training set
, yielding .
Step 3 (Predict).
For each , compute the ensemble cross-fitted
prediction
Since every observation belongs to exactly one fold, the result is
a complete -vector of out-of-sample predictions.
Crucially, both the stacking weights and the
base learner fits depend only on
, which does not contain observation
.
When a single learner is used (), no stacking or inner
cross-validation is performed: the learner is simply fitted on
and predictions are made for .
crosspred returns a list containing the following components:
cf_fittedA matrix of out-of-sample predictions, each column corresponding to an ensemble type (in chronological order).
weightsAn array, providing the weight assigned to each base learner (in chronological order) by the ensemble procedures.
mspeA numeric vector of per-learner out-of-sample MSPEs, computed from cross-fitted residuals.
r2A numeric vector of per-learner out-of-sample R-squared values.
cv_resid_byfoldA list (length sample_folds)
of inner cross-validation residual matrices used for ensemble
weight estimation. NULL when a single learner is used.
auxiliary_fittedWhen auxiliary_X is not
NULL, a list of matrices with additional predictions.
cf_fitted_bylearnerA matrix of out-of-sample predictions, each column corresponding to a base learner (in chronological order).
cf_resid_bylearnerA matrix of out-of-sample
residuals (y - cf_fitted_bylearner), each column
corresponding to a base learner.
auxiliary_fitted_bylearnerWhen auxiliary_X
is not NULL, a list of matrices with additional
predictions for each learner.
Ahrens A, Hansen C B, Schaffer M E, Wiemann T (2024). "Model Averaging and Double Machine Learning." Journal of Applied Econometrics, 40(3): 249-269.
Wolpert D H (1992). "Stacked generalization." Neural Networks, 5(2), 241-259.
Other utilities:
crossval(),
ddml(),
diagnostics(),
ensemble(),
ensemble_weights(),
shortstacking()
# Construct variables from the included Angrist & Evans (1998) data y = AE98[, "worked"] X = AE98[, c("morekids", "age","agefst","black","hisp","othrace","educ")] # Compute cross-predictions using stacking with base learners ols and lasso. # Two stacking approaches are simultaneously computed: Equally # weighted (ensemble_type = "average") and MSPE-minimizing with weights # in the unit simplex (ensemble_type = "nnls1"). Predictions for each # learner are also calculated. crosspred_res <- crosspred(y, X, learners = list(list(what = ols), list(what = mdl_glmnet)), ensemble_type = c("average", "nnls1", "singlebest"), sample_folds = 2, cv_folds = 2, silent = TRUE) dim(crosspred_res$cf_fitted) # = length(y) by length(ensemble_type) dim(crosspred_res$cf_fitted_bylearner) # = length(y) by length(learners)# Construct variables from the included Angrist & Evans (1998) data y = AE98[, "worked"] X = AE98[, c("morekids", "age","agefst","black","hisp","othrace","educ")] # Compute cross-predictions using stacking with base learners ols and lasso. # Two stacking approaches are simultaneously computed: Equally # weighted (ensemble_type = "average") and MSPE-minimizing with weights # in the unit simplex (ensemble_type = "nnls1"). Predictions for each # learner are also calculated. crosspred_res <- crosspred(y, X, learners = list(list(what = ols), list(what = mdl_glmnet)), ensemble_type = c("average", "nnls1", "singlebest"), sample_folds = 2, cv_folds = 2, silent = TRUE) dim(crosspred_res$cf_fitted) # = length(y) by length(ensemble_type) dim(crosspred_res$cf_fitted_bylearner) # = length(y) by length(learners)
Estimator of the mean squared prediction error of different learners using cross-validation.
crossval( y, X, learners, cv_folds = 10, cluster_variable = seq_along(y), cv_subsamples = NULL, silent = FALSE, parallel = NULL )crossval( y, X, learners, cv_folds = 10, cluster_variable = seq_along(y), cv_subsamples = NULL, silent = FALSE, parallel = NULL )
y |
The outcome variable. |
X |
A (sparse) matrix of predictive variables. |
learners |
Omission of the |
cv_folds |
Number of folds used for cross-validation. |
cluster_variable |
A vector of cluster indices. |
cv_subsamples |
List of vectors with sample indices for cross-validation. |
silent |
Boolean to silence estimation updates. |
parallel |
An optional named list with parallel processing
options. When
|
crossval estimates the mean squared prediction error
(MSPE) of base learners via -fold
cross-validation. It is the inner workhorse of the stacking
machinery used by ensemble_weights to determine
ensemble weights.
Given a generic conditional expectation function
(e.g., , ), let
be a -fold partition of
and let denote
learner trained on all observations outside fold
. The out-of-sample residual for observation
is
Since every observation belongs to exactly one fold, this yields a
complete residual matrix. The cross-validated
MSPE for learner is
and the cross-validated is
where is the sample variance of .
crossval returns a list containing the following components:
mspeA vector of MSPE estimates, each corresponding to a base learner (in chronological order).
r2A vector of cross-validated
values, each corresponding to a base learner (in
chronological order).
cv_residA matrix of out-of-sample residuals, each column corresponding to a base learner (in chronological order).
cv_subsamplesPass-through of
cv_subsamples. See above.
Other utilities:
crosspred(),
ddml(),
diagnostics(),
ensemble(),
ensemble_weights(),
shortstacking()
# Construct variables from the included Angrist & Evans (1998) data y = AE98[, "worked"] X = AE98[, c("morekids", "age","agefst","black","hisp","othrace","educ")] # Compare ols, lasso, and ridge using 4-fold cross-validation cv_res <- crossval(y, X, learners = list(list(what = ols), list(what = mdl_glmnet), list(what = mdl_glmnet, args = list(alpha = 0))), cv_folds = 4, silent = TRUE) cv_res$mspe# Construct variables from the included Angrist & Evans (1998) data y = AE98[, "worked"] X = AE98[, c("morekids", "age","agefst","black","hisp","othrace","educ")] # Compare ols, lasso, and ridge using 4-fold cross-validation cv_res <- crossval(y, X, learners = list(list(what = ols), list(what = mdl_glmnet), list(what = mdl_glmnet, args = list(alpha = 0))), cv_folds = 4, silent = TRUE) cv_res$mspe
ddml Object.Build a "ddml" object from user-supplied score
components. The resulting object inherits all S3 methods
available for ddml objects, including
summary.ddml, confint.ral,
vcov.ral, and tidy.ddml.
ddml( coefficients, scores, J, inf_func, nobs, coef_names, estimator_name, ensemble_type = colnames(coefficients), cluster_variable = seq_len(nobs), sample_folds = NULL, cv_folds = NULL, shortstack = FALSE, ensemble_weights = NULL, mspe = NULL, r2 = NULL, fitted = NULL, splits = NULL, call = match.call(), subclass = NULL, dinf_dtheta = NULL, ... )ddml( coefficients, scores, J, inf_func, nobs, coef_names, estimator_name, ensemble_type = colnames(coefficients), cluster_variable = seq_len(nobs), sample_folds = NULL, cv_folds = NULL, shortstack = FALSE, ensemble_weights = NULL, mspe = NULL, r2 = NULL, fitted = NULL, splits = NULL, call = match.call(), subclass = NULL, dinf_dtheta = NULL, ... )
coefficients |
A |
scores |
A 3D array of evaluated Neyman orthogonal scores
with dimensions |
J |
A 3D array of evaluated Jacobians with dimensions
|
inf_func |
A 3D array of evaluated influence functions
with dimensions |
nobs |
Number of observations. |
coef_names |
Character vector of coefficient names
(length |
estimator_name |
Character string identifying the estimator
(e.g., |
ensemble_type |
Character vector of ensemble types. Defaults
to |
cluster_variable |
A vector of cluster indices. Defaults to
|
sample_folds |
Number of cross-fitting folds used. Optional. |
cv_folds |
Number of cross-validation folds used. Optional. |
shortstack |
Logical indicating whether short-stacking was
used. Default |
ensemble_weights |
A named list of ensemble weight matrices. Optional. |
mspe |
A named list of per-learner MSPEs. Optional. |
r2 |
A named list of per-learner R-squared values. Optional. |
fitted |
A named list of per-equation cross-fitted prediction objects. Optional. |
splits |
A list of sample split objects. Optional. |
call |
The matched call. Defaults to |
subclass |
Optional character string for a subclass name. If
provided, the object will have class
|
dinf_dtheta |
An optional 4D array of dimensions |
... |
Additional named components to include in the object. |
An object of S3 class "ddml" (or
c(subclass, "ddml") if subclass is specified).
See ddml-intro for the output structure.
Other utilities:
crosspred(),
crossval(),
diagnostics(),
ensemble(),
ensemble_weights(),
shortstacking()
# A minimal example: construct a ddml object from pre-computed # score components for a simple mean estimator. n <- 100 y <- rnorm(n) theta <- mean(y) scores <- array(y - theta, dim = c(n, 1, 1)) J <- array(-1, dim = c(1, 1, 1)) psi_b <- list(matrix(y, ncol = 1)) psi_a <- list(array(-1, dim = c(n, 1, 1))) inf_func <- array(y - theta, dim = c(n, 1, 1)) dinf_dtheta <- array(1, dim = c(n, 1, 1, 1)) coef <- matrix(theta, 1, 1, dimnames = list("mean", "custom")) fit <- ddml(coefficients = coef, scores = scores, J = J, inf_func = inf_func, nobs = n, coef_names = "mean", dinf_dtheta = dinf_dtheta, estimator_name = "Sample Mean") summary(fit)# A minimal example: construct a ddml object from pre-computed # score components for a simple mean estimator. n <- 100 y <- rnorm(n) theta <- mean(y) scores <- array(y - theta, dim = c(n, 1, 1)) J <- array(-1, dim = c(1, 1, 1)) psi_b <- list(matrix(y, ncol = 1)) psi_a <- list(array(-1, dim = c(n, 1, 1))) inf_func <- array(y - theta, dim = c(n, 1, 1)) dinf_dtheta <- array(1, dim = c(n, 1, 1, 1)) coef <- matrix(theta, 1, 1, dimnames = list("mean", "custom")) fit <- ddml(coefficients = coef, scores = scores, J = J, inf_func = inf_func, nobs = n, coef_names = "mean", dinf_dtheta = dinf_dtheta, estimator_name = "Sample Mean") summary(fit)
Estimator for the average potential outcome, allowing for
custom weights .
ddml_apo( y, D, X, d = 1, weights = NULL, learners, learners_DX = learners, sample_folds = 10, ensemble_type = "nnls", shortstack = FALSE, cv_folds = 10, custom_ensemble_weights = NULL, custom_ensemble_weights_DX = custom_ensemble_weights, cluster_variable = seq_along(y), stratify = TRUE, trim = 0.01, silent = FALSE, parallel = NULL, fitted = NULL, splits = NULL, save_crossval = TRUE, ... )ddml_apo( y, D, X, d = 1, weights = NULL, learners, learners_DX = learners, sample_folds = 10, ensemble_type = "nnls", shortstack = FALSE, cv_folds = 10, custom_ensemble_weights = NULL, custom_ensemble_weights_DX = custom_ensemble_weights, cluster_variable = seq_along(y), stratify = TRUE, trim = 0.01, silent = FALSE, parallel = NULL, fitted = NULL, splits = NULL, save_crossval = TRUE, ... )
y |
The outcome variable. |
D |
The endogenous variable of interest. Can be discrete or continuous. |
X |
A (sparse) matrix of control variables. |
d |
The treatment level of interest. The default is |
weights |
A numeric vector of length |
learners |
May take one of two forms, depending on whether a
single learner or stacking with multiple learners is used for
estimation of the conditional expectation functions.
If a single learner is used,
If stacking with multiple learners is used,
Omission of the |
learners_DX |
Optional argument to allow for different estimators of
|
sample_folds |
Number of cross-fitting folds. |
ensemble_type |
Ensemble method to combine base learners into final estimate of the conditional expectation functions. Possible values are:
Multiple ensemble types may be passed as a vector of strings. |
shortstack |
Boolean to use short-stacking. |
cv_folds |
Number of folds used for cross-validation in ensemble construction. |
custom_ensemble_weights |
A numerical matrix with
user-specified ensemble weights. Each column corresponds to a
custom ensemble specification, each row corresponds to a base
learner in |
custom_ensemble_weights_DX |
Optional argument to allow for different
custom ensemble weights for |
cluster_variable |
A vector of cluster indices. |
stratify |
Boolean for stratified cross-fitting: if |
trim |
Number in (0, 1) for trimming the estimated propensity scores at
|
silent |
Boolean to silence estimation updates. |
parallel |
An optional named list with parallel processing
options. When
|
fitted |
An optional named list of per-equation cross-fitted
predictions, typically obtained from a previous fit via
|
splits |
An optional list of sample split objects. For
|
save_crossval |
Logical indicating whether to store the inner
cross-validation residuals used for ensemble weight
computation. Default |
... |
Additional arguments passed to internal methods. |
Parameter of Interest: ddml_apo provides a
Double/Debiased Machine Learning estimator for the average potential
outcome. Under conditional unconfoundedness and overlap, the parameter
is identified by the following reduced form conditional expectation:
where is the observed random vector and
is a known weighting function. If ,
this parameter corresponds to the average potential outcome
at treatment level .
Nuisance Parameters: The nuisance parameters are
taking true values and
.
Neyman Orthogonal Score / Moment Equation: The Neyman orthogonal score is:
Jacobian:
See ddml-intro for how the influence function
and inference are derived from these components.
ddml_apo returns an object of S3 class
ddml_apo and ddml. See ddml-intro
for the common output structure. Additional pass-through
fields: learners, learners_DX.
Other ddml estimators:
ddml-intro,
ddml_ate(),
ddml_attgt(),
ddml_fpliv(),
ddml_late(),
ddml_pliv(),
ddml_plm(),
ddml_policy()
# Construct variables from the included Angrist & Evans (1998) data y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace","educ")] # Estimate the APO for d = 1 using a single base learner, ridge. apo_fit <- ddml_apo(y, D, X, learners = list(what = mdl_glmnet), sample_folds = 2, silent = TRUE) summary(apo_fit)# Construct variables from the included Angrist & Evans (1998) data y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace","educ")] # Estimate the APO for d = 1 using a single base learner, ridge. apo_fit <- ddml_apo(y, D, X, learners = list(what = mdl_glmnet), sample_folds = 2, silent = TRUE) summary(apo_fit)
Estimator for the average treatment effect and the average treatment effect on the treated.
ddml_ate( y, D, X, learners, learners_DX = learners, sample_folds = 10, ensemble_type = "nnls", shortstack = FALSE, cv_folds = 10, custom_ensemble_weights = NULL, custom_ensemble_weights_DX = custom_ensemble_weights, cluster_variable = seq_along(y), stratify = TRUE, trim = 0.01, silent = FALSE, parallel = NULL, fitted = NULL, splits = NULL, save_crossval = TRUE, ... ) ddml_att( y, D, X, learners, learners_DX = learners, sample_folds = 10, ensemble_type = "nnls", shortstack = FALSE, cv_folds = 10, custom_ensemble_weights = NULL, custom_ensemble_weights_DX = custom_ensemble_weights, cluster_variable = seq_along(y), stratify = TRUE, trim = 0.01, silent = FALSE, parallel = NULL, fitted = NULL, splits = NULL, save_crossval = TRUE, ... )ddml_ate( y, D, X, learners, learners_DX = learners, sample_folds = 10, ensemble_type = "nnls", shortstack = FALSE, cv_folds = 10, custom_ensemble_weights = NULL, custom_ensemble_weights_DX = custom_ensemble_weights, cluster_variable = seq_along(y), stratify = TRUE, trim = 0.01, silent = FALSE, parallel = NULL, fitted = NULL, splits = NULL, save_crossval = TRUE, ... ) ddml_att( y, D, X, learners, learners_DX = learners, sample_folds = 10, ensemble_type = "nnls", shortstack = FALSE, cv_folds = 10, custom_ensemble_weights = NULL, custom_ensemble_weights_DX = custom_ensemble_weights, cluster_variable = seq_along(y), stratify = TRUE, trim = 0.01, silent = FALSE, parallel = NULL, fitted = NULL, splits = NULL, save_crossval = TRUE, ... )
y |
The outcome variable. |
D |
The binary endogenous variable of interest. |
X |
A (sparse) matrix of control variables. |
learners |
May take one of two forms, depending on whether a
single learner or stacking with multiple learners is used for
estimation of the conditional expectation functions.
If a single learner is used,
If stacking with multiple learners is used,
Omission of the |
learners_DX |
Optional argument to allow for different estimators of
|
sample_folds |
Number of cross-fitting folds. |
ensemble_type |
Ensemble method to combine base learners into final estimate of the conditional expectation functions. Possible values are:
Multiple ensemble types may be passed as a vector of strings. |
shortstack |
Boolean to use short-stacking. |
cv_folds |
Number of folds used for cross-validation in ensemble construction. |
custom_ensemble_weights |
A numerical matrix with
user-specified ensemble weights. Each column corresponds to a
custom ensemble specification, each row corresponds to a base
learner in |
custom_ensemble_weights_DX |
Optional argument to allow for different
custom ensemble weights for |
cluster_variable |
A vector of cluster indices. |
stratify |
Boolean for stratified cross-fitting: if |
trim |
Number in (0, 1) for trimming the estimated propensity scores at
|
silent |
Boolean to silence estimation updates. |
parallel |
An optional named list with parallel processing
options. When
|
fitted |
An optional named list of per-equation cross-fitted
predictions, typically obtained from a previous fit via
|
splits |
An optional list of sample split objects. For
|
save_crossval |
Logical indicating whether to store the inner
cross-validation residuals used for ensemble weight
computation. Default |
... |
Additional arguments passed to internal methods. |
Parameter of Interest: ddml_ate and ddml_att provide
Double/Debiased Machine Learning estimators for the average treatment
effect and the average treatment effect on the treated, respectively.
Under conditional unconfoundedness and overlap, the parameters
are identified by the following reduced form parameters:
and the average treatment effect on the treated (ATT) is defined as
where is the observed random vector.
Neyman Orthogonal Score: The Neyman orthogonal scores are:
where the nuisance parameters are taking true values
, , and .
Jacobian:
See ddml-intro for how the influence function
and inference are derived from these components.
ddml_ate and ddml_att return objects of S3
class ddml_ate/ddml_att and ddml. See
ddml-intro for the common output structure.
Additional pass-through fields: learners,
learners_DX.
Other ddml estimators:
ddml-intro,
ddml_apo(),
ddml_attgt(),
ddml_fpliv(),
ddml_late(),
ddml_pliv(),
ddml_plm(),
ddml_policy()
# Construct variables from the included Angrist & Evans (1998) data y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace","educ")] # Estimate the average treatment effect using a single base learner, ridge. ate_fit <- ddml_ate(y, D, X, learners = list(what = mdl_glmnet, args = list(alpha = 0)), sample_folds = 2, silent = TRUE) summary(ate_fit) # Estimate the average treatment effect using short-stacking with base # learners ols, lasso, and ridge. We can also use custom_ensemble_weights # to estimate the ATE using every individual base learner. weights_everylearner <- diag(1, 3) colnames(weights_everylearner) <- c("mdl:ols", "mdl:lasso", "mdl:ridge") ate_fit <- ddml_ate(y, D, X, learners = list(list(what = ols), list(what = mdl_glmnet), list(what = mdl_glmnet, args = list(alpha = 0))), ensemble_type = 'nnls', custom_ensemble_weights = weights_everylearner, shortstack = TRUE, sample_folds = 2, silent = TRUE) summary(ate_fit)# Construct variables from the included Angrist & Evans (1998) data y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace","educ")] # Estimate the average treatment effect using a single base learner, ridge. ate_fit <- ddml_ate(y, D, X, learners = list(what = mdl_glmnet, args = list(alpha = 0)), sample_folds = 2, silent = TRUE) summary(ate_fit) # Estimate the average treatment effect using short-stacking with base # learners ols, lasso, and ridge. We can also use custom_ensemble_weights # to estimate the ATE using every individual base learner. weights_everylearner <- diag(1, 3) colnames(weights_everylearner) <- c("mdl:ols", "mdl:lasso", "mdl:ridge") ate_fit <- ddml_ate(y, D, X, learners = list(list(what = ols), list(what = mdl_glmnet), list(what = mdl_glmnet, args = list(alpha = 0))), ensemble_type = 'nnls', custom_ensemble_weights = weights_everylearner, shortstack = TRUE, sample_folds = 2, silent = TRUE) summary(ate_fit)
Estimator for group-time average treatment effects on the treated (GT-ATT) in staggered Difference-in-Differences designs.
ddml_attgt( y, X = NULL, t, G, learners, learners_qX = learners, sample_folds = 10, ensemble_type = "nnls", shortstack = FALSE, cv_folds = 10, custom_ensemble_weights = NULL, custom_ensemble_weights_qX = custom_ensemble_weights, cluster_variable = seq_len(nrow(as.matrix(y))), trim = 0.01, control_group = c("notyettreated", "nevertreated"), anticipation = 0, silent = FALSE, parallel = NULL, fitted = NULL, splits = NULL, save_crossval = TRUE, ... )ddml_attgt( y, X = NULL, t, G, learners, learners_qX = learners, sample_folds = 10, ensemble_type = "nnls", shortstack = FALSE, cv_folds = 10, custom_ensemble_weights = NULL, custom_ensemble_weights_qX = custom_ensemble_weights, cluster_variable = seq_len(nrow(as.matrix(y))), trim = 0.01, control_group = c("notyettreated", "nevertreated"), anticipation = 0, silent = FALSE, parallel = NULL, fitted = NULL, splits = NULL, save_crossval = TRUE, ... )
y |
An |
X |
An |
t |
A numeric vector of length |
G |
A numeric vector of length |
learners |
May take one of two forms, depending on whether a
single learner or stacking with multiple learners is used for
estimation of the conditional expectation functions.
If a single learner is used,
If stacking with multiple learners is used,
Omission of the |
learners_qX |
Optional argument to allow for different
estimators of the cell-level propensity score
|
sample_folds |
Number of cross-fitting folds. |
ensemble_type |
Ensemble method to combine base learners into final estimate of the conditional expectation functions. Possible values are:
Multiple ensemble types may be passed as a vector of strings. |
shortstack |
Boolean to use short-stacking. |
cv_folds |
Number of folds used for cross-validation in ensemble construction. |
custom_ensemble_weights |
A numerical matrix with
user-specified ensemble weights. Each column corresponds to a
custom ensemble specification, each row corresponds to a base
learner in |
custom_ensemble_weights_qX |
Optional argument to allow for
different custom ensemble weights for |
cluster_variable |
A vector of cluster indices. |
trim |
Number in (0, 1) for trimming the estimated
propensity scores at |
control_group |
Character. |
anticipation |
Non-negative integer. Number of periods before treatment where anticipation effects may occur. Default 0. |
silent |
Boolean to silence estimation updates. |
parallel |
An optional named list with parallel processing
options. When
|
fitted |
An optional named list of per-equation cross-fitted
predictions, typically obtained from a previous fit via
|
splits |
An optional list of sample split objects, typically
obtained from a previous fit via |
save_crossval |
Logical indicating whether to store the inner
cross-validation residuals used for ensemble weight
computation. Default |
... |
Additional arguments passed to internal methods. |
Parameter of Interest: ddml_attgt provides a
Double/Debiased Machine Learning estimator for the group-time
average treatment effects on the treated (GT-ATT) in the
staggered adoption model. For each group and time
period , define the differenced outcome
where
is the universal base period. The GT-ATT is:
where is the
observed random vector.
Neyman Orthogonal Score: The Neyman orthogonal score is:
where the nuisance parameters are
taking true values
,
,
and .
Jacobian:
See ddml-intro for how the influence function
and inference are derived from these components.
ddml_attgt returns an object of S3 class
ddml_attgt and ddml. See ddml-intro
for the common output structure. Additional pass-through
fields: learners, learners_qX,
cell_info, control_group, anticipation.
Callaway B, Sant'Anna P H C (2021). "Difference-in-Differences with multiple time periods." Journal of Econometrics, 225(2), 200-230.
Chang N-C (2020). "Double/debiased machine learning for difference-in-differences models." Econometrics Journal, 23(2), 177-191.
Ahrens A, Chernozhukov V, Hansen C B, Kozbur D, Schaffer M E, Wiemann T (2026). "An Introduction to Double/Debiased Machine Learning." Journal of Economic Literature, forthcoming.
Other ddml estimators:
ddml-intro,
ddml_apo(),
ddml_ate(),
ddml_fpliv(),
ddml_late(),
ddml_pliv(),
ddml_plm(),
ddml_policy()
set.seed(42) n <- 200; T_ <- 4 X <- matrix(rnorm(n * 2), n, 2) G <- sample(c(3, 4, Inf), n, replace = TRUE, prob = c(0.3, 0.3, 0.4)) y <- matrix(rnorm(n * T_), n, T_) # Add treatment effect for treated units for (i in seq_len(n)) { if (is.finite(G[i])) { for (j in seq_len(T_)) { if (j >= G[i]) y[i, j] <- y[i, j] + 1 } } } fit <- ddml_attgt(y, X, t = 1:T_, G = G, learners = list(what = ols), sample_folds = 2, silent = TRUE) summary(fit)set.seed(42) n <- 200; T_ <- 4 X <- matrix(rnorm(n * 2), n, 2) G <- sample(c(3, 4, Inf), n, replace = TRUE, prob = c(0.3, 0.3, 0.4)) y <- matrix(rnorm(n * T_), n, T_) # Add treatment effect for treated units for (i in seq_len(n)) { if (is.finite(G[i])) { for (j in seq_len(T_)) { if (j >= G[i]) y[i, j] <- y[i, j] + 1 } } } fit <- ddml_attgt(y, X, t = 1:T_, G = G, learners = list(what = ols), sample_folds = 2, silent = TRUE) summary(fit)
Estimator for the flexible partially linear IV coefficient.
ddml_fpliv( y, D, Z, X, learners, learners_DXZ = learners, learners_DX = learners, sample_folds = 10, ensemble_type = "nnls", shortstack = FALSE, cv_folds = 10, custom_ensemble_weights = NULL, custom_ensemble_weights_DXZ = custom_ensemble_weights, custom_ensemble_weights_DX = custom_ensemble_weights, cluster_variable = seq_along(y), silent = FALSE, parallel = NULL, fitted = NULL, splits = NULL, save_crossval = TRUE, ... )ddml_fpliv( y, D, Z, X, learners, learners_DXZ = learners, learners_DX = learners, sample_folds = 10, ensemble_type = "nnls", shortstack = FALSE, cv_folds = 10, custom_ensemble_weights = NULL, custom_ensemble_weights_DXZ = custom_ensemble_weights, custom_ensemble_weights_DX = custom_ensemble_weights, cluster_variable = seq_along(y), silent = FALSE, parallel = NULL, fitted = NULL, splits = NULL, save_crossval = TRUE, ... )
y |
The outcome variable. |
D |
A matrix of endogenous variables. |
Z |
A (sparse) matrix of instruments. |
X |
A (sparse) matrix of control variables. |
learners |
May take one of two forms, depending on whether a
single learner or stacking with multiple learners is used for
estimation of the conditional expectation functions.
If a single learner is used,
If stacking with multiple learners is used,
Omission of the |
learners_DXZ, learners_DX
|
Optional arguments to allow for different
base learners for estimation of |
sample_folds |
Number of cross-fitting folds. |
ensemble_type |
Ensemble method to combine base learners into final estimate of the conditional expectation functions. Possible values are:
Multiple ensemble types may be passed as a vector of strings. |
shortstack |
Boolean to use short-stacking. |
cv_folds |
Number of folds used for cross-validation in ensemble construction. |
custom_ensemble_weights |
A numerical matrix with
user-specified ensemble weights. Each column corresponds to a
custom ensemble specification, each row corresponds to a base
learner in |
custom_ensemble_weights_DXZ, custom_ensemble_weights_DX
|
Optional
arguments to allow for different
custom ensemble weights for |
cluster_variable |
A vector of cluster indices. |
silent |
Boolean to silence estimation updates. |
parallel |
An optional named list with parallel processing
options. When
|
fitted |
An optional named list of per-equation cross-fitted
predictions, typically obtained from a previous fit via
|
splits |
An optional list of sample split objects, typically
obtained from a previous fit via |
save_crossval |
Logical indicating whether to store the inner
cross-validation residuals used for ensemble weight
computation. Default |
... |
Additional arguments passed to internal methods. |
Parameter of Interest: ddml_fpliv provides a Double/Debiased
Machine Learning estimator for the flexible partially linear instrumental
variable (IV) coefficient , defined by the partially linear
IV model:
where is a random vector such that
, and is an unknown nuisance function.
Neyman Orthogonal Score: The Neyman orthogonal score is:
where the nuisance parameters are taking
true values , , and .
Jacobian:
See ddml-intro for how the influence function
and inference are derived from these components.
ddml_fpliv returns an object of S3 class
ddml_fpliv and ddml. See
ddml-intro for the common output structure.
Additional pass-through fields: learners,
learners_DXZ, learners_DX.
Other ddml estimators:
ddml-intro,
ddml_apo(),
ddml_ate(),
ddml_attgt(),
ddml_late(),
ddml_pliv(),
ddml_plm(),
ddml_policy()
# Construct variables from the included Angrist & Evans (1998) data y = AE98[, "worked"] D = AE98[, "morekids"] Z = AE98[, "samesex", drop = FALSE] X = AE98[, c("age","agefst","black","hisp","othrace","educ")] # Estimate the partially linear IV model using a single base learner, ridge. fpliv_fit <- ddml_fpliv(y, D, Z, X, learners = list(what = mdl_glmnet, args = list(alpha = 0)), sample_folds = 2, silent = TRUE) summary(fpliv_fit)# Construct variables from the included Angrist & Evans (1998) data y = AE98[, "worked"] D = AE98[, "morekids"] Z = AE98[, "samesex", drop = FALSE] X = AE98[, c("age","agefst","black","hisp","othrace","educ")] # Estimate the partially linear IV model using a single base learner, ridge. fpliv_fit <- ddml_fpliv(y, D, Z, X, learners = list(what = mdl_glmnet, args = list(alpha = 0)), sample_folds = 2, silent = TRUE) summary(fpliv_fit)
Estimator for the local average treatment effect.
ddml_late( y, D, Z, X, learners, learners_DXZ = learners, learners_ZX = learners, sample_folds = 10, ensemble_type = "nnls", shortstack = FALSE, cv_folds = 10, custom_ensemble_weights = NULL, custom_ensemble_weights_DXZ = custom_ensemble_weights, custom_ensemble_weights_ZX = custom_ensemble_weights, cluster_variable = seq_along(y), stratify = TRUE, trim = 0.01, silent = FALSE, parallel = NULL, fitted = NULL, splits = NULL, save_crossval = TRUE, ... )ddml_late( y, D, Z, X, learners, learners_DXZ = learners, learners_ZX = learners, sample_folds = 10, ensemble_type = "nnls", shortstack = FALSE, cv_folds = 10, custom_ensemble_weights = NULL, custom_ensemble_weights_DXZ = custom_ensemble_weights, custom_ensemble_weights_ZX = custom_ensemble_weights, cluster_variable = seq_along(y), stratify = TRUE, trim = 0.01, silent = FALSE, parallel = NULL, fitted = NULL, splits = NULL, save_crossval = TRUE, ... )
y |
The outcome variable. |
D |
A matrix of endogenous variables. |
Z |
Binary instrumental variable. |
X |
A (sparse) matrix of control variables. |
learners |
May take one of two forms, depending on whether a
single learner or stacking with multiple learners is used for
estimation of the conditional expectation functions.
If a single learner is used,
If stacking with multiple learners is used,
Omission of the |
learners_DXZ, learners_ZX
|
Optional arguments to allow for different
base learners for estimation of |
sample_folds |
Number of cross-fitting folds. |
ensemble_type |
Ensemble method to combine base learners into final estimate of the conditional expectation functions. Possible values are:
Multiple ensemble types may be passed as a vector of strings. |
shortstack |
Boolean to use short-stacking. |
cv_folds |
Number of folds used for cross-validation in ensemble construction. |
custom_ensemble_weights |
A numerical matrix with
user-specified ensemble weights. Each column corresponds to a
custom ensemble specification, each row corresponds to a base
learner in |
custom_ensemble_weights_DXZ, custom_ensemble_weights_ZX
|
Optional
arguments to allow for different
custom ensemble weights for |
cluster_variable |
A vector of cluster indices. |
stratify |
Boolean for stratified cross-fitting: if |
trim |
Number in (0, 1) for trimming the estimated propensity scores at
|
silent |
Boolean to silence estimation updates. |
parallel |
An optional named list with parallel processing
options. When
|
fitted |
An optional named list of per-equation cross-fitted
predictions, typically obtained from a previous fit via
|
splits |
An optional list of sample split objects. For
|
save_crossval |
Logical indicating whether to store the inner
cross-validation residuals used for ensemble weight
computation. Default |
... |
Additional arguments passed to internal methods. |
Parameter of Interest: ddml_late provides a
Double/Debiased Machine Learning estimator for the local average
treatment effect. Under the standard instrumental variable assumptions
(conditional independence, exclusion restriction, relevance, and
monotonicity) with a binary instrument and a binary treatment
, the parameter is identified by the following reduced form
parameter:
where is the observed random vector.
Nuisance Parameters: The nuisance parameters are
taking true values
, ,
and .
Neyman Orthogonal Score / Moment Equation: The Neyman orthogonal score is:
Jacobian:
See ddml-intro for how the influence function
and inference are derived from these components.
ddml_late returns an object of S3 class
ddml_late and ddml. See
ddml-intro for the common output structure.
Additional pass-through fields: learners,
learners_DXZ, learners_ZX.
Imbens G, Angrist J (1994). "Identification and Estimation of Local Average Treatment Effects." Econometrica, 62(2), 467-475.
Other ddml estimators:
ddml-intro,
ddml_apo(),
ddml_ate(),
ddml_attgt(),
ddml_fpliv(),
ddml_pliv(),
ddml_plm(),
ddml_policy()
# Construct variables from the included Angrist & Evans (1998) data y = AE98[, "worked"] D = AE98[, "morekids"] Z = AE98[, "samesex"] X = AE98[, c("age","agefst","black","hisp","othrace","educ")] # Estimate the local average treatment effect using a single base learner, # ridge. late_fit <- ddml_late(y, D, Z, X, learners = list(what = mdl_glmnet, args = list(alpha = 0)), sample_folds = 2, silent = TRUE) summary(late_fit) # Estimate the local average treatment effect using short-stacking with base # learners ols, lasso, and ridge. We can also use custom_ensemble_weights # to estimate the LATE using every individual base learner. weights_everylearner <- diag(1, 3) colnames(weights_everylearner) <- c("mdl:ols", "mdl:lasso", "mdl:ridge") late_fit <- ddml_late(y, D, Z, X, learners = list(list(what = ols), list(what = mdl_glmnet), list(what = mdl_glmnet, args = list(alpha = 0))), ensemble_type = 'nnls', custom_ensemble_weights = weights_everylearner, shortstack = TRUE, sample_folds = 2, silent = TRUE) summary(late_fit)# Construct variables from the included Angrist & Evans (1998) data y = AE98[, "worked"] D = AE98[, "morekids"] Z = AE98[, "samesex"] X = AE98[, c("age","agefst","black","hisp","othrace","educ")] # Estimate the local average treatment effect using a single base learner, # ridge. late_fit <- ddml_late(y, D, Z, X, learners = list(what = mdl_glmnet, args = list(alpha = 0)), sample_folds = 2, silent = TRUE) summary(late_fit) # Estimate the local average treatment effect using short-stacking with base # learners ols, lasso, and ridge. We can also use custom_ensemble_weights # to estimate the LATE using every individual base learner. weights_everylearner <- diag(1, 3) colnames(weights_everylearner) <- c("mdl:ols", "mdl:lasso", "mdl:ridge") late_fit <- ddml_late(y, D, Z, X, learners = list(list(what = ols), list(what = mdl_glmnet), list(what = mdl_glmnet, args = list(alpha = 0))), ensemble_type = 'nnls', custom_ensemble_weights = weights_everylearner, shortstack = TRUE, sample_folds = 2, silent = TRUE) summary(late_fit)
Estimator for the partially linear IV coefficient.
ddml_pliv( y, D, Z, X, learners, learners_DX = learners, learners_ZX = learners, sample_folds = 10, ensemble_type = "nnls", shortstack = FALSE, cv_folds = 10, custom_ensemble_weights = NULL, custom_ensemble_weights_DX = custom_ensemble_weights, custom_ensemble_weights_ZX = custom_ensemble_weights, cluster_variable = seq_along(y), silent = FALSE, parallel = NULL, fitted = NULL, splits = NULL, save_crossval = TRUE, ... )ddml_pliv( y, D, Z, X, learners, learners_DX = learners, learners_ZX = learners, sample_folds = 10, ensemble_type = "nnls", shortstack = FALSE, cv_folds = 10, custom_ensemble_weights = NULL, custom_ensemble_weights_DX = custom_ensemble_weights, custom_ensemble_weights_ZX = custom_ensemble_weights, cluster_variable = seq_along(y), silent = FALSE, parallel = NULL, fitted = NULL, splits = NULL, save_crossval = TRUE, ... )
y |
The outcome variable. |
D |
A matrix of endogenous variables. |
Z |
A matrix of instruments. |
X |
A (sparse) matrix of control variables. |
learners |
May take one of two forms, depending on whether a
single learner or stacking with multiple learners is used for
estimation of the conditional expectation functions.
If a single learner is used,
If stacking with multiple learners is used,
Omission of the |
learners_DX, learners_ZX
|
Optional arguments to allow for different
base learners for estimation of |
sample_folds |
Number of cross-fitting folds. |
ensemble_type |
Ensemble method to combine base learners into final estimate of the conditional expectation functions. Possible values are:
Multiple ensemble types may be passed as a vector of strings. |
shortstack |
Boolean to use short-stacking. |
cv_folds |
Number of folds used for cross-validation in ensemble construction. |
custom_ensemble_weights |
A numerical matrix with
user-specified ensemble weights. Each column corresponds to a
custom ensemble specification, each row corresponds to a base
learner in |
custom_ensemble_weights_DX, custom_ensemble_weights_ZX
|
Optional
arguments to allow for different
custom ensemble weights for |
cluster_variable |
A vector of cluster indices. |
silent |
Boolean to silence estimation updates. |
parallel |
An optional named list with parallel processing
options. When
|
fitted |
An optional named list of per-equation cross-fitted
predictions, typically obtained from a previous fit via
|
splits |
An optional list of sample split objects, typically
obtained from a previous fit via |
save_crossval |
Logical indicating whether to store the inner
cross-validation residuals used for ensemble weight
computation. Default |
... |
Additional arguments passed to internal methods. |
Parameter of Interest: ddml_pliv provides a Double/Debiased
Machine Learning estimator for the partially linear instrumental variable
(IV) coefficient , defined by the partially linear IV model:
where is a random vector such that
, and is an unknown nuisance function.
Neyman Orthogonal Score: The Neyman orthogonal score is:
where the nuisance parameters are taking
true values , , and .
Jacobian:
See ddml-intro for how the influence function
and inference are derived from these components.
ddml_pliv returns an object of S3 class
ddml_pliv and ddml. See ddml-intro
for the common output structure. Additional pass-through
fields: learners, learners_DX,
learners_ZX.
Other ddml estimators:
ddml-intro,
ddml_apo(),
ddml_ate(),
ddml_attgt(),
ddml_fpliv(),
ddml_late(),
ddml_plm(),
ddml_policy()
# Construct variables from the included Angrist & Evans (1998) data y = AE98[, "worked"] D = AE98[, "morekids"] Z = AE98[, "samesex"] X = AE98[, c("age","agefst","black","hisp","othrace","educ")] # Estimate the partially linear IV model using a single base learner, ridge. pliv_fit <- ddml_pliv(y, D, Z, X, learners = list(what = mdl_glmnet, args = list(alpha = 0)), sample_folds = 2, silent = TRUE) summary(pliv_fit)# Construct variables from the included Angrist & Evans (1998) data y = AE98[, "worked"] D = AE98[, "morekids"] Z = AE98[, "samesex"] X = AE98[, c("age","agefst","black","hisp","othrace","educ")] # Estimate the partially linear IV model using a single base learner, ridge. pliv_fit <- ddml_pliv(y, D, Z, X, learners = list(what = mdl_glmnet, args = list(alpha = 0)), sample_folds = 2, silent = TRUE) summary(pliv_fit)
Estimator for the partially linear regression coefficient.
ddml_plm( y, D, X, learners, learners_DX = learners, sample_folds = 10, ensemble_type = "nnls", shortstack = FALSE, cv_folds = 10, custom_ensemble_weights = NULL, custom_ensemble_weights_DX = custom_ensemble_weights, cluster_variable = seq_along(y), silent = FALSE, parallel = NULL, fitted = NULL, splits = NULL, save_crossval = TRUE, ... )ddml_plm( y, D, X, learners, learners_DX = learners, sample_folds = 10, ensemble_type = "nnls", shortstack = FALSE, cv_folds = 10, custom_ensemble_weights = NULL, custom_ensemble_weights_DX = custom_ensemble_weights, cluster_variable = seq_along(y), silent = FALSE, parallel = NULL, fitted = NULL, splits = NULL, save_crossval = TRUE, ... )
y |
The outcome variable. |
D |
A matrix of endogenous variables. |
X |
A (sparse) matrix of control variables. |
learners |
May take one of two forms, depending on whether a
single learner or stacking with multiple learners is used for
estimation of the conditional expectation functions.
If a single learner is used,
If stacking with multiple learners is used,
Omission of the |
learners_DX |
Optional argument to allow for different estimators of
|
sample_folds |
Number of cross-fitting folds. |
ensemble_type |
Ensemble method to combine base learners into final estimate of the conditional expectation functions. Possible values are:
Multiple ensemble types may be passed as a vector of strings. |
shortstack |
Boolean to use short-stacking. |
cv_folds |
Number of folds used for cross-validation in ensemble construction. |
custom_ensemble_weights |
A numerical matrix with
user-specified ensemble weights. Each column corresponds to a
custom ensemble specification, each row corresponds to a base
learner in |
custom_ensemble_weights_DX |
Optional argument to allow for different
custom ensemble weights for |
cluster_variable |
A vector of cluster indices. |
silent |
Boolean to silence estimation updates. |
parallel |
An optional named list with parallel processing
options. When
|
fitted |
An optional named list of per-equation cross-fitted
predictions, typically obtained from a previous fit via
|
splits |
An optional list of sample split objects, typically
obtained from a previous fit via |
save_crossval |
Logical indicating whether to store the inner
cross-validation residuals used for ensemble weight
computation. Default |
... |
Additional arguments passed to internal methods. |
Parameter of Interest: ddml_plm provides a Double/Debiased
Machine Learning estimator for the partially linear regression
coefficient , defined by the partially linear regression
model:
where is a random vector such that
, and is an unknown nuisance function.
Neyman Orthogonal Score: The Neyman orthogonal score is:
where the nuisance parameters are taking
true values and .
Jacobian:
See ddml-intro for how the influence function
and inference are derived from these components.
ddml_plm returns an object of S3 class
ddml_plm and ddml. See ddml-intro
for the common output structure. Additional pass-through
fields: learners, learners_DX.
Other ddml estimators:
ddml-intro,
ddml_apo(),
ddml_ate(),
ddml_attgt(),
ddml_fpliv(),
ddml_late(),
ddml_pliv(),
ddml_policy()
# Construct variables from the included Angrist & Evans (1998) data y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace","educ")] # Estimate the partially linear model using a single base learner, ridge. plm_fit <- ddml_plm(y, D, X, learners = list(what = mdl_glmnet, args = list(alpha = 0)), sample_folds = 2, silent = TRUE) summary(plm_fit) # Estimate the partially linear model using short-stacking with base learners # ols, lasso, and ridge. We can also use custom_ensemble_weights # to estimate the ATE using every individual base learner. weights_everylearner <- diag(1, 3) colnames(weights_everylearner) <- c("mdl:ols", "mdl:lasso", "mdl:ridge") plm_fit <- ddml_plm(y, D, X, learners = list(list(what = ols), list(what = mdl_glmnet), list(what = mdl_glmnet, args = list(alpha = 0))), ensemble_type = 'nnls', custom_ensemble_weights = weights_everylearner, shortstack = TRUE, sample_folds = 2, silent = TRUE) summary(plm_fit) # Re-estimate with a different ensemble type using pass-through # (skips cross-fitting, only recomputes ensemble weights). plm_fit2 <- ddml_plm(y, D, X, learners = list(list(what = ols), list(what = mdl_glmnet), list(what = mdl_glmnet, args = list(alpha = 0))), ensemble_type = 'average', shortstack = TRUE, sample_folds = 2, silent = TRUE, fitted = plm_fit$fitted, splits = plm_fit$splits) summary(plm_fit2)# Construct variables from the included Angrist & Evans (1998) data y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace","educ")] # Estimate the partially linear model using a single base learner, ridge. plm_fit <- ddml_plm(y, D, X, learners = list(what = mdl_glmnet, args = list(alpha = 0)), sample_folds = 2, silent = TRUE) summary(plm_fit) # Estimate the partially linear model using short-stacking with base learners # ols, lasso, and ridge. We can also use custom_ensemble_weights # to estimate the ATE using every individual base learner. weights_everylearner <- diag(1, 3) colnames(weights_everylearner) <- c("mdl:ols", "mdl:lasso", "mdl:ridge") plm_fit <- ddml_plm(y, D, X, learners = list(list(what = ols), list(what = mdl_glmnet), list(what = mdl_glmnet, args = list(alpha = 0))), ensemble_type = 'nnls', custom_ensemble_weights = weights_everylearner, shortstack = TRUE, sample_folds = 2, silent = TRUE) summary(plm_fit) # Re-estimate with a different ensemble type using pass-through # (skips cross-fitting, only recomputes ensemble weights). plm_fit2 <- ddml_plm(y, D, X, learners = list(list(what = ols), list(what = mdl_glmnet), list(what = mdl_glmnet, args = list(alpha = 0))), ensemble_type = 'average', shortstack = TRUE, sample_folds = 2, silent = TRUE, fitted = plm_fit$fitted, splits = plm_fit$splits) summary(plm_fit2)
Estimator for the expected value of a multi-action policy, with optional per-level margins.
ddml_policy( y, D, X, policy, margins = NULL, learners, learners_DX = learners, sample_folds = 10, ensemble_type = "nnls", shortstack = FALSE, cv_folds = 10, custom_ensemble_weights = NULL, custom_ensemble_weights_DX = custom_ensemble_weights, cluster_variable = seq_along(y), stratify = TRUE, trim = 0.01, silent = FALSE, parallel = NULL, fitted = NULL, splits = NULL, save_crossval = TRUE, ... )ddml_policy( y, D, X, policy, margins = NULL, learners, learners_DX = learners, sample_folds = 10, ensemble_type = "nnls", shortstack = FALSE, cv_folds = 10, custom_ensemble_weights = NULL, custom_ensemble_weights_DX = custom_ensemble_weights, cluster_variable = seq_along(y), stratify = TRUE, trim = 0.01, silent = FALSE, parallel = NULL, fitted = NULL, splits = NULL, save_crossval = TRUE, ... )
y |
The outcome variable. |
D |
The observed discrete (potentially multi-valued) treatment variable. |
X |
A (sparse) matrix of control variables. |
policy |
A vector of length |
margins |
An optional numeric vector of length |
learners |
May take one of two forms, depending on whether a
single learner or stacking with multiple learners is used for
estimation of the conditional expectation functions.
If a single learner is used,
If stacking with multiple learners is used,
Omission of the |
learners_DX |
Optional argument to allow for different estimators of
|
sample_folds |
Number of cross-fitting folds. |
ensemble_type |
Ensemble method to combine base learners into final estimate of the conditional expectation functions. Possible values are:
Multiple ensemble types may be passed as a vector of strings. |
shortstack |
Boolean to use short-stacking. |
cv_folds |
Number of folds used for cross-validation in ensemble construction. |
custom_ensemble_weights |
A numerical matrix with
user-specified ensemble weights. Each column corresponds to a
custom ensemble specification, each row corresponds to a base
learner in |
custom_ensemble_weights_DX |
Optional argument to allow for different
custom ensemble weights for |
cluster_variable |
A vector of cluster indices. |
stratify |
Boolean for stratified cross-fitting: if |
trim |
Number in (0, 1) for trimming the estimated propensity scores at
|
silent |
Boolean to silence estimation updates. |
parallel |
An optional named list with parallel processing
options. When
|
fitted |
An optional named list of per-equation cross-fitted
predictions, typically obtained from a previous fit via
|
splits |
An optional list of sample split objects. For
|
save_crossval |
Logical indicating whether to store the inner
cross-validation residuals used for ensemble weight
computation. Default |
... |
Additional arguments passed to internal methods. |
Parameter of Interest: ddml_policy provides a
Double/Debiased Machine Learning estimator for the expected
value of a multi-action policy
that assigns each unit to one of treatment levels.
The target parameter is
where the known weight functions are
,
and are user-supplied margins.
When all margins equal one (margins = NULL), the
parameter reduces to the policy value
.
Each term in the sum is a weighted average potential outcome
(wAPO), estimated internally via ddml_apo.
Nuisance Parameters: For each treatment level
, the nuisance parameters are
taking true values
and
.
Only propensity models are estimated;
the last is derived as
.
Neyman Orthogonal Score / Moment Equation: The Neyman orthogonal score is:
Jacobian:
See ddml-intro for how the influence function
and inference are derived from these components.
ddml_policy returns an object of S3 class
ddml_policy and ddml. See
ddml-intro for the common output structure.
Additional pass-through fields: learners,
learners_DX, policy, margins.
Dudik M, Langford J, Li L (2011). "Doubly Robust Policy Evaluation and Learning." Proceedings of the 28th International Conference on Machine Learning, 1097-1104.
Zhou Z, Athey S, Wager S (2023). "Offline Multi-Action Policy Learning: Generalization and Optimization." Operations Research, 71(2), 698-722.
Other ddml estimators:
ddml-intro,
ddml_apo(),
ddml_ate(),
ddml_attgt(),
ddml_fpliv(),
ddml_late(),
ddml_pliv(),
ddml_plm()
# Construct variables from the included Angrist & Evans (1998) data y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace","educ")] # Define a simple policy: assign D=1 if age > median, else D=0 policy <- ifelse(X[, "age"] > median(X[, "age"]), 1, 0) # Estimate the policy value using a single base learner, ridge. policy_fit <- ddml_policy(y, D, X, policy = policy, learners = list(what = mdl_glmnet), sample_folds = 2, silent = TRUE) summary(policy_fit)# Construct variables from the included Angrist & Evans (1998) data y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace","educ")] # Define a simple policy: assign D=1 if age > median, else D=0 policy <- ifelse(X[, "age"] > median(X[, "age"]), 1, 0) # Estimate the policy value using a single base learner, ridge. policy_fit <- ddml_policy(y, D, X, policy = policy, learners = list(what = mdl_glmnet), sample_folds = 2, silent = TRUE) summary(policy_fit)
Validates a list of ddml fits and stamps class
"ddml_rep" for multi-resample aggregation.
ddml_rep(fits) ## S3 method for class 'ddml_rep' print(x, ...)ddml_rep(fits) ## S3 method for class 'ddml_rep' print(x, ...)
fits |
A list of at least 2 objects inheriting from
class |
x |
A |
... |
Currently unused. |
An object of class c("ddml_rep", "ral_rep")
with fields:
List of ddml objects.
Number of resamples.
Primary class of the fits.
Coefficient names.
Ensemble types.
Number of observations.
Number of cross-fitting folds.
Logical, whether short-stacking was used.
Other ddml replication:
ddml_replicate()
y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace")] fits = lapply(1:3, function(r) { ddml_plm(y, D, X, learners = list(what = ols), sample_folds = 2, silent = TRUE) }) reps = ddml_rep(fits) summary(reps)y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace")] fits = lapply(1:3, function(r) { ddml_plm(y, D, X, learners = list(what = ols), sample_folds = 2, silent = TRUE) }) reps = ddml_rep(fits) summary(reps)
Convenience wrapper that calls a ddml_* estimator
function multiple times with independent sample splits
and returns a ddml_rep object for aggregated
inference.
ddml_replicate(fn, ..., resamples = 5, silent = FALSE)ddml_replicate(fn, ..., resamples = 5, silent = FALSE)
fn |
A |
... |
Arguments passed to |
resamples |
Integer number of independent resamples. Must be >= 2. Default 5. |
silent |
Logical. If |
An object of class "ddml_rep".
Other ddml replication:
ddml_rep()
y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace")] reps = ddml_replicate(ddml_plm, y = y, D = D, X = X, learners = list(what = ols), sample_folds = 2, resamples = 3, silent = TRUE) summary(reps)y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace")] reps = ddml_replicate(ddml_plm, y = y, D = D, X = X, learners = list(what = ols), sample_folds = 2, resamples = 3, silent = TRUE) summary(reps)
All ddml_* estimators (ddml_plm,
ddml_pliv, ddml_fpliv,
ddml_ate, ddml_att,
ddml_late, ddml_apo,
ddml_policy) return
objects that inherit from S3 class "ddml".
Each object is a list containing the components described below. Estimator-specific fields (e.g., pass-through learner arguments) are documented on the individual estimator pages.
The ddml() constructor can also be used directly to build
a "ddml" object from user-supplied score components,
enabling implementation of custom DML estimators that inherit
all S3 methods.
y |
The outcome variable. |
D |
A matrix of endogenous variables. |
X |
A (sparse) matrix of control variables. |
learners |
May take one of two forms, depending on whether a
single learner or stacking with multiple learners is used for
estimation of the conditional expectation functions.
If a single learner is used,
If stacking with multiple learners is used,
Omission of the |
sample_folds |
Number of cross-fitting folds. |
ensemble_type |
Ensemble method to combine base learners into final estimate of the conditional expectation functions. Possible values are:
Multiple ensemble types may be passed as a vector of strings. |
shortstack |
Boolean to use short-stacking. |
cv_folds |
Number of folds used for cross-validation in ensemble construction. |
custom_ensemble_weights |
A numerical matrix with
user-specified ensemble weights. Each column corresponds to a
custom ensemble specification, each row corresponds to a base
learner in |
cluster_variable |
A vector of cluster indices. |
silent |
Boolean to silence estimation updates. |
parallel |
An optional named list with parallel processing
options. When
|
fitted |
An optional named list of per-equation cross-fitted
predictions, typically obtained from a previous fit via
|
splits |
An optional list of sample split objects, typically
obtained from a previous fit via |
save_crossval |
Logical indicating whether to store the inner
cross-validation residuals used for ensemble weight
computation. Default |
... |
Additional arguments passed to internal methods. |
All ddml_* estimators target a low-dimensional
parameter identified by a moment condition
where denotes observed random variables and
is a (potentially high-dimensional) nuisance
parameter. Throughout, the score is assumed to be
Neyman orthogonal.
Estimation proceeds via cross-fitting: the sample is randomly
partitioned into folds . For
each fold , nuisance parameters are estimated on the
complementary folds () and the scores are
evaluated on fold . The DML estimator
solves
Inference is based on the influence function. Define the Jacobian
and the influence function
The variance of is then estimated by
,
where is the sample analog of the Jacobian:
HC1 and HC3 variance estimators are described in
vcov.ral. The leverage
(see hatvalues.ral) for the DML estimator
is
and its sample analog is
, stored in
dinf_dtheta.
Under regularity conditions and sufficient convergence of
, the DML estimator is asymptotically normal:
Further details and regularity conditions are given in
Chernozhukov et al. (2018). The specific forms of the
score and Jacobian for each estimator
are documented on their respective help pages (e.g.,
ddml_plm, ddml_ate).
coefficientsA matrix of estimated target
parameters: rows correspond to components of
, columns to ensemble types.
ensemble_weightsA named list. Each element
is a weight matrix (or 3D array when
shortstack = TRUE) showing the weight assigned
to each base learner by the ensemble procedure for
the corresponding nuisance equation.
mspeA named list of numeric vectors containing per-learner out-of-sample MSPEs, computed from cross-fitted residuals.
r2A named list of numeric vectors containing per-learner out-of-sample R-squared values.
inf_funcA 3D array of evaluated influence
functions (n x p x nensb).
dinf_dthetaAn optional 4D array of dimension
(n x p x p x nensb) containing the derivatives of the
influence functions with respect to . Used
internally by hatvalues.ral for HC3
inference.
scoresA 3D array of evaluated Neyman
orthogonal scores (n x p x nensb).
JA 3D array of evaluated Jacobians
(p x p x nensb).
fittedA named list of per-equation
cross-fitted prediction objects. Can be passed back
via the fitted argument together with
splits to skip cross-fitting on
re-estimation.
splitsThe data splitting structure (subsamples, CV subsamples, and any stratification indices).
ensemble_typeCharacter vector of ensemble types used.
cluster_variableThe cluster variable vector used for sample splitting and inference.
nobsNumber of observations.
sample_foldsNumber of cross-fitting folds.
shortstackLogical indicating whether short-stacking was used.
callThe matched call.
coef_namesCharacter vector of coefficient names.
estimator_nameCharacter string identifying
the estimator (e.g., "Partially Linear Model").
The following generic methods are available for all
ddml objects: summary.ddml,
coef.ral, vcov.ral,
confint.ral, hatvalues.ral,
nobs.ral, tidy.ddml,
glance.ddml, and
diagnostics.
Ahrens A, Chernozhukov V, Hansen C B, Kozbur D, Schaffer M E, Wiemann T (2026). "An Introduction to Double/Debiased Machine Learning." Journal of Economic Literature, forthcoming.
Chernozhukov V, Chetverikov D, Demirer M, Duflo E, Hansen C B, Newey W, Robins J (2018). "Double/debiased machine learning for treatment and structural parameters." The Econometrics Journal, 21(1), C1-C68.
Other ddml estimators:
ddml_apo(),
ddml_ate(),
ddml_attgt(),
ddml_fpliv(),
ddml_late(),
ddml_pliv(),
ddml_plm(),
ddml_policy()
Computes per-learner diagnostics including MSPE, R-squared, ensemble weights, and optionally cross-validation comparison (CVC) p-values for each nuisance equation.
diagnostics(object, cvc = FALSE, bootnum = 500, ...)diagnostics(object, cvc = FALSE, bootnum = 500, ...)
object |
An object of class |
cvc |
Logical. Compute CVC p-values via multiplier
bootstrap? Default |
bootnum |
Number of bootstrap replications for CVC.
Default 500. Ignored when |
... |
Currently unused. |
An object of class ddml_diagnostics containing per-equation
learner diagnostics. Use print() for formatted output or
tidy() for a flat data.frame.
Lei J (2020). "Cross-Validation With Confidence." Journal of the American Statistical Association, 115(532), 1978-1997.
Other utilities:
crosspred(),
crossval(),
ddml(),
ensemble(),
ensemble_weights(),
shortstacking()
y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace")] learners = list(list(what = ols), list(what = mdl_glmnet)) plm_fit = ddml_plm(y, D, X, learners = learners, sample_folds = 2, silent = TRUE) diagnostics(plm_fit, cvc = TRUE) tidy(diagnostics(plm_fit, cvc = TRUE))y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace")] learners = list(list(what = ols), list(what = mdl_glmnet)) plm_fit = ddml_plm(y, D, X, learners = learners, sample_folds = 2, silent = TRUE) diagnostics(plm_fit, cvc = TRUE) tidy(diagnostics(plm_fit, cvc = TRUE))
Computes an ensemble of learners based on the specified aggregation type and computes cross-validated out-of-sample predictions to inform the weights.
ensemble( y, X, type = "average", learners, cv_folds = 5, cv_subsamples = NULL, cv_results = NULL, custom_weights = NULL, silent = FALSE )ensemble( y, X, type = "average", learners, cv_folds = 5, cv_subsamples = NULL, cv_results = NULL, custom_weights = NULL, silent = FALSE )
y |
The outcome variable. |
X |
The feature matrix. |
type |
A character string indicating the type of ensemble to compute.
Default is |
learners |
A list of base learners. See
|
cv_folds |
Number of cross-validation folds. |
cv_subsamples |
Optional list of subsamples for cross-validation. |
cv_results |
Optional pre-computed cross-validation results. |
custom_weights |
Optional custom weights matrix. |
silent |
A boolean indicating whether to suppress progress messages. |
An object of class ensemble containing:
mdl_fitsList of fitted base learners.
weightsComputed ensemble weights.
learnersThe base learners used.
cv_resultsCross-validation results if computed.
mean_yMean of the outcome variable.
constant_yBoolean indicating if y is constant.
Other utilities:
crosspred(),
crossval(),
ddml(),
diagnostics(),
ensemble_weights(),
shortstacking()
# Construct variables from the included Angrist & Evans (1998) data y = AE98[, "worked"] X = AE98[, c("age","agefst","black","hisp","othrace")] # Fit an ensemble of ols, lasso, and ridge ens_fit = ensemble(y, X, type = "nnls", learners = list(list(what = ols), list(what = mdl_glmnet), list(what = mdl_glmnet, args = list(alpha = 0))), cv_folds = 5, silent = TRUE) ens_fit$weights predict(ens_fit, newdata = X)[1:5]# Construct variables from the included Angrist & Evans (1998) data y = AE98[, "worked"] X = AE98[, c("age","agefst","black","hisp","othrace")] # Fit an ensemble of ols, lasso, and ridge ens_fit = ensemble(y, X, type = "nnls", learners = list(list(what = ols), list(what = mdl_glmnet), list(what = mdl_glmnet, args = list(alpha = 0))), cv_folds = 5, silent = TRUE) ens_fit$weights predict(ens_fit, newdata = X)[1:5]
Computes the stacking weights for an ensemble of base learners using cross-validated out-of-sample predictions.
ensemble_weights( y, X, type = "average", learners = NULL, cv_folds = 5, cv_subsamples = NULL, cv_results = NULL, custom_weights = NULL, silent = FALSE )ensemble_weights( y, X, type = "average", learners = NULL, cv_folds = 5, cv_subsamples = NULL, cv_results = NULL, custom_weights = NULL, silent = FALSE )
y |
The outcome variable. |
X |
The feature matrix. |
type |
A character string or vector indicating the type(s) of ensemble
weights to compute. Default is |
learners |
Optional list of base learners.
Required when |
cv_folds |
Number of cross-validation folds. |
cv_subsamples |
Optional list of subsamples for cross-validation. |
cv_results |
Optional pre-computed cross-validation results. |
custom_weights |
Optional custom weights matrix. |
silent |
A boolean indicating whether to suppress progress messages. |
A list containing:
weightsA matrix of computed ensemble weights.
cv_resultsCross-validation results used for computing weights.
Other utilities:
crosspred(),
crossval(),
ddml(),
diagnostics(),
ensemble(),
shortstacking()
y = AE98[, "worked"] X = AE98[, c("age","agefst","black","hisp","othrace")] # Compute stacking weights via NNLS ew = ensemble_weights(y, X, type = "nnls", learners = list(list(what = ols), list(what = mdl_glmnet)), cv_folds = 5, silent = TRUE) ew$weightsy = AE98[, "worked"] X = AE98[, c("age","agefst","black","hisp","othrace")] # Compute stacking weights via NNLS ew = ensemble_weights(y, X, type = "nnls", learners = list(list(what = ols), list(what = mdl_glmnet)), cv_folds = 5, silent = TRUE) ew$weights
DML-specific glance method. Includes DML fields like
sample_folds, shortstack, and
model_type.
## S3 method for class 'ddml' glance(x, ...)## S3 method for class 'ddml' glance(x, ...)
x |
A |
... |
Currently unused. |
A one-row data.frame.
y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace")] plm_fit = ddml_plm(y, D, X, learners = list(what = ols), sample_folds = 2, silent = TRUE) glance(plm_fit)y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace")] plm_fit = ddml_plm(y, D, X, learners = list(what = ols), sample_folds = 2, silent = TRUE) glance(plm_fit)
DML-specific glance method. Includes DML fields.
## S3 method for class 'ddml_rep' glance(x, ...)## S3 method for class 'ddml_rep' glance(x, ...)
x |
A |
... |
Currently unused. |
A one-row data.frame.
y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace")] reps = ddml_replicate(ddml_plm, y = y, D = D, X = X, learners = list(what = ols), sample_folds = 2, resamples = 3, silent = TRUE) glance(reps)y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace")] reps = ddml_replicate(ddml_plm, y = y, D = D, X = X, learners = list(what = ols), sample_folds = 2, resamples = 3, silent = TRUE) glance(reps)
Returns a one-row summary of model-level statistics.
## S3 method for class 'ral' glance(x, ...)## S3 method for class 'ral' glance(x, ...)
x |
An object inheriting from class |
... |
Currently unused. |
A one-row data.frame with columns
nobs and estimator_name.
Glance at a RAL Rep Object
## S3 method for class 'ral_rep' glance(x, ...)## S3 method for class 'ral_rep' glance(x, ...)
x |
An object inheriting from class |
... |
Currently unused. |
A one-row data.frame with columns
nobs, nresamples, and
estimator_name.
Computes the leverage (hat values) for a RAL estimator. Used internally for HC3 standard errors.
## S3 method for class 'ral' hatvalues(model, fit_idx = 1, ...)## S3 method for class 'ral' hatvalues(model, fit_idx = 1, ...)
model |
An object inheriting from class |
fit_idx |
Integer index of the fit to extract leverage values for. Defaults to 1. |
... |
Currently unused. |
The leverage for observation
is
The sample analog replaces with its
estimate :
The derivative
is stored in the dinf_dtheta slot. For the specific
form of this derivative in the DML context, see
ddml-intro. For the leverage of linear
combinations, see lincom.
A numeric vector of leverage values.
Computes linear combinations
of DDML coefficient estimates.
lincom(fit, R, ...) ## S3 method for class 'ddml' lincom( fit, R, fit_idx = NULL, labels = NULL, inf_func_R = NULL, dinf_dR = NULL, ... ) ## S3 method for class 'ddml_rep' lincom( fit, R, inf_func_R = NULL, dinf_dR = NULL, fit_idx = NULL, labels = NULL, ... ) ## S3 method for class 'lincom' print(x, ...) ## S3 method for class 'lincom_rep' print(x, ...)lincom(fit, R, ...) ## S3 method for class 'ddml' lincom( fit, R, fit_idx = NULL, labels = NULL, inf_func_R = NULL, dinf_dR = NULL, ... ) ## S3 method for class 'ddml_rep' lincom( fit, R, inf_func_R = NULL, dinf_dR = NULL, fit_idx = NULL, labels = NULL, ... ) ## S3 method for class 'lincom' print(x, ...) ## S3 method for class 'lincom_rep' print(x, ...)
fit |
A |
R |
A |
... |
Currently unused. |
fit_idx |
Integer index of the fit to use, or
|
labels |
Optional character vector of length |
inf_func_R |
An optional |
dinf_dR |
An optional |
x |
A |
For a -dimensional coefficient vector
and a contrast
matrix , the linear combination is
.
The influence function for is
where is the influence function of
(see ral and
ddml-intro),
is the
matrix of influence functions for the contrast matrix
(the -th slice of inf_func_R),
and the second term vanishes when is fixed.
The estimated influence function is
The leverage for is
where is the structural leverage from
the parent estimator (see hatvalues.ral)
and is the weighting leverage. The sample
analog is
where is mapped from the
parent's dinf_dtheta and
from the optional dinf_dR argument.
The resulting lincom object inherits from
ral and supports all standard inference methods:
vcov, confint, summary, tidy,
and hatvalues. For ddml_rep objects,
lincom returns a lincom_rep inheriting
from ral_rep.
Note that inf_func_R is needed for inference when
is estimated. Leverage computation further requires
dinf_dR. See vcov.ral and
hatvalues.ral for more details.
An object of class "lincom" (inheriting
from "ral") for ddml input, or
"lincom_rep" (inheriting from "ral_rep")
for ddml_rep input.
Chernozhukov V, Chetverikov D, Demirer M, Duflo E, Hansen C B, Newey W, Robins J (2018). "Double/debiased machine learning for treatment and structural parameters." The Econometrics Journal, 21(1), C1-C68.
lincom_weights_did for constructing
DiD aggregation weights.
Other ddml inference:
summary.ddml()
set.seed(42) n <- 200; T_ <- 4 X <- matrix(rnorm(n * 2), n, 2) G <- sample(c(3, 4, Inf), n, replace = TRUE, prob = c(0.3, 0.3, 0.4)) y <- matrix(rnorm(n * T_), n, T_) for (i in seq_len(n)) { if (is.finite(G[i])) { for (j in seq_len(T_)) { if (j >= G[i]) y[i, j] <- y[i, j] + 1 } } } fit <- ddml_attgt(y, X, t = 1:T_, G = G, learners = list(what = ols), sample_folds = 2, silent = TRUE) # Simple contrast: first cell minus second p <- nrow(fit$coefficients) R <- matrix(0, p, 1) R[1, 1] <- 1; R[2, 1] <- -1 lc <- lincom(fit, R = R, labels = "ATT1-ATT2") summary(lc)set.seed(42) n <- 200; T_ <- 4 X <- matrix(rnorm(n * 2), n, 2) G <- sample(c(3, 4, Inf), n, replace = TRUE, prob = c(0.3, 0.3, 0.4)) y <- matrix(rnorm(n * T_), n, T_) for (i in seq_len(n)) { if (is.finite(G[i])) { for (j in seq_len(T_)) { if (j >= G[i]) y[i, j] <- y[i, j] + 1 } } } fit <- ddml_attgt(y, X, t = 1:T_, G = G, learners = list(what = ols), sample_folds = 2, silent = TRUE) # Simple contrast: first cell minus second p <- nrow(fit$coefficients) R <- matrix(0, p, 1) R[1, 1] <- 1; R[2, 1] <- -1 lc <- lincom(fit, R = R, labels = "ATT1-ATT2") summary(lc)
Constructs the contrast matrix and
its influence function matrix inf_func_R for
standard DiD aggregation types. The output is
designed to be passed directly to
lincom.
lincom_weights_did( fit, type = c("dynamic", "group", "simple", "calendar"), min_e = -Inf, max_e = Inf, fit_idx = NULL )lincom_weights_did( fit, type = c("dynamic", "group", "simple", "calendar"), min_e = -Inf, max_e = Inf, fit_idx = NULL )
fit |
A |
type |
Aggregation type: |
min_e, max_e
|
Event-time range filter (dynamic only).
Cells with event time outside |
fit_idx |
Integer index of the fit (ensemble type)
to use for computing the weighting leverage, or
|
Let denote the GT-ATT
from ddml_attgt. Each aggregation type
defines a summary parameter as a weighted average of
GT-ATTs over a subset of post-treatment cells
().
Dynamic (type = "dynamic"): aggregates
by event time (Callaway and Sant'Anna,
2021, eq. 9). For each :
Group (type = "group"): aggregates by
cohort . For each :
where and the
weights reduce to uniform within each cohort.
Calendar (type = "calendar"): aggregates
by time period . For each :
Simple (type = "simple"): a single
weighted average across all post-treatment cells:
The influence function for the estimated weights is
derived via the quotient rule and passed to
lincom as inf_func_R.
A list with elements:
RA contrast
matrix where is the number of GT cells.
inf_func_RAn
n x C x q array of influence functions for
the contrast matrix . Slice [,,k]
contains the IFs for column of .
dinf_dRAn
n x q x q array of weighting leverage. Each
slice is the constant matrix where
. See
D89 §5.3.
labelsCharacter vector of length
naming the aggregated quantities.
Callaway B, Sant'Anna P H C (2021). "Difference-in-Differences with multiple time periods." Journal of Econometrics, 225(2), 200-230.
set.seed(42) n <- 200; T_ <- 4 X <- matrix(rnorm(n * 2), n, 2) G <- sample(c(3, 4, Inf), n, replace = TRUE, prob = c(0.3, 0.3, 0.4)) y <- matrix(rnorm(n * T_), n, T_) fit <- ddml_attgt(y, X, t = 1:T_, G = G, learners = list(what = ols), sample_folds = 2, silent = TRUE) w <- lincom_weights_did(fit, type = "dynamic") dyn <- lincom(fit, R = w$R, inf_func_R = w$inf_func_R, dinf_dR = w$dinf_dR, labels = w$labels) summary(dyn)set.seed(42) n <- 200; T_ <- 4 X <- matrix(rnorm(n * 2), n, 2) G <- sample(c(3, 4, Inf), n, replace = TRUE, prob = c(0.3, 0.3, 0.4)) y <- matrix(rnorm(n * T_), n, T_) fit <- ddml_attgt(y, X, t = 1:T_, G = G, learners = list(what = ols), sample_folds = 2, silent = TRUE) w <- lincom_weights_did(fit, type = "dynamic") dyn <- lincom(fit, R = w$R, inf_func_R = w$inf_func_R, dinf_dR = w$dinf_dR, labels = w$labels) summary(dyn)
Simple wrapper for glmnet::bigGlm(), designed for sparse matrices.
mdl_bigGlm(y, X, ...)mdl_bigGlm(y, X, ...)
y |
The outcome variable. |
X |
The (sparse) feature matrix. |
... |
Additional arguments passed to |
mdl_bigGlm returns an object of S3 class mdl_bigGlm.
Other ml_wrapper:
mdl_glm(),
mdl_glmnet(),
mdl_ranger(),
mdl_xgboost(),
ols()
bigglm_fit <- mdl_bigGlm(rnorm(100), matrix(rnorm(1000), 100, 10)) class(bigglm_fit)bigglm_fit <- mdl_bigGlm(rnorm(100), matrix(rnorm(1000), 100, 10)) class(bigglm_fit)
Simple wrapper for stats::glm().
mdl_glm(y, X, ...)mdl_glm(y, X, ...)
y |
The outcome variable. |
X |
The feature matrix. |
... |
Additional arguments passed to |
mdl_glm returns an object of S3 class mdl_glm as a
simple mask of the return object of stats::glm().
Other ml_wrapper:
mdl_bigGlm(),
mdl_glmnet(),
mdl_ranger(),
mdl_xgboost(),
ols()
glm_fit <- mdl_glm(sample(0:1, 100, replace = TRUE), matrix(rnorm(1000), 100, 10)) class(glm_fit)glm_fit <- mdl_glm(sample(0:1, 100, replace = TRUE), matrix(rnorm(1000), 100, 10)) class(glm_fit)
Simple wrapper for glmnet::glmnet() and glmnet::cv.glmnet().
mdl_glmnet(y, X, cv = TRUE, ...)mdl_glmnet(y, X, cv = TRUE, ...)
y |
The outcome variable. |
X |
The (sparse) feature matrix. |
cv |
Boolean to indicate use of lasso with cross-validated penalty. |
... |
Additional arguments passed to |
mdl_glmnet returns an object of S3 class mdl_glmnet as
a simple mask of the return object of glmnet::glmnet() or
glmnet::cv.glmnet().
Friedman J, Hastie T, Tibshirani R (2010). "Regularization Paths for Generalized Linear Models via Coordinate Descent." Journal of Statistical Software, 33(1), 1-22.
Simon N, Friedman J, Hastie T, Tibshirani R (2011). "Regularization Paths for Cox's Proportional Hazards Model via Coordinate Descent." Journal of Statistical Software, 39(5), 1-13.
glmnet::glmnet(),glmnet::cv.glmnet()
Other ml_wrapper:
mdl_bigGlm(),
mdl_glm(),
mdl_ranger(),
mdl_xgboost(),
ols()
glmnet_fit <- mdl_glmnet(rnorm(100), matrix(rnorm(1000), 100, 10)) class(glmnet_fit)glmnet_fit <- mdl_glmnet(rnorm(100), matrix(rnorm(1000), 100, 10)) class(glmnet_fit)
Simple wrapper for ranger::ranger(). Supports regression
(default) and probability forests (set probability = TRUE).
mdl_ranger(y, X, ...)mdl_ranger(y, X, ...)
y |
The outcome variable. |
X |
The feature matrix. |
... |
Additional arguments passed to |
mdl_ranger returns an object of S3 class ranger as a
simple mask of the return object of ranger::ranger().
Wright M N, Ziegler A (2017). "ranger: A fast implementation of random forests for high dimensional data in C++ and R." Journal of Statistical Software 77(1), 1-17.
Other ml_wrapper:
mdl_bigGlm(),
mdl_glm(),
mdl_glmnet(),
mdl_xgboost(),
ols()
ranger_fit <- mdl_ranger(rnorm(100), matrix(rnorm(1000), 100, 10)) class(ranger_fit)ranger_fit <- mdl_ranger(rnorm(100), matrix(rnorm(1000), 100, 10)) class(ranger_fit)
Simple wrapper for xgboost::xgboost() with some changes to the
default arguments.
mdl_xgboost(y, X, nrounds = 500, verbosity = 0, ...)mdl_xgboost(y, X, nrounds = 500, verbosity = 0, ...)
y |
The outcome variable. |
X |
The (sparse) feature matrix. |
nrounds |
Number of boosting iterations / rounds. Note that the number of default boosting rounds here is not automatically tuned, and different problems will have vastly different optimal numbers of boosting rounds. |
verbosity |
Verbosity of printing messages. Valid values of 0 (silent), 1 (warning), 2 (info), and 3 (debug). |
... |
Additional arguments passed to |
mdl_xgboost returns an object of S3 class mdl_xgboost
as a simple mask to the return object of xgboost::xgboost().
Chen T, Guestrin C (2011). "Xgboost: A Scalable Tree Boosting System." Proceedings of the 22nd ACM SIGKDD International Conference on Knowledge Discovery and Data Mining, 785-794.
Other ml_wrapper:
mdl_bigGlm(),
mdl_glm(),
mdl_glmnet(),
mdl_ranger(),
ols()
xgboost_fit <- mdl_xgboost(rnorm(50), matrix(rnorm(150), 50, 3), nrounds = 1) class(xgboost_fit)xgboost_fit <- mdl_xgboost(rnorm(50), matrix(rnorm(150), 50, 3), nrounds = 1) class(xgboost_fit)
Number of Observations in a RAL Object
## S3 method for class 'ral' nobs(object, ...)## S3 method for class 'ral' nobs(object, ...)
object |
An object inheriting from class |
... |
Currently unused. |
Integer.
Simple implementation of ordinary least squares that computes with sparse feature matrices.
ols(y, X, const = TRUE, w = NULL)ols(y, X, const = TRUE, w = NULL)
y |
The outcome variable. |
X |
The feature matrix. |
const |
Boolean equal to |
w |
A vector of weights for weighted least squares. |
ols returns an object of S3 class
ols. An object of class ols is a list containing
the following components:
coefA vector with the regression coefficents.
y, X, const, w
Pass-through of the user-provided arguments. See above.
Other ml_wrapper:
mdl_bigGlm(),
mdl_glm(),
mdl_glmnet(),
mdl_ranger(),
mdl_xgboost()
ols_fit <- ols(rnorm(100), cbind(rnorm(100), rnorm(100)), const = TRUE) ols_fit$coefols_fit <- ols(rnorm(100), cbind(rnorm(100), rnorm(100)), const = TRUE) ols_fit$coef
Plots point estimates with confidence intervals
from an object inheriting from class ral.
## S3 method for class 'ral' plot( x, parm = NULL, level = 0.95, uniform = TRUE, fit_idx = 1, type = "HC1", xlab = NULL, ylab = NULL, main = NULL, col = "black", pch = 19, lwd = 1.5, ... ) ## S3 method for class 'ral_rep' plot( x, parm = NULL, level = 0.95, uniform = TRUE, type = "HC1", xlab = NULL, ylab = NULL, main = NULL, col = "black", pch = 19, lwd = 1.5, ... )## S3 method for class 'ral' plot( x, parm = NULL, level = 0.95, uniform = TRUE, fit_idx = 1, type = "HC1", xlab = NULL, ylab = NULL, main = NULL, col = "black", pch = 19, lwd = 1.5, ... ) ## S3 method for class 'ral_rep' plot( x, parm = NULL, level = 0.95, uniform = TRUE, type = "HC1", xlab = NULL, ylab = NULL, main = NULL, col = "black", pch = 19, lwd = 1.5, ... )
x |
An object inheriting from class |
parm |
A specification of which parameters to plot. Either a vector of names or indices. Default: all. |
level |
Numeric. Confidence level. Default |
uniform |
Logical. If |
fit_idx |
Integer. Which fit to plot (column index of
|
type |
Character. HC type for standard errors.
Default |
xlab |
Character. Label for the x-axis. |
ylab |
Character. Label for the y-axis. |
main |
Character. Title for the plot. |
col |
Color for points and segments.
Default |
pch |
Point character. Default |
lwd |
Line width for confidence interval segments.
Default |
... |
Additional arguments passed to
|
Invisibly returns a list with components
coefficients, ci, and labels.
# Simulate a simple example n <- 200 X <- cbind(1, stats::rnorm(n)) theta <- c(0.5, -0.3) inf <- matrix(stats::rnorm(n * 2), n, 2) obj <- ral(matrix(theta, 2, 1), array(inf, c(n, 2, 1)), nobs = n, coef_names = c("b1", "b2")) plot(obj)# Simulate a simple example n <- 200 X <- cbind(1, stats::rnorm(n)) theta <- c(0.5, -0.3) inf <- matrix(stats::rnorm(n * 2), n, 2) obj <- ral(matrix(theta, 2, 1), array(inf, c(n, 2, 1)), nobs = n, coef_names = c("b1", "b2")) plot(obj)
ensemble ObjectsPredict Method for ensemble Objects
## S3 method for class 'ensemble' predict(object, newdata, ..., type = "ensemble")## S3 method for class 'ensemble' predict(object, newdata, ..., type = "ensemble")
object |
A fitted |
newdata |
A feature matrix for prediction. |
... |
Currently unused. |
type |
Character; |
A matrix of predictions. When type = "ensemble",
one column per ensemble type; when type = "bylearner",
one column per base learner.
Predict Method for mdl_bigGlm Objects
## S3 method for class 'mdl_bigGlm' predict(object, newdata = NULL, ...)## S3 method for class 'mdl_bigGlm' predict(object, newdata = NULL, ...)
object |
A fitted |
newdata |
A (sparse) feature matrix for prediction. |
... |
Currently unused. |
A numeric vector of predicted values.
Predict Method for mdl_glm Objects
## S3 method for class 'mdl_glm' predict(object, newdata, ...)## S3 method for class 'mdl_glm' predict(object, newdata, ...)
object |
A fitted |
newdata |
A feature matrix for prediction. |
... |
Additional arguments passed to
|
A numeric vector of predicted response values.
Predict Method for mdl_glmnet Objects
## S3 method for class 'mdl_glmnet' predict(object, newdata = NULL, ...)## S3 method for class 'mdl_glmnet' predict(object, newdata = NULL, ...)
object |
A fitted |
newdata |
A (sparse) feature matrix for prediction. |
... |
Additional arguments passed to
|
A numeric vector of predicted values.
Predict Method for mdl_ranger Objects
## S3 method for class 'mdl_ranger' predict(object, newdata = NULL, ...)## S3 method for class 'mdl_ranger' predict(object, newdata = NULL, ...)
object |
A fitted |
newdata |
A feature matrix for prediction. |
... |
Additional arguments passed to
|
A numeric vector of predicted values (probabilities for probability forests, point predictions for regression forests).
Predict Method for mdl_xgboost Objects
## S3 method for class 'mdl_xgboost' predict(object, newdata = NULL, ...)## S3 method for class 'mdl_xgboost' predict(object, newdata = NULL, ...)
object |
A fitted |
newdata |
A feature matrix for prediction. |
... |
Additional arguments passed to
|
A numeric vector of predicted values.
Predict Method for ols Objects
## S3 method for class 'ols' predict(object, newdata = NULL, ...)## S3 method for class 'ols' predict(object, newdata = NULL, ...)
object |
A fitted |
newdata |
A feature matrix for prediction. If |
... |
Currently unused. |
A numeric vector of predicted values.
Print Stacking Diagnostics
## S3 method for class 'ddml_diagnostics' print(x, digits = 4, ...)## S3 method for class 'ddml_diagnostics' print(x, digits = 4, ...)
x |
An object of class |
digits |
Number of significant digits. Default 4. |
... |
Currently unused. |
x, invisibly.
Creates a regular asymptotically linear (RAL) inference object from pre-computed influence functions. This is the base class for all influence-function-based inference in ddml.
ral( coefficients, inf_func, dinf_dtheta = NULL, nobs, coef_names, cluster_variable = NULL, estimator_name = "RAL estimator", subclass = NULL, ... )ral( coefficients, inf_func, dinf_dtheta = NULL, nobs, coef_names, cluster_variable = NULL, estimator_name = "RAL estimator", subclass = NULL, ... )
coefficients |
A |
inf_func |
A 3D array of dimension
|
dinf_dtheta |
Optional 4D array of dimension
|
nobs |
Integer number of observations. |
coef_names |
Character vector of parameter names (length |
cluster_variable |
Optional vector of cluster identifiers (length |
estimator_name |
Character string for display. |
subclass |
Optional character string prepended to the class vector. |
... |
Additional named elements stored in the object. |
A regular asymptotically linear (RAL) estimator
satisfies
where is the influence
function. This package stores the estimated influence
function
in the inf_func slot.
When an observation-level derivative
is available (stored in dinf_dtheta), the estimator
supports HC3 inference via leverage; see
hatvalues.ral.
The RAL framework is estimator-agnostic: it consumes
pre-computed influence functions and does not prescribe how
they are obtained. For the specific construction under
cross-fitting and Neyman-orthogonal scores, see
ddml-intro. For linear combinations of
ddml estimators, see lincom.
An object of class ral (or c(subclass, "ral")).
Creates a replicated RAL inference object
from a list of ral objects. Provides
cross-resample aggregation for coefficients and
covariance matrices.
ral_rep(fits, subclass = NULL, ...)ral_rep(fits, subclass = NULL, ...)
fits |
A list of at least 2 objects inheriting from
class |
subclass |
Optional character string prepended to the class vector. |
... |
Additional named elements stored in the object. |
An object of class "ral_rep" (or
c(subclass, "ral_rep")).
Predictions using short-stacking.
shortstacking( y, X, learners, sample_folds = 2, ensemble_type = "average", custom_ensemble_weights = NULL, cluster_variable = seq_along(y), subsamples = NULL, silent = FALSE, auxiliary_X = NULL, parallel = NULL )shortstacking( y, X, learners, sample_folds = 2, ensemble_type = "average", custom_ensemble_weights = NULL, cluster_variable = seq_along(y), subsamples = NULL, silent = FALSE, auxiliary_X = NULL, parallel = NULL )
y |
The outcome variable. |
X |
A (sparse) matrix of predictive variables. |
learners |
Omission of the |
sample_folds |
Number of cross-fitting folds. |
ensemble_type |
Ensemble method to combine base learners into final estimate of the conditional expectation functions. Possible values are:
Multiple ensemble types may be passed as a vector of strings. |
custom_ensemble_weights |
A numerical matrix with
user-specified ensemble weights. Each column corresponds to a
custom ensemble specification, each row corresponds to a base
learner in |
cluster_variable |
A vector of cluster indices. |
subsamples |
List of vectors with sample indices for cross-fitting. |
silent |
Boolean to silence estimation updates. |
auxiliary_X |
An optional list of matrices of length
|
parallel |
An optional named list with parallel processing
options. When
|
shortstack returns a list containing the following components:
cf_fittedA matrix of out-of-sample predictions, each column corresponding to an ensemble type (in chronological order).
weightsAn array, providing the weight assigned to each base learner (in chronological order) by the ensemble procedures.
mspeA numeric vector of per-learner out-of-sample MSPEs, computed from cross-fitted residuals.
r2A numeric vector of per-learner out-of-sample R-squared values.
auxiliary_fittedWhen auxiliary_X is not
NULL, a list of matrices with additional predictions.
cf_fitted_bylearnerA matrix of out-of-sample predictions, each column corresponding to a base learner (in chronological order).
cf_resid_bylearnerA matrix of per-learner out-of-sample residuals used for weight estimation.
auxiliary_fitted_bylearnerWhen auxiliary_X is
not NULL, a list of matrices with additional predictions
for each learner.
Note that unlike crosspred, shortstack always computes
out-of-sample predictions for each base learner (at no additional
computational cost).
Ahrens A, Hansen C B, Schaffer M E, Wiemann T (2024). "Model Averaging and Double Machine Learning." Journal of Applied Econometrics, 40(3): 249-269.
Wolpert D H (1992). "Stacked generalization." Neural Networks, 5(2), 241-259.
Other utilities:
crosspred(),
crossval(),
ddml(),
diagnostics(),
ensemble(),
ensemble_weights()
# Construct variables from the included Angrist & Evans (1998) data y = AE98[, "worked"] X = AE98[, c("morekids", "age","agefst","black","hisp","othrace","educ")] # Compute predictions using shortstacking with base learners ols and lasso. # Two stacking approaches are simultaneously computed: Equally # weighted (ensemble_type = "average") and MSPE-minimizing with weights # in the unit simplex (ensemble_type = "nnls1"). Predictions for each # learner are also calculated. shortstack_res <- shortstacking(y, X, learners = list(list(what = ols), list(what = mdl_glmnet)), ensemble_type = c("average", "nnls1", "singlebest"), sample_folds = 2, silent = TRUE) dim(shortstack_res$cf_fitted) # = length(y) by length(ensemble_type) dim(shortstack_res$cf_fitted_bylearner) # = length(y) by length(learners)# Construct variables from the included Angrist & Evans (1998) data y = AE98[, "worked"] X = AE98[, c("morekids", "age","agefst","black","hisp","othrace","educ")] # Compute predictions using shortstacking with base learners ols and lasso. # Two stacking approaches are simultaneously computed: Equally # weighted (ensemble_type = "average") and MSPE-minimizing with weights # in the unit simplex (ensemble_type = "nnls1"). Predictions for each # learner are also calculated. shortstack_res <- shortstacking(y, X, learners = list(list(what = ols), list(what = mdl_glmnet)), ensemble_type = c("average", "nnls1", "singlebest"), sample_folds = 2, silent = TRUE) dim(shortstack_res$cf_fitted) # = length(y) by length(ensemble_type) dim(shortstack_res$cf_fitted_bylearner) # = length(y) by length(learners)
Computes a coefficient table with estimates,
standard errors, z-values, and p-values for all
ensemble types. Standard errors are based on a
heteroskedasticity-robust sandwich variance; see
vcov.ral for the HC0/HC1/HC3 formulas.
## S3 method for class 'ddml' summary(object, type = "HC1", ...) ## S3 method for class 'summary.ddml' print(x, digits = 3, ...)## S3 method for class 'ddml' summary(object, type = "HC1", ...) ## S3 method for class 'summary.ddml' print(x, digits = 3, ...)
object |
An object of class |
type |
Character. HC type ( |
... |
Currently unused. |
x |
An object of class |
digits |
Number of significant digits. Default 3. |
An object of class summary.ddml with:
coefficientsA 3-dimensional array
( nensb) of estimates, standard
errors, z-values, and p-values.
typeThe HC type used.
nobsNumber of observations.
sample_foldsNumber of cross-fitting folds.
ensemble_typeEnsemble type labels.
Other ddml inference:
lincom()
y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace")] plm_fit = ddml_plm(y, D, X, learners = list(what = ols), sample_folds = 2, silent = TRUE) summary(plm_fit) summary(plm_fit, type = "HC3")y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace")] plm_fit = ddml_plm(y, D, X, learners = list(what = ols), sample_folds = 2, silent = TRUE) summary(plm_fit) summary(plm_fit, type = "HC3")
DML-specific summary override. Adds ensemble type labels,
folds, shortstack status to the base ral_rep
summary.
## S3 method for class 'ddml_rep' summary( object, aggregation = c("median", "mean", "spectral"), type = "HC1", ... ) ## S3 method for class 'summary.ddml_rep' print(x, digits = 3, ...)## S3 method for class 'ddml_rep' summary( object, aggregation = c("median", "mean", "spectral"), type = "HC1", ... ) ## S3 method for class 'summary.ddml_rep' print(x, digits = 3, ...)
object |
A |
aggregation |
Character string: |
type |
Character. HC type. Default |
... |
Currently unused. |
x |
An object of class |
digits |
Number of significant digits. Default 3. |
See summary.ral_rep for the aggregation
formulas.
An object of class "summary.ddml_rep".
Chernozhukov V, Chetverikov D, Demirer M, Duflo E, Hansen C B, Newey W, Robins J (2018). "Double/debiased machine learning for treatment and structural parameters." The Econometrics Journal, 21(1), C1-C68.
y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace")] reps = ddml_replicate(ddml_plm, y = y, D = D, X = X, learners = list(what = ols), sample_folds = 2, resamples = 3, silent = TRUE) summary(reps) summary(reps, aggregation = "mean")y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace")] reps = ddml_replicate(ddml_plm, y = y, D = D, X = X, learners = list(what = ols), sample_folds = 2, resamples = 3, silent = TRUE) summary(reps) summary(reps, aggregation = "mean")
Computes a coefficient table with estimates, standard errors, z-values, and p-values.
## S3 method for class 'ral' summary(object, type = "HC1", ...) ## S3 method for class 'summary.ral' print(x, digits = 3, ...)## S3 method for class 'ral' summary(object, type = "HC1", ...) ## S3 method for class 'summary.ral' print(x, digits = 3, ...)
object |
An object inheriting from class |
type |
Character. HC type. Default |
... |
Currently unused. |
x |
An object of class |
digits |
Number of significant digits. Default 3. |
An object of class summary.ral with:
coefficientsA 3-dimensional array
( nfit).
typeThe HC type used.
nobsNumber of observations.
Aggregates coefficient estimates and covariance matrices across independent replications.
## S3 method for class 'ral_rep' summary( object, aggregation = c("median", "mean", "spectral"), type = "HC1", ... ) ## S3 method for class 'summary.ral_rep' print(x, digits = 3, ...)## S3 method for class 'ral_rep' summary( object, aggregation = c("median", "mean", "spectral"), type = "HC1", ... ) ## S3 method for class 'summary.ral_rep' print(x, digits = 3, ...)
object |
An object inheriting from class
|
aggregation |
Character string. Aggregation rule. |
type |
Character. HC type. Default |
... |
Currently unused. |
x |
An object of class |
digits |
Number of significant digits. Default 3. |
Let and denote
the coefficient vector and sandwich covariance matrix
from replication .
Coefficient aggregation.
For "mean":
.
For "median" and "spectral":
.
Covariance aggregation. Define the inflated per-replication covariance as
For "mean":
.
For "median":
.
For "spectral":
solved via CVXR, guaranteeing PSD.
An object of class "summary.ral_rep".
Chernozhukov V, Chetverikov D, Demirer M, Duflo E, Hansen C B, Newey W, Robins J (2018). "Double/debiased machine learning for treatment and structural parameters." The Econometrics Journal, 21(1), C1-C68.
DML-specific tidy method. Adds ensemble_type
column labeling based on the estimator's learner/ensemble
configuration. Delegates to tidy.ral for the base
table computation.
## S3 method for class 'ddml' tidy( x, ensemble_idx = 1, conf.int = FALSE, conf.level = 0.95, type = "HC1", uniform = FALSE, bootstraps = 999L, ... )## S3 method for class 'ddml' tidy( x, ensemble_idx = 1, conf.int = FALSE, conf.level = 0.95, type = "HC1", uniform = FALSE, bootstraps = 999L, ... )
x |
A |
ensemble_idx |
Integer index of the ensemble type to
report. Defaults to 1. Set to |
conf.int |
Logical. Include confidence intervals?
Default |
conf.level |
Confidence level. Default 0.95. |
type |
Character. HC type. Default |
uniform |
Logical. Uniform CIs? Default |
bootstraps |
Integer. Bootstrap draws. Default 999. |
... |
Currently unused. |
A data.frame with columns term,
estimate, std.error, statistic,
p.value, and ensemble_type.
y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace")] plm_fit = ddml_plm(y, D, X, learners = list(what = ols), sample_folds = 2, silent = TRUE) tidy(plm_fit) tidy(plm_fit, conf.int = TRUE)y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace")] plm_fit = ddml_plm(y, D, X, learners = list(what = ols), sample_folds = 2, silent = TRUE) tidy(plm_fit) tidy(plm_fit, conf.int = TRUE)
Returns a flat data.frame of per-learner stacking
diagnostics for all nuisance equations. Suitable for
table creation with kable(), gt(), or
modelsummary::datasummary().
## S3 method for class 'ddml_diagnostics' tidy(x, ...)## S3 method for class 'ddml_diagnostics' tidy(x, ...)
x |
An object of class |
... |
Currently unused. |
A data.frame with columns equation, learner,
mspe, r2, weight, and optionally cvc_pval.
y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace")] learners = list(list(what = ols), list(what = mdl_glmnet)) plm_fit = ddml_plm(y, D, X, learners = learners, sample_folds = 2, silent = TRUE) tidy(diagnostics(plm_fit, cvc = TRUE))y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace")] learners = list(list(what = ols), list(what = mdl_glmnet)) plm_fit = ddml_plm(y, D, X, learners = learners, sample_folds = 2, silent = TRUE) tidy(diagnostics(plm_fit, cvc = TRUE))
DML-specific tidy method. Adds ensemble_type and
aggregation columns. Delegates to
tidy.ral_rep for the base table computation.
## S3 method for class 'ddml_rep' tidy( x, ensemble_idx = 1, aggregation = c("median", "mean", "spectral"), type = "HC1", conf.int = FALSE, conf.level = 0.95, uniform = FALSE, bootstraps = 999L, ... )## S3 method for class 'ddml_rep' tidy( x, ensemble_idx = 1, aggregation = c("median", "mean", "spectral"), type = "HC1", conf.int = FALSE, conf.level = 0.95, uniform = FALSE, bootstraps = 999L, ... )
x |
A |
ensemble_idx |
Integer index of the ensemble type
to report. Defaults to 1. Set to |
aggregation |
Character string. Aggregation method. |
type |
Character. HC type. Default |
conf.int |
Logical. Include CIs? Default
|
conf.level |
Confidence level. Default 0.95. |
uniform |
Logical. Uniform CIs? Default
|
bootstraps |
Integer. Bootstrap draws. Default 999. |
... |
Currently unused. |
A data.frame with columns term,
estimate, std.error, statistic,
p.value, ensemble_type, and
aggregation.
summary.ddml_rep for the
aggregation equations.
y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace")] reps = ddml_replicate(ddml_plm, y = y, D = D, X = X, learners = list(what = ols), sample_folds = 2, resamples = 3, silent = TRUE) tidy(reps) tidy(reps, conf.int = TRUE)y = AE98[, "worked"] D = AE98[, "morekids"] X = AE98[, c("age","agefst","black","hisp","othrace")] reps = ddml_replicate(ddml_plm, y = y, D = D, X = X, learners = list(what = ols), sample_folds = 2, resamples = 3, silent = TRUE) tidy(reps) tidy(reps, conf.int = TRUE)
Extracts coefficient estimates, standard errors, test statistics, and p-values in a tidy data frame.
## S3 method for class 'ral' tidy( x, fit_idx = 1, conf.int = FALSE, conf.level = 0.95, type = "HC1", uniform = FALSE, bootstraps = 999L, ... )## S3 method for class 'ral' tidy( x, fit_idx = 1, conf.int = FALSE, conf.level = 0.95, type = "HC1", uniform = FALSE, bootstraps = 999L, ... )
x |
An object inheriting from class |
fit_idx |
Integer index of the fit to report.
Defaults to 1. Set to |
conf.int |
Logical. Include confidence intervals?
Default |
conf.level |
Confidence level. Default 0.95. |
type |
Character. HC type. Default |
uniform |
Logical. Uniform confidence bands?
Default |
bootstraps |
Integer. Bootstrap draws. Default 999. |
... |
Currently unused. |
A data.frame with columns term,
estimate, std.error, statistic,
p.value, and fit_label.
Chernozhukov V, Chetverikov D, Kato K (2013). "Gaussian approximations and multiplier bootstrap for maxima of sums of high-dimensional random vectors." Annals of Statistics, 41(6), 2786-2819.
Tidy a RAL Rep Object
## S3 method for class 'ral_rep' tidy( x, fit_idx = 1, aggregation = c("median", "mean", "spectral"), type = "HC1", conf.int = FALSE, conf.level = 0.95, uniform = FALSE, bootstraps = 999L, ... )## S3 method for class 'ral_rep' tidy( x, fit_idx = 1, aggregation = c("median", "mean", "spectral"), type = "HC1", conf.int = FALSE, conf.level = 0.95, uniform = FALSE, bootstraps = 999L, ... )
x |
An object inheriting from class |
fit_idx |
Integer index of the fit. Defaults to 1.
Set to |
aggregation |
Character string. Aggregation rule. |
type |
Character. HC type. Default |
conf.int |
Logical. Include CIs? Default
|
conf.level |
Confidence level. Default 0.95. |
uniform |
Logical. Uniform CIs? Default
|
bootstraps |
Integer. Bootstrap draws. Default 999. |
... |
Currently unused. |
A data.frame with columns term,
estimate, std.error, statistic,
p.value, fit_label, and
aggregation.
Computes a heteroskedasticity-robust variance-covariance matrix.
## S3 method for class 'ral' vcov(object, fit_idx = 1, type = "HC1", ...)## S3 method for class 'ral' vcov(object, fit_idx = 1, type = "HC1", ...)
object |
An object inheriting from class |
fit_idx |
Integer index of the fit. Defaults to 1. |
type |
Character. One of |
... |
Currently unused. |
Let denote the estimated
influence function at observation . Three
variance estimators are available:
HC0:
HC1 (default):
HC3:
where is the leverage;
see hatvalues.ral.
Cluster-robust inference. When
cluster_variable is non-NULL and identifies
fewer groups than observations, the observation-level
influence functions are aggregated to cluster-level
influence functions
and the variance is computed as
A variance-covariance
matrix.
Variance-Covariance Matrix for RAL Rep Objects
## S3 method for class 'ral_rep' vcov( object, fit_idx = 1, aggregation = c("median", "mean", "spectral"), type = "HC1", ... )## S3 method for class 'ral_rep' vcov( object, fit_idx = 1, aggregation = c("median", "mean", "spectral"), type = "HC1", ... )
object |
An object inheriting from class
|
fit_idx |
Integer index of the fit. Defaults to 1. |
aggregation |
Character string. Aggregation rule. |
type |
Character. HC type. Default |
... |
Currently unused. |
A variance-covariance
matrix.