<?php // Copyright 2003, Paul Meagher // Distributed under GPL class SimpleLinearRegression { var $n; var $X = array(); var $Y = array(); var $ConfInt; var $Alpha; var $XMean; var $YMean; var $SumXX; var $SumXY; var $SumYY; var $Slope; var $YInt; var $PredictedY = array(); var $Error = array(); var $SquaredError = array(); var $TotalError; var $SumError; var $SumSquaredError; var $ErrorVariance; var $StdErr; var $SlopeStdErr; var $SlopeVal; // T value of Slope var $YIntStdErr; var $YIntTVal; // T value for Y Intercept var $R; var $RSquared; var $DF; // Degrees of Freedom var $SlopeProb; // Probability of Slope Estimate var $YIntProb; // Probability of Y Intercept Estimate var $AlphaTVal; // T Value for given alpha setting var $ConfIntOfSlope; var $RPath = "/usr/local/bin/R"; // Your path here var $format = "%01.2f"; // Used for formatting output } ?>
复制代码
<P> 清单 1. SimpleLinearRegression 类的实例变量 机关函数
<P> SimpleLinearRegression 类的机关函数办法承受一个 X 和一个 Y 向量,每一个向量都有不异数目的值。您还可觉得您估计的 Y 值设置一个缺省为 95% 的相信区间(confidence interval)。 <P> 机关函数办法从验证数据模式是不是合适于处置入手下手。一旦输出向量经由过程了“巨细相等”和“值大于 1”测试,就履行算法的中心局部。 <P> 履行这项义务触及到经由过程一系列 getter 办法盘算统计进程的两头值和汇总值。将每一个办法挪用的前往值赋给该类的一个实例变量。用这类办法存储盘算了局确保了前后链接的盘算中的挪用例程可使用两头值和汇总值。还可以经由过程挪用该类的输入办法来显示这些了局,如清单 2 所描写的那样。 <P>
<?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; } ?>