<?
//-------------pdf_helper.inc --------------//
/*
This code is to compute a p-value from means of two groups.(not the paired data) The paired t-test should be done with different df calculation
p-value is area under the curve from t* to infinity.
I used pdf of t distribution:
y=gamma[(v+1)/2)]/*[(1+(t^2/df)]^((-v-1)/2)
where gamma(alpha)=integral of [x^(alpha-1)*exp(-x)] from 0 to infinity
There can be more than one form for distribution functions. They are actually the same distribution function but in different ways to express it.
*/
//this function calculates t value(aka t statistics)
function getT($n1, $n2, $sd1, $sd2, $mn1, $mn2, $df)
{
$var1=pow($sd1, 2);
$var2=pow($sd2, 2);
$pvar=(($n1-1)*$var1+($n2-1)*$var2)/$df;
if ($pvar>0) $t=($mn1-$mn2)/sqrt($pvar*(1/$n1+1/$n2));
else $t=-9999; //no value
if ($t>9999.999) $t=9999.999; //no value
if(($var1/$n1+$var2/$n2)>0) $t=($mn1-$mn2)/sqrt($var1/$n1+$var2/$n2);
else $t=-9999; //no value
$t=abs($t); //since it's one sided test,
echo "t value $t<br>"; //we take the absolute value
return $t;
}
//this function calculates area under the curve of gamma dist from 0 to 1
//function getGamma($alpha)
{
$height=0;
$sum=0;
$width=0.1;
for($i=0; $i<101; $i++) $seg[$i]= $i/10;
for($k=0; $k<100; $k++)
{
$mp=($seg[$k]+$seg[$k+1])/2;
$height=pow($mp, $alpha-1)*exp(-$mp);
$sum=($height*$width)+$sum;
}
return $sum;
}
//this integrates t function from t* to infinity
function T_dist($df, $alpha1, $alpha2, $t)
{
/* t distribution integration */
$sum=0;
$t_seg[0]=$t;
$t_height=0;
$t_wd=(6-$t)/100; //I just took 6 instead of infinity since the difference is
$temp=pi()*$df; //insignificant
for($j=1;$j<101; $j++) $t_seg[$j]=$t_seg[$j-1]+$t_wd;
for ($l=0; $l<100; $l++)
{
$t_mp=($t_seg[$l]+$t_seg[$l+1])/2;
$t_height=($alpha1/(sqrt($temp)*$alpha2))*pow(1/(1+(($t_mp*$t_mp)/$df)), 1/2*($df+1));
$sum=$t_height*$t_wd+$sum;
}
return $sum*2;
}
?>
//----------------mytest.php---------------//
<html>
<body>
<?
include("pdf_helper.inc");
//pdf_helper has formulas for several pdf(probability distribution functions) to derive p-values.
//p value is area under t*(t statistics) to infinity
/******************************************************/
Values needed to be loaded from MySQL(the surveyor)
*******************************************************/
$n1=3; $n2=3; //n1= number of samples in group1; n2=number of samples in group2
$mn1=0.0629; //mn1= mean from group1
$mn2=0.0775; // mn2= mean from group2
$sd1=.0258; // sd1= standard deviation of group1
$sd2=.0199; //sd2=standard deviation of group2
$w1=($sd1*$sd1)/$n1; //w1 and w2 are temp variables to calcuate df(degrees of freedom)
$w2=($sd2*$sd2)/$n2;
$df=pow(($w1+$w2), 2)/($w1*$w1/($n1-1)+$w2*$w2/($n2-1)); //calculation of df
$alpha1=($df+1)/2;
$alpha2=$df/2;
$t=getT($n1, $n2, $sd1, $sd2, $mn1, $mn2, $df); //calculates t statistics
$pval=T_dist($df,getGamma($alpha1), getGamma($alpha2), $t); //calculates p-value
echo "t statistics is $t<br>";
echo "p-value is $pval";
?>
</body>
</html> From:http://www.zend.com//code/codex.php?ozid=971&single=1
|