将相关表显示为降序列表

发布于 2024-11-25 15:41:08 字数 240 浏览 2 评论 0原文

当在具有大量变量的时间序列上运行 cor() 时,我得到一个表,其中每个变量都有一行和一列,显示它们之间的相关性。

我如何将此表视为从最相关到​​最不相关的列表(消除所有 NA 结果和映射回自身的结果(即 A 到 A 的相关性))。我还想将逆(负)结果计算为绝对值,但仍将其显示为负数。

所以期望的输出会是这样的:

A,B,0.98
A,C,0.9
C,R,-0.8
T,Z,0.5

When running cor() on a times series with a lot of variables, I get a table back that has a row and column for each variable, showing the correlation between them.

How can I view this table as a list from most correlated to least correlated (eliminating all NA results and results that map back to themselves (i.e. the correlation of A to A)). I would also like to count inverse (negative) results as absolute values, but still show them as negative.

So the desired output would be something like:

A,B,0.98
A,C,0.9
C,R,-0.8
T,Z,0.5

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彻夜缠绵 2024-12-02 15:41:08

这是我能想到的多种方法之一。我使用了 reshape 包,因为 melt() 语法对我来说很容易记住,但是 melt() 命令可以很容易地使用基本 R 命令来完成:

require(reshape)
## set up dummy data
a <- rnorm(100)
b <- a + (rnorm(100, 0, 2))
c <- a + b + (rnorm(100)/10)
df <- data.frame(a, b, c)
c <- cor(df)
## c is the correlations matrix

## keep only the lower triangle by 
## filling upper with NA
c[upper.tri(c, diag=TRUE)] <- NA

m <- melt(c)

## sort by descending absolute correlation
m <- m[order(- abs(m$value)), ]

## omit the NA values
dfOut <- na.omit(m)

## if you really want a list and not a data.frame
listOut <- split(dfOut, 1:nrow(dfOut))

Here's one of many ways I could think to do this. I used the reshape package because the melt() syntax was easy for me to remember, but the melt() command could pretty easily be done with base R commands:

require(reshape)
## set up dummy data
a <- rnorm(100)
b <- a + (rnorm(100, 0, 2))
c <- a + b + (rnorm(100)/10)
df <- data.frame(a, b, c)
c <- cor(df)
## c is the correlations matrix

## keep only the lower triangle by 
## filling upper with NA
c[upper.tri(c, diag=TRUE)] <- NA

m <- melt(c)

## sort by descending absolute correlation
m <- m[order(- abs(m$value)), ]

## omit the NA values
dfOut <- na.omit(m)

## if you really want a list and not a data.frame
listOut <- split(dfOut, 1:nrow(dfOut))
年华零落成诗 2024-12-02 15:41:08

使用基 R(其中 cors 是相关矩阵):

up <- upper.tri(cors)
out <- data.frame(which(up, arr.ind=TRUE), cor=cors[up])
out <- out[!is.na(out$cor),]
out[order(abs(out$cor), decreasing=TRUE),]

Using base R (where cors is the correlation matrix):

up <- upper.tri(cors)
out <- data.frame(which(up, arr.ind=TRUE), cor=cors[up])
out <- out[!is.na(out$cor),]
out[order(abs(out$cor), decreasing=TRUE),]
楠木可依 2024-12-02 15:41:08

... 替换为您的相关调用。

library(reshape)
x <- subset(melt(cor(...)), value != 1 | value != NA)
x <- x[with(x, order(-abs(x$value))),]

如果您在关联中遇到很多 NA,也许可以尝试在关联调用中使用 use="complete.obs" 参数。

Replace ... with your correlation call.

library(reshape)
x <- subset(melt(cor(...)), value != 1 | value != NA)
x <- x[with(x, order(-abs(x$value))),]

If you're getting a lot of NA in your correlations, perhaps try using the use="complete.obs" argument in your correlation call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文