Nonparametric Conditional Density Estimation with the Highly Adaptive Lasso

Authors: Nima Hejazi, David Benkeser, and Mark van der Laan

What’s haldensify?

The haldensify R package is designed to provide facilities for nonparametric conditional density estimation based on the procedure proposed by Díaz and van der Laan (2011). The core of the implemented methodology involves recovering conditional density estimates by performing pooled hazards regressions so as to assess the conditional hazard that an observation falls in a given bin over the support of the variable of interest. Such conditional density estimates are required to estimate the propensity score when the intervention variable considered is continuous (Díaz and van der Laan 2012, 2018; Díaz and Hejazi 2019). Though future generalization of the core routines may be possible, for the time being, haldensify is a minimal implementation of this strategy for use only with the highly adaptive lasso (Benkeser and van der Laan 2016; van der Laan 2017; van der Laan and Benkeser 2018; Coyle and Hejazi 2018).


Install the most recent version from the master branch on GitHub via remotes:



A simple example illustrates how haldensify may be used to construct conditional density estimates:


# simulate data: W ~ U[-4, 4] and A|W ~ N(mu = W, sd = 0.5)
n_train <- 100
w <- runif(n_train, -4, 4)
a <- rnorm(n_train, w, 0.5)

# learn relationship A|W using HAL-based density estimation procedure
mod_haldensify <- haldensify(
  A = a, W = w,
  n_bins = c(5, 10, 15),
  lambda_seq = exp(seq(-1, -13, length = 300))

# predictions to recover conditional density of A|W
new_a <- seq(-4, 4, by = 0.01)
new_w_neg <- rep(-2, length(new_a))
new_w_zero <- rep(0, length(new_a))
new_w_pos <- rep(2, length(new_a))
new_dat <- = new_a, w_neg = new_w_neg,
                              w_null = new_w_zero, w_pos = new_w_pos))
new_dat$pred_w_neg <- predict(mod_haldensify,
                              new_A = new_dat$a, new_W = new_dat$w_neg)
new_dat$pred_w_null <- predict(mod_haldensify,
                               new_A = new_dat$a, new_W = new_dat$w_null)
new_dat$pred_w_pos <- predict(mod_haldensify,
                              new_A = new_dat$a, new_W = new_dat$w_pos)

# visualize results
p <- new_dat %>%
  melt(id = c("a"), measure.vars =
       c("pred_w_pos", "pred_w_null", "pred_w_neg")) %>%
  ggplot(aes(x = a, y = value, colour = variable)) +
  geom_point() +
  geom_line() +
  stat_function(fun = dnorm, args = list(mean = -2, sd = 0.5),
                colour = "blue", linetype = "dashed") +
  stat_function(fun = dnorm, args = list(mean = 0, sd = 0.5),
                colour = "darkgreen", linetype = "dashed") +
  stat_function(fun = dnorm, args = list(mean = 2, sd = 0.5),
                colour = "red", linetype = "dashed") +
  xlab("Observed value") +
  ylab("Predicted probability") +
  ggtitle("Conditional density p(A|W)") +
  theme_bw() +
  theme(legend.position = "none")
p %>% print()


If you encounter any bugs or have any specific feature requests, please file an issue.


Contributions are very welcome. Interested contributors should consult our contribution guidelines prior to submitting a pull request.


After using the haldensify R package, please cite the following:

      author = {Hejazi, Nima S and Benkeser, David C and {van der Laan},
        Mark J},
      title = {{haldensify}: Conditional density estimation with the highly
        adaptive lasso},
      year  = {2019},
      url = {},
      note = {R package version 0.0.4}


Benkeser, David, and Mark J van der Laan. 2016. “The Highly Adaptive Lasso Estimator.” In *Proceedings of the International Conference on Data Science and Advanced Analytics. IEEE International Conference on Data Science and Advanced Analytics*, 2016:689. NIH Public Access.
Coyle, Jeremy R, and Nima S Hejazi. 2018. *hal9001: The Scalable Highly Adaptive LASSO*. .
Díaz, Iván, and Nima S Hejazi. 2019. “Causal Mediation Analysis for Stochastic Interventions.” *Submitted*. .
Díaz, Iván, and Mark J van der Laan. 2011. “Super Learner Based Conditional Density Estimation with Application to Marginal Structural Models.” *The International Journal of Biostatistics* 7 (1). De Gruyter: 1–20.
———. 2012. “Population Intervention Causal Effects Based on Stochastic Interventions.” *Biometrics* 68 (2). Wiley Online Library: 541–49.
———. 2018. “Stochastic Treatment Regimes.” In *Targeted Learning in Data Science: Causal Inference for Complex Longitudinal Studies*, 167–80. Springer Science & Business Media.
van der Laan, Mark J. 2017. “A Generally Efficient Targeted Minimum Loss Based Estimator Based on the Highly Adaptive Lasso.” *The International Journal of Biostatistics* 13 (2). De Gruyter.
van der Laan, Mark J, and David Benkeser. 2018. “Highly Adaptive Lasso (HAL).” In *Targeted Learning in Data Science*, 77–94. Springer.