stats::regress
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 OlsA 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.
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);
The coefficients (the intercept first, if fitted).
Their standard errors,
sqrt(diag(sigma^2 (X^T X)^-1)).coef / se.The two-sided p-values of
coef = 0ondf_residdegrees of freedom.The coefficient covariance
sigma^2 (X^T X)^-1.1 - SSR / SST(SST about the mean with an intercept, about 0 without, as statsmodels).1 - (1 - r2) (n - c) / (n - k),c= 1 with an intercept.The overall F statistic of all slopes = 0.
Its p-value.
The residual standard error
sqrt(SSR / df_resid).n - k.k - c, the slopes.y - X b.X b.Whether
coef[0]is an intercept.
Sourcepub fn predict(§self, x: Matrix<f64>) throws (StatsError) -> Vector<f64> X bfor new rowsx(without the intercept column; it is added as in the fit). ThrowsShapeMismatchfor a wrong number of columns.nabiuse 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);Confidence intervals for the coefficients at
level:coef[i] +- t_(df, (1 + level) / 2) se[i].nabiuse 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 LogisticA fitted logistic regression P(y = 1 | x) = 1 / (1 + e^-(x b)).
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);
The coefficients (the intercept first, if fitted).
Their standard errors from the inverse Fisher information.
coef / se(Wald statistics).The two-sided Wald p-values.
The maximized log-likelihood.
The residual deviance
-2 loglik.The deviance of the intercept-only model (or of
p = 1/2without an intercept).Newton (IRLS) steps taken.
Whether the steps met the tolerance before
max_iter.Whether
coef[0]is an intercept.
Sourcepub fn predict_proba(§self, x: Matrix<f64>) throws (StatsError) -> Vector<f64> P(y = 1)for new rowsx(without the intercept column).nabiuse 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.
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.
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.
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.
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.
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]));