1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | <?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 } ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | <?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; } ?> |
1 | $PredictedY[$i] = $YIntercept + $Slope * $X[$i] |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php // Copyright 2003, Paul Meagher // Distributed under GPL class SimpleLinearRegression { var $RPath = "/usr/local/bin/R"; // Your path here function getStudentProb($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; } function getInverseStudentProb($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; } } ?> |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |