Board logo

标题: 用 PHP 实现的简单线性回归(2) [打印本页]

作者: look_w    时间: 2018-7-15 09:01     标题: 用 PHP 实现的简单线性回归(2)

实例变量当对统计测试或过程进行建模时,您需要指出声明哪些实例变量。
实例变量的选择可以通过说明由分析过程生成的中间值和汇总值来确定。每个中间值和汇总值都可以有一个相应的实例变量,将变量的值作为对象属性。
我采用这样的分析来确定为 中的         SimpleLinearRegression类声明哪些变量。可以对         MultipleRegression、         ANOVA或         TimeSeries过程执行类似的分析。     
清单 1. SimpleLinearRegression 类的实例变量
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
  
}
?>




构造函数SimpleLinearRegression类的构造函数方法接受一个         X和一个        Y向量,每个向量都有相同数量的值。您还可以为您预计的        Y值设置一个缺省为 95% 的置信区间(confidence interval)。     
构造函数方法从验证数据形式是否适合于处理开始。一旦输入向量通过了“大小相等”和“值大于 1”测试,就执行算法的核心部分。
执行这项任务涉及到通过一系列 getter 方法计算统计过程的中间值和汇总值。将每个方法调用的返回值赋给该类的一个实例变量。用这种方法存储计算结果确保了前后链接的计算中的调用例程可以使用中间值和汇总值。还可以通过调用该类的输出方法来显示这些结果,如清单 2 所描述的那样。
清单 2. 调用类输出方法
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;
}
?>




方法名及其序列是通过结合逆向链接和参考大学本科学生使用的统计学教科书推导得出的,该教科书一步一步地说明了如何计算中间值。我需要计算的中间值的名称带有“get”前缀,从而推导出方法名。
使模型与数据相吻合SimpleLinearRegression过程用于产生与数据相吻合的直线,其中直线具有以下标准方程:     
y = b + mx
该方程的 PHP 格式看起来类似于清单 3:
清单 3. 使模型与数据相吻合的 PHP 方程
1
$PredictedY[$i] = $YIntercept + $Slope * $X[$i]




SimpleLinearRegression类使用最小二乘法准则推导出 Y 轴截距(Y Intercept)和斜率(Slope)参数的估计值。这些估计的参数用来构造线性方程(请参阅 ),该方程对        X和        Y值之间的关系进行建模。     
使用推导出的线性方程,您就可以得到每个        X值对应的预测        Y值。如果线性方程与数据非常吻合,那么        Y的观测值与预测值趋近于一致。     
如何确定是否非常吻合SimpleLinearRegression类生成了相当多的汇总值。一个重要的汇总值是         T统计值,它可以用来衡量一个线性方程与数据的        吻合程度。如果非常吻合,那么 T 统计值往往很大。如果 T 统计值很小,那么应当用一个模型替换该线性方程,该模型假设        Y值的均值是最佳预测值(也就是说,一组值的均值通常是下一个观测值有用的预测值,使之成为缺省模型)。     
要测试 T 统计值是否大得足以不把        Y值的均值作为最佳预测值,您需要计算获取 T 统计值的随机概率。如果获取 T 统计值的概率很低,那么您可以否定均值是最佳预测值这个无效假设,与此相对应,也就确信简单线性模型与数据非常吻合。     
那么,如何计算 T 统计值的概率呢?
计算 T 统计值概率由于 PHP 缺少计算 T 统计值概率的数学例程,因此我决定将此任务交给统计计算包 R(请参阅 中的 www.r-project.org)来获得必要的值。我还想提醒大家注意该包,因为:     
清单 4 中的代码演示了交给 R 来处理以获取一个值是多么容易。
清单 4. 交给 R 统计计算包来处理以获取一个值
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;
}
}
?>




请注意,这里已经设置了到 R 可执行文件的路径,并在两个函数中使用了该路径。第一个函数根据学生的 T 分布返回了与 T 统计值相关的概率值,而第二个反函数计算了与给定的 alpha 设置相对应的 T 统计值。         getStudentProb方法用来评估线性模型的吻合程度;         getInverseStudentProb方法返回一个中间值,它用来计算每个预测的         Y值的置信区间。     
由于篇幅有限,我不可能逐个详细说明这个类中的所有函数,因此如果您想搞清楚简单线性回归分析中所涉及的术语和步骤,我鼓励您参考大学本科学生使用的统计学教科书。




欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0