luminsports/linear-regression

Linear regression

Installs: 13

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 0

Forks: 3

Open Issues: 1

pkg:composer/luminsports/linear-regression

1.2.2 2025-02-27 01:31 UTC

This package is auto-updated.

Last update: 2025-09-27 02:58:55 UTC


README

Code Style Tests

A Linear regression class that uses the least squares method to approximate a straight line to a data set. Forked from davebarnwell/ml-regression-least-squares, this version uses new PHP 8.1 syntax, and BCMath for high precision.

composer install luminsports/linear-regression

Usage:

$x = [...]; // target values
$y = [...]; // observation values

$linearRegression = new \LuminSports\LinearRegression\LeastSquares($x, $y);

$slope = $linearRegression->getSlope();
$yIntercept = $linearRegression->getIntercept();
    
// return array of differences of y values from the regression line
$differences = $linearRegression->getDifferencesFromRegressionLine();

// return array of cumulative sum of the differences of y values from the regression line
$cumulativeSum = $linearRegression->getCumulativeSumOfDifferencesFromRegressionLine();

// return array of Point objects giving the x,y values of the regression line
// for current data
$regressionLine = $linearRegression->getRegressionLinePoints();

$regressionLine[0]->getX();
$regressionLine[0]->getY();

$predictedX = $linearRegression->predictX($anObservationValue);

$predictedY = $linearRegression->predictY($aTargetValue);

$rSquared = $linearRegression->getRSquared(); // Regression fit; 1 = perfect fit 0 = no fit