# TODO: Add comment
# 
# Author: E.Korsching Mar 25, 2013
###############################################################################


m.chisq.fisher <- function(x, method="c"){
	# Non-parametric test of dataframe or matrix columns, all vs. all 
	# x: matrix or data frame,  method: "c":chi square, "f":Fisher test
	
	#ini
	nr <- nrow(x)
	nc <- ncol(x)
	if(nc<2){ cat("\n minimum of two columns"); return() }
	
	erg <- data.frame(matrix(0,nc,nc), row.names=dimnames(x)[[2]])
	dimnames(erg)[[2]] <- dimnames(x)[[2]]
	
	#work
	if(method=="c"){
		k <- 2
		for(i in 1:(nc-1)){
			for(j in k:nc){
				erg[i,j] <- chisq.test(x=x[,i], y=x[,j],
					correct=TRUE, rescale.p=FALSE, simulate.p.value=FALSE, B=2000)$p.value
				erg[j,i] <- erg[i,j]
			}
			k <- k+1
		}
	}
	if(method=="f"){
		k <- 2
		for(i in 1:(nc-1)){
			for(j in k:nc){
				erg[i,j] <- fisher.test(x=x[,i], y=x[,j],
						workspace=2000000, hybrid=TRUE, alternative="two.sided",
						conf.int=TRUE, conf.level=0.95, simulate.p.value=FALSE, B=2000)$p.value
				erg[j,i] <- erg[i,j]
			}
			k <- k+1
		}
	}
	
	return(erg)
}


#a <- m.chisq.fisher( x=fro.orig.1a[,c(1:25)] )



