#r #correlation
#r #корреляция
Вопрос:
Я провожу тест корреляции для всех имен столбцов в matrix.dge.cpm.t
отношении вектора samplesheet$cond1
. Я хотел бы извлечь результаты теста для всех столбцов, в matrix.dge.cpm.t
которых они тестируются.
cor.test <- применить(matrix.dge.cpm.t,2, функция(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
Комментарии:
1.
res <- summary(cor.test)
, затем вы можете получить доступ к каждому элементу с$
помощью оператора2. не уверен, как это должно работать? Пожалуйста, посмотрите мое обновление
Ответ №1:
Попробуйте это
# 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)
РЕДАКТИРОВАТЬ 8 октября 2020 года
Для запуска матрицы с одной переменной
# 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) )
}
Комментарии:
1. Спасибо. Что, если я захочу изменить matrix.dge.cpm.t[[j]] на вектор в моем примере? Может быть, просто добавить вектор в фрейм данных matrix.dge.cpm.t?