首页 > 其他分享 >calculation of t value and p-value of unpaired t-test

calculation of t value and p-value of unpaired t-test

时间:2023-11-08 12:02:26浏览次数:29  
标签:no calculation value test n1 n2 unpaired

calculation of t value and p-value of unpaired t-test  





this program prompts for means, standard dev, and sample sizes of both groups, and calculates the p-value of unpaired t-test of two independent group.


<?
                //-------------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



标签:no,calculation,value,test,n1,n2,unpaired
From: https://blog.51cto.com/emanlee/8247955

相关文章

  • 在线计算t-test P-Value
    在线计算T-testP-Valuep-ValueCalculatorfortheStudentt-Testhttp://www.danielsoper.com/statcalc/calc08.aspxTDistributionCalculatorhttp://www.stat.tamu.edu/~west/applets/tdemo.html......
  • 计算t-test 的C程序
    /*gdboutput程序还未调试成功:http://ubuntuforums.org/archive/index.php/t-412096.html*//*(gdb)runStartingprogram:/home/nrh1/code/testtProgramreceivedsignalSIGSEGV,Segmentationfault.0x0804967finvar()*//*functiontocalculatettest......
  • vue-test ------事件监听
    <template><h3>监听器</h3><p>{{message}}</p><button@click="updateHandle">点击</button></template><script>exportdefault{name:"WatchDemo",data(){return{mess......
  • vue-test -------style绑定
    <template><p:style="{color:activeColor,fontSize:fontsize+'px'}"></p></template><script>exportdefault{name:"StyleDemo",data(){return{activeColor:"green",......
  • vue-test -----ListDemo 列表渲染
    <template><h3>数组</h3><button@click="addnums">添加数据</button><pv-for="(item,index)innums":key="index">{{item}}</p></template><script>exportdefault{name:......
  • vue-test ------class绑定
    <template><p:class="{'active':isActive}">Class样式绑定</p><p:class="Objec">Class样式绑定</p><p:class="[arrActive,arrHasErr]">Class样式绑定3</p><p:class="isActive......
  • vue-test --------事件修饰符
    <template><h3>事件修饰符</h3><a@click="clickHandle"href="www.baidu.com">百度</a><div@click="clickDiv"><p@click="clickP">测试冒泡</p></div></template&......
  • vue-test --------模板引用
    <template><inputtype="text"v-model.lazy="message"><div>{{message}}</div><inputtype="checkbox"v-model="flag"><div>{{flag}}</div></template><script......
  • vue-test --------ref
    <template><divref="contain">{{content}}</div><button@click="getElemet">获取元素</button><inputtype="text"ref="username"></template><script>exportdefault{......
  • AtCoder Beginner Contest 327 (ABC327)
    A.ab直接根据题意模拟即可。CodeB.A^A直接枚举\(i=1,2,\dots,15\),每次看看\(i^i\)是否等于\(A\)即可。CodeC.NumberPlaceDescription给你一个\(9\times9\)的矩阵\(A\),判断是否合法,满足以下三个条件,即为合法。对于每一行,包含数字\(1\sim9\);对于......