使用二进制比较在数据帧上的R chisq.test() [英] R chisq.test() on dataframe using binary comparsion

查看:88
本文介绍了使用二进制比较在数据帧上的R chisq.test()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对尺寸为(50x752)的数据框进行chisq.test。我想获取所有列的所有可能的成对比较的pvalues(通过多次测试进行调整)。最后,我想返回一个矩阵(50x50)以生成调整后的chisq p值的热图。这是我目前正在做的事情,但这简直是天作之合。



第一步:进行成对比较

  function(data,p.adjust.method = holm)
{
cor.mat<-cor(data)
x<- ncol(data)#nb在这里矩阵的列50
y <-nrow(data)#nb在这里矩阵的列758
index <-t(combn(x,2))#创建所有可能组合的输出矩阵位置
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))
}

第二步:使用



<$将输出表转换为矩阵p $ p> dcast(df,V2〜V1,fill = 1)#thanx到Roland的这个功能!

但这不能很好地工作,因为我没有在最终矩阵中反映pvalue,操纵1st函数的输出,以对角线填充0(在将列与其自身进行比较时)。

解决方案

喜欢这个吗?

 #一些数据
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))

#函数计算调整p值
fun<-函数(x,y){
p.adjust(chisq.test(df [,x],df [,y])$ p.value,method = holm,n =选择(ncol(df),2))
}

p.adj<-外部(names(df),names(df),FUN = Vectorize( #使用外部获取矩阵
diag(p.adj)<-1#您应该找出为什么chisq.test在diag
rownames(p.adj)处返回零的原因-名称(df)
colnames(p.adj)<-名称(df)

p.adj
#abcd
#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


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!

解决方案

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

这篇关于使用二进制比较在数据帧上的R chisq.test()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆