luminsports/linear-regression

1.2.0 2022-08-19 06:05 UTC

This package is auto-updated.

Last update: 2024-11-19 10:46:19 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