HEX
Server: Apache/2.4.46 (Win64) OpenSSL/1.1.1j PHP/8.4.25
System: Windows NT DESKTOP-4TAV2RJ 10.0 build 19045 (Windows 10) AMD64
User: fred (0)
PHP: 8.4.25
Disabled: NONE
Upload Files
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";

?>