【问题标题】:Pearson correlation (cor.test) using specific columns of a data matrix使用数据矩阵的特定列的 Pearson 相关性 (cor.test)
【发布时间】:2016-12-20 03:52:04
【问题描述】:

我正在尝试确定两组各 6 列之间的成对 pearson 相关系数和显着性(p 值)。

我正在使用以下脚本:

output <- matrix(dim(data2)[1]*4,dim(data2)[1],4)
for (i in dim(data)[1]){
    r<-cor.test(data[i,c(2:7)],data[i,c(9:14)],method="pearson")
    output[i,3]<-r$p.value
    output[i,4]<-r$estimate
    output[i,1]<-data[,1] # target geneID
    output[i,2]<-data[i,8] # miRNAID
}
colnames(output) <- c("geneID","miRNAID","p-val","corr")
head(output)

但我对数据矩阵中的向量类型有疑问

非常感谢您对此问题的意见。

谢谢 V

【问题讨论】:

  • 错误信息很容易解释 - cor.test 仅适用于向量。您正在发送数据帧,因此它不起作用。查看corrr package 了解计算相关性的便捷方法

标签: r


【解决方案1】:

我想知道你是否想这样做:

gene<-c("gene.17472635","gene.17436226","gene.17424189")
gene.C1<-c(216.22244180,0.04166939,29.58526377)
gene.C2<-c(210.4407513,0.6071574,24.684428)
gene.C3<-c(153.9317645,0.8830194,19.8798697)
gene.S1<-c(156.23494974,2.308891,23.345456)
gene.S2<-c(128.602680,4.568860,12.725349)
gene.S3<-c(52.928491,6.04392,7.174450)

data<-as.matrix(cbind(gene.C1,gene.C2,gene.C3,gene.S1,gene.S2,gene.S3))
cols<-t(combn(1:6,2))

gene_data_pval<-apply( cols , 1 , function(x) cor.test( data[,x[1]] , data[,x[2]],method="pearson")$p.value )
gene_data_estimate<-apply( cols , 1 , function(x) cor.test( data[,x[1]] , data[,x[2]],method="pearson")$estimate)
result1<-cbind(as.matrix(gene_data_pval),as.matrix(gene_data_estimate))
colnames(result1)<-c("p-val","est")

我不确定你是否想要这个或其他东西,因此只有基因数据,但代码可以很容易地用于核磁共振。

备注:可以根据组合制作一个id向量。

【讨论】:

    猜你喜欢
    • 2019-04-22
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 2019-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多