File: C:/xampp/htdocs/FograPC10/rTest.php
<?php
// Load the polynomial regression class.
//require_once( 'RootDirectory.inc.php' );
require_once( 'PolynomialRegression.php' );
$data =
array
(
array( 0.00, 0.0 ), array( 25, 19.2 ),
array( 50, 31.55 ), array( 75, 70.14 ),
array( 100, 100 )
);
// Precision digits in BC math.
bcscale( 10 );
// Start a regression class of order 2--linear regression.
$leastSquareRegression = new PolynomialRegression( 2 );
// Add all the data to the regression analysis.
foreach ( $data as $dataPoint )
$leastSquareRegression->addData( $dataPoint[ 0 ], $dataPoint[ 1 ] );
// Get coefficients for the polynomial.
$coefficients = $leastSquareRegression->getCoefficients();
// Print slope and intercept of linear regression.
echo "Slope : " . round( $coefficients[ 1 ], 2 ) . "<br />\n";
echo "Y-intercept : " . round( $coefficients[ 0 ], 2 ) . "<br />\n";
//
// Get average of Y-data.
//
$Y_Average = 0.0;
foreach ( $data as $dataPoint )
$Y_Average += $dataPoint[ 1 ];
$Y_Average /= count( $data );
//
// Calculate R Squared.
//
$Y_MeanSum = 0.0;
$Y_ErrorSum = 0.0;
foreach ( $data as $dataPoint )
{
$x = $dataPoint[ 0 ];
$y = $dataPoint[ 1 ];
$error = $y;
$error -= $leastSquareRegression->interpolate( $coefficients, $x );
$Y_ErrorSum += $error * $error;
$error = $y;
$error -= $Y_Average;
$Y_MeanSum += $error * $error;
}
$R_Squared = 1.0 - ( $Y_ErrorSum / $Y_MeanSum );
echo "R Squared : $R_Squared<br />\n";
?>