如何在r?

问题描述 投票:0回答:5

ci

)应与

cj

ci

.
之间的相关性。

我怎么做?

一个例子,
cj
    
您可以使用“ Corrplot”软件包。 

d <- data.frame(x1=rnorm(10), x2=rnorm(10), x3=rnorm(10)) cor(d) # get correlations (returns matrix)

r matrix visualization correlation
5个回答
107
投票

更多信息在这里:

http://cran.r-project.org/web/packages/corrplot/vignettes/corrplot-intro.html

    

76
投票

cor函数将在相关的计算中使用矩阵的列。因此,矩阵

x
和矩阵
YY

之间的行数必须相同。例如:enter image description here

d <- data.frame(x1=rnorm(10), x2=rnorm(10), x3=rnorm(10)) M <- cor(d) # get correlations library('corrplot') #package corrplot corrplot(M, method = "circle") #plot matrix


18
投票
the是用单个矩阵计算的相关矩阵上自定义行和列标签的示例:

set.seed(1) x <- matrix(rnorm(20), nrow=5, ncol=4) y <- matrix(rnorm(15), nrow=5, ncol=3) COR <- cor(x,y) COR image(x=seq(dim(x)[2]), y=seq(dim(y)[2]), z=COR, xlab="x column", ylab="y column") text(expand.grid(x=seq(dim(x)[2]), y=seq(dim(y)[2])), labels=round(c(COR),2))


enter image description hereqtlcharts

。它允许您创建

I Interactive

相关矩阵:

png("corplot.png", width=5, height=5, units="in", res=200) op <- par(mar=c(6,6,1,1), ps=10) COR <- cor(iris[,1:4]) image(x=seq(nrow(COR)), y=seq(ncol(COR)), z=cor(iris[,1:4]), axes=F, xlab="", ylab="") text(expand.grid(x=seq(dim(COR)[1]), y=seq(dim(COR)[2])), labels=round(c(COR),2)) box() axis(1, at=seq(nrow(COR)), labels = rownames(COR), las=2) axis(2, at=seq(ncol(COR)), labels = colnames(COR), las=1) par(op) dev.off()

enter image description here 当您关联更多变量时,它会更令人印象深刻,例如包装的小插图:


16
投票

还有其他方法可以在此处实现这一目标:(plot相关矩阵中的图),但是我喜欢您的版本,其中包装盒中的相关性。有没有办法将变量名添加到X和Y列,而不仅仅是这些索引号?对我来说,这将使这是一个完美的解决方案。谢谢!

eDit:我试图在[框中的Marc]对帖子上发表评论,但我显然不知道我在做什么。但是,我确实为自己回答了这个问题。

如果是矩阵(或原始数据框架),列名是您想要的,那么以下作品:

library(qtlcharts) data(iris) iris$Species <- NULL iplotCorr(iris, reorder=TRUE) enter image description here las= 0会将名称翻转回其正常位置,我的长度很长,所以我使用las = 2使其垂直于轴。

EDIT2:要抑制网格上的image()函数打印数字(否则它们重叠您的变量标签),请添加xaxt ='n',例如

axis(1, 1:dim(d)[2], colnames(d), las=2) axis(2, 1:dim(d)[2], colnames(d), las=2) enter image description here the Modelsummary的功能。

here是典型相关表的一个示例:

2
投票
image(x=seq(dim(x)[2]), y=seq(dim(y)[2]), z=COR, col=rev(heat.colors(20)), xlab="x column", ylab="y column", xaxt='n')

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.