A package that allows for conducting linear regression in PHP by OLS estimation. It relies on leonickl/matrix for the underlying matrix algebra.
composer require leonickl/regressionExamples can be found in playground.php.
// define regressand y and regressor matrix X (do not forget the constant in the first column)
$y = vector([10, 16, 19, 27, 30, 40]);
$X = matrix([
[1, 1, 5],
[1, 2, 1],
[1, 3, 9],
[1, 4, 10],
[1, 7, 20],
[1, 8, 27],
]);
// initialize the OLS object
$reg = new OLS($y, $X);
// the parameters called on $reg are calculated on first usage and then cached
// resulting vectors can be rounded by appending ->round(<decimals>)
$reg->beta // parameter estimators
$reg->fitted // fitted values for $y
$reg->resid // residuals
$reg->SSR // sum of squares – residuals (variation of residuals)
$reg->SSE // sum of squares – explained (variation of fitted values y_hat)
$reg->SST // sum of squares – total (variation of actual values y)
$reg->R2 // R² (coefficient of determination)
$reg->R2adj // adjusted R²
$reg->sigma // residual standard error
$reg->betaStdErr // standard errors for estimated parameters
$reg->tValues // T-statistics for test with H0: beta_j = 0
$reg->pValues // corresponding p-values for the significance test
// display regression output
echo $reg->summary()