R chisq.test() on dataframe using binary comparsio

2019-08-06 11:08发布

问题:

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!

回答1:

Like this?

#some data
set.seed(42)
df <- data.frame(a=rbinom(1000,5,0.3),
                 b=rbinom(1000,5,0.001),
                 c=rbinom(1000,5,0.1),
                 d=rbinom(1000,5,0.9))

#function to calculate the adj. p-value
fun <- function(x,y) {
  p.adjust(chisq.test(df[,x],df[,y])$p.value,method="holm",n=choose(ncol(df),2))
}

p.adj <- outer(names(df),names(df),FUN=Vectorize(fun)) #use outer to get a matrix
diag(p.adj) <- 1  #you should find out why chisq.test returns zero at the diag
rownames(p.adj) <- names(df)
colnames(p.adj) <- names(df)

p.adj
#  a         b c         d
#a 1 1.0000000 1 1.0000000
#b 1 1.0000000 1 0.6152165
#c 1 1.0000000 1 1.0000000
#d 1 0.6152165 1 1.0000000