How do I improve this linear regression function?
Posted
by
user558383
on Stack Overflow
See other posts from Stack Overflow
or by user558383
Published on 2010-12-30T14:50:17Z
Indexed on
2010/12/30
14:54 UTC
Read the original article
Hit count: 119
I have the following PHP function that I'm using to draw a trend line. However, it sometimes plots the line below all the points in the scatter graph. Is there an error in my function or is there a better way to do it. I think it might be something to do with that with the line it produces, it treats all the residuals (the distances from the scatter points to the line) as positive regardless of them being above or below the line.
function linear_regression($x, $y) {
$n = count($x);
$x_sum = array_sum($x); $y_sum = array_sum($y);
$xx_sum = 0; $xy_sum = 0;
for($i = 0; $i < $n; $i++) { $xy_sum+=($x[$i]*$y[$i]); $xx_sum+=($x[$i]*$x[$i]); }
$m = (($n * $xy_sum) - ($x_sum * $y_sum)) / (($n * $xx_sum) - ($x_sum * $x_sum)); $b = ($y_sum - ($m * $x_sum)) / $n; return array("m"=>$m, "b"=>$b);
}
© Stack Overflow or respective owner