仅特定变量之间的相关性
Correlation between only specific variables
我有一些数据如下所示:
data <- structure(list(Gene1 = c(8.030835566, 7.523710852, 8.132012083,
6.351834008, 6.325281674, 6.810264692), Gene2 = c(8.755819384,
8.101372382, 8.691221404, 7.963358311, 7.003639076, 8.860927111
), Gene3 = c(6.504333248, 8.832470479, 10.7661583, 12.48262164,
6.003163216, 6.810264692), Gene4 = c(7.773754938, 6.545394663,
7.499168201, 5.587808389, 5.587808389, 5.587808389), Gene5 = c(9.782169709,
9.208945473, 10.07340718, 8.452540072, 6.810926028, 8.757832761
), Gene6 = c(6.808970669, 6.545394663, 7.206623134, 6.848704398,
5.587808389, 8.090400794), Gene7 = c(10.32869976, 10.09035118,
9.541872802, 10.57521096, 7.910751686, 10.02532033), Gene8 = c(7.274379599,
6.545394663, 7.499168201, 7.963358311, 6.003163216, 7.909825918
), Gene9 = c(8.605820177, 8.101372382, 8.293403668, 10.21655691,
6.003163216, 6.324818701), Gene10 = c(12.30601467, 12.27069432,
12.47496174, 12.23379266, 10.04845125, 12.4664811), X.1 = c(NA,
NA, NA, NA, NA, NA)), class = "data.frame", row.names = c("S1",
"S2", "S3", "S4", "S5", "S6"))
以上数据仅为示例。原始数据超过 1000 行。我实际上只想与下面的特定变量进行关联。
Gene1 * Gene7
Gene2 * Gene8
Gene3 * Gene10
Gene5 * Gene6
Gene4 * Gene9
我只对上述相关性感兴趣。我不希望 Gene1 与所有其他基因之间存在相关性。我可以对所有变量进行关联并过滤掉感兴趣的变量,但我有 1000 多个,所以我很难过滤。
那么,有没有办法只取特定感兴趣的 GeneX * GeneY
并进行关联?
我正在使用 Hmisc
库和 rcorr
函数进行关联。
试试这个例子:
# make combinations
x <- read.table(text = "g1 g2
Gene1 Gene7
Gene2 Gene8
Gene3 Gene10
Gene5 Gene6
Gene4 Gene9", header = TRUE)
# then get correlation
cbind(x, Corr = apply(x, 1, function(i) cor(data[, i[ 1 ]], data[, i[ 2 ]])))
# g1 g2 Corr
# 1 Gene1 Gene7 0.3064922
# 2 Gene2 Gene8 0.7185533
# 3 Gene3 Gene10 0.4521448
# 4 Gene5 Gene6 0.5747986
# 5 Gene4 Gene9 0.3009623
要获得 p 值,运行 cor.test
:
cbind(x, t(apply(x, 1, function(i){
res <- cor.test(data[, i[ 1 ]], data[, i[ 2 ]])
c(cor = res$estimate[[ "cor" ]], p = res$p.value)
})))
# g1 g2 cor p
# 1 Gene1 Gene7 0.3064922 0.5546572
# 2 Gene2 Gene8 0.7185533 0.1076713
# 3 Gene3 Gene10 0.4521448 0.3680000
# 4 Gene5 Gene6 0.5747986 0.2327570
# 5 Gene4 Gene9 0.3009623 0.5621868
我有一些数据如下所示:
data <- structure(list(Gene1 = c(8.030835566, 7.523710852, 8.132012083,
6.351834008, 6.325281674, 6.810264692), Gene2 = c(8.755819384,
8.101372382, 8.691221404, 7.963358311, 7.003639076, 8.860927111
), Gene3 = c(6.504333248, 8.832470479, 10.7661583, 12.48262164,
6.003163216, 6.810264692), Gene4 = c(7.773754938, 6.545394663,
7.499168201, 5.587808389, 5.587808389, 5.587808389), Gene5 = c(9.782169709,
9.208945473, 10.07340718, 8.452540072, 6.810926028, 8.757832761
), Gene6 = c(6.808970669, 6.545394663, 7.206623134, 6.848704398,
5.587808389, 8.090400794), Gene7 = c(10.32869976, 10.09035118,
9.541872802, 10.57521096, 7.910751686, 10.02532033), Gene8 = c(7.274379599,
6.545394663, 7.499168201, 7.963358311, 6.003163216, 7.909825918
), Gene9 = c(8.605820177, 8.101372382, 8.293403668, 10.21655691,
6.003163216, 6.324818701), Gene10 = c(12.30601467, 12.27069432,
12.47496174, 12.23379266, 10.04845125, 12.4664811), X.1 = c(NA,
NA, NA, NA, NA, NA)), class = "data.frame", row.names = c("S1",
"S2", "S3", "S4", "S5", "S6"))
以上数据仅为示例。原始数据超过 1000 行。我实际上只想与下面的特定变量进行关联。
Gene1 * Gene7
Gene2 * Gene8
Gene3 * Gene10
Gene5 * Gene6
Gene4 * Gene9
我只对上述相关性感兴趣。我不希望 Gene1 与所有其他基因之间存在相关性。我可以对所有变量进行关联并过滤掉感兴趣的变量,但我有 1000 多个,所以我很难过滤。
那么,有没有办法只取特定感兴趣的 GeneX * GeneY
并进行关联?
我正在使用 Hmisc
库和 rcorr
函数进行关联。
试试这个例子:
# make combinations
x <- read.table(text = "g1 g2
Gene1 Gene7
Gene2 Gene8
Gene3 Gene10
Gene5 Gene6
Gene4 Gene9", header = TRUE)
# then get correlation
cbind(x, Corr = apply(x, 1, function(i) cor(data[, i[ 1 ]], data[, i[ 2 ]])))
# g1 g2 Corr
# 1 Gene1 Gene7 0.3064922
# 2 Gene2 Gene8 0.7185533
# 3 Gene3 Gene10 0.4521448
# 4 Gene5 Gene6 0.5747986
# 5 Gene4 Gene9 0.3009623
要获得 p 值,运行 cor.test
:
cbind(x, t(apply(x, 1, function(i){
res <- cor.test(data[, i[ 1 ]], data[, i[ 2 ]])
c(cor = res$estimate[[ "cor" ]], p = res$p.value)
})))
# g1 g2 cor p
# 1 Gene1 Gene7 0.3064922 0.5546572
# 2 Gene2 Gene8 0.7185533 0.1076713
# 3 Gene3 Gene10 0.4521448 0.3680000
# 4 Gene5 Gene6 0.5747986 0.2327570
# 5 Gene4 Gene9 0.3009623 0.5621868