我想使用函数
rcorr
将一个矩阵的变量与另一个矩阵的变量相关联,但我不想要一个所有相关的相关矩阵。
示例数据
mtcars
:
m1 <- as.matrix(mtcars[,c(1,3)])
m2 <- as.matrix(mtcars[,c(4,6)])
cor <- rcorr(y =m1,x =m2, type = "spearman")
output: hp wt mpg disp
hp 1.0000000 0.7746767 -0.8946646 0.8510426
wt 0.7746767 1.0000000 -0.8864220 0.8977064
mpg -0.8946646 -0.8864220 1.0000000 -0.9088824
disp 0.8510426 0.8977064 -0.9088824 1.0000000
我不想要这个输出,因为我对 m1 的相关变量与 m2 的变量感兴趣,而不是全部反对全部。 我该怎么做?
正如评论者所提到的,
rcorr()
函数不灵活,因为它给出了完整的相关矩阵。
我想你可能对这个功能感兴趣,因为它也给了你p-values
。但是,你可以做一个技巧
并在第二步中通过从输出中提取请求的相关性来访问您的信息,即
相关性和相应的 p 值如下:
m1 <- as.matrix(mtcars[,c(1,3)])
m2 <- as.matrix(mtcars[,c(4,6)])
library(Hmisc)
cor <- rcorr(y =m1,x =m2, type = "spearman")
# extract only your correlations of interest
my_cor<-cor$r[c("hp","wt"),c("mpg","disp")]
my_cor
mpg disp
hp -0.8946646 0.8510426
wt -0.8864220 0.8977064
#likewise, you could extract the corresponding p-values of your correlations of interest
my_pval<-cor$P[c("hp","wt"),c("mpg","disp")]
my_pval
mpg disp
hp 5.085932e-12 6.791336e-10
wt 1.487610e-11 3.346212e-12
希望对你有帮助