1

次元(50x752)のデータフレームでchisq.testを実行したいと思います。すべての列のすべての可能なペアワイズ比較のp値(多重検定によって調整)を取得したいと思います。最後に、行列(50x50)を取得して、調整されたカイ2乗p値のヒートマップを生成します。これが私が今していることですが、これははるかに理想的です。

ステップ1:ペアワイズ比較を行う

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))
}

ステップ2:出力テーブルはを使用して行列に変換されます

   dcast(df,V2~V1,fill=1) # thanx to Roland for this function!

しかし、これはうまく機能していません。最終的な行列のp値をミラーリングしておらず、1番目の関数の出力を操作して対角線を0で埋める必要があるためです(列をそれ自体と比較する場合)。あなたの助けは大歓迎です!

4

1 に答える 1

2

このような?

#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
于 2012-10-26T16:41:22.513 に答える