#include "R.h" #include #include #include #include #include #include #include using namespace std; extern "C"{ double ilogit(double x) { return(exp(x)/(1+exp(x))); } double runif(int size) { if(size==1) { int random_integer; int lowest=1, highest=1000; int range=(highest-lowest)+1; random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0)); double random_number = double(random_integer)/double(range+1); return(random_number); } if(size>1) cout << "size is greater than 1!" << endl; } /* int random_int(int lower_bound,int upper_bound) { srand((unsigned)time(0)+rand()); int random_integer; int lowest=lower_bound, highest=upper_bound; int range=(highest-lowest)+1; random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0)); return(random_integer); } */ int rbinom(int size,double p) { int random_integer; int lowest=1, highest=1000; int range=(highest-lowest)+1; random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0)); double random_number = double(random_integer)/double(range+1); if(size==1) { double cut_1=(1-p); double cut_2=p; if(random_number <= cut_1) return(0); else return (1); } if(size==2) { double cut_1=(1-p)*(1-p); double cut_2=2*p*(1-p); double cut_3=p*p; if(random_number <= cut_1) return (0); else if(random_number <=cut_1+cut_2) return (1); else return (2); } if(size>2) cout << "size is greater than 2!!" << endl; } double f(int i,int x,double hat_p1,double hat_p2) { if(i==1) return(pow(2.0,double(x==1))*pow(hat_p1,double((x==1)+2*(x==2)))*pow((1-hat_p1),double(2*(x==0)+(x==1)))); if(i==2) return(pow(2.0,double(x==1))*pow(hat_p2,double((x==1)+2*(x==2)))*pow((1-hat_p2),double(2*(x==0)+(x==1)))); } double mean_2(int array[],int N) { double sum = 0 ; for (int i = 1; i <= N; i++) sum = sum + double(array [i-1]); return sum/double(N); } double var(int array[], int N) { double sum = 0; double STD_DEV = 0; for (int i = 1; i <= N; i++) { sum = sum + double(array [i-1]); } double average = sum/double(N); for(int i = 1; i <= N; i++) { STD_DEV+=(double(array[i-1])-average)*(double(array[i-1])-average); } return(STD_DEV/double(N-1)); } double maximum(double stat[],int nSNP,int SNP0) { double max=-100; for(int i=0;imax) max = stat[i]; } } return max; } double mean(double stat[],int nSNP,int SNP0) { double m=0; for(int i=0;imax) { max=likelihood_record[i]; which=i; } } double likelihood_alternative = max-C*log(hat_mixture_record[which][0]); double likelihood_null=0; for(int j=1;j<=n;j++) { likelihood_null= likelihood_null + log(f(2,data_case[j-1],hat_p2,hat_p2)); } double test_statistics = 2*(likelihood_alternative-likelihood_null); if((rep % (B+1)==1)&&(allele==SNP0-1)) { hat_p1_backup=hat_p1_record[which]; hat_p2_backup=hat_p2; mixing_proportion_backup=hat_mixture_record[which][0]; } double test_statistics_z = (mean_2(data_case,n)-mean_2(data_control,m))/ sqrt(var(data_case,n)/n+var(data_control,m)/m); double p1_CA = double(r1)/double(n); double p2_CA = double(r2)/double(n); double q1_CA = double(s1)/double(m); double q2_CA = double(s2)/double(m); double optimal_x = 0.5; // # since it is additive model double CA_numerator = pow((optimal_x*(m*r1-n*s1)+(m*r2-n*s2)),2.0)/pow(double(n+m),2.0); double CA_denominator = n*m/(pow(double(n+m),3.0))* ((n+m)*(pow(optimal_x,2.0)*(r1+s1)+(r2+s2))-pow((optimal_x*(r1+s1)+(r2+s2)),2.0)); double test_statistics_CA = CA_numerator/CA_denominator; replicate_test_statistics[(rep-1)*nSNP+allele] = test_statistics; replicate_test_statistics_z[(rep-1)*nSNP+allele] = test_statistics_z; replicate_test_statistics_CA[(rep-1)*nSNP+allele] = test_statistics_CA; } // the loop for allele if(rep % (B+1)==0) { double * replicate_test_statistics_max=new double[B+1]; double * replicate_test_statistics_z_max=new double[B+1]; double * replicate_test_statistics_CA_max=new double[B+1]; double * replicate_test_statistics_mean=new double[B+1]; double * replicate_test_statistics_z_mean=new double[B+1]; double * replicate_test_statistics_CA_mean=new double[B+1]; int * replicate_test_statistics_count = new int[nSNP]; for(int i=1;i<=nSNP;i++) replicate_test_statistics_count[i-1]=0; int replicate_test_statistics_max_count=0; int replicate_test_statistics_mean_count=0; for(int print=1;print<=(1+B);print++) { if(print>1) { for(int i=0;i