当前位置: 首页 > 图文教程 > 网络编程 > PHP > PHP实现简单线性回归之数学库的重要性
exec_shell调用,那么为何还要麻烦用PHP实现相同的统计计算功能呢?有关SSystem、它的ACM奖或R的更多信息,请参阅相关参考资料。SimpleLinearRegression,它演示了一个可以用来开发PHP数学库的通用方法。让我们从讨论一些通用的原则开始,这些原则指导我开发这个SimpleLinearRegression类。SimpleLinearRegression类的开发。MultipleRegression、TimeSeries或ChiSquared)所希望生成的标准输出值。从解决问题的角度出发,这意味着您可以使用逆向链接来开发数学类的方法。SimpleLinearRegression类中的成员函数和实例变量制定命名方案时,我发现:如果我使用较长的名称(类似于getSumSquaredError这样的名称,而不是getYY2)来描述成员函数和实例变量,那么就更容易了解函数的操作内容和变量所代表的意义。SimpleLinearRegression类声明哪些变量。可以对MultipleRegression、ANOVA或TimeSeries过程执行类似的分析。<?php//Copyright2003,PaulMeagher//DistributedunderGPLclassSimpleLinearRegression{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;//TvalueofSlopevar$YIntStdErr;var$YIntTVal;//TvalueforYInterceptvar$R;var$RSquared;var$DF;//DegreesofFreedomvar$SlopeProb;//ProbabilityofSlopeEstimatevar$YIntProb;//ProbabilityofYInterceptEstimatevar$AlphaTVal;//TValueforgivenalphasettingvar$ConfIntOfSlope;var$RPath="/usr/local/bin/R";//Yourpathherevar$format="%01.2f";//Usedforformattingoutput}?>
SimpleLinearRegression类的构造函数方法接受一个X和一个Y向量,每个向量都有相同数量的值。您还可以为您预计的Y值设置一个缺省为95%的置信区间(confidenceinterval)。<?php//Copyright2003,PaulMeagher//DistributedunderGPLfunctionSimpleLinearRegression($X,$Y,$ConfidenceInterval="95"){$numX=count($X);$numY=count($Y);if($numX!=$numY){die("Error:SizeofXandYvectorsmustbethesame.");}if($numX<=1){die("Error:Sizeofinputarraymustbeatleast2.");}$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();returntrue;}?>
SimpleLinearRegression过程用于产生与数据相吻合的直线,其中直线具有以下标准方程:$PredictedY[$i]=$YIntercept+$Slope*$X[$i]
SimpleLinearRegression类使用最小二乘法准则推导出Y轴截距(YIntercept)和斜率(Slope)参数的估计值。这些估计的参数用来构造线性方程(请参阅清单3),该方程对X和Y值之间的关系进行建模。
使用推导出的线性方程,您就可以得到每个X值对应的预测Y值。如果线性方程与数据非常吻合,那么Y的观测值与预测值趋近于一致。
如何确定是否非常吻合SimpleLinearRegression类生成了相当多的汇总值。一个重要的汇总值是T统计值,它可以用来衡量一个线性方程与数据的吻合程度。如果非常吻合,那么T统计值往往很大。如果T统计值很小,那么应当用一个模型替换该线性方程,该模型假设Y值的均值是最佳预测值(也就是说,一组值的均值通常是下一个观测值有用的预测值,使之成为缺省模型)。
要测试T统计值是否大得足以不把Y值的均值作为最佳预测值,您需要计算获取T统计值的随机概率。如果获取T统计值的概率很低,那么您可以否定均值是最佳预测值这个无效假设,与此相对应,也就确信简单线性模型与数据非常吻合。
那么,如何计算T统计值的概率呢?
<?php//Copyright2003,PaulMeagher//DistributedunderGPLclassSimpleLinearRegression{var$RPath="/usr/local/bin/R";//YourpathherefunctiongetStudentProb($T,$df){$Probability=0.0;$cmd="echo'dt($T,$df)'|$this->RPath--slave";$result=shell_exec($cmd);list($LineNumber,$Probability)=explode("",trim($result));return$Probability;}functiongetInverseStudentProb($alpha,$df){$InverseProbability=0.0;$cmd="echo'qt($alpha,$df)'|$this->RPath--slave";$result=shell_exec($cmd);list($LineNumber,$InverseProbability)=explode("",trim($result));return$InverseProbability;}}?>
getStudentProb方法用来评估线性模型的吻合程度;getInverseStudentProb方法返回一个中间值,它用来计算每个预测的Y值的置信区间。<?php//BurnoutStudy.php//Copyright2003,PaulMeagher//DistributedunderGPLinclude"SimpleLinearRegression.php";//Loaddatafromburnoutstudy$Concentration=array(20,60,38,88,79,87,68,12,35,70,80,92,77,86,83,79,75,81,75,77,77,77,17,85,96);$ExhaustionIndex=array(100,525,300,980,310,900,410,296,120,501,920,810,506,493,892,527,600,855,709,791,718,684,141,400,970);$slr=newSimpleLinearRegression($Concentration,$ExhaustionIndex);$YInt=sprintf($slr->format,$slr->YInt);$Slope=sprintf($slr->format,$slr->Slope);$SlopeTVal=sprintf($slr->format,$slr->SlopeTVal);$SlopeProb=sprintf("%01.6f",$slr->SlopeProb);?><tableborder='1'cellpadding='5'><tr><thalign='right'>Equation:</th><td></td></tr><tr><thalign='right'>T:</th><td></td></tr><tr><thalign='right'>Prob>T:</th><td><td></tr></table>
评论 (0) All