Expand description
Pure-Rust numerical core for robust local-polynomial regression-discontinuity
(RD) estimation: a faithful port of the Calonico-Cattaneo-Farrell-Titiunik
rdrobust package (R source R/rdrobust/R/{functions,rdrobust,rdbwselect,rdplot}.R).
§What it computes
rdrobust(): the sharp or fuzzy RD point estimate, with conventional and bias-corrected coefficients, CCT robust variance, z-statistics, p-values, and confidence intervals.rdbwselect: MSE- and CER-optimal bandwidth selection (all ten selectors).rdplot(): the binned-scatter quantities and the global polynomial curve.rdrobust_batchandrdrobust_multi_cutoff: rayon-parallel fan-out over independent fits for Monte-Carlo and many-cutoff workloads.
Outputs match the R reference to ~1e-8 on continuous quantities and ~1e-11 on clustered variances, and exactly on discrete decisions (neighbor sets, mass-point grouping, bin counts).
§Example
use rdrobust_core::{rdrobust_batch, Dataset, RdRobustOptions};
// A small sharp RD: the outcome jumps by about 2 at the cutoff c = 0.
let x: Vec<f64> = (0..200).map(|i| -1.0 + 2.0 * (i as f64 + 0.5) / 200.0).collect();
let y: Vec<f64> = x
.iter()
.enumerate()
.map(|(i, &xi)| 0.5 * xi + if xi >= 0.0 { 2.0 } else { 0.0 } + 0.3 * (i as f64).sin())
.collect();
let opts = RdRobustOptions::default(); // R defaults: p=1, q=2, triangular kernel, nn variance
let ds = Dataset { y: &y, x: &x, t: None, z: None, dz: 0, cluster: None, weights: None };
let out = rdrobust_batch(&[ds], &opts);
// Each result's rows are [Conventional, Bias-Corrected, Robust].
assert_eq!(out.len(), 1);
assert!(out[0].coef[0].is_finite());
assert!(out[0].se[2] >= 0.0);§Scope and limitations
The main output set (coef/se/z/pv/ci/bws) is complete. Per-side
decompositions, fuzzy first-stage diagnostics, and the rdplot QS / covariate /
CI extensions are not ported. The raw rdrobust/rdrobust_with entry points
still return NaN on degenerate inputs (a cutoff outside the support, a fuzzy
treatment with no first stage); try_rdrobust_with adds a typed validation
channel (RdError) that rejects those inputs up front. See CHANGELOG.md
for the full list.
Structs§
- BwParts
- The four scalars
rdrobust_bwreturns (R functions.R:359): the variance, bias, regularization, and rate ingredients of the MSE/CER bandwidth formula. - Bws
- Selected bandwidths:
h/bfor the left and right sides (common selectors duplicate the scalar into both sides). - Dataset
- One dataset in a batch: sharp design when
tisNone, fuzzy otherwise. - RdPlot
- Numeric outputs behind an RD plot: selected bin counts, global polynomial curve coefficients, and per-bin scatter means for each side.
- RdRobust
- Headline
rdrobustresults, one row per inference method (rows are Conventional, Bias-Corrected, Robust; ports rdrobust.R:854-863). - RdRobust
Options - Scalar/config options for
rdrobust_with, a typed alternative to the rawrdrobustpositional arguments. The data inputs (y,x,fuzzy,covs,dz,cluster,weights) stay separate; this struct carries everything else.
Enums§
- Kernel
- Local-polynomial weighting kernel (mirrors
rdrobust_kweight). - RdError
- Validation failure for
try_rdrobust_with: a degenerate input or option that the rawrdrobust/rdrobust_withpath would otherwise carry intoNaNresults. Each variant mirrors a check the Python binding already performs, with the same message text, so the two channels agree on what they reject. - Vce
- Variance-covariance estimator (mirrors the
vceargument).
Functions§
- kweight
- Computes local-polynomial kernel weights
w_i = K((x_i - c)/h) / h, portingrdrobust_kweight(functions.R:134-144). - rdbwselect
- Select the RD bandwidths
h(point estimate) andb(bias correction) by running the ~14rdrobust_bwpilot fits and combining theirV/B/Rper the chosen criterion (R rdbwselect.R).xmust be sorted ascending whenvce = nn. - rdplot
- Computes the data behind an RD plot for the evenly-spaced (ES) binselect family:
the global polynomial curve coefficients, the IMSE/MV bin counts, and the per-bin
scatter means. Ports R
rdplot(rdplot.R), ES half only, no covariates and no CI. - rdrobust
- Port of
rdrobust(rdrobust.R:540-907) for the scoped option set: the RD point estimate (conventional and bias-corrected) with CCT robust variance and confidence intervals. - rdrobust_
batch - Runs
crate::rdrobust::rdrobust_withon many datasets in parallel. - rdrobust_
bw - Port of
rdrobust_bw(R functions.R:213-361) for the sharp/fuzzy, optional-covariate, nn/hc0-hc3/crv2/crv3 case: the per-side local-polynomial fit engine producing theV/B/R/rateingredients of the MSE/CER bandwidth formula. - rdrobust_
multi_ cutoff - Runs
crate::rdrobust::rdrobust_withat many cutoffs on one dataset. - rdrobust_
with - Ergonomic, options-struct entry point to
rdrobust: the seven data inputs stay explicit while every scalar/config knob lives inRdRobustOptions, so a caller no longer threads 27 positional arguments (where swapping two bools/strings silently changes the estimate). - try_
rdrobust_ with - Validated wrapper over
rdrobust_with: runvalidate_rdrobust_inputsfirst and, when every check passes, forward verbatim tordrobust_withand returnOk(result). On any degenerate input or option it returns the first matchingRdErrorinstead of letting the raw path produceNaN. The numerics on valid inputs are identical tordrobust_with; only the rejection of bad inputs differs.