Skip to main content

Crate rdrobust_core

Crate rdrobust_core 

Source
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_batch and rdrobust_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_bw returns (R functions.R:359): the variance, bias, regularization, and rate ingredients of the MSE/CER bandwidth formula.
Bws
Selected bandwidths: h/b for the left and right sides (common selectors duplicate the scalar into both sides).
Dataset
One dataset in a batch: sharp design when t is None, 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 rdrobust results, one row per inference method (rows are Conventional, Bias-Corrected, Robust; ports rdrobust.R:854-863).
RdRobustOptions
Scalar/config options for rdrobust_with, a typed alternative to the raw rdrobust positional 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 raw rdrobust/rdrobust_with path would otherwise carry into NaN results. 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 vce argument).

Functions§

kweight
Computes local-polynomial kernel weights w_i = K((x_i - c)/h) / h, porting rdrobust_kweight (functions.R:134-144).
rdbwselect
Select the RD bandwidths h (point estimate) and b (bias correction) by running the ~14 rdrobust_bw pilot fits and combining their V/B/R per the chosen criterion (R rdbwselect.R). x must be sorted ascending when vce = 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_with on 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 the V/B/R/rate ingredients of the MSE/CER bandwidth formula.
rdrobust_multi_cutoff
Runs crate::rdrobust::rdrobust_with at 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 in RdRobustOptions, 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: run validate_rdrobust_inputs first and, when every check passes, forward verbatim to rdrobust_with and return Ok(result). On any degenerate input or option it returns the first matching RdError instead of letting the raw path produce NaN. The numerics on valid inputs are identical to rdrobust_with; only the rejection of bad inputs differs.