We calculate p values \(p_j\) for each permutation \(r\) and compute \(T^r=\min_{j=1,\ldots,10} p^r_j\)
T_obs=min(pvalues_obs)
replicates0=matrix(NA,nrow=500,ncol=1000) #stores one permutation per column for later use
pvalues0=matrix(NA,nrow=10,ncol=1000) #stores the SNP p values in columns, one per replicate
for(repl in 1:1000){
perm=sample(pheno)
replicates0[,repl]=perm
for(snp in 1:10){
pvalues0[snp,repl]=fisher.test(perm,geno[,snp])$p
}
}
T_sample0=apply(pvalues0,2,min)
At last we calculate the empirical p value for \(T\) as \[
\frac{\# \{T^r < T_{obs}\}}{\# \mbox{ replicates}}
\]
mean(T_sample0<T_obs)
## [1] 0.008