Toolbox

The npcausal Package in R: Nonparametric Dose-Response Estimation for Continuous Treatments

1 What Problem Does This Tool Solve?

Most causal inference estimators are designed for binary treatments: treatment on or off, enrolled or not enrolled, above or below a threshold. But many important policy variables are continuous: the level of pollution exposure, the number of years of education, the dollar amount of a subsidy, the dose of a medication.

For continuous treatments, the natural causal estimand is the Average Dose-Response Function (ADRF):

μ(d) = 𝔼[Yi(d)],     d𝔻, (1)

where Yi(d) is the potential outcome under treatment level d and D is the support of the continuous treatment. The ADRF traces how the average outcome changes across all possible treatment levels a curve rather than a single scalar.

The npcausal package in R implements the doubly robust, nonparametric ADRF estimator proposed by Kennedy [2017]. The key innovations are: (a) double robustness the estimator remains consistent if either the outcome model or the treatment density model is correctly specified; (b) flexibility both nuisance models can be estimated using machine learning; and (c) valid inference the package provides confidence bands for the entire ADRF curve, not just pointwise intervals.

2 The ADRF Estimand Under Unconfoundedness

Identification of the ADRF requires the following conditions:

Weak unconfoundedness [Hirano and Imbens, 2004]: conditional on observed covariates Xi the potential outcome under any dose d is independent of the actual treatment received:

Yi(d) Di | Xi,     ∀d𝔻. (2)

Positivity: every treatment level has positive probability conditional on covariates:

fD|X(d | x) > 0,     ∀d𝔻, x𝕏. (3)

Under these conditions:

μ(d) = 𝔼
fD(d)
fD|X(d | Xi)
Yi] = 𝔼[𝔼[Yi | Di = d, Xi]], (4)

where fD|X(d|x) is the conditional density of D given X (the generalised propensity score).

3 The Kennedy (2017) Doubly Robust Estimator

The doubly robust estimator for μ(d) uses the influence function of the ADRF functional. Define the effective dose-response influence function:

φd(Yi, Di, Xi; μ, f) =
fD(d)
fD|X(d | Xi)
(Yi − m(Di, Xi)) + m(d, Xi) − μ(d), (5)

where m(d,x) = E[Yi|Di=d, Xi=x] is the outcome regression. The doubly robust estimator solves Pn[φd] = 0

^μ(d) =
1
n
n i=1
[
^fD(d)
^fD|X(d | Xi)
(Yi^m(Di, Xi)) + ^m(d, Xi) ], (6)

where m and f̂D|X are estimated nuisance functions, and f̂D is the marginal density of D.

The estimator achieves the nonparametric efficiency bound and, crucially, is doubly robust: it remains consistent if either m or f̂D|X is consistent, even if the other is misspecified. When both are consistent, it achieves a √n rate of convergence even if the nuisance rates are slower (e.g., n1/3 from nonparametric estimation).

Kennedy [2017] implement this using cross-fitting (sample splitting), where nuisance models are estimated on a held-out fold and applied to the remaining fold, removing the bias from overfitting.

4 Installation and Setup

Listing 1: Install npcausal from GitHub

#npcausal is available on GitHub (Kennedy, CMU) #install.packages("devtools") devtools::install_github("ehkennedy/npcausal") library(npcausal) # Also requires: library(ranger) # Random forest for outcome model library(np) # Nonparametric density estimation

5 Minimal Working Example: Air Pollution and Health

We simulate data inspired by the EPA air quality literature, where the treatment is continuous PM2.5 exposure:

Listing 2: Simulated dose-response: pollution and mortality

library(npcausal) set.seed(2026) n <- 1000 # Covariates: income (X1) and age (X2) X <- data.frame(income = rnorm(n, 50, 10), age = rnorm(n, 45, 12)) # Continuous treatment: PM2.5 (ug/m^3), depends on income D <- 12 + (-0.1) * X$income + rnorm(n, sd=3) D <- pmax(D, 2) # truncate at 2 ug/m^3 # Outcome: annual mortality rate (per 1000) # True ADRF: mu(d) = 8 + 0.3*d + 0.005*d^2 (nonlinear) Y <- 8 + 0.3*D + 0.005*D^2 + 0.05*X$income - 0.02*X$age + rnorm(n, sd=1.5) # Evaluate ADRF at a grid of treatment values d_grid <- seq(5, 25, length.out=50) # Fit doubly robust ADRF adrf_fit <- adrf(y=Y, a=D, x=X, a.vals = d_grid, nsplits=5, # 5-fold cross-fitting sl.lib = c("SL.ranger", "SL.glm")) # SuperLearner with random forest + GLM

6 Plotting and Inference

Listing 3: Plot ADRF with confidence bands

# Plot the estimated ADRF plot(adrf_fit, xlab = expression(paste("PM"[2.5], " concentration(ug/m^3)")), ylab = "Expected mortality rate (per 1000)", main = "Doubly Robust Average Dose-Response Function") abline(v=12, lty=2, col="gray") # WHO guideline # Extract estimates and confidence intervals adrf_df <- data.frame( d = d_grid, est = adrf_fit$est, lwr = adrf_fit$ci[,1], upr = adrf_fit$ci[,2] ) head(adrf_df)

The output provides a 95% pointwise confidence band for μ̂(d) at each value in d grid. For uniform confidence bands valid simultaneously across all d, bootstrap the entire procedure.

7 Key Options and Pitfalls

7.1 Choice of Machine Learners

The doubly robust ADRF estimator allows any consistent estimator for m(d,x) and fD|X(d|x). In practice:

  • Outcome model m̂(d,x): use Super Learner with SL.ranger (random forest), SL.xgboost, and SL.glm as candidates.
  • Treatment density f̂D|X(d|x): use kernel regression (np package) or conditional normalising flows for complex distributions. Avoid parametric normal assumptions if D|X is skewed.

7.2 Cross-Fitting (nsplits)

Use at least 5-fold cross-fitting (nsplits=5). More splits reduce overfitting bias at the cost of smaller training sets per fold. With small n (<500), 2-fold cross-fitting may be necessary.

7.3 Positivity Violations

When f̂D|X(d|Xi) ≈ 0 for some observations, the IPW weights explode. Trim extreme weights (e.g., cap at the 99th percentile) and restrict the evaluation grid a.vals to the interior of the treatment support where positivity is plausible.

7.4 Bandwidth for Density Estimation

The np package selects bandwidth for f̂D|X(d|x) automatically via least-squares cross-validation, but this can be slow for large n. A practical shortcut: use a Gaussian kernel with bandwidth selected by Silverman's rule as a starting value.

8 Comparison to Alternatives

Package Estimand Doubly robust ML nuisance ADRF curve
npcausal ADRF Yes Yes Yes
contdid ADRF (staggered) Yes Partial Yes
CausalGPS ADRF Partial Yes (CBPS) Yes
GPS parametric ADRF No No Yes
grf (causal forest) CATE (binary) Yes Yes No

npcausal is the most principled choice when: (a) treatment is truly continuous, (b) the ADRF is potentially nonlinear, (c) unconfoundedness is credible, and (d) one wants doubly robust inference with valid confidence bands.

9 Conclusion

The npcausal package implements the state-of-the-art doubly robust ADRF estimator for continuous treatments. By combining cross-fitting with machine learning nuisance estimation, it achieves efficiency and robustness to nuisance misspecification simultaneously. The key workflow is: estimate the ADRF on a grid of treatment values using adrf(), plot the resulting curve with pointwise confidence bands, and check for positivity violations by inspecting the conditional treatment density. For researchers studying environmental exposures, drug dosing, financial interventions, or any other continuous-valued causal variable, npcausal provides a rigorous and flexible estimation framework.

References

  1. Hirano, K. and Imbens, G. W. (2004). The propensity score with continuous treatments. In Gelman, A. and Meng, X.-L., editors, Applied Bayesian Modeling and Causal Inference from Incomplete-Data Perspectives, pages 73-84. Wiley.
  2. Kennedy, E. H. (2017). Nonparametric causal effects based on incremental propensity score interventions. Journal of the American Statistical Association, 114(526):645-656.\
  3. Imai, K. and van Dyk, D. A. (2004). Causal inference with general treatment regimes: Generalizing the propensity score. Journal of the American Statistical Association, 99(467):854-866.
  4. Robins, J. M., Hernán, M. A., and Brumback, B. (2000). Marginal structural models and causal inference in epidemiology. Epidemiology, 11(5):550-560.
  5. van der Laan, M. J. and Rose, S. (2011). Targeted Learning: Causal Inference for Observational and Experimental Data. Springer.
  6. Wu, X., Braun, D., Schwartz, J., Kioumourtzoglou, M. A., and Dominici, F. (2020). Evaluating the impact of long-term exposure to fine particulate matter on cardiovascular and respiratory disease incidence. Science Advances, 6(29):eaba5986.

Continue Reading

Browse All Sections →
Home
This is some text inside of a div block.
This is some text inside of a div block.
This is some text inside of a div block.

Article Title