virtual_odin Posted July 21, 2008 Share Posted July 21, 2008 Hey! It's been years since I did a stats course, but I know what these babies can do... And Excel has this built in! I now need one for a site I am building. If only I could find the code to generate the results from my dataset. I really do not fancy going back to school to re-learn how to construct them and then have to convert those principles to PHP. I'm easy as to whether it is PHP, taking a two dimensional array and producing the a, b (as in y = ax + b) and the correlation, or MySQL (I'm running version 5 and I saw somewhere that some additional functions in v5 might help. As always, thanks in anticipation. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 21, 2008 Share Posted July 21, 2008 Pulling out my analytical chemistry book excel's "slope" equation is said to calculate using m = Matrix{ sigma(Xiyi) sigma(Xi) // Sigma(Yi) n} divided by D b = MATRIX{ sigma(X^2i) sigma(XiYi) // sigma(Xi) sigma(Yi)} divdied by D D is defined as the 2x2 matrix{sigma(x(^2)i) sigma(xi) // sigma(xi) n} So using the 2 arrays you need to develop the sigmas of xiyi xi, yi x^2i, yi and get your n and then solve your matrixs and your done Its a lot of plug n chug PEAR might be able to calculate this m and b for you Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 21, 2008 Share Posted July 21, 2008 here is a good start to it <?php function slope($d_set1, $d_set2){ $XY = array() foreach($d_set1 as $key=>$value){ $XY[$key] = $value*$d_set2[$key]; } $X2 = array(); foreach($d_set1 as $key => $value){ $X2[$key] = pow($value,2); } $Xi = array_sum($d_set1); $Yi = array_sum($d_set2); $XiYi = array_sum($XY); $X2i = array_sum($X2); } ?> Quote Link to comment Share on other sites More sharing options...
virtual_odin Posted July 21, 2008 Author Share Posted July 21, 2008 I've found the below, which seems to be pretty much what you are suggesting. Does your book give any help on R squared? Thanks. /** * linear regression function * @param $x array x-coords * @param $y array y-coords * @returns array() slope=>slope, intercept=>intercept */ function linear_regression($x, $y) { // calculate number points $n = count($x); // ensure both arrays of points are the same size if ($n != count($y)) { trigger_error("linear_regression(): Number of elements in coordinate arrays do not match.", E_USER_ERROR); } // calculate sums $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("slope"=>$m, "intercept"=>$b); } var_dump( linear_regression(array(1, 2, 3, 4), array(1.5, 2.1, 1.3, 1.4)) ); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.