I want to do a chisq.test on a dataframe of dimension (50x752). I want to get the pvalues (adjusted by multiple testing) for all possible paire-wise comparison for all columns. At the end I want to get back a matrix (50x50) to generate a heatmap of the adjusted chisq pvalues. Here is what I do at the moment but this is far beeing ideal.
Step1: do the pairewise comparison
function(data,p.adjust.method="holm")
{
cor.mat <- cor(data)
x<-ncol(data)#nb of column in matrix here 50
y<-nrow(data)#nb of column in matrix here 758
index<-t(combn(x, 2)) #create the matrix position of output for all possible combination
nindex <- nrow(index)
pvals <- numeric(nindex)
for (i in 1:nindex)
{
pvals[i]<-chisq.test(data[, index[i, 1]], data[, index[i,2]])$p.value
}
pvals<-p.adjust(pvals,method = p.adjust.method)
out <- as.data.frame(cbind(index, pvals))
}
Step2: The output table is transform into a matrix using
dcast(df,V2~V1,fill=1) # thanx to Roland for this function!
But this is not working well, as I do not mirror the pvalue in the final matrix and I have to manipulate the output of the 1st function to get the diagonal filled with 0 (when comparing a column to itself). Your help will be greatly appreciated!