Reputation: 2395
I am doing a correlation test for all colnames in matrix.dge.cpm.t
against a vector samplesheet$cond1
. I would like to extract the results of the test for all columns in matrix.dge.cpm.t
which are tested.
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
Output:
> 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
Summary looks like this:
> 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
Examples of output I want:
LOC117736868 p-value = 0.09579 cor = 0.2995587
Upvotes: 1
Views: 1359
Reputation: 556
Try this
# 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
Another option is to use the Hmisc package and code presented here.
# ++++++++++++++++++++++++++++
# 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)
To run the matrix against one variable
# 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) )
}
Upvotes: 3