"evaluate.profiles" <- function(npl, group.labels, funstat=max, times=1000){ ## npl: matrix of m pedigrees by n positions, from Genehunter ## group.labels: labels for groups ## funstat: funstat=max takes the maximum over all profiles. ## times: permute labels this number of times ## i.e., take sample of size times from permutation distribution. funprof <- function(m, group, fun=funstat, profiles=FALSE){ gp.subscripts <- split(1:m, group) gpprofs <- sapply(gp.subscripts, function(x, npl)apply(npl[x,], 2, sum), npl=npl) gpprofs <- sweep(gpprofs,2, sqrt(num.in.gps), "/") if(profiles) gpprofs else fun(gpprofs) } m <- dim(npl)[1] if(m != length(group.labels)) stop("# of group labels must equal # of rows of npl") num.in.gps <- table(group.labels) ngps <- length(tab) group.profiles <- funprof(m=m, group=group.labels, profiles=TRUE) samp.stat <- funstat(group.profiles) permstats <- numeric(times) for(i in 1:times){ newgp <- group.labels[sample(m)] permstats[i] <- funprof(m=m, group=newgp, profiles=FALSE) } p <- 1-sum(samp.stat>permstats)/times list(p=p, profiles=group.profiles) }