Expand description
§linreg-core
A lightweight, self-contained linear regression library in pure Rust.
No external math dependencies. All linear algebra (matrices, QR decomposition) and statistical functions (distributions, hypothesis tests) are implemented from scratch. Compiles to WebAssembly for browser use or runs as a native Rust crate.
§What This Does
- OLS Regression — Ordinary Least Squares with numerically stable QR decomposition
- Regularized Regression — Ridge, Lasso, and Elastic Net via coordinate descent
- Diagnostic Tests — 8+ statistical tests for validating regression assumptions
- WASM Support — Same API works in browsers via WebAssembly
§Quick Start
§Native Rust
Add to Cargo.toml (no WASM overhead):
[dependencies]
linreg-core = { version = "0.2", default-features = false }use linreg_core::core::ols_regression;
let y = vec![2.5, 3.7, 4.2, 5.1, 6.3];
let x1 = vec![1.0, 2.0, 3.0, 4.0, 5.0];
let x2 = vec![2.0, 4.0, 5.0, 4.0, 3.0];
let names = vec!["Intercept".into(), "Temp".into(), "Pressure".into()];
let result = ols_regression(&y, &[x1, x2], &names)?;
println!("R²: {}", result.r_squared);
println!("F-statistic: {}", result.f_statistic);§WebAssembly (JavaScript)
[dependencies]
linreg-core = "0.2"Build with wasm-pack build --target web, then use in JavaScript:
import init, { ols_regression } from './linreg_core.js';
await init();
const result = JSON.parse(ols_regression(
JSON.stringify([2.5, 3.7, 4.2, 5.1, 6.3]),
JSON.stringify([[1,2,3,4,5], [2,4,5,4,3]]),
JSON.stringify(["Intercept", "X1", "X2"])
));
console.log("R²:", result.r_squared);§Regularized Regression
use linreg_core::regularized::{ridge, lasso};
use linreg_core::linalg::Matrix;
let x = Matrix::new(100, 3, vec![0.0; 300]);
let y = vec![0.0; 100];
// Ridge regression (L2 penalty - shrinks coefficients, handles multicollinearity)
let ridge_result = ridge::ridge_fit(&x, &y, &ridge::RidgeFitOptions {
lambda: 1.0,
intercept: true,
standardize: true,
})?;
// Lasso regression (L1 penalty - does variable selection by zeroing coefficients)
let lasso_result = lasso::lasso_fit(&x, &y, &lasso::LassoFitOptions {
lambda: 0.1,
intercept: true,
standardize: true,
..Default::default()
})?;§Diagnostic Tests
After fitting a model, validate its assumptions:
| Test | Tests For | Use When |
|---|---|---|
rainbow_test | Linearity | Checking if relationships are linear |
harvey_collier_test | Functional form | Suspecting model misspecification |
breusch_pagan_test | Heteroscedasticity | Variance changes with predictors |
white_test | Heteroscedasticity | More general than Breusch-Pagan |
shapiro_wilk_test | Normality | Small to moderate samples (n ≤ 5000) |
jarque_bera_test | Normality | Large samples, skewness/kurtosis |
anderson_darling_test | Normality | Tail-sensitive, any sample size |
durbin_watson_test | Autocorrelation | Time series or ordered data |
cooks_distance_test | Influential points | Identifying high-impact observations |
use linreg_core::diagnostics::{rainbow_test, breusch_pagan_test, RainbowMethod};
// Rainbow test for linearity
let rainbow = rainbow_test(&y, &[x1.clone(), x2.clone()], 0.5, RainbowMethod::R)?;
if rainbow.r_result.as_ref().map_or(false, |r| r.p_value < 0.05) {
println!("Warning: relationship may be non-linear");
}
// Breusch-Pagan test for heteroscedasticity
let bp = breusch_pagan_test(&y, &[x1, x2])?;
if bp.p_value < 0.05 {
println!("Warning: residuals have non-constant variance");
}§Feature Flags
| Flag | Default | Description |
|---|---|---|
wasm | Yes | Enables WASM bindings and browser support |
validation | No | Includes test data for validation tests |
For native-only builds (smaller binary, no WASM deps):
linreg-core = { version = "0.2", default-features = false }§Why This Library?
- Zero dependencies — No
nalgebra, nostatrs, nondarray. Pure Rust. - Validated — Outputs match R’s
lm()and Python’sstatsmodels - WASM-ready — Same code runs natively and in browsers
- Small — Core is ~2000 lines, compiles quickly
- Permissive license — MIT OR Apache-2.0
§Module Structure
core— OLS regression, coefficients, residuals, VIFregularized— Ridge, Lasso, Elastic Net, regularization pathsdiagnostics— All diagnostic tests (linearity, heteroscedasticity, normality, autocorrelation)distributions— Statistical distributions (t, F, χ², normal, beta, gamma)linalg— Matrix operations, QR decomposition, linear system solvererror— Error types and Result alias
§Links
§Disclaimer
This library is under active development and has not reached 1.0 stability. While outputs are validated against R and Python implementations, do not use this library for critical applications (medical, financial, safety-critical systems) without independent verification. See the LICENSE for full terms. The software is provided “as is” without warranty of any kind.
Re-exports§
pub use core::RegressionOutput;pub use core::VifResult;pub use diagnostics::CooksDistanceResult;pub use diagnostics::DiagnosticTestResult;pub use diagnostics::RainbowMethod;pub use diagnostics::RainbowSingleResult;pub use diagnostics::RainbowTestOutput;pub use diagnostics::WhiteMethod;pub use diagnostics::WhiteSingleResult;pub use diagnostics::WhiteTestOutput;pub use diagnostics::rainbow_test as rainbow_test_core;pub use diagnostics::white_test as white_test_core;pub use error::error_json;pub use error::error_to_json;pub use error::Error;pub use error::Result;
Modules§
- core
- Core OLS regression implementation.
- diagnostics
- Statistical diagnostic tests for linear regression assumptions.
- distributions
- Custom statistical special functions and distribution utilities (CDF/SF/quantiles),
primarily to avoid pulling in
statrsfor regression diagnostics. - error
- Error types for the linear regression library.
- linalg
- Minimal Linear Algebra module to replace nalgebra dependency.
- regularized
- Ridge and Lasso regression (glmnet-compatible implementations).
Functions§
- anderson_
darling_ test - Performs the Anderson-Darling test for normality via WASM.
- breusch_
pagan_ test - Performs the Breusch-Pagan test for heteroscedasticity via WASM.
- cooks_
distance_ test - Computes Cook’s distance for identifying influential observations via WASM.
- durbin_
watson_ test - Performs the Durbin-Watson test for autocorrelation via WASM.
- get_
normal_ inverse - Computes the inverse of the standard normal CDF (probit function).
- get_
t_ cdf - Computes the Student’s t-distribution cumulative distribution function.
- get_
t_ critical - Computes the critical t-value for a given significance level.
- get_
version - Returns the current version of the library.
- harvey_
collier_ test - Performs the Harvey-Collier test for linearity via WASM.
- jarque_
bera_ test - Performs the Jarque-Bera test for normality via WASM.
- lasso_
regression - Performs Lasso regression via WASM.
- make_
lambda_ path - Generates a lambda path for regularized regression via WASM.
- ols_
regression - Performs OLS regression via WASM.
- parse_
csv - Parses CSV data and returns it as a JSON string.
- python_
white_ test - Performs the Python method White test for heteroscedasticity via WASM.
- r_
white_ test - Performs the R method White test for heteroscedasticity via WASM.
- rainbow_
test - Performs the Rainbow test for linearity via WASM.
- ridge_
regression - Performs Ridge regression via WASM.
- shapiro_
wilk_ test - Performs the Shapiro-Wilk test for normality via WASM.
- test
- Simple test function to verify WASM is working.
- test_ci
- Test function for confidence interval computation.
- test_
housing_ regression - Test function for regression validation against R reference values.
- test_
r_ accuracy - Test function for R accuracy validation.
- test_
t_ critical - Test function for t-critical value computation.
- white_
test - Performs the White test for heteroscedasticity via WASM.