gastonstat / AssotesteR

R package AssotesteR
13 stars 4 forks source link

factor out permutations method #1

Open brentp opened 12 years ago

brentp commented 12 years ago

the permutation code is repeated in many places, could be factored out into a single function

my_perm_method <-
function(my_method, y, X, perm=-1, my_stat=NA)
{
    if(perm <= 0){ return NA }
    x.perm = rep(0, perm)
    my_stat = ifelse(is.na(my_stat, my_method(y, X), my_stat))
    for(i in 1:perm)
    {
      perm.sample = sample(1:length(y))
      x.perm[i] = my_method(y[perm.sample], X)
    }
    # p-value 
    perm.pval = sum(x.perm > my_stat) / perm
    return(perm.pval)
}