Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Content-MD5: xmtAukfIhMzCPOmiKcm+Jg== Attached are the two programs for the hybrid methods.Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Content-MD5: b9sE0rwF0tNMwUFu4cXUBQ== Content-Disposition: inline; filename="hybrideqcov.c" Content-Description: hybrideqcov.c X-Content-Name: hybrideqcov.c /********************************************************* * C PROGRAM TO COMPARE TWO TREATMENTS ON MULTIPLE * * ENDPOINTS, BY COMPUTING ADJUSTED P-VALUES USING THE * * HYBRID METHOD OF LOGAN AND TAMHANE (2001), PUBLISHED * * IN THE BIOMETRICAL JOURNAL. * * THIS ALGORITHM USES THE MINIMUM INDIVIDUAL P-VALUE, * * THE OLS P-VALUE, AND THE ALR P-VALUE, IN A CLOSED * * TESTING PROCEDURE. ALL STATISTICS ARE COMPUTED * * UNDER THE ASSUMPTION THAT THE COVARIANCE MATRICES MAY * * NOT BE EQUAL. * *********************************************************/ #include #include #include #include /* Command to compile in Unix: cc -Aa hybrideqcov.c -lm This will create an executable file called a.out. */ /********************************************************* * PARAMETERS TO BE INPUT : * * --sample sizes for each group, n_1 and n_2, where * * group1 is the group expected to have a higher * * response. * * --number of endpoints, cap_k * * --number of bootstrap iterations, cap_m * * --location of datafile, medata * * --location for bootstrap t and s data * * * * Each observation is represented by a row in the data * * file. The first column in the data file should * * indicate a 1 for the group with the higher mean, and a* * 2 for the other group. The remaining cap_k columns * * indicate the responses for the observation in that * * row. * *********************************************************/ #define n_1 54 #define n_2 57 #define cap_k 4 #define cap_m 1000 #define medata "/scr01/brl866/neqex/medata.txt" #define boottdata "/scr01/brl866/neq/boott.dat" #define bootsdata "/scr01/brl866/neq/boots.dat" /* Other parameter declarations */ #define alpha 0.05 #define MAX(a,b) ((a) > (b) ? (a) :(b)) #define MIN(a,b) ((a) < (b) ? (a) :(b)) #define SQ(a) (a*a) #define TINY 1.0e-20 /* Function declarations. All functions follow the main body of the program */ void olstestctp(); void btolstestctp(); void olstest(); void olstestctpneq(); double alrtest(); void alrctp(); double tailprobt(); void normbtolsctp(); void btolstestctp(); double compute_adjp(); double tailprobf(); double bincoeff(); double betai(); double betacf(); double gammln(); void nrerror(); double ran3(); double *dvector(); int *ivector(); double **dmatrix(); int **imatrix(); double **submatrix(); void free_dvector(); void free_ivector(); void free_dmatrix(); void free_imatrix(); void free_submatrix(); double **convert_matrix(); void free_convert_matrix(); void matmult(); void scalmult(); void matadd(); void matcopy(); void ludcmp(); void lubksb(); void ludinv(); void cholesky(); void d_PrintMatrix(); void d_Transpose(); void matmean(); void matcov(); void matpoolcov(); void matsub(); void **delmatind(); void delrmatind(); void gramschmidt(); void main() { long *seed,temp3; int simct,i,j,k,k2,numsig; double **data1,**data2,*ptil,*ptilols,*ptilh,*padjalr,*padjols; double **rawpval; int *sigolsb,*trtmt,cnt1,cnt2; FILE *data1file,*data2file; /* initialize the random number generator */ srand(time(NULL)); temp3=-1*rand(); seed=&temp3; printf("Hybrid Method for Equal Covariance Matrices \n\n"); data1=dmatrix(1,n_1,1,cap_k); data2=dmatrix(1,n_2,1,cap_k); rawpval=dmatrix(1,1,1,cap_k); ptil=dvector(1,cap_k); ptilh=dvector(1,cap_k); padjalr=dvector(1,cap_k); padjols=dvector(1,cap_k); sigolsb=ivector(1,cap_k+1); trtmt=ivector(1,1); /* Read in data */ cnt1=0; cnt2=0; data1file=fopen(medata,"r"); for (j=1;j<=(n_1+n_2);j++) { fscanf(data1file, "%d",&trtmt[1]); if (trtmt[1]==1) { cnt1+=1; for (k=1;k<=cap_k;k++) { fscanf(data1file, "%lf",&data1[cnt1][k]); } } else { cnt2+=1; for (k=1;k<=cap_k;k++) { fscanf(data1file, "%lf",&data2[cnt2][k]); } } } /* To print data files out d_PrintMatrix(data1,n_1,cap_k," %f "); printf("\n\n"); d_PrintMatrix(data2,n_2,cap_k," %f "); */ /* Call function to apply hybrid method */ normbtolsctp(data1,data2,n_1,n_2,cap_k,cap_m,ptil,rawpval,seed,sigolsb,ptilh); /* Call function to apply ALR closed test procedure */ alrctp(data1,data2,cap_k,n_1,n_2,padjalr); /* Call function to apply OLS closed test procedure */ olstestctp(data1,data2,cap_k,n_1,n_2,padjols); /* Print results */ for (k=1;k<=cap_k;k++) { printf("Endpoint k=%d\n",k); printf(" Unadjusted p-value = %f\n",rawpval[1][k]); printf(" Adjusted p-value using max t = %f\n",ptil[k]); printf(" Adjusted p-value using hybrid max t/OLS = %f\n",ptilh[k]); printf(" Adjusted p-value using ALR CTP = %f\n",padjalr[k]); printf(" Adjusted p-value using OLS CTP = %f\n",padjols[k]); } free_dmatrix(data1,1,n_1,1,cap_k); free_dmatrix(data2,1,n_2,1,cap_k); free_dmatrix(rawpval,1,1,1,cap_k); free_dvector(ptil,1,cap_k); free_dvector(ptilols,1,cap_k); free_dvector(padjalr,1,cap_k); free_dvector(padjols,1,cap_k); free_ivector(sigolsb,1,cap_k+1); fclose(data1file); fclose(data2file); } void normbtolsctp(double **x1,double **x2,int n1,int n2,int capk,int capm,double *origptilde,double **rawp,long *idum, int *origsigendpt,double *origptildeh) { int i,j,k,l,m,temp1,ranind; double **Sstar,**S,**xbar1,**xbar2,**sorts; double **y1,**y2,**ystarbar1,**ystarbar2,**sortxbar1,**sortxbar2; double *t,*tstar,*sortt,*bootp,*minbootp,*ptilde,rannum,*issig,*tols; int *rawind,*sortind,step,*sigendpt,*sigendptols,k2,df; double **sortp,**sorty1,**sorty2,**ystar1,**ystar2,*pols,*bootpols,*minbootpols; double **sortx1,**sortx2,*palr,*bootpalr,*sortadjph; double tempd,w1,w2,tempd2,**mnvect1,**mnvect2,**smat1,**smat2,tempmin; int *indendpt,sumindk; double ran3(); void btolstestctp(); FILE *tdata,*nudata,*sdata,*s1data,*s2data,*mn1data,*mn2data; /* double *tempvec; */ rawind=ivector(1,capk); indendpt=ivector(1,capk); sortind=ivector(1,capk); issig=dvector(1,capk); Sstar=dmatrix(1,capk,1,capk); sorts=dmatrix(1,capk,1,capk); S=dmatrix(1,capk,1,capk); y1=dmatrix(1,n1,1,capk); y2=dmatrix(1,n2,1,capk); ystar1=dmatrix(1,n1,1,capk); ystar2=dmatrix(1,n2,1,capk); sorty1=dmatrix(1,n1,1,capk); sorty2=dmatrix(1,n2,1,capk); sortx1=dmatrix(1,n1,1,capk); sortx2=dmatrix(1,n2,1,capk); xbar1=dmatrix(1,1,1,capk); xbar2=dmatrix(1,1,1,capk); ystarbar1=dmatrix(1,1,1,capk); ystarbar2=dmatrix(1,1,1,capk); sortxbar1=dmatrix(1,1,1,capk); sortxbar2=dmatrix(1,1,1,capk); t=dvector(1,capk); tstar=dvector(1,capk); sortt=dvector(1,capk); bootp=dvector(1,capk); pols=dvector(1,capk); palr=dvector(1,capk); bootpols=dvector(1,capk); bootpalr=dvector(1,capk); tols=dvector(1,capk); minbootp=dvector(1,capk); minbootpols=dvector(1,capk); ptilde=dvector(1,capk); sortadjph=dvector(1,capk); sortp=dmatrix(1,1,1,capk); sigendpt=ivector(1,capk); tdata=fopen(boottdata,"w+"); sdata=fopen(bootsdata,"w+"); matmean(x1,n1,capk,xbar1); matmean(x2,n2,capk,xbar2); matpoolcov(x1,n1,capk,x2,n2,capk,xbar1,xbar2,S); /* To print out sample covariance matrices or sample mean vectors d_PrintMatrix(S,capk,capk," %12.11f "); d_PrintMatrix(xbar1,1,capk," %12.11f "); d_PrintMatrix(xbar2,1,capk," %12.11f "); */ /* Compute mean centered vectors to draw from */ for (j=1;j<=n1;j++) { matsub(x1+(j-1),1,capk,xbar1,1,capk,y1+(j-1)); } for (j=1;j<=n2;j++) { matsub(x2+(j-1),1,capk,xbar2,1,capk,y2+(j-1)); } /*compute rawp, order them */ for (k=1;k<=capk;k++) { t[k]=sqrt(n1*n2/(n1+n2))*(xbar1[1][k]-xbar2[1][k])/sqrt((S[k][k])); rawp[1][k]=tailprobt(t[k],n1+n2-2); rawind[k]=1; for (l=1;l=1;k--) { ptilde[k]=MAX(ptilde[k+1],issig[k]/capm); } /* Closed Testing Procedure for hybrid method - calls function btolstestctp */ k=capk; sigendptols=ivector(1,k+1); for (k2=1;k2<=(k+1);k2++) sigendptols[k2]=0; btolstestctp(tdata,sdata,k,n1,n2,sigendptols,sortt,sorts,capk,sortadjph); free_ivector(sigendptols,1,k+1); /* Unsort adjusted p-values */ for (k=1;k<=capk;k++) { temp1=sortind[k]; origptilde[temp1]=ptilde[k]; origptildeh[temp1]=sortadjph[k]; } /* Free up memory*/ fclose(tdata); fclose(sdata); free_ivector(sigendpt,1,capk); free_ivector(indendpt,1,capk); free_ivector(rawind,1,capk); free_ivector(sortind,1,capk); free_dvector(issig,1,capk); free_dmatrix(Sstar,1,capk,1,capk); free_dmatrix(sorts,1,capk,1,capk); free_dmatrix(S,1,capk,1,capk); free_dmatrix(y1,1,n1,1,capk); free_dmatrix(y2,1,n2,1,capk); free_dmatrix(ystar1,1,n1,1,capk); free_dmatrix(ystar2,1,n2,1,capk); free_dmatrix(sorty1,1,n1,1,capk); free_dmatrix(sorty2,1,n2,1,capk); free_dmatrix(sortx1,1,n1,1,capk); free_dmatrix(sortx2,1,n2,1,capk); free_dmatrix(xbar1,1,1,1,capk); free_dmatrix(xbar2,1,1,1,capk); free_dmatrix(sortxbar1,1,1,1,capk); free_dmatrix(sortxbar2,1,1,1,capk); free_dmatrix(ystarbar1,1,1,1,capk); free_dmatrix(ystarbar2,1,1,1,capk); free_dvector(t,1,capk); free_dvector(tstar,1,capk); free_dvector(sortt,1,capk); free_dvector(bootp,1,capk); free_dvector(pols,1,capk); free_dvector(palr,1,capk); free_dvector(tols,1,capk); free_dvector(bootpols,1,capk); free_dvector(bootpalr,1,capk); free_dvector(minbootp,1,capk); free_dvector(minbootpols,1,capk); free_dvector(ptilde,1,capk); free_dmatrix(sortp,1,1,1,capk); } /* Apply Closed Test Procedure using the hybrid method, with only the OLS test and not the ALR test */ void btolstestctp(tfile,sfile,numk,n1,n2,sigols,origt,sigmac,origk,padjh) double *origt,**sigmac; int numk,n1,n2, *sigols; double *padjh; FILE *tfile,*sfile; { int j2,k2,tempdf,i2,allones,allzeros,result,sumindk,*indendpt,tempi,df; double tempnum,tempdenom,zk,tstat,tempd,tempd2; double adjpval,*pind,w1,w2,tempnume,tempdenome; double compute_adjp(); double tailprobt(); indendpt=ivector(1,numk); pind=dvector(1,numk); allones=pow(2,numk)-1; allzeros=0; result=pow(2,numk)-1; /* Initial check to weed out unadjusted p-values >alpha*/ for (k2=1;k2<=numk;k2++) { pind[k2]=tailprobt(origt[k2],n1+n2-2); padjh[k2]=pind[k2]; } for (i2=(allones);i2>=1;i2--) { sumindk=0; for (k2=1;k2<=numk;k2++) { if (i2 & (1 << (k2-1))) indendpt[k2]=1; else indendpt[k2]=0; sumindk+=indendpt[k2]; } if (sumindk>1) { adjpval=compute_adjp(tfile,sfile,indendpt,numk,n1,n2,origt,pind,sigmac,sumindk); for (k2=1;k2<=numk;k2++) { if (indendpt[k2]==1) padjh[k2]=MAX(padjh[k2],adjpval); } /* to print out subset adjusted p-values printf("i2=%d\n",i2); printf("adjpval=%f\n",adjpval); */ } } free_ivector(indendpt,1,numk); free_dvector(pind,1,numk); } /* Compute adjusted p-value using the hybrid test statistic at any stage of the closed test procedure */ double compute_adjp(tfile,sfile,var_ind,kpr,n1,n2,origt,origp,origs,sumindk) double *origt,*origp,**origs; int kpr,n1,n2, *var_ind,sumindk; FILE *tfile,*sfile; { double maxt,bootmaxt,tols,pind,pols,pmin,minp,minbootp,bootpind,bootminp; double bootpols,*boott,**boots,sigind,padj,num,denom; int i,j,k,m,df; char dumstr2[640]; char *dumstr=dumstr2; boott=dvector(1,kpr); boots=dmatrix(1,kpr,1,kpr); rewind(tfile); rewind(sfile); minp=1.0; num=0.0; denom=0.0; for (i=1;i<=kpr;i++) { if (var_ind[i]==1) { minp=MIN(minp,origp[i]); num+=origt[i]; denom+=1.0; for (j=1;j1.0) nrerror("Bad x in routine betai"); if (x==0.0 || x ==1.0) bt=0.0; else bt=exp(gammln(a+b)-gammln(a)-gammln(b)+a*log(x)+b*log(1.0-x)); if (x<((a+1.0)/(a+b+2.0))) return bt*betacf(a,b,x)/a; else return 1.0-bt*betacf(b,a,1.0-x)/b; } double betacf(double a,double b, double x) /*used by betai: Evaluates continued fraction for incomplete beta function by modified Lentz's method*/ { void nrerror(char error_text[]); int m,m2; double aa,c,d,del,h,qab,qam,qap; qab=a+b; qap=a+1.0; qam=a-1.0; c=1.0; d=1.0-qab*x/qap; if (fabs(d) < FPMIN) d=FPMIN; d=1.0/d; h=d; for (m=1;m<=MAXIT;m++) { m2=2*m; aa=m*(b-m)*x/((qam+m2)*(a+m2)); d=1.0+aa*d; if (fabs(d) < FPMIN) d=FPMIN; c=1.0+aa/c; if (fabs(c) < FPMIN) c=FPMIN; d=1.0/d; h *=d*c; aa = -(a+m)*(qab+m)*x/((a+m2)*(qap+m2)); d=1.0+aa*d; if (fabs(d)MAXIT) nrerror("a or b too big, or MAXIT too small in betacf"); return h; } double gammln(double xx) /* Returns the log of the gamma function*/ { double x,y,tmp,ser,tmp2; static double cof[6]={76.18009172947146,-86.50532032941677, 24.01409824083091,-1.231739572450155,0.1208650973866179e-2, -0.5395239384953e-5}; int j; y=x=xx; tmp=x+5.5; tmp-=(x+0.5)*log(tmp); ser=1.000000000190015; for (j=0;j<=5;j++) ser += cof[j]/++y; tmp2= -tmp+log(2.5066282746310005*ser/x); return tmp2; } void nrerror(char error_text[]) /*Numerical Recipes standard error handler */ { fprintf(stderr,"Numerical Recipes run-time error...\n"); fprintf(stderr,"%s\n",error_text); fprintf(stderr,"...now exiting to system...\n"); exit(1); } double tailprobt(double t,int dof) { double df,tpr,tabs,temptpr; df=dof+0.0; tabs=fabs(t); temptpr=betai(df/2.0,0.5,df/(df+tabs*tabs)); if (t>0.0) tpr=temptpr/2.0; else tpr=1.0-temptpr/2.0; return tpr; } /* Tail probabilities of F distribution */ double tailprobf(double f,int dof1,int dof2) { double df1,df2,fpr; df1=dof1+0.0; df2=dof2+0.0; fpr=betai(df2/2.0,df1/2.0,df2/(df2+df1*f)); return fpr; } /* Binomial Coefficients, for use in ALR test */ double bincoeff(int n, int k) { double bnk; int i,num,denom; num=1; denom=1; for (i=n;i>(n-k);i--) { num=num*i; } for (i=1;i<=k;i++) { denom=denom*i; } bnk=num/denom; return bnk; } #undef EPS /* Random number generator from Numerical Recipes in C Call with idum a negative integer to initialize */ #define IM1 2147483563 #define IM2 2147483399 #define AM (1.0/IM1) #define IMM1 (IM1-1) #define IA1 40014 #define IA2 40692 #define IQ1 53668 #define IQ2 52774 #define IR1 12211 #define IR2 3791 #define NTAB 32 #define NDIV (1+IMM1/NTAB) #define EPS 1.2e-7 #define RNMX (1.0-EPS) double ran3(long *idum) { int j; long k; static long idum2=123456789; static long iy=0; static long iv[NTAB]; double tempdoub; if (*idum <=0) /* Initialize*/ { if (-(*idum) <1) *idum=1; /* To prevent idum=0*/ else *idum=-(*idum); idum2=(*idum); for (j=NTAB+7;j>=0;j--) /* Load the shuffle table,after 8 warmups*/ { k=(*idum)/IQ1; *idum=IA1*(*idum-k*IQ1)-k*IR1; if (*idum<0) *idum += IM1; if (j < NTAB) iv[j] = *idum; } iy=iv[0]; } k=(*idum)/IQ1; /* Start here when not initializing*/ *idum=IA1*(*idum-k*IQ1)-k*IR1; /*Compute idum=(IA1*idum)%IM1 without overflow by Schrage's method*/ if (*idum < 0) *idum +=IM1; k=idum2/IQ2; idum2=IA2*(idum2-k*IQ2)-k*IR2; /*Compute idum2=(IA2*idum)%IM2 likewise*/ if (idum2 < 0) idum2 +=IM2; j=iy/NDIV; /* will be in the range 0..NTAB-1*/ iy=iv[j]-idum2; /*Here idum is shuffled, idum and idum2 are combined to generate output*/ iv[j]= *idum; if (iy < 1) iy += IMM1; tempdoub=AM*iy; if (tempdoub > RNMX) tempdoub=RNMX; /*To avoid endpoint values*/ return tempdoub; } #undef IM1 #undef IM2 #undef AM #undef IMM1 #undef IA1 #undef IA2 #undef IQ1 #undef IQ2 #undef IR1 #undef IR2 #undef NTAB #undef NDIV #undef EPS #undef RNMX /*************************************** * Matrix Manipulation Functions Used * * Throughout the Program * ***************************************/ /* assigns memory for a (nh-nl+1)-dimension double precision vector */ double *dvector(nl, nh) int nl, nh; { double *v; v=(double *)malloc((unsigned) (nh-nl+1)*sizeof(double)); if (!v) { printf("Run time error:\n"); printf("allocation failure in dvector()\n"); printf("Exit...\n"); exit(1); } return v-nl; } /* assigns memory for a (nh-nl+1)-dimension integer vector */ int *ivector(nl,nh) int nl,nh; { int *v; v=(int *)malloc((unsigned) (nh-nl+1)*sizeof(int)); if (!v) { printf("Run time error:\n"); printf("allocation failure in ivector()\n"); printf("Exit...\n"); exit(1); } return v-nl; } /* assigns memory for a (nrh-nrl+1)x(nch-ncl+1) double precision matrix */ double **dmatrix(nrl,nrh,ncl,nch) int nrl,nrh,ncl,nch; { int i; double **m; m=(double **)malloc((unsigned) (nrh-nrl+1)*sizeof(double*)); if (!m){ printf("Run time error:\n"); printf("allocation failure 1 in dmatrix()\n"); printf("Exit...\n"); exit(1); } m-=nrl; for (i=nrl;i<=nrh;i++){ m[i]=(double *)malloc((unsigned) (nch-ncl+1)*sizeof(double)); if (!m[i]){ printf("Run time error:\n"); printf("allocation failure 2 in dmatrix()\n"); printf("Exit...\n"); exit(1); } m[i]-=ncl; } return m; } /* assigns memory for a (nrh-nrl+1)x(nch-ncl+1) integer matrix */ int **imatrix(nrl,nrh,ncl,nch) int nrl,nrh,ncl,nch; { int i; int **m; m=(int **)malloc((unsigned) (nrh-nrl+1)*sizeof(int*)); if (!m) { printf("Run time error:\n"); printf("allocation failure 1 in imatrix()\n"); printf("Exit...\n"); exit(1); } m-=nrl; for (i=nrl;i<=nrh;i++) { m[i]=(int *)malloc((unsigned) (nch-ncl+1)*sizeof(int)); if (!m[i]) { printf("Run time error:\n"); printf("allocation failure 2 in imatrix()\n"); printf("Exit...\n"); exit(1); } m[i]-=ncl; } return m; } /* partition a matrix to get a submatrix */ double **submatrix(a,oldrl,oldrh,oldcl,oldch,newrl,newcl) double **a; int oldrl,oldrh,oldcl,oldch,newrl,newcl; { int i,j; double **m; m=(double **)malloc((unsigned) (oldrh-oldrl+1)*sizeof(double*)); if (!m) { printf("Run time error:\n"); printf("allocation failure 1 in submatrix()\n"); printf("Exit...\n"); exit(1); } m-=newrl; for (i=oldrl,j=newrl;i<=oldrh;i++,j++) m[j]=a[i]+oldcl-newcl; return m; } /* eliminate indices of a matrix to get a submatrix */ void **delmatind(a,olddim,indexset,b,newdim) double **a,**b; int olddim,newdim,*indexset; { int i1,i2,j1,j2; i2=1; for (i1=1;i1<=olddim;i1++) { if (indexset[i1]==1) { b[i2][i2]=a[i1][i1]; j2=1; for (j1=1;j1newdim) printf("Error in dimension of submatrix\n"); i2+=1; } } } /* eliminate indices of a vector to get a subvector */ void delvecind(a,olddim,indexset,b,newdim) double *a,*b; int olddim,newdim,*indexset; { int i1,i2; i2=1; for (i1=1;i1<=olddim;i1++) { if (indexset[i1]==1) { b[i2]=a[i1]; if (i2>newdim) printf("Error in dimension of submatrix\n"); i2+=1; } } } /* eliminate indices of a single row matrix to get a submatrix */ void delrmatind(a,olddim,indexset,b,newdim) double **a,**b; int olddim,newdim,*indexset; { int i1,i2; i2=1; for (i1=1;i1<=olddim;i1++) { if (indexset[i1]==1) { b[1][i2]=a[1][i1]; if (i2>newdim) printf("Error in dimension of submatrix\n"); i2+=1; } } } /* free up memory assigned to a double-precision vector */ void free_dvector(v,nl,nh) double *v; int nl,nh; { free((char*) (v+nl)); } /* free up memory assigned to an integer vector */ void free_ivector(v,nl,nh) int *v,nl,nh; { free((char*) (v+nl)); } /* free up memory assigned to a double-precision vector */ void free_dmatrix(m,nrl,nrh,ncl,nch) double **m; int nrl,nrh,ncl,nch; { int i; for (i=nrh; i>=nrl; i--) free((char*) (m[i]+ncl)); free((char *) (m+nrl)); } /* free up memory assigned to an integer matrix */ void free_imatrix(m,nrl,nrh,ncl,nch) int **m; int nrl,nrh,ncl,nch; { int i; for (i=nrh; i>=nrl; i--) free((char*) (m[i]+ncl)); free((char *) (m+nrl)); } /* free up memory assigned to a submatrix */ void free_submatrix(b,nrl,nrh,ncl,nch) double **b; int nrl,nrh,ncl,nch; { free((char*) (b+nrl)); } /* convert a vector into a matrix */ double **convert_matrix(a,nrl,nrh,ncl,nch) double *a; int nrl,nrh,ncl,nch; { int i,j,nrow,ncol; double **m; nrow=nrh-nrl+1; ncol=nch-ncl+1; m=(double **)malloc((unsigned) (nrow)*sizeof(double*)); if (!m) { printf("Runtime error!\n"); printf("allocation failure in convert_matrix()!"); exit(1); } m-=nrl; for (i=1,j=nrl;i<=nrow;i++,j++) m[j]=a+ncol*i-ncl; return m; } /* free up memory assigned to converted matrix*/ void free_convert_matrix(b,nrl,nrh,ncl,nch) double **b; int nrl,nrh,ncl,nch; { free((char*) (b+nrl)); } /* perform lower-upper triangular decomposition */ void ludcmp(a,n,indx,d) int n,*indx; double **a,*d; { int i,imax,j,k; double big,dum,sum,temp; double *vv,*dvector(); void free_dvector(); vv=dvector(1,n); *d=1.0; for (i=1;i<=n;i++) { for (j=1;j<=n;j++) if ((temp=fabs(a[i][j])) > big) big=temp; if (big == 0.0) { printf("singular matrix in routine LUDCMP()\n"); exit(1); } vv[i]=1.0/big; } for(j=1;j<=n;j++){ for (i=1;i= big) { big=dum; imax=i; } } if ( j !=imax){ for (k=i;k<=n;k++){ dum=a[imax][k]; a[imax][k]=a[j][k]; a[j][k]=dum; } *d = -1.0*(*d); vv[imax]=vv[j]; } indx[j]=imax; if (a[j][j] == 0.0) a[j][j]=TINY; if (j != n) { dum=1.0/(a[j][j]); for (i=j+1;i<=n;i++) a[i][j] *= dum; } } free_dvector(vv,1,n); } void lubksb(a,n,indx,b) double **a, b[]; int n, *indx; { int i,j; double sum; for (i=2;i<=n;i++) { sum=b[1]; for (j=1;j=1; i--) { for (j=i+1; j<=n; j++) b[i]=b[i]-a[i][j]*b[j]; b[i]=b[i]/a[i][i]; } } /* using LU decomposition to invert a matrix */ void ludinv(a,y,n) double **a, **y; int n; { double d, *col; int i,j,*indx; void free_dvector(); double *dvector(); int *ivector(); void free_ivector(); col=dvector(1,n); indx=ivector(1,n); d=1.0; ludcmp(a,n,indx,&d); for (j=1; j<=n; j++) { for (i=1; i<=n; i++) col[i]=0.0; col[j]=1.0; lubksb(a,n,indx,col); for (i=1; i<=n; i++) y[i][j]=col[i]; } free_dvector(col,1,n); free_ivector(indx,1,n); } /* performs Cholesky decomposition */ void cholesky(a,y,n) double **a, **y; int n; { int i,j,k; double sum1,sum2; for (i=1; i<=n; i++) for (j=n; j>i; j--) y[i][j]=0.0; for (i=1; i<=n; i++) { for (j=1; j<=i; j++) { sum1=0.0; sum2=0.0; for (k=1; k=1;i2--) { sumindk=0; /* select subset of endpoints */ for (k2=1;k2<=capk;k2++) { if (i2 & (1 << (k2-1))) indendpt[k2]=1; else indendpt[k2]=0; sumindk+=indendpt[k2]; } /* Compute ALR test for subset */ if (sumindk>1) { mnvect1=dmatrix(1,1,1,sumindk); mnvect2=dmatrix(1,1,1,sumindk); smat1=dmatrix(1,sumindk,1,sumindk); delrmatind(meanvect1,capk,indendpt,mnvect1,sumindk); delrmatind(meanvect2,capk,indendpt,mnvect2,sumindk); delmatind(sigmamat1,capk,indendpt,smat1,sumindk); teststat=alrtest(mnvect1,mnvect2,smat1,sumindk,n1,n2); for (k2=1;k2<=capk;k2++) { if (indendpt[k2]==1) padj[k2]=MAX(padj[k2],teststat); } free_dmatrix(mnvect1,1,1,1,sumindk); free_dmatrix(mnvect2,1,1,1,sumindk); free_dmatrix(smat1,1,sumindk,1,sumindk); } } free_ivector(indendpt,1,capk); free_dmatrix(meanvect1,1,1,1,capk); free_dmatrix(meanvect2,1,1,1,capk); free_dmatrix(sigmamat1,1,capk,1,capk); } /* To apply OLS test in a closed test procedure */ void olstestctp(datax1,datax2,numk,n1,n2,padj) double **datax1,**datax2,*padj; int numk,n1,n2; { int j2,k2,tempdf,i2,allones,allzeros,resulte,resultpgt,sumindk,*indendpt,covind; double tempnume,tempdenome,zk,tstat,*tmarg; double pvalue,r1,r2; double **meanvect1,**meanvect2,**sigmamat1,**sigmamat2,**sigmamate; meanvect1=dmatrix(1,1,1,numk); meanvect2=dmatrix(1,1,1,numk); sigmamat1=dmatrix(1,numk,1,numk); sigmamat2=dmatrix(1,numk,1,numk); sigmamate=dmatrix(1,numk,1,numk); indendpt=ivector(1,numk); tmarg=dvector(1,numk); allones=pow(2,numk)-1; allzeros=0; resulte=pow(2,numk)-1; for (k2=1;k2<=numk;k2++) { padj[k2]=0.0; } matmean(datax1,n1,numk,meanvect1); matmean(datax2,n2,numk,meanvect2); matpoolcov(datax1,n1,numk,datax2,n2,numk,meanvect1,meanvect2,sigmamate); /* Apply CTP */ for (i2=allones;i2>=1;i2--) { sumindk=0; /* Select subset of endpoints */ for (k2=1;k2<=numk;k2++) { if (i2 & (1 << (k2-1))) indendpt[k2]=1; else indendpt[k2]=0; sumindk+=indendpt[k2]; } tempnume=0.0; tempdenome=0.0; /* Compute OLS test for subset of endpoints */ for (k2=1;k2<=numk;k2++) { if (indendpt[k2]==1) { tmarg[k2]=(meanvect1[1][k2]-meanvect2[1][k2])*sqrt(n1*n2/((n1+n2)*sigmamate[k2][k2])); tempnume=tempnume+tmarg[k2]; tempdenome=tempdenome+1.0; for (j2=1;j2 #include #include #include /* Command to compile in Unix: cc -Aa hybriduneqcov.c -lm This will create an executable file called a.out. */ /********************************************************* * PARAMETERS TO BE INPUT : * * --sample sizes for each group, n_1 and n_2, where * * group1 is the group expected to have a higher * * response. * * --number of endpoints, cap_k * * --number of bootstrap iterations, cap_m * * --location of datafile, medata * * --location used for various bootstrap results * * * * Each observation is represented by a row in the data * * file. The first column in the data file should * * indicate a 1 for the group with the higher mean, and a* * 2 for the other group. The remaining cap_k columns * * indicate the responses for the observation in that * * row. * *********************************************************/ #define n_1 54 #define n_2 57 #define cap_k 4 #define cap_m 500 #define medata "/scr01/brl866/neqex/medata.txt" #define boottdata "/scr01/brl866/neq/boott.dat" #define bootmn1data "/scr01/brl866/neq/bootmn1.dat" #define bootmn2data "/scr01/brl866/neq/bootmn2.dat" #define bootnudata "/scr01/brl866/neq/bootnu.dat" #define bootsdata "/scr01/brl866/neq/boots.dat" #define boots1data "/scr01/brl866/neq/boots1.dat" #define boots2data "/scr01/brl866/neq/boots2.dat" /* Other parameter declarations */ #define alpha 0.05 #define MAX(a,b) ((a) > (b) ? (a) :(b)) #define MIN(a,b) ((a) < (b) ? (a) :(b)) #define SQ(a) (a*a) #define TINY 1.0e-20 /* Function declarations. All functions follow the main body of the program */ void olstestctp(); void btolstestctp(); void olstestneq(); void olstestctpneq(); double alrtest1(); void alrctp(); double tailprobt(); void normbtolsctp(); void btolstestctp(); double compute_adjp(); double tailprobf(); double bincoeff(); double betai(); double betacf(); double gammln(); void nrerror(); double ran3(); double *dvector(); int *ivector(); double **dmatrix(); int **imatrix(); double **submatrix(); void free_dvector(); void free_ivector(); void free_dmatrix(); void free_imatrix(); void free_submatrix(); double **convert_matrix(); void free_convert_matrix(); void matmult(); void scalmult(); void matadd(); void matcopy(); void ludcmp(); void lubksb(); void ludinv(); void cholesky(); void d_PrintMatrix(); void d_Transpose(); void matmean(); void matcov(); void matpoolcov(); void matsub(); void **delmatind(); void delrmatind(); void gramschmidt(); void main() { long *seed,temp3; int simct,i,j,k,k2,numsig; double **data1,**data2,*ptil,*ptilols,*ptilh,*padjalr,*padjols; double **rawpval; int *sigolsb,*trtmt,cnt1,cnt2; FILE *data1file,*data2file; /* initialize the random number generator */ srand(time(NULL)); temp3=-1*rand(); seed=&temp3; printf("Hybrid Method for Unequal Covariance Matrices \n\n"); data1=dmatrix(1,n_1,1,cap_k); data2=dmatrix(1,n_2,1,cap_k); rawpval=dmatrix(1,1,1,cap_k); ptil=dvector(1,cap_k); ptilh=dvector(1,cap_k); padjalr=dvector(1,cap_k); padjols=dvector(1,cap_k); sigolsb=ivector(1,cap_k+1); trtmt=ivector(1,1); /* Read in data */ cnt1=0; cnt2=0; data1file=fopen(medata,"r"); for (j=1;j<=(n_1+n_2);j++) { fscanf(data1file, "%d",&trtmt[1]); if (trtmt[1]==1) { cnt1+=1; for (k=1;k<=cap_k;k++) { fscanf(data1file, "%lf",&data1[cnt1][k]); } } else { cnt2+=1; for (k=1;k<=cap_k;k++) { fscanf(data1file, "%lf",&data2[cnt2][k]); } } } /* To print data files out d_PrintMatrix(data1,n_1,cap_k," %f "); printf("\n\n"); d_PrintMatrix(data2,n_2,cap_k," %f "); */ /* Call function to apply hybrid method */ normbtolsctp(data1,data2,n_1,n_2,cap_k,cap_m,ptil,rawpval,seed,sigolsb,ptilh); /* Call function to apply ALR closed test procedure */ alrctp(data1,data2,cap_k,n_1,n_2,padjalr); /* Call function to apply OLS closed test procedure */ olstestctpneq(data1,data2,cap_k,n_1,n_2,padjols); /* Print results */ for (k=1;k<=cap_k;k++) { printf("Endpoint k=%d\n",k); printf(" Unadjusted p-value = %f\n",rawpval[1][k]); printf(" Adjusted p-value using max t = %f\n",ptil[k]); printf(" Adjusted p-value using hybrid max t/OLS/ALR = %f\n",ptilh[k]); printf(" Adjusted p-value using ALR CTP = %f\n",padjalr[k]); printf(" Adjusted p-value using OLS CTP = %f\n",padjols[k]); } free_dmatrix(data1,1,n_1,1,cap_k); free_dmatrix(data2,1,n_2,1,cap_k); free_dmatrix(rawpval,1,1,1,cap_k); free_dvector(ptil,1,cap_k); free_dvector(ptilols,1,cap_k); free_dvector(padjalr,1,cap_k); free_dvector(padjols,1,cap_k); free_ivector(sigolsb,1,cap_k+1); fclose(data1file); fclose(data2file); } void normbtolsctp(double **x1,double **x2,int n1,int n2,int capk,int capm,double *origptilde,double **rawp,long *idum, int *origsigendpt,double *origptildeh) { int i,j,k,l,m,temp1,ranind; double **Sstar1,**Sstar2,**Sstare,**S1,**S2,**xbar1,**xbar2,**sorts1,**sorts2,**sortse; double **y1,**y2,**ystarbar1,**ystarbar2,**sortxbar1,**sortxbar2; double *t,*tstar,*sortt,*bootp,*minbootp,*ptilde,rannum,*issig,*tols; int *rawind,*sortind,step,*sigendpt,*sigendptols,k2,df; double **sortp,**sorty1,**sorty2,**ystar1,**ystar2,*pols,*bootpols,*minbootpols; double **sortx1,**sortx2,*palr,*bootpalr,*sortadjph; double tempd,w1,w2,tempd2,**mnvect1,**mnvect2,**smat1,**smat2,tempmin; int *indendpt,sumindk; double ran3(); void btolstestctp(); FILE *tdata,*nudata,*sdata,*s1data,*s2data,*mn1data,*mn2data; /* double *tempvec; */ rawind=ivector(1,capk); indendpt=ivector(1,capk); sortind=ivector(1,capk); issig=dvector(1,capk); Sstar1=dmatrix(1,capk,1,capk); Sstar2=dmatrix(1,capk,1,capk); Sstare=dmatrix(1,capk,1,capk); sorts1=dmatrix(1,capk,1,capk); sorts2=dmatrix(1,capk,1,capk); sortse=dmatrix(1,capk,1,capk); S1=dmatrix(1,capk,1,capk); S2=dmatrix(1,capk,1,capk); y1=dmatrix(1,n1,1,capk); y2=dmatrix(1,n2,1,capk); ystar1=dmatrix(1,n1,1,capk); ystar2=dmatrix(1,n2,1,capk); sorty1=dmatrix(1,n1,1,capk); sorty2=dmatrix(1,n2,1,capk); sortx1=dmatrix(1,n1,1,capk); sortx2=dmatrix(1,n2,1,capk); xbar1=dmatrix(1,1,1,capk); xbar2=dmatrix(1,1,1,capk); ystarbar1=dmatrix(1,1,1,capk); ystarbar2=dmatrix(1,1,1,capk); sortxbar1=dmatrix(1,1,1,capk); sortxbar2=dmatrix(1,1,1,capk); t=dvector(1,capk); tstar=dvector(1,capk); sortt=dvector(1,capk); bootp=dvector(1,capk); pols=dvector(1,capk); palr=dvector(1,capk); bootpols=dvector(1,capk); bootpalr=dvector(1,capk); tols=dvector(1,capk); minbootp=dvector(1,capk); minbootpols=dvector(1,capk); ptilde=dvector(1,capk); sortadjph=dvector(1,capk); sortp=dmatrix(1,1,1,capk); sigendpt=ivector(1,capk); tdata=fopen(boottdata,"w+"); mn1data=fopen(bootmn1data,"w+"); mn2data=fopen(bootmn2data,"w+"); nudata=fopen(bootnudata,"w+"); sdata=fopen(bootsdata,"w+"); s1data=fopen(boots1data,"w+"); s2data=fopen(boots2data,"w+"); matmean(x1,n1,capk,xbar1); matmean(x2,n2,capk,xbar2); matcov(x1,n1,capk,xbar1,S1); matcov(x2,n2,capk,xbar2,S2); /* To print out sample covariance matrices or sample mean vectors d_PrintMatrix(S1,capk,capk," %12.11f "); d_PrintMatrix(S2,capk,capk," %12.11f "); d_PrintMatrix(xbar1,1,capk," %12.11f "); d_PrintMatrix(xbar2,1,capk," %12.11f "); */ /* Compute mean centered vectors to draw from */ for (j=1;j<=n1;j++) { matsub(x1+(j-1),1,capk,xbar1,1,capk,y1+(j-1)); } for (j=1;j<=n2;j++) { matsub(x2+(j-1),1,capk,xbar2,1,capk,y2+(j-1)); } /*compute rawp, order them */ for (k=1;k<=capk;k++) { t[k]=(xbar1[1][k]-xbar2[1][k])/sqrt((S1[k][k]/n1+S2[k][k]/n2)); w1=S1[k][k]/n1; w2=S2[k][k]/n2; tempd2=w1+w2; tempd=SQ(tempd2)/(SQ(w1)/(n1-1)+SQ(w2)/(n2-1)); df=ceil(tempd); rawp[1][k]=tailprobt(t[k],df); rawind[k]=1; for (l=1;l=1;k--) { ptilde[k]=MAX(ptilde[k+1],issig[k]/capm); } /* Closed Testing Procedure for hybrid method - calls function btolstestctp */ k=capk; sigendptols=ivector(1,k+1); for (k2=1;k2<=(k+1);k2++) sigendptols[k2]=0; btolstestctp(tdata,mn1data,mn2data,nudata,sdata,s1data,s2data,k,n1,n2,sigendptols,sortt,sortxbar1,sortxbar2,sorts1,sorts2,sortse,capk,sortadjph); free_ivector(sigendptols,1,k+1); /* Unsort adjusted p-values */ for (k=1;k<=capk;k++) { temp1=sortind[k]; origptilde[temp1]=ptilde[k]; origptildeh[temp1]=sortadjph[k]; } /* Free up memory*/ fclose(tdata); fclose(mn1data); fclose(mn2data); fclose(nudata); fclose(sdata); fclose(s1data); fclose(s2data); free_ivector(sigendpt,1,capk); free_ivector(indendpt,1,capk); free_ivector(rawind,1,capk); free_ivector(sortind,1,capk); free_dvector(issig,1,capk); free_dmatrix(Sstar1,1,capk,1,capk); free_dmatrix(Sstar2,1,capk,1,capk); free_dmatrix(sorts1,1,capk,1,capk); free_dmatrix(sorts2,1,capk,1,capk); free_dmatrix(sortse,1,capk,1,capk); free_dmatrix(S1,1,capk,1,capk); free_dmatrix(S2,1,capk,1,capk); free_dmatrix(y1,1,n1,1,capk); free_dmatrix(y2,1,n2,1,capk); free_dmatrix(ystar1,1,n1,1,capk); free_dmatrix(ystar2,1,n2,1,capk); free_dmatrix(sorty1,1,n1,1,capk); free_dmatrix(sorty2,1,n2,1,capk); free_dmatrix(sortx1,1,n1,1,capk); free_dmatrix(sortx2,1,n2,1,capk); free_dmatrix(xbar1,1,1,1,capk); free_dmatrix(xbar2,1,1,1,capk); free_dmatrix(sortxbar1,1,1,1,capk); free_dmatrix(sortxbar2,1,1,1,capk); free_dmatrix(ystarbar1,1,1,1,capk); free_dmatrix(ystarbar2,1,1,1,capk); free_dvector(t,1,capk); free_dvector(tstar,1,capk); free_dvector(sortt,1,capk); free_dvector(bootp,1,capk); free_dvector(pols,1,capk); free_dvector(palr,1,capk); free_dvector(tols,1,capk); free_dvector(bootpols,1,capk); free_dvector(bootpalr,1,capk); free_dvector(minbootp,1,capk); free_dvector(minbootpols,1,capk); free_dvector(ptilde,1,capk); free_dmatrix(sortp,1,1,1,capk); } /* Apply Closed Test Procedure using the hybrid method */ void btolstestctp(tfile,mn1file,mn2file,nufile,sfile,s1file,s2file,numk,n1,n2,sigols,origt,origmn1,origmn2,origs1,origs2,sigmac,origk,padjh) double *origt,*origmn1,*origmn2,**origs1,**origs2,**sigmac; int numk,n1,n2, *sigols; double *padjh; FILE *tfile,*mn1file,*mn2file,*nufile,*sfile,*s1file,*s2file; { int j2,k2,tempdf,i2,allones,allzeros,result,sumindk,*indendpt,tempi,df; double tempnum,tempdenom,zk,tstat,tempd,tempd2; double adjpval,*pind,w1,w2,tempnume,tempdenome; double compute_adjp(); double tailprobt(); double alrtest1(); indendpt=ivector(1,numk); pind=dvector(1,numk); allones=pow(2,numk)-1; allzeros=0; result=pow(2,numk)-1; /* Initial check to weed out unadjusted p-values >alpha*/ for (k2=1;k2<=numk;k2++) { w1=origs1[k2][k2]/n1; w2=origs2[k2][k2]/n2; tempd2=w1+w2; tempd=SQ(tempd2)/(SQ(w1)/(n1-1)+SQ(w2)/(n2-1)); df=ceil(tempd); pind[k2]=tailprobt(origt[k2],df); padjh[k2]=pind[k2]; } for (i2=(allones);i2>=1;i2--) { sumindk=0; for (k2=1;k2<=numk;k2++) { if (i2 & (1 << (k2-1))) indendpt[k2]=1; else indendpt[k2]=0; sumindk+=indendpt[k2]; } if (sumindk>1) { adjpval=compute_adjp(tfile,mn1file,mn2file,nufile,sfile,s1file,s2file,indendpt,numk,n1,n2,origt,origmn1,origmn2,pind,sigmac,origs1,origs2,sumindk); for (k2=1;k2<=numk;k2++) { if (indendpt[k2]==1) padjh[k2]=MAX(padjh[k2],adjpval); } /* to print out subset adjusted p-values printf("i2=%d\n",i2); printf("adjpval=%f\n",adjpval); */ } } free_ivector(indendpt,1,numk); free_dvector(pind,1,numk); } /* Compute adjusted p-value using the hybrid test statistic at any stage of the closed test procedure */ double compute_adjp(tfile,mn1file,mn2file,nufile,sfile,s1file,s2file,var_ind,kpr,n1,n2,origt,origmn1,origmn2,origp,origse,origs1,origs2,sumindk) double *origt,*origmn1,*origmn2,*origp,**origse,**origs1,**origs2; int kpr,n1,n2, *var_ind,sumindk; FILE *tfile,*mn1file,*mn2file,*nufile,*sfile,*s1file,*s2file; { double maxt,bootmaxt,tols,pind,pols,pmin,minp,minbootp,bootpind,bootminp; double bootpols,*boott,**bootmn1,**bootmn2,**bootse,**boots1,**boots2,sigind,padj,num,denom,w1,w2; double **mnvect1,**mnvect2,**smat1,**smat2,palr,bootpalr; int i,j,k,m,df,*bootnu; char dumstr2[640]; char *dumstr=dumstr2; boott=dvector(1,kpr); bootmn1=dmatrix(1,1,1,kpr); bootmn2=dmatrix(1,1,1,kpr); bootnu=ivector(1,kpr); bootse=dmatrix(1,kpr,1,kpr); boots1=dmatrix(1,kpr,1,kpr); boots2=dmatrix(1,kpr,1,kpr); rewind(tfile); rewind(mn1file); rewind(mn2file); rewind(nufile); rewind(sfile); rewind(s1file); rewind(s2file); minp=1.0; num=0.0; denom=0.0; for (i=1;i<=kpr;i++) { if (var_ind[i]==1) { minp=MIN(minp,origp[i]); num+=origt[i]; denom+=1.0; for (j=1;j1.0) nrerror("Bad x in routine betai"); if (x==0.0 || x ==1.0) bt=0.0; else bt=exp(gammln(a+b)-gammln(a)-gammln(b)+a*log(x)+b*log(1.0-x)); if (x<((a+1.0)/(a+b+2.0))) return bt*betacf(a,b,x)/a; else return 1.0-bt*betacf(b,a,1.0-x)/b; } double betacf(double a,double b, double x) /*used by betai: Evaluates continued fraction for incomplete beta function by modified Lentz's method*/ { void nrerror(char error_text[]); int m,m2; double aa,c,d,del,h,qab,qam,qap; qab=a+b; qap=a+1.0; qam=a-1.0; c=1.0; d=1.0-qab*x/qap; if (fabs(d) < FPMIN) d=FPMIN; d=1.0/d; h=d; for (m=1;m<=MAXIT;m++) { m2=2*m; aa=m*(b-m)*x/((qam+m2)*(a+m2)); d=1.0+aa*d; if (fabs(d) < FPMIN) d=FPMIN; c=1.0+aa/c; if (fabs(c) < FPMIN) c=FPMIN; d=1.0/d; h *=d*c; aa = -(a+m)*(qab+m)*x/((a+m2)*(qap+m2)); d=1.0+aa*d; if (fabs(d)MAXIT) nrerror("a or b too big, or MAXIT too small in betacf"); return h; } double gammln(double xx) /* Returns the log of the gamma function*/ { double x,y,tmp,ser,tmp2; static double cof[6]={76.18009172947146,-86.50532032941677, 24.01409824083091,-1.231739572450155,0.1208650973866179e-2, -0.5395239384953e-5}; int j; y=x=xx; tmp=x+5.5; tmp-=(x+0.5)*log(tmp); ser=1.000000000190015; for (j=0;j<=5;j++) ser += cof[j]/++y; tmp2= -tmp+log(2.5066282746310005*ser/x); return tmp2; } void nrerror(char error_text[]) /*Numerical Recipes standard error handler */ { fprintf(stderr,"Numerical Recipes run-time error...\n"); fprintf(stderr,"%s\n",error_text); fprintf(stderr,"...now exiting to system...\n"); exit(1); } double tailprobt(double t,int dof) { double df,tpr,tabs,temptpr; df=dof+0.0; tabs=fabs(t); temptpr=betai(df/2.0,0.5,df/(df+tabs*tabs)); if (t>0.0) tpr=temptpr/2.0; else tpr=1.0-temptpr/2.0; return tpr; } /* Tail probabilities of F distribution */ double tailprobf(double f,int dof1,int dof2) { double df1,df2,fpr; df1=dof1+0.0; df2=dof2+0.0; fpr=betai(df2/2.0,df1/2.0,df2/(df2+df1*f)); return fpr; } /* Binomial Coefficients, for use in ALR test */ double bincoeff(int n, int k) { double bnk; int i,num,denom; num=1; denom=1; for (i=n;i>(n-k);i--) { num=num*i; } for (i=1;i<=k;i++) { denom=denom*i; } bnk=num/denom; return bnk; } #undef EPS /* Random number generator from Numerical Recipes in C Call with idum a negative integer to initialize */ #define IM1 2147483563 #define IM2 2147483399 #define AM (1.0/IM1) #define IMM1 (IM1-1) #define IA1 40014 #define IA2 40692 #define IQ1 53668 #define IQ2 52774 #define IR1 12211 #define IR2 3791 #define NTAB 32 #define NDIV (1+IMM1/NTAB) #define EPS 1.2e-7 #define RNMX (1.0-EPS) double ran3(long *idum) { int j; long k; static long idum2=123456789; static long iy=0; static long iv[NTAB]; double tempdoub; if (*idum <=0) /* Initialize*/ { if (-(*idum) <1) *idum=1; /* To prevent idum=0*/ else *idum=-(*idum); idum2=(*idum); for (j=NTAB+7;j>=0;j--) /* Load the shuffle table,after 8 warmups*/ { k=(*idum)/IQ1; *idum=IA1*(*idum-k*IQ1)-k*IR1; if (*idum<0) *idum += IM1; if (j < NTAB) iv[j] = *idum; } iy=iv[0]; } k=(*idum)/IQ1; /* Start here when not initializing*/ *idum=IA1*(*idum-k*IQ1)-k*IR1; /*Compute idum=(IA1*idum)%IM1 without overflow by Schrage's method*/ if (*idum < 0) *idum +=IM1; k=idum2/IQ2; idum2=IA2*(idum2-k*IQ2)-k*IR2; /*Compute idum2=(IA2*idum)%IM2 likewise*/ if (idum2 < 0) idum2 +=IM2; j=iy/NDIV; /* will be in the range 0..NTAB-1*/ iy=iv[j]-idum2; /*Here idum is shuffled, idum and idum2 are combined to generate output*/ iv[j]= *idum; if (iy < 1) iy += IMM1; tempdoub=AM*iy; if (tempdoub > RNMX) tempdoub=RNMX; /*To avoid endpoint values*/ return tempdoub; } #undef IM1 #undef IM2 #undef AM #undef IMM1 #undef IA1 #undef IA2 #undef IQ1 #undef IQ2 #undef IR1 #undef IR2 #undef NTAB #undef NDIV #undef EPS #undef RNMX /*************************************** * Matrix Manipulation Functions Used * * Throughout the Program * ***************************************/ /* assigns memory for a (nh-nl+1)-dimension double precision vector */ double *dvector(nl, nh) int nl, nh; { double *v; v=(double *)malloc((unsigned) (nh-nl+1)*sizeof(double)); if (!v) { printf("Run time error:\n"); printf("allocation failure in dvector()\n"); printf("Exit...\n"); exit(1); } return v-nl; } /* assigns memory for a (nh-nl+1)-dimension integer vector */ int *ivector(nl,nh) int nl,nh; { int *v; v=(int *)malloc((unsigned) (nh-nl+1)*sizeof(int)); if (!v) { printf("Run time error:\n"); printf("allocation failure in ivector()\n"); printf("Exit...\n"); exit(1); } return v-nl; } /* assigns memory for a (nrh-nrl+1)x(nch-ncl+1) double precision matrix */ double **dmatrix(nrl,nrh,ncl,nch) int nrl,nrh,ncl,nch; { int i; double **m; m=(double **)malloc((unsigned) (nrh-nrl+1)*sizeof(double*)); if (!m){ printf("Run time error:\n"); printf("allocation failure 1 in dmatrix()\n"); printf("Exit...\n"); exit(1); } m-=nrl; for (i=nrl;i<=nrh;i++){ m[i]=(double *)malloc((unsigned) (nch-ncl+1)*sizeof(double)); if (!m[i]){ printf("Run time error:\n"); printf("allocation failure 2 in dmatrix()\n"); printf("Exit...\n"); exit(1); } m[i]-=ncl; } return m; } /* assigns memory for a (nrh-nrl+1)x(nch-ncl+1) integer matrix */ int **imatrix(nrl,nrh,ncl,nch) int nrl,nrh,ncl,nch; { int i; int **m; m=(int **)malloc((unsigned) (nrh-nrl+1)*sizeof(int*)); if (!m) { printf("Run time error:\n"); printf("allocation failure 1 in imatrix()\n"); printf("Exit...\n"); exit(1); } m-=nrl; for (i=nrl;i<=nrh;i++) { m[i]=(int *)malloc((unsigned) (nch-ncl+1)*sizeof(int)); if (!m[i]) { printf("Run time error:\n"); printf("allocation failure 2 in imatrix()\n"); printf("Exit...\n"); exit(1); } m[i]-=ncl; } return m; } /* partition a matrix to get a submatrix */ double **submatrix(a,oldrl,oldrh,oldcl,oldch,newrl,newcl) double **a; int oldrl,oldrh,oldcl,oldch,newrl,newcl; { int i,j; double **m; m=(double **)malloc((unsigned) (oldrh-oldrl+1)*sizeof(double*)); if (!m) { printf("Run time error:\n"); printf("allocation failure 1 in submatrix()\n"); printf("Exit...\n"); exit(1); } m-=newrl; for (i=oldrl,j=newrl;i<=oldrh;i++,j++) m[j]=a[i]+oldcl-newcl; return m; } /* eliminate indices of a matrix to get a submatrix */ void **delmatind(a,olddim,indexset,b,newdim) double **a,**b; int olddim,newdim,*indexset; { int i1,i2,j1,j2; i2=1; for (i1=1;i1<=olddim;i1++) { if (indexset[i1]==1) { b[i2][i2]=a[i1][i1]; j2=1; for (j1=1;j1newdim) printf("Error in dimension of submatrix\n"); i2+=1; } } } /* eliminate indices of a vector to get a subvector */ void delvecind(a,olddim,indexset,b,newdim) double *a,*b; int olddim,newdim,*indexset; { int i1,i2; i2=1; for (i1=1;i1<=olddim;i1++) { if (indexset[i1]==1) { b[i2]=a[i1]; if (i2>newdim) printf("Error in dimension of submatrix\n"); i2+=1; } } } /* eliminate indices of a single row matrix to get a submatrix */ void delrmatind(a,olddim,indexset,b,newdim) double **a,**b; int olddim,newdim,*indexset; { int i1,i2; i2=1; for (i1=1;i1<=olddim;i1++) { if (indexset[i1]==1) { b[1][i2]=a[1][i1]; if (i2>newdim) printf("Error in dimension of submatrix\n"); i2+=1; } } } /* free up memory assigned to a double-precision vector */ void free_dvector(v,nl,nh) double *v; int nl,nh; { free((char*) (v+nl)); } /* free up memory assigned to an integer vector */ void free_ivector(v,nl,nh) int *v,nl,nh; { free((char*) (v+nl)); } /* free up memory assigned to a double-precision vector */ void free_dmatrix(m,nrl,nrh,ncl,nch) double **m; int nrl,nrh,ncl,nch; { int i; for (i=nrh; i>=nrl; i--) free((char*) (m[i]+ncl)); free((char *) (m+nrl)); } /* free up memory assigned to an integer matrix */ void free_imatrix(m,nrl,nrh,ncl,nch) int **m; int nrl,nrh,ncl,nch; { int i; for (i=nrh; i>=nrl; i--) free((char*) (m[i]+ncl)); free((char *) (m+nrl)); } /* free up memory assigned to a submatrix */ void free_submatrix(b,nrl,nrh,ncl,nch) double **b; int nrl,nrh,ncl,nch; { free((char*) (b+nrl)); } /* convert a vector into a matrix */ double **convert_matrix(a,nrl,nrh,ncl,nch) double *a; int nrl,nrh,ncl,nch; { int i,j,nrow,ncol; double **m; nrow=nrh-nrl+1; ncol=nch-ncl+1; m=(double **)malloc((unsigned) (nrow)*sizeof(double*)); if (!m) { printf("Runtime error!\n"); printf("allocation failure in convert_matrix()!"); exit(1); } m-=nrl; for (i=1,j=nrl;i<=nrow;i++,j++) m[j]=a+ncol*i-ncl; return m; } /* free up memory assigned to converted matrix*/ void free_convert_matrix(b,nrl,nrh,ncl,nch) double **b; int nrl,nrh,ncl,nch; { free((char*) (b+nrl)); } /* perform lower-upper triangular decomposition */ void ludcmp(a,n,indx,d) int n,*indx; double **a,*d; { int i,imax,j,k; double big,dum,sum,temp; double *vv,*dvector(); void free_dvector(); vv=dvector(1,n); *d=1.0; for (i=1;i<=n;i++) { for (j=1;j<=n;j++) if ((temp=fabs(a[i][j])) > big) big=temp; if (big == 0.0) { printf("singular matrix in routine LUDCMP()\n"); exit(1); } vv[i]=1.0/big; } for(j=1;j<=n;j++){ for (i=1;i= big) { big=dum; imax=i; } } if ( j !=imax){ for (k=i;k<=n;k++){ dum=a[imax][k]; a[imax][k]=a[j][k]; a[j][k]=dum; } *d = -1.0*(*d); vv[imax]=vv[j]; } indx[j]=imax; if (a[j][j] == 0.0) a[j][j]=TINY; if (j != n) { dum=1.0/(a[j][j]); for (i=j+1;i<=n;i++) a[i][j] *= dum; } } free_dvector(vv,1,n); } void lubksb(a,n,indx,b) double **a, b[]; int n, *indx; { int i,j; double sum; for (i=2;i<=n;i++) { sum=b[1]; for (j=1;j=1; i--) { for (j=i+1; j<=n; j++) b[i]=b[i]-a[i][j]*b[j]; b[i]=b[i]/a[i][i]; } } /* using LU decomposition to invert a matrix */ void ludinv(a,y,n) double **a, **y; int n; { double d, *col; int i,j,*indx; void free_dvector(); double *dvector(); int *ivector(); void free_ivector(); col=dvector(1,n); indx=ivector(1,n); d=1.0; ludcmp(a,n,indx,&d); for (j=1; j<=n; j++) { for (i=1; i<=n; i++) col[i]=0.0; col[j]=1.0; lubksb(a,n,indx,col); for (i=1; i<=n; i++) y[i][j]=col[i]; } free_dvector(col,1,n); free_ivector(indx,1,n); } /* performs Cholesky decomposition */ void cholesky(a,y,n) double **a, **y; int n; { int i,j,k; double sum1,sum2; for (i=1; i<=n; i++) for (j=n; j>i; j--) y[i][j]=0.0; for (i=1; i<=n; i++) { for (j=1; j<=i; j++) { sum1=0.0; sum2=0.0; for (k=1; k=1;i2--) { sumindk=0; /* select subset of endpoints */ for (k2=1;k2<=capk;k2++) { if (i2 & (1 << (k2-1))) indendpt[k2]=1; else indendpt[k2]=0; sumindk+=indendpt[k2]; } /* Compute ALR test for subset */ if (sumindk>1) { mnvect1=dmatrix(1,1,1,sumindk); mnvect2=dmatrix(1,1,1,sumindk); smat1=dmatrix(1,sumindk,1,sumindk); smat2=dmatrix(1,sumindk,1,sumindk); delrmatind(meanvect1,capk,indendpt,mnvect1,sumindk); delrmatind(meanvect2,capk,indendpt,mnvect2,sumindk); delmatind(sigmamat1,capk,indendpt,smat1,sumindk); delmatind(sigmamat2,capk,indendpt,smat2,sumindk); teststat=alrtest1(mnvect1,mnvect2,smat1,smat2,sumindk,n1,n2); for (k2=1;k2<=capk;k2++) { if (indendpt[k2]==1) padj[k2]=MAX(padj[k2],teststat); } free_dmatrix(mnvect1,1,1,1,sumindk); free_dmatrix(mnvect2,1,1,1,sumindk); free_dmatrix(smat1,1,sumindk,1,sumindk); free_dmatrix(smat2,1,sumindk,1,sumindk); } } free_ivector(indendpt,1,capk); free_dmatrix(meanvect1,1,1,1,capk); free_dmatrix(meanvect2,1,1,1,capk); free_dmatrix(sigmamat1,1,capk,1,capk); free_dmatrix(sigmamat2,1,capk,1,capk); } /* To apply OLS test in a closed test procedure */ void olstestctpneq(datax1,datax2,numk,n1,n2,padj) double **datax1,**datax2,*padj; int numk,n1,n2; { int j2,k2,tempdf,i2,allones,allzeros,resulte,resultpgt,sumindk,*indendpt,covind; double tempnume,tempdenome,zk,tstat,*tmarg; double pvalue,r1,r2; double **meanvect1,**meanvect2,**sigmamat1,**sigmamat2,**sigmamate; meanvect1=dmatrix(1,1,1,numk); meanvect2=dmatrix(1,1,1,numk); sigmamat1=dmatrix(1,numk,1,numk); sigmamat2=dmatrix(1,numk,1,numk); sigmamate=dmatrix(1,numk,1,numk); indendpt=ivector(1,numk); tmarg=dvector(1,numk); allones=pow(2,numk)-1; allzeros=0; resulte=pow(2,numk)-1; for (k2=1;k2<=numk;k2++) { padj[k2]=0.0; } matmean(datax1,n1,numk,meanvect1); matmean(datax2,n2,numk,meanvect2); matcov(datax1,n1,numk,meanvect1,sigmamat1); matcov(datax2,n2,numk,meanvect2,sigmamat2); /* Compute appropriate covariance matrix */ for (k2=1;k2<=numk;k2++) { for (j2=1;j2=1;i2--) { sumindk=0; /* Select subset of endpoints */ for (k2=1;k2<=numk;k2++) { if (i2 & (1 << (k2-1))) indendpt[k2]=1; else indendpt[k2]=0; sumindk+=indendpt[k2]; } tempnume=0.0; tempdenome=0.0; /* Compute OLS test for subset of endpoints */ for (k2=1;k2<=numk;k2++) { if (indendpt[k2]==1) { tmarg[k2]=(meanvect1[1][k2]-meanvect2[1][k2])/sqrt(sigmamat1[k2][k2]/n1+sigmamat2[k2][k2]/n2); tempnume=tempnume+tmarg[k2]; tempdenome=tempdenome+1.0; for (j2=1;j2