luminsports / linear-regression
Linear regression
1.2.0
2022-08-19 06:05 UTC
Requires
- php: ^8.1
- ext-bcmath: *
- brick/math: ^0.10.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.2.1
- phpunit/php-code-coverage: ^9.2.7
- phpunit/phpunit: ^9.0
README
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