[英]get output of cor.test as data frame
我做的所有colnames的相關測試中matrix.dge.cpm.t
針對矢量samplesheet$cond1
。 我想提取matrix.dge.cpm.t
中所有經過測試的列的測試結果。
cor.test <- apply(matrix.dge.cpm.t,2, function(col)cor.test(col,log(samplesheet$cond1,2)))
> matrix.dge.cpm.t[,1:5][1:5,]
LOC117736868 naf1 vegfc tenm3 dctd
1 6.519403 4.930011 4.223574 7.088435 5.065795
2 6.504621 5.113774 4.139711 7.149168 4.702029
3 6.424178 4.903654 4.007805 7.111132 4.822668
4 6.585717 4.861847 3.716316 7.170937 5.025178
5 6.442080 5.048240 4.078584 7.110264 4.635633
輸出:
> head(cor.test )
$LOC117736868
Pearson's product-moment correlation
data: col and log(samplesheet$cond1, 2)
t = 1.7197, df = 30, p-value = 0.09579
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
-0.05486633 0.58694383
sample estimates:
cor
0.2995587
$naf1
Pearson's product-moment correlation
data: col and log(ss.Likith.numeric.order$Cortisol, 2)
t = 0.78998, df = 30, p-value = 0.4357
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
-0.2167293 0.4681440
sample estimates:
cor
0.1427528
摘要如下所示:
> head(summary(cor.test))
Length Class Mode
LOC117736868 9 htest list
naf1 9 htest list
vegfc 9 htest list
tenm3 9 htest list
dctd 9 htest list
cep44 9 htest list
我想要的輸出示例:
LOC117736868 p-value = 0.09579 cor = 0.2995587
嘗試這個
# Create dataframe where to save results
res <- data.frame(matrix(nrow = 0, ncol = 4))
colnames(res) <- c("var1", "var2", "correlation", "pvalue")
# Correlation in loop
for(i in colnames(matrix.dge.cpm.t[,3:5])) {
for(j in colnames(matrix.dge.cpm.t[,3:5])) {
a <- cor.test(matrix.dge.cpm.t[[i]], matrix.dge.cpm.t[[j]])
res <- rbind(res, data.frame(
"var1" = i,
"var2" = j,
"correlation" = a$estimate,
"pvalue" = a$p.value) )
}
}
# Remove rownames
rownames(res) <- NULL
另一種選擇是使用此處提供的 Hmisc 包和代碼。
# ++++++++++++++++++++++++++++
# flattenCorrMatrix
# ++++++++++++++++++++++++++++
# cormat : matrix of the correlation coefficients
# pmat : matrix of the correlation p-values
flattenCorrMatrix <- function(cormat, pmat) {
ut <- upper.tri(cormat)
data.frame(
row = rownames(cormat)[row(cormat)[ut]],
column = rownames(cormat)[col(cormat)[ut]],
cor =(cormat)[ut],
p = pmat[ut]
)
}
library(Hmisc)
res <- rcorr(as.matrix(matrix.dge.cpm.t[,1:5]))
flattenCorrMatrix(res$r, res$P)
針對一個變量運行矩陣
# Create dataframe where to save results
res <- data.frame(matrix(nrow = 0, ncol = 3))
colnames(res) <- c("var1", "correlation", "pvalue")
# Correlation in loop
for(i in colnames(matrix.dge.cpm.t[,3:5])) {
a <- cor.test(matrix.dge.cpm.t[[i]], samplesheet$cond1)
res <- rbind(res, data.frame(
"var1" = i,
"correlation" = a$estimate,
"pvalue" = a$p.value) )
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.