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):
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:
Positivity: every treatment level has positive probability conditional on covariates:
Under these conditions:
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:
where m(d,x) = E[Yi|Di=d, Xi=x] is the outcome regression. The doubly robust estimator solves Pn[φd] = 0
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
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
6 Plotting and Inference
Listing 3: Plot ADRF with confidence bands
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
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
- 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.
- Kennedy, E. H. (2017). Nonparametric causal effects based on incremental propensity score interventions. Journal of the American Statistical Association, 114(526):645-656.\
- 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.
- Robins, J. M., Hernán, M. A., and Brumback, B. (2000). Marginal structural models and causal inference in epidemiology. Epidemiology, 11(5):550-560.
- van der Laan, M. J. and Rose, S. (2011). Targeted Learning: Causal Inference for Observational and Experimental Data. Springer.
- 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.