如何计算滑动窗口的相关性?

发布于 2024-12-13 03:26:58 字数 749 浏览 0 评论 0原文

我有一个名为 aux 的动物园对象,其中包含 1961 年到 2009 年的年度数据:

     x$nao x[, 2]
1961 -0.03   63.3
1962  0.20  155.9
1963 -2.98  211.0

我想使用 20 年滑动窗口计算两列之间的相关性。我正在尝试使用 rollapply,但似乎无法使其工作。我尝试了几种不同的方法,但总是没有成功......

> rollapply(aux,20, cor(aux[,1],aux[,2],method="pearson"))
Error in match.fun(FUN) : 'cor(aux[, 1], aux[, 2], method = "pearson")' is not a function, character or symbol

> rollapply(aux,20, cor,method="pearson")
Error in FUN(coredata(data)[posns], ...) : supply both 'x' and 'y' or a matrix-like 'x'

> rollapply(aux,20, cor)
Error in FUN(coredata(data)[posns], ...) : supply both 'x' and 'y' or a matrix-like 'x'

任何人都可以告诉我如何使 rollapply 工作吗?

感谢您的帮助!

I have a zoo object called aux with yearly data from 1961 to 2009:

     x$nao x[, 2]
1961 -0.03   63.3
1962  0.20  155.9
1963 -2.98  211.0

I want to calculate the correlation between the two columns using a 20 years sliding window. I am trying to use rollapply, but I don't seem to be able to make it work. I tried several different ways of doing it but always without success...

> rollapply(aux,20, cor(aux[,1],aux[,2],method="pearson"))
Error in match.fun(FUN) : 'cor(aux[, 1], aux[, 2], method = "pearson")' is not a function, character or symbol

> rollapply(aux,20, cor,method="pearson")
Error in FUN(coredata(data)[posns], ...) : supply both 'x' and 'y' or a matrix-like 'x'

> rollapply(aux,20, cor)
Error in FUN(coredata(data)[posns], ...) : supply both 'x' and 'y' or a matrix-like 'x'

Can anybody tell me how to make rollapply work?

Thanks for helping!

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

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

发布评论

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

评论(1

原谅我要高飞 2024-12-20 03:26:58

试试这个。

library(quantmod)   
library(TTR)   

#Set the seed so results can be duplicated   
set.seed(123)   

#Build a zoo object with typical price data   
var1 <- zoo(cumprod(1+rnorm(50, 0.01, 0.05)), seq(1961, 2001, 1))   
var2 <- zoo(cumprod(1+rnorm(50, 0.015, 0.1)), seq(1961, 2001, 1))   
dat <- merge(var1=var1, var2=var2)   
plot(dat)   
grid()   

#Calculate the percent returns for the two prices   
del1 <- Delt(dat$var1)   
del2 <- Delt(dat$var2)   
dat <- merge(dat, del1=del1, del2=del2)   
dimnames(dat)[[2]][3] <- "del1"   
dimnames(dat)[[2]][4] <- "del2"   
head(dat)   
plot(dat)   

#Calculate the correlation between the two returns using a 5 year sliding window   
delcor <- runCor(dat$del1, dat$del2, n=5, use="all.obs", sample=TRUE, cumulative=FALSE)   
dat <- merge(dat, delcor)   
plot(dat$delcor, type="l", main="Sliding Window Correlation of Two Return Series", xlab="", col="red")   
grid()   

在此处输入图像描述

Try this.

library(quantmod)   
library(TTR)   

#Set the seed so results can be duplicated   
set.seed(123)   

#Build a zoo object with typical price data   
var1 <- zoo(cumprod(1+rnorm(50, 0.01, 0.05)), seq(1961, 2001, 1))   
var2 <- zoo(cumprod(1+rnorm(50, 0.015, 0.1)), seq(1961, 2001, 1))   
dat <- merge(var1=var1, var2=var2)   
plot(dat)   
grid()   

#Calculate the percent returns for the two prices   
del1 <- Delt(dat$var1)   
del2 <- Delt(dat$var2)   
dat <- merge(dat, del1=del1, del2=del2)   
dimnames(dat)[[2]][3] <- "del1"   
dimnames(dat)[[2]][4] <- "del2"   
head(dat)   
plot(dat)   

#Calculate the correlation between the two returns using a 5 year sliding window   
delcor <- runCor(dat$del1, dat$del2, n=5, use="all.obs", sample=TRUE, cumulative=FALSE)   
dat <- merge(dat, delcor)   
plot(dat$delcor, type="l", main="Sliding Window Correlation of Two Return Series", xlab="", col="red")   
grid()   

enter image description here

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