Skip to content

Regression least squares algorithm using BCMath for float precision

License

Notifications You must be signed in to change notification settings

luminsports/php-linear-regression

 
 

Repository files navigation

Least Squares Linear Regression class

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

About

Regression least squares algorithm using BCMath for float precision

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%