如何计算R中的相关性

发布于 2024-11-25 05:42:32 字数 228 浏览 1 评论 0原文

我想计算 RI 中数据集 x 的子集的列之间的相关系数,其中有 40 行模型,每行 200 个模拟,总共 8000 行 我想计算每个模拟(40 行)的列之间的相关系数

cor(x[c(3,5)]) 从所有 8000 行计算
我需要 cor(x[c(3,5)]) 但只有当 X$nsimul=1 等时

你才会在这方面帮助我 桑

I wanted to calculate correlation coeficient between colunms of a subset of a data set x in R
I have rows of 40 models each 200 simulations in total 8000 rows
I wanted to calculate the corr coeficient between colums for each simulation (40 rows)

cor(x[c(3,5)]) calculates from all 8000 rows
I need cor(x[c(3,5)]) but only when X$nsimul=1 and so on

would you help me in this regards
San

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

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

发布评论

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

评论(2

最近可好 2024-12-02 05:42:32

我不确定您到底在使用 x[c(3,5)] 做什么,但看起来您想要执行如下操作:您有一个数据框 X 像这样:

set.seed(123)
X <- data.frame(nsimul = rep(1:2, each=5), a = sample(1:10), b = sample(1:10))

> X
   nsimul  a  b
1       1  1  6
2       1  8  2
3       1  9  1
4       1 10  4
5       1  3  9
6       2  4  8
7       2  6  5
8       2  7  7
9       2  2 10
10      2  5  3

并且您想按 nsimul 列分割此数据帧,并计算 ab 之间的相关性> 每组。这是一个经典的 split-apply-combine 问题,plyr 包非常适合:

require(plyr)
> ddply(X, .(nsimul), summarize, cor_a_b = cor(a,b))
  nsimul    cor_a_b
1      1 -0.7549232
2      2 -0.5964848

I'm not sure what exactly you're doing with x[c(3,5)] but it looks like you want to do something like the following: You have a data-frame X like this:

set.seed(123)
X <- data.frame(nsimul = rep(1:2, each=5), a = sample(1:10), b = sample(1:10))

> X
   nsimul  a  b
1       1  1  6
2       1  8  2
3       1  9  1
4       1 10  4
5       1  3  9
6       2  4  8
7       2  6  5
8       2  7  7
9       2  2 10
10      2  5  3

And you want to split this data-frame by the nsimul column, and calculate the correlation between a and b in each group. This is a classic split-apply-combine problem for which the plyr package is very well-suited:

require(plyr)
> ddply(X, .(nsimul), summarize, cor_a_b = cor(a,b))
  nsimul    cor_a_b
1      1 -0.7549232
2      2 -0.5964848
各自安好 2024-12-02 05:42:32

您可以使用 by 函数,例如:

correlations <- as.list(by(data=x,INDICES=x$nsimul,FUN=function(x) cor(x[3],x[5])))

# now you can access to correlation for each simulation
correlations["simulation 1"]
correlations["simulation 2"]
...
correlations["simulation 40"]

You can use by function e.g.:

correlations <- as.list(by(data=x,INDICES=x$nsimul,FUN=function(x) cor(x[3],x[5])))

# now you can access to correlation for each simulation
correlations["simulation 1"]
correlations["simulation 2"]
...
correlations["simulation 40"]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文