Skip to main content

Crate regression_diagnostics

Crate regression_diagnostics 

Source
Expand description

§regression-diagnostics

Statistical diagnostics for ordinary least squares regression models in Rust — the surface R’s car/lmtest and Python’s statsmodels expose, in a dependency-light crate. It fills the specific ecosystem gap that no maintained Rust crate offers Variance Inflation Factor, adjusted R², or residual diagnostics (autocorrelation, heteroskedasticity, normality, influence).

Everything operates on a single fitted-model type, OlsFit, computed once and reused by every diagnostic.

§Scope

The core is OLS diagnostics, whose closed-form structure (a well-defined hat matrix and Gaussian residuals) is what makes them well-defined. Two further model families each get their own module, because their diagnostics are genuinely different — not reskinned OLS formulas:

  • regularized — ridge, lasso, elastic net, and penalized logistic, where the hat matrix changes (H_λ = X(XᵀX + λI)⁻¹Xᵀ) or does not exist in closed form; diagnostics are built on effective degrees of freedom and the active set.
  • logistic — binary logistic regression, a non-Gaussian likelihood with deviance/Pearson residuals, pseudo-R², and the Hosmer–Lemeshow test.
  • glm — the other exponential-family GLMs (Poisson, negative binomial, Gamma) behind one IRLS solver and a Family trait, with the same deviance/Pearson residual, dispersion, and influence diagnostics.
  • categorical — multinomial and ordinal (proportional-odds) logistic for multi-class responses, each reducing to binary logistic at K = 2.
  • survival — Cox proportional hazards (stratified and time-varying too), parametric accelerated-failure-time models, and Kaplan–Meier for censored time-to-event data, with martingale/deviance/Schoenfeld residuals.
  • mixed — random-intercept, random-slope, crossed and generalized (Laplace GLMM) mixed models for grouped data, with variance components, ICC, and BLUPs.

§Linear algebra

Internals use nalgebra (pure-Rust QR/SVD, no system BLAS — chosen for portability); the public API speaks ndarray. That boundary and its tradeoff are documented in the README.

§Layout

§Quick start

use ndarray::array;
use regression_diagnostics::OlsFit;
use regression_diagnostics::multicollinearity::vif;

// Caller supplies the intercept column (first column of ones).
let x = array![
    [1.0, 1.0, 2.0],
    [1.0, 2.0, 4.1],
    [1.0, 3.0, 5.9],
    [1.0, 4.0, 8.0],
    [1.0, 5.0, 10.1],
];
let y = array![2.0, 4.1, 6.1, 8.0, 10.2];
let fit = OlsFit::new(x, y).unwrap();

println!("{}", fit.summary());     // full statsmodels-style report
let v = vif(&fit);                 // per-predictor VIF (NaN for intercept)
assert!(v[1].is_finite());

Re-exports§

pub use error::RegressionError;
pub use error::Result;

Modules§

categorical
Diagnostics for categorical responses with more than two classes — the two GLMs that generalize binary logistic to a multi-class outcome.
coefficients
Coefficient-level utilities.
error
Error type shared across every diagnostic in the crate.
fit_statistics
Summary-level fit statistics: r_squared, adjusted_r_squared, f_statistic, and the likelihood-based aic / bic / log_likelihood.
glm
Diagnostics for generalized linear models beyond binary logistic — the exponential-dispersion families that share one iteratively-reweighted least-squares (IRLS) engine but differ in link, variance, and residual scale.
influence
Influence diagnostics: which individual observations disproportionately move the fit.
logistic
Diagnostics for binary logistic regression — a different statistical framework from OLS, not an extension of it, and provided here as its own self-contained set of types.
mixed
Mixed / hierarchical linear models — regression for grouped (clustered, repeated-measures, multilevel) data, where observations within a group are correlated and ordinary least squares would understate the uncertainty.
multicollinearity
Multicollinearity diagnostics: vif and condition_number.
regularized
Diagnostics for regularized linear regression — the family the OLS diagnostics deliberately excluded, now provided as first-class types.
residuals
Residual diagnostics: the scaled residual forms plus the classic assumption-checking tests.
survival
Survival / time-to-event diagnostics — a framework apart from the regression models elsewhere in the crate, because the response is a (possibly right-censored) time and the likelihood is built around risk sets rather than residual variance.

Structs§

CoefficientRow
One row of the coefficient table.
OlsFit
A fitted ordinary-least-squares model: the shared representation every diagnostic in this crate is computed from.
Summary
A structured snapshot of every diagnostic in the crate for one fit.