Developer Forums | About Us | Site Map
Search  
HOME > TUTORIALS > SERVER SIDE CODING > PHP TUTORIALS > SIMPLE LINEAR REGRESSION WITH PHP: PART 1


Sponsors





Useful Lists

Web Host
site hosted by netplex

Online Manuals

Simple linear regression with PHP: Part 1
By Paul Meagher - 2004-05-12 Page:  1 2 3 4 5 6 7 8

The constructor

The constructor method for the SimpleLinearRegression class accepts an X and a Y vector with the same number of values in each vector. You can also set a confidence interval for your predicted Y values (default is a 95 percent confidence interval).

The constructor method begins by verifying that the data is in a form suitable for processing. Once the input vectors have passed the "equal size" and "size greater than 1" tests, the heart of the algorithm is executed.

Performing this task involves computing intermediate and summary values for the statistical procedure through a series of getter methods. The return value from each method call is assigned to an instance variable for the class. Storing calculational results in this way ensures that the intermediate and summary values can be used by the calling routine in chained calculations. The results can also be displayed by calling the output methods for the class, as is described in Listing 2.

Listing 2. Calling class output methods


<?php 

// Copyright 2003, Paul Meagher 
// Distributed under GPL   

function SimpleLinearRegression($X, $Y, $ConfidenceInterval="95") {

  $numX = count($X);
  $numY = count($Y);

  if ($numX != $numY) {
    die("Error: Size of X and Y vectors must be the same.");

  } 
  if ($numX <= 1) { 
    die("Error: Size of input array must be at least 2.");
  }
  
  $this->n               = $numX;
  $this->X               = $X;
  $this->Y               = $Y;  
  
  $this->ConfInt         = $ConfidenceInterval; 
  $this->Alpha           = (1 + ($this->ConfInt / 100) ) / 2;

  $this->XMean           = $this->getMean($this->X);
  $this->YMean           = $this->getMean($this->Y);
  $this->SumXX           = $this->getSumXX();
  $this->SumYY           = $this->getSumYY();
  $this->SumXY           = $this->getSumXY();    
  $this->Slope           = $this->getSlope();
  $this->YInt            = $this->getYInt();
  $this->PredictedY      = $this->getPredictedY();
  $this->Error           = $this->getError();
  $this->SquaredError    = $this->getSquaredError();
  $this->SumError        = $this->getSumError();
  $this->TotalError      = $this->getTotalError();    
  $this->SumSquaredError = $this->getSumSquaredError();
  $this->ErrorVariance   = $this->getErrorVariance();
  $this->StdErr          = $this->getStdErr();  
  $this->SlopeStdErr     = $this->getSlopeStdErr();     
  $this->YIntStdErr      = $this->getYIntStdErr();         
  $this->SlopeTVal       = $this->getSlopeTVal();            
  $this->YIntTVal        = $this->getYIntTVal();                
  $this->R               = $this->getR();   
  $this->RSquared        = $this->getRSquared();
  $this->DF              = $this->getDF();          
  $this->SlopeProb       = $this->getStudentProb($this->SlopeTVal, $this->DF);
  $this->YIntProb        = $this->getStudentProb($this->YIntTVal, $this->DF);
  $this->AlphaTVal       = $this->getInverseStudentProb($this->Alpha, $this->DF);
  $this->ConfIntOfSlope  = $this->getConfIntOfSlope(); 

  return true;
}

?>

The method names and their sequence were derived by a combination of backward chaining and consulting an undergraduate statistics textbook that provided step-by-step instructions for computing intermediate values. The names of the intermediate values that I needed to compute were prefixed with "get" to derive the method name.



View Simple linear regression with PHP: Part 1 Discussion

Page:  1 2 3 4 5 6 7 8 Next Page: Fit the model to the data

First published by IBM developerWorks


Copyright 2004-2017 GrindingGears.com. All rights reserved.
Article copyright and all rights retained by the author.