Skip to main content

multiple_linear_regression

Function multiple_linear_regression 

Source
pub fn multiple_linear_regression(
    predictors: &[&[f64]],
    y: &[f64],
) -> Option<MultipleRegressionResult>
Expand description

Computes multiple linear regression via OLS (Cholesky solve).

§Arguments

  • predictors — Slice of predictor variable slices. Each inner slice is one predictor’s observations. All must have the same length.
  • y — Response variable observations.

§Algorithm

Constructs the design matrix X = [1 | x₁ | x₂ | … | xₚ] (intercept column prepended), then solves the normal equations X’Xβ = X’y via Cholesky decomposition.

§Returns

None if n < p+2, predictor lengths differ, inputs contain non-finite values, or the system is singular.

§References

Draper & Smith (1998). “Applied Regression Analysis”, 3rd edition. Montgomery, Peck & Vining (2012). “Introduction to Linear Regression Analysis”, 5th edition.

§Examples

use u_analytics::regression::multiple_linear_regression;

let x1 = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
let x2 = [2.0, 1.0, 3.0, 2.0, 4.0, 3.0, 5.0, 4.0];
let y  = [5.1, 5.0, 9.2, 8.9, 13.1, 12.0, 17.2, 15.9];
let result = multiple_linear_regression(&[&x1, &x2], &y).unwrap();
assert!(result.r_squared > 0.95);
assert_eq!(result.coefficients.len(), 3); // intercept + 2 predictors