Nonparametric Conditional Density Estimation with the Highly Adaptive Lasso

Author: Nima Hejazi

## 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).

## Installation

Install the most recent stable release from GitHub via devtools:

devtools::install_github("nhejazi/haldensify")

## Example

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

library(data.table)
library(hal9001)
library(haldensify)
library(tidyverse)
set.seed(76924)

# 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 <- as.data.table(list(a = 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 ## Issues

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

## Contributions

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

## Citation

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

    @manual{hejazi2019haldensify,
author = {Hejazi, Nima S},
title = {haldensify: Nonparametric conditional density estimation
with the highly adaptive lasso in {R}},
year  = {2019},
url = {https://github.com/nhejazi/haldensify},
note = {R package version 0.0.3}
}

## License

© 2019 Nima S. Hejazi

The contents of this repository are distributed under the MIT license. See below for details:

MIT License

Copyright (c) 2019 Nima S. Hejazi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.