BioPHP - Linear Correlation and Regresion Curve
Original code submitted by josebaCode bellow is covered by GNU GPL v2 license.
Description
Last change: 2010/10/18 17:09 | Recent Changes | Original descriptionWill calculate a lineal relation ship between x and y values by solving a and b values at y=ax+b. Correlation coeficient will be computed (r)
Code
Last change: 2010/10/18 17:09 | Recent Changes | Download | Original code and<html>
<head>
<title>Linear Correlation and Regression</title>
</head>
<body bgcolor=FFFFFF>
<center>
<h1>Linear Correlation and Regression</h1>
<?
// author Joseba Bikandi
// license GNU GPL v2
// biophp.org
error_reporting(0);
if (!$_POST){
if ($_GET["show"]=="example"){
// when nothing is posted, and an example is requested
// example is included within the form
print_form("10\n20\n30\n40\n50\n60\n70\n80\n90\n100","31\n58\n93\n125\n144\n177\n209\n249\n270\n303");
// tipical output for results
print_results(3.03,-0.67,0.999);
// example is explained
print_example(3.03,-0.67,0.999);
}else{
// print out form
print_form();
}
}else{
// when data is posted, discriminatory power is computed
// get the data
$vals_x=$_POST["vals_x"];
$vals_x=preg_replace("/ |\r/","",$vals_x); // removed spaces and returns
$vals_y=$_POST["vals_y"];
$vals_y=preg_replace("/ |\r/","",$vals_y); // removed spaces and returns
// parse data to an array
$vals_x_array=preg_split("/\n/",$vals_x,-1,PREG_SPLIT_NO_EMPTY);
$vals_y_array=preg_split("/\n/",$vals_y,-1,PREG_SPLIT_NO_EMPTY);
// compute discriminatory power
$curve=correlation_regression ($vals_x_array,$vals_y_array);
// print results
print_form($vals_x,$vals_y);
if ($curve){
print_results($curve["a"],$curve["b"],$curve["r"]);
}else{
print "error: input data is not correct";
}
}
function correlation_regression ($vals_x,$vals_y){
if (sizeof($vals_x)!= sizeof($vals_y)){return;}
$sum_x=0;
$sum_x2=0;
$sum_y=0;
$sum_y2=0;
$sum_xy=0;
$n=sizeof($vals_x);
foreach($vals_x as $key => $val){
$val_x=$val;
$val_y=$vals_y[$key];
$sum_x+=$val_x;
$sum_x2+=$val_x*$val_x;
$sum_y+=$val_y;
$sum_y2+=$val_y*$val_y;
$sum_xy+=$val_x*$val_y;
//print "$val_x\t$val_y\n";
}
//print "<hr>sum_x\t$sum_x\nsum_y\t$sum_y\nsum_x2\t$sum_x2\nsum_y2\t$sum_y2\nsum_xy\t$sum_xy\n";
// y=ax+b
// calculate a
$curve["a"]=($n*$sum_xy-$sum_x*$sum_y)/($n*$sum_x2-$sum_x*$sum_x);
// calculate b
$curve["b"]=($sum_y/$n)-($curve["a"]*$sum_x/$n);
// calculate regression
$curve["r"]=($sum_xy-(1/$n)*$sum_x*$sum_y)/((sqrt($sum_x2-(1/$n)*$sum_x*$sum_x)*(sqrt($sum_y2-(1/$n)*$sum_y*$sum_y))));
return $curve;
}
//########print form
function print_form($vals_x,$vals_y,$a,$b,$r){
?>
<form method=post action="<? print $_SERVER["PHP_SELF"]; ?>">
<table width=600>
<tr><td align=center>
Values for x:<br><textarea cols="5" rows="12" name="vals_x"><? print $vals_x; ?></textarea>
</td><td align=center>
Values for y:<br><textarea cols="5" rows="12" name="vals_y"><? print $vals_y; ?></textarea>
</td><td align=center valign=bottom>
<input type=submit value=compute>
<br><a href=?show=example>example</a>
</td></tr>
</table>
</form>
<?
}
//########print results
function print_results($a,$b,$r){
print " <table bgcolor=CCCCFF>
<tr><td>
Values for curve <b>y=ax+b</b>
<br> a = $a
<br> b = $b
<br> Regresion (r) = $r
</tr>
</table>";
}
//########print example
function print_example($a,$b,$r){
$apples=round($a*35+$b);
print "
<table width=550>
<tr><td>
<b>Example</b>: the number of apples per kilograms are counted in
plastic boxes in order to get a formula to calculate this parameter
when a new box arrives to the restaurant, so that we may decide
the number of menus with apples we may offer to our clients.
<p>When a box with 35 kilos arrives, by applying the formula
y=ax+b, number of apples in the box may easily calculated:
<center><p>y= $a *35 + $b = $apples apples</center>
<p>As regresion is good (r = $r), the number of apples we have calculated
will be a good stimation.
</td></tr>
</table>
";
}
?>
<p><hr>
Source code available at <a href=http://www.biophp.org/stats/linear_correlation_regression/>biophp.org</a>
</center>
</body>
</html>