首页 > 编程语言 >计算t-test 的C程序

计算t-test 的C程序

时间:2023-11-08 12:01:26浏览次数:35  
标签:code 1.0 int double 程序 unsigned 计算 test ttest

/* gdb output   程序还未调试成功:http://ubuntuforums.org/archive/index.php/t-412096.html   */

/*(gdb) run

Starting program: /home/nrh1/code/testt


Program received signal SIGSEGV, Segmentation fault.

0x0804967f in var ()   */

/* function to calculate ttest ttest.c
*/

#include <stdio.h>

#include <math.h>

#define square(x) (x*x)
void ttest(double *data1, unsigned int n1, double *data2, unsigned int n2, double *tstatistic, double *tprob);
int main()


{

/* declare arrays of x and y data from statistical methods in cell biology 2nd edition pg 45*/
double a[8]= {3.2,1.6,5.7,2.8,5.5,1.2,6.1,2.9};
double b[8]= {3.8,1.0,8.4,3.6,5.0,3.5,7.3,4.8};

unsigned int na=8;

unsigned int nb=8;
double* t;
double *prob;


ttest( a, na, b, nb, &t, &prob);

printf("tvalue %f\n", &t);


printf("tvalue %f\n", &prob);

} /* program closing brace    */

/*uses part of stats lib stats.c the relevant functions are shown below ftest code is nt mine but works very well I've used it in other contexts   */

/*

ttest assumes homogenity of variance, uses ftest probability code to calculate probability of the significance of the t statistic

*/


double var(double *data, unsigned int n)

{

unsigned int i;
double average=0.0;
double s, temp, var;

for (i=0; i<n; n++)

average=average+data[n]; /* calculate average   */


average=average/n;


var=temp=s=0.0; /* now variance  */
for (i=0; i<n; n++)

{

s=data[n]-average;

temp=temp+s;

var=var+(s*s);


}
return (var-temp*temp/n)/(n-1);

} /* function closing brace   */
/* end of function */

void ttest(double *data1, unsigned int n1, double *data2, unsigned int n2, double *tstatistic, double *tprob)

{

double nmean(unsigned int count, double *data); /* function declarations within function    */
double pof(double F, unsigned int df1, unsigned int df2);
double var(double *data, unsigned int n);
double svar=0.0;
double var1=0.0;
double var2=0.0;
double mean1=0.0;
double mean2=0.0;
double df=0.0;
/*double *tstatistic=0.0;  */
/*double *tprob=0.0     */


mean1=nmean(n1, data1); /*calculate means for individual data sets    */

mean2=nmean(n2, data2);

var1=var(data1, n1); /*calculate variances for individual data sets     */

var2=var(data2, n2);

df=n1+n2-2; /* calculate overall df                 */

svar=((n1-1)*var1+(n2-1)*var2)/df; /* pooled variance  */
*tstatistic=(mean1-mean2)/sqrt(svar*(1.0/n1+1.0/n2)); /* calculate t value    */
*tprob=pof(((*tstatistic)*(*tstatistic)), 1, df); /*probability        */

} /* function closing brace      */

/* end of function */


/*FUNCTION pof: probability of F */
/*ALGORITHM Compute probability of F ratio.

*/

/*

example if F=1.432923 and df1=3 and df2=5 p=0.337564
*/
double pof(double F, unsigned int df1, unsigned int df2)

{

int i, j;
int a, b;
double w, y, z, d, p;

if (F < F_EPSILON || df1 < 1 || df2 < 1)

p=1.0;
else

{

a = df1%2 ? 1 : 2;

b = df2%2 ? 1 : 2;

w = (F * df1) / df2;


z = 1.0 / (1.0 + w);
if (a == 1)
if (b == 1)

{

p = sqrt (w);

y = I_PI; /* 1 / 3.14159 */

d = y * z / p;

p = 2.0 * y * atan (p);

}
else

{

p = sqrt (w * z);

d = 0.5 * p * z / w;

}
else if (b == 1)

{

p = sqrt (z);

d = 0.5 * z * p;

p = 1.0 - p;

}
else

{

d = z * z;

p = w * z;

}

y = 2.0 * w / z;

for (j = b + 2; j <= df2; j += 2)

{

d *= (1.0 + a / (j - 2.0)) * z;

p = (a == 1 ? p + d * y / (j - 1.0) : (p + w) * z);

}


y = w * z;

z = 2.0 / z;

b = df2 - 2;
for (i = a + 2; i <= df1; i += 2)

{

j = i + b;

d *= y * j / (i - 2.0);

p -= z * d / j;

}
/* correction for approximation errors suggested in certification */
if (p < 0.0)

p = 0.0;
else if (p > 1.0)

p = 1.0;

p= (1.0-p);
return p;

}
return p;

}

标签:code,1.0,int,double,程序,unsigned,计算,test,ttest
From: https://blog.51cto.com/emanlee/8247969

相关文章

  • 现代密码学 - 计算题
    第一章4.设多表代换密码Ci=AMi+B(mod26)中,A是2×2矩阵,B是0矩阵,又知明文“dont”被加密为“elni”,求矩阵A。解:明文对应数字为:3,14,13,19;密文对应数字为4,11,13,8设A为,则由名密文对应关系可得:a11×3+a12×14=4(mod26)a21×3+a22×14=11(mod26)a11×13+a12×19=13(mod26)a21×13+a22×19......
  • C#C++,opencv的dll中detach相关:关于调用dll后程序退出后进程仍然驻留系统列表的问题
    我在c#中调用C++的dll,内部使用了线程并detach使其独立于主线程UI运行。但后来发现程序关闭后,任务列表中的进场依然存在,即app并未实际正常退出。这个问题有很多人碰到和争论,但都没有给出明确的答案。这里提供一个理论解释和绝佳的调试排除方法:根本原因:程序退出之前,系统(或程序员......
  • 2008秋季-计算机软件基础-0901课堂用例
    #include<stdio.h>voidupdate(intxiabiao,intb[],intxinshu);voidcharu(intweizhi,intb[],intcharushu,intshuzuchang);voidmain(){/*顺序存储的线性表-顺序表*/inta[5]={1,2,4,5};inti;intweizhi=2;/*for(i=......
  • 2008秋季-计算机软件基础-0903课堂用例(1)
    #include<stdio.h>voidupdate(intxiabiao,intb[],intxinshu);voidcharu(intweizhi,intb[],intcharushu,intshuzuchang);voidshanchu(intweizhi,intb[],int*changdu);voidshuchu(intaa[],intbiaochang);voidchazhao(int......
  • C语言程序设计 练习题参考答案 第四章 (2) 二维数组
    /*4.165*5矩阵中每行的绝对值最大值,与同行对角线交换*/#include"stdio.h"#include"math.h"voidmain(){inta[5][5]={{1,2,3,4,-5},{3,5,-2,4,2},{4,1,2,3,-2},{1,3,-2,4,6},{2,2,0,7,4}};inti,k,max,sub,temp;/*i循环变量,控制行,k循......
  • C语言程序设计 练习题参考答案 第四章 (3) 字符数组
     /*  例4.19 电文加密,每个字母转换为字母表中循环右移的第三个字母,解法一 */#include"stdio.h"voidmain(){chars[256];inti=0;printf("请输入一行字符,之后按回车键\n");gets(s);while(s[i]!=0){if(s[i]>=65&&s[i]<=87)/*A-W*/......
  • C语言程序设计 练习题参考答案 第五章 (1) 函数定义调用
    /*5.6编写函数,输出所有水仙花数*/#include"stdio.h"intisdaffodil(intn);/*isdaffodil函数原型声明*/voidmain(){inti;for(i=100;i<=999;i++)if(isdaffodil(i))printf("%5d",i);}intisdaffodil(intn)/*is......
  • C语言程序设计 练习题参考答案 第五章 (2) 递归函数
    /*5.10编写函数,求Fibonacci数列的第n项*/#include"stdio.h"intfibonacci(intn);voidmain(){intn;printf("求Fibonacci数列的第n项,请输入n\n");scanf("%d",&n);/*VC6中n要小于?*/printf("Fibonacci数列的第%d项为%d",n,......
  • C语言程序设计 课程实施细则
    C语言程序设计-课程实施细则课程学时:36+20教材:《C语言程序设计教程》  西安交通大学出版社  张毅坤等编著实验指导书:《C语言程序设计教程学习指南与实验指导》西安交通大学出版社  张毅坤等编著金花校区东门口书店有售。 第1章  概述            ......
  • C语言程序设计 练习题参考答案 第三章 (3) 循环结构
    /*3.9求派的值*/#include"stdio.h"voidmain(){intn;doublesum=0;for(n=1;n<=10000;n++){sum=sum+1.0/(4*n-3)-1.0/(4*n-1);}printf("pi的值为%lf\n",4*sum);}/*3.9求派的值*/#include<stdio.......