简体   繁体   English

配对列和系数中R中的相关矩阵

[英]Correlation matrix in R from paired columns and coefficients

I have a dataframe that has three variables: a, b, c. 我有一个包含三个变量的数据框:a,b,c。 The first two columns in the data set are the pairing of two of the variables for all possible combinations and the third is the correlation between them. 数据集中的前两列是所有可能组合的两个变量的配对,第三列是它们之间的相关性。 Shown below. 如下所示。

> var1 <- c("a","a","b")
> var2 <- c("b","c","c")
> r <- c(.55,.25,.75)
> as.data.frame(cbind(var1,var2,r))
  var1 var2    r
1    a    b  0.55
2    a    c  0.25
3    b    c  0.75

My question is whether it is possible to turn this dataframe containing the correlations into a correlation matrix object in R? 我的问题是,是否可以将包含相关性的数据帧转换为R中的相关性矩阵对象? I also want to use some of R's plotting and graphing functions. 我也想使用R的一些绘图和图形功能。

Ultimately what I want is a matrix that looks like this 最终,我想要的是一个看起来像这样的矩阵

  a    b    c
a 1   .55  .25

b .55  1   .25

c .25  .75  .75

Try this: 尝试这个:

vars <- unique(c(var1, var2))
df <- cbind.data.frame(var1,var2,r)
cor.df <- expand.grid(vars, vars)
cor.df <- rbind(merge(cor.df, df, by.x=c('Var1', 'Var2'), by.y=c('var1', 'var2')),
                merge(cor.df, df, by.x=c('Var2', 'Var1'), by.y=c('var1', 'var2')),
                data.frame(Var1=vars, Var2=vars, r=1))
library(reshape2)
cor.mat <- dcast(cor.df, Var1~Var2, value.var='r')
rownames(cor.mat) <- cor.mat[,1]
cor.mat <- as.matrix(cor.mat[-1])
cor.mat
#      a    b    c
# a 1.00 0.55 0.25
# b 0.55 1.00 0.75
# c 0.25 0.75 1.00

# plot the correlation matrix
library(ggplot2)
ggplot(data = cor.df, aes(x=Var1, y=Var2, fill=r)) + 
  geom_tile()

在此处输入图片说明

Obviously you have the upper triangular and lower triangular parts of your correlation matrix (provided by r ) and you do not need the data.frame for your purpose. 显然,您具有相关矩阵的上三角部分和下三角部分(由r提供),并且不需要data.frame来满足您的目的。 Supplying the lower and upper triangle for a matrix which elements are 1 is enough. 为元素为1的矩阵提供上下三角就足够了。

var <- unique(c(var1,var2))
corr <- matrix(1,nrow=length(r),ncol=length(r)) # a matrix with 1s
corr[lower.tri(corr,diag = FALSE)] <- r # lower triangular matrix to be r
corr[upper.tri(corr,diag = FALSE)] <- r # upper triangular matrix to be r 
corr <- as.data.frame(corr) # formatting
row.names(corr) <- var # row names
colnames(corr) <- var # column names

Package corrplot has a function corrplot perfect for plotting correlation matrix with different options (see the argument method ). corrplot具有一个功能corrplot非常适合绘制具有不同选项的相关矩阵(请参阅参数method )。 Here is an example: 这是一个例子:

library(corrplot)
corrplot(as.matrix(corr),method="circle")

corrplot

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM