相对时间序列

2024-03-27

我正在寻找一种标准化的方法来按相对时间排列数据。应用程序包括会计数据(例如 FY1、FY2 等)和经济数据(例如使用 1 年、2 年、3 年等的利率期限结构)。

我希望能够比较当前的一组时间序列数据和代表类似情况或历史规范的几个历史时间序列集。我正在查看 xts,但看起来我需要使用绝对时间参考。

我最终希望使用 Quantmod 的图表函数或具有同等功能的图表来可视化数据。由于 ChartSeries 需要一个时间序列对象,有谁知道如何做到这一点?即使方向正确的一点也会有所帮助。谢谢。

require(quantmod)
symbols=c("DGS1","DGS2","DGS3","DGS5","DGS7","DGS10","DGS20")
getSymbols(symbols,src="FRED")
one.h=mean(na.omit(DGS1));two.h=mean(na.omit(DGS2));three.h=mean(na.omit(DGS3));five.h=mean(na.omit(DGS5));seven.h=mean(na.omit(DGS7));ten.h=mean(na.omit(DGS10));twenty.h=mean(na.omit(DGS20))
historic=c(one.h,two.h,three.h,five.h,seven.h,ten.h,twenty.h)
current=c(last(DGS1),last(DGS2),last(DGS3),last(DGS5),last(DGS7),last(DGS10),last(DGS20))
years=c(1,2,3,5,7,10,20)
plot(years,current,type="o",pch=20,ann=FALSE)
lines(years,historic,type="o",pch=20,col="red",lty=3)
title(main="Term Structure of Interest Rates",col.main="red", font.main=4)
title(xlab="Years to Maturity",ylab="Interest Rate",col.lab=rgb(0,0.5,0))
legend(3, c("Current","Historic"),cex=0.8,col=c("black","red"),pch=20)

问题: 我希望能够选择一个时间段,例如 2007 年 9 月,并获取每条每日收益率曲线来绘制当前收益率曲线。我确信我可以使用几页的第一个和最后一个函数,但这比在 Excel 中构建它需要更多的工作。


xts需要一个明确的时间索引,但它基于zoo,没有这样的要求。所以zoo只要索引是有序的,就可以让你做这样的事情:

> x <- zoo(rnorm(5),sprintf("FY%02d",1:5))
> y <- zoo(rnorm(5),sprintf("FY%02d",1:5))
> merge(x,y)
               x           y
FY01  0.32707886 -1.81414982
FY02 -0.95177700  0.37772862
FY03 -0.03052571 -1.13047719
FY04  1.19139973  0.96962871
FY05 -0.76484142 -0.08187144

缺点是您将无法使用这些对象quantmod::chartSeries因为它需要一个xts目的。我怀疑这能回答你的问题,但我希望它能给你一些想法。

编辑以合并OP的示例:

library(quantmod)
symbols=c("DGS1","DGS2","DGS3","DGS5","DGS7","DGS10","DGS20")
getSymbols(symbols,src="FRED")
all <- na.omit(merge(DGS1,DGS2,DGS3,DGS5,DGS7,DGS10,DGS20))

years <- c(1,2,3,5,7,10,20)
# use xts indexing, since getSymbols returns xts
histDate <- "2007-09-01/2007-09-10"
# create zoo objects for non-time-based indexing
hist <- zoo(t(all[histDate]), order.by=years)
curr <- zoo(t(last(all)), order.by=years)

currHist <- merge(curr,hist)
plotCol <- rainbow(NCOL(currHist))
plot(currHist, screens=1, col=plotCol, pch=20, type="o", ann=FALSE)
title(main="Term Structure of Interest Rates",col.main="red", font.main=4)
title(xlab="Years to Maturity",ylab="Interest Rate",col.lab=rgb(0,0.5,0))
legend(15,1.5,colnames(currHist),cex=0.8,col=plotCol,pch=20)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

相对时间序列 的相关文章

随机推荐