if (FALSE) {
# 1. Downloading a lookup table from site
# download from site http://www.math.tau.ac.il/~ruheller/Software.html
####################################################################
#using an already ready null table as object (for use in test functions)
#for example, ADP likelihood ratio statistics, for the independence problem,
#for sample size n=300
load('Object-ADP-n_300.Rdata') #=>null.table
#or using a matrix of statistics generated for the null distribution,
#to create your own table.
load('ADP-nullsim-n_300.Rdata') #=>mat
null.table = hhg.univariate.nulltable.from.mstats(m.stats = mat,minm = 2,
maxm = 5,type = 'Independence', variant = 'ADP',size = 300,
score.type = 'LikelihoodRatio',aggregation.type = 'sum')
# 2. generating an independence null table using multiple cores,
#and then compiling to object.
####################################################################
library(parallel)
library(doParallel)
library(foreach)
library(doRNG)
#generate an independence null table
nr.cores = 4 #this is computer dependent
n = 30 #size of independence problem
nr.reps.per.core = 25
mmax =5
score.type = 'LikelihoodRatio'
aggregation.type = 'sum'
variant = 'ADP'
#generating null table of size 4*25
#single core worker function
generate.null.distribution.statistic =function(){
library(HHG)
null.table = matrix(NA,nrow=nr.reps.per.core,ncol = mmax-1)
for(i in 1:nr.reps.per.core){
#note that the statistic is distribution free (based on ranks),
#so creating a null table (for the null distribution)
#is essentially permuting over the ranks
statistic = hhg.univariate.ind.stat(1:n,sample(1:n),
variant = variant,
aggregation.type = aggregation.type,
score.type = score.type,
mmax = mmax)$statistic
null.table[i,]=statistic
}
rownames(null.table)=NULL
return(null.table)
}
#parallelize over cores
cl = makeCluster(nr.cores)
registerDoParallel(cl)
res = foreach(core = 1:nr.cores, .combine = rbind, .packages = 'HHG',
.export=c('variant','aggregation.type','score.type',
'mmax','nr.reps.per.core','n'), .options.RNG=1234) %dorng%
{ generate.null.distribution.statistic() }
stopCluster(cl)
#the null table:
head(res)
#as object to be used:
null.table = hhg.univariate.nulltable.from.mstats(res,minm=2,
maxm = mmax,type = 'Independence',
variant = variant,size = n,score.type = score.type,
aggregation.type = aggregation.type)
#using the null table, checking for dependence in a linear relation
x=rnorm(n)
y=x+rnorm(n)
ADP.test = hhg.univariate.ind.combined.test(x,y,null.table)
ADP.test$MinP.pvalue #pvalue
# 3. generating a k-sample null table using multiple cores
# and then compiling to object.
####################################################################
library(parallel)
library(doParallel)
library(foreach)
library(doRNG)
#generate a k sample null table
nr.cores = 4 #this is computer dependent
n1 = 25 #size of first group
n2 = 25 #size of first group
nr.reps.per.core = 25
mmax =5
score.type = 'LikelihoodRatio'
aggregation.type = 'sum'
#generating null table of size 4*25
#single core worker function
generate.null.distribution.statistic =function(){
library(HHG)
null.table = matrix(NA,nrow=nr.reps.per.core,ncol = mmax-1)
for(i in 1:nr.reps.per.core){
#note that the statistic is distribution free (based on ranks),
#so creating a null table (for the null distribution)
#is essentially permuting over the ranks
statistic = hhg.univariate.ks.stat(1:(n1+n2),sample(c(rep(0,n1),rep(1,n2))),
aggregation.type = aggregation.type,
score.type = score.type,
mmax = mmax)$statistic
null.table[i,]=statistic
}
rownames(null.table)=NULL
return(null.table)
}
#parallelize over cores
cl = makeCluster(nr.cores)
registerDoParallel(cl)
res = foreach(core = 1:nr.cores, .combine = rbind, .packages = 'HHG',
.export=c('n1','n2','aggregation.type','score.type','mmax',
'nr.reps.per.core'), .options.RNG=1234) %dorng%
{generate.null.distribution.statistic()}
stopCluster(cl)
#the null table:
head(res)
#as object to be used:
null.table = hhg.univariate.nulltable.from.mstats(res,minm=2,
maxm = mmax,type = 'KSample',
variant = 'KSample-Variant',size = c(n1,n2),score.type = score.type,
aggregation.type = aggregation.type)
#using the null table, checking for dependence in a case of two distinct samples
x=1:(n1+n2)
y=c(rep(0,n1),rep(1,n2))
Sm.test = hhg.univariate.ks.combined.test(x,y,null.table)
Sm.test$MinP.pvalue #pvalue
}
Run the code above in your browser using DataLab