HT 在数据框中创建一个新向量,该向量采用现有向量的相关性

发布于 2024-08-25 17:50:18 字数 150 浏览 9 评论 0原文

我有两个指数的时间序列,每一行代表同一天的收盘价。我想转到第 30 行并回顾过去 30 天并计算皮尔逊相关性。然后将该值存储在一个新向量中。然后,对整个时间序列重复计算。

这在 Excel 中是一项微不足道的任务,所以我相信它可以在 R 中完成。但我不知道要使用的方法。

I have a time series of two indexes, with each row representing the closing price on the same day. I'd like to go to row 30 and lookback over the last 30 'days' and calculate the pearson correlation. And then store that value in a new vector. Then, repeat the calculation for the entire time series.

It is a trivial task in Excel, so I'm convinced it can be done in R. I don't know the method to use though.

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

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

发布评论

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

评论(1

季末如歌 2024-09-01 17:50:18

有很多方法可以做到这一点(与 R 中的所有内容一样)。我始终建议在处理时间序列数据时使用时间序列。

zoo 包可能是最流行的时间序列包(尽管您也可以查看其他包,例如 xts、timeSeries、its、fts):

library(zoo)
z <- zoo(data.frame(a=1:50, b=3:52), as.Date(1:50))
rollapply(z, 30, cor, by.column=F, align = "right")

您还可以找到 chart.RollingCorrelationPerformanceAnalytics 包中的 code> 函数很有用。

There are many ways to do this (as with everything in R). I always recommend using a time series when working with time series data.

The zoo package is probably the most popular time series package (although you can also look at others such as xts, timeSeries, its, fts):

library(zoo)
z <- zoo(data.frame(a=1:50, b=3:52), as.Date(1:50))
rollapply(z, 30, cor, by.column=F, align = "right")

You may also find the chart.RollingCorrelation function in the PerformanceAnalytics package useful.

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