packages

stats::regress

On this page

Regression: ordinary and weighted least squares with inference, polynomial fits and logistic regression. Rows of the design matrix are observations, columns are predictors; an intercept column is added unless intercept: false.

Structs

OlsSource

pub struct Ols

A fitted linear model y = X b + e: coefficients, their standard errors, t statistics and p-values, the fit's R^2, the residuals. With an intercept it is coef[0], the predictors follow.

nabi
use core::array::{Matrix, Vector};

let x: Matrix<f64> = [[1], [2], [3], [4], [5]];
let y: Vector<f64> = [2.1, 3.9, 6.2, 7.8, 10.1];
let m = try stats::ols(x, y);
assert((m.coef[1] - 1.99).abs() < 1e-12 && m.r2 > 0.99);
  • pub coef: Vector<f64>§

    Source

    The coefficients (the intercept first, if fitted).

  • pub se: Vector<f64>§

    Source

    Their standard errors, sqrt(diag(sigma^2 (X^T X)^-1)).

  • pub t: Vector<f64>§

    Source

    coef / se.

  • pub p: Vector<f64>§

    Source

    The two-sided p-values of coef = 0 on df_resid degrees of freedom.

  • pub cov: Matrix<f64>§

    Source

    The coefficient covariance sigma^2 (X^T X)^-1.

  • pub r2: f64§

    Source

    1 - SSR / SST (SST about the mean with an intercept, about 0 without, as statsmodels).

  • pub adj_r2: f64§

    Source

    1 - (1 - r2) (n - c) / (n - k), c = 1 with an intercept.

  • pub fstat: f64§

    Source

    The overall F statistic of all slopes = 0.

  • pub f_pvalue: f64§

    Source

    Its p-value.

  • pub sigma: f64§

    Source

    The residual standard error sqrt(SSR / df_resid).

  • pub df_resid: f64§

    Source

    n - k.

  • pub df_model: f64§

    Source

    k - c, the slopes.

  • pub resid: Vector<f64>§

    Source

    y - X b.

  • pub fitted: Vector<f64>§

    Source

    X b.

  • pub intercept: bool§

    Source

    Whether coef[0] is an intercept.

  • pub fn predict(self, x: Matrix<f64>) throws (StatsError) -> Vector<f64>§

    Source

    X b for new rows x (without the intercept column; it is added as in the fit). Throws ShapeMismatch for a wrong number of columns.

    nabi
    use core::array::{Matrix, Vector};
    
    let x: Matrix<f64> = [[0], [1], [2]];
    let y: Vector<f64> = [1, 3, 5];
    let m = try stats::ols(x, y);
    let new: Matrix<f64> = [[10]];
    assert(((try m.predict(new))[0] - 21.0).abs() < 1e-13);
    
  • pub fn ci(self, level: f64) -> List<Interval>§

    Source

    Confidence intervals for the coefficients at level: coef[i] +- t_(df, (1 + level) / 2) se[i].

    nabi
    use core::array::{Matrix, Vector};
    
    let x: Matrix<f64> = [[1], [2], [3], [4]];
    let y: Vector<f64> = [1.1, 1.9, 3.2, 3.9];
    let m = try stats::ols(x, y);
    let ci = m.ci(95pct);
    assert(m.coef[1] in ci[1] && ci.len() == 2);
    

LogisticSource

pub struct Logistic

A fitted logistic regression P(y = 1 | x) = 1 / (1 + e^-(x b)).

nabi
use core::array::{Matrix, Vector};

let x: Matrix<f64> = [[0.5], [1.0], [1.5], [2.0], [2.5], [3.0], [3.5], [4.0]];
let y: Vector<f64> = [0, 0, 1, 0, 1, 0, 1, 1];
let m = try stats::logistic(x, y);
assert(m.converged && m.coef[1] > 0.0);
  • pub coef: Vector<f64>§

    Source

    The coefficients (the intercept first, if fitted).

  • pub se: Vector<f64>§

    Source

    Their standard errors from the inverse Fisher information.

  • pub z: Vector<f64>§

    Source

    coef / se (Wald statistics).

  • pub p: Vector<f64>§

    Source

    The two-sided Wald p-values.

  • pub loglik: f64§

    Source

    The maximized log-likelihood.

  • pub deviance: f64§

    Source

    The residual deviance -2 loglik.

  • pub null_deviance: f64§

    Source

    The deviance of the intercept-only model (or of p = 1/2 without an intercept).

  • pub iterations: usize§

    Source

    Newton (IRLS) steps taken.

  • pub converged: bool§

    Source

    Whether the steps met the tolerance before max_iter.

  • pub intercept: bool§

    Source

    Whether coef[0] is an intercept.

  • pub fn predict_proba(self, x: Matrix<f64>) throws (StatsError) -> Vector<f64>§

    Source

    P(y = 1) for new rows x (without the intercept column).

    nabi
    use core::array::{Matrix, Vector};
    
    let x: Matrix<f64> = [[1], [2], [3], [4], [5], [6]];
    let y: Vector<f64> = [0, 0, 1, 0, 1, 1];
    let m = try stats::logistic(x, y);
    let new: Matrix<f64> = [[3.5]];
    assert((try m.predict_proba(new))[0] > 0.4);
    

Functions

olsSource

pub fn ols(x: Matrix<f64>, y: Vector<f64>, intercept: bool = true) throws (StatsError) -> Ols

Ordinary least squares y = X b + e by Householder QR (linalg): coefficients from R b = Q^T y, covariance sigma^2 R^-1 R^-T, so X^T X is never formed. O(n k^2). Throws ShapeMismatch, TooFew (needs more rows than coefficients), NotFinite, or Singular for collinear columns.

nabi
use core::array::{Matrix, Vector};

// Draper and Smith style check: y = 1 + 2 x1 - x2 exactly.
let x: Matrix<f64> = [[1, 0], [2, 1], [3, 1], [4, 3], [5, 2]];
let y: Vector<f64> = [3, 4, 6, 6, 9];
let m = try stats::ols(x, y);
assert((m.coef[0] - 1.0).abs() < 1e-12 && (m.coef[2] + 1.0).abs() < 1e-12);

wlsSource

pub fn wls(x: Matrix<f64>, y: Vector<f64>, w: Vector<f64>, intercept: bool = true) throws (StatsError) -> Ols

Weighted least squares: minimizes sum w_i (y_i - x_i b)^2 (OLS on rows scaled by sqrt(w)), with weights >= 0, e.g. inverse variances. R^2 and the residual error are weighted, as statsmodels' WLS; resid and fitted are on the original scale.

nabi
use core::array::{Matrix, Vector};

let x: Matrix<f64> = [[1], [2], [3], [4]];
let y: Vector<f64> = [1.1, 1.9, 3.2, 3.9];
let w: Vector<f64> = [1, 1, 1, 1];
let a = try stats::wls(x, y, w);
let b = try stats::ols(x, y);
assert((a.coef[1] - b.coef[1]).abs() < 1e-14);

logisticSource

pub fn logistic(x: Matrix<f64>, y: Vector<f64>, intercept: bool = true, max_iter: usize = 100, tol: f64 = 1e-10) throws (StatsError) -> Logistic

Logistic regression of 0/1 outcomes y on the columns of x by Newton's method in its IRLS form (McCullagh and Nelder 1989): each step solves X^T W X d = X^T (y - mu) by Cholesky (linalg), from b = 0, until no coefficient moves more than tol (relative) or max_iter steps. Throws InvalidArgument for outcomes other than 0 and 1 or for perfectly separated data (the estimates would be infinite), Singular for collinear columns.

nabi
use core::array::{Matrix, Vector};

let x: Matrix<f64> = [[1], [2], [3], [4], [5], [6]];
let y: Vector<f64> = [0, 0, 1, 0, 1, 1];
let m = try stats::logistic(x, y);
// statsmodels: Logit(y, add_constant(x)).fit().params[1]
assert((m.coef[1] - 1.2140275858514205).abs() < 1e-12);

polyfitSource

pub fn polyfit(x: Vector<f64>, y: Vector<f64>, deg: usize) throws (StatsError) -> Vector<f64>

The least-squares polynomial of degree deg through the points (x, y): coefficients rising, c[0] + c[1] x + ... + c[deg] x^deg. The Vandermonde columns are scaled to unit norm before the solve (pivoted QR via linalg::lstsq), as NumPy's polyfit. Throws TooFew below deg + 1 points.

nabi
use core::array::Vector;

let x: Vector<f64> = [0, 1, 2, 3];
let y: Vector<f64> = [1, 2, 5, 10];
let c = try stats::polyfit(x, y, 2);
assert((c[0] - 1.0).abs() < 1e-13 && (c[2] - 1.0).abs() < 1e-13);

polyvalSource

pub fn polyval<const N: usize>(c: Vector<f64>, x: Array<f64, N>) -> Array<f64, N>

The polynomial with rising coefficients c at each element of x (Horner's rule), in the shape of x.

nabi
use core::array::Vector;

let c: Vector<f64> = [1, 0, 2];
let x: Vector<f64> = [0, 1, 3];
assert(stats::polyval(c, x) == Vector::<f64>::from([1.0, 3.0, 19.0]));