【问题标题】:Relative Time Series相对时间序列
【发布时间】:2011-04-02 04:19:39
【问题描述】:

我正在寻找一种按相对时间排列数据的标准化方法。应用包括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 月,并抓取每条每日收益率曲线以绘制当前收益率曲线。我确信我可以使用几页的 first 和 last 函数,但这比在 Excel 中构建它更有效。

【问题讨论】:

  • 我不确定您要做什么。你能举个例子吗?
  • “相对时间”是指从 1980 年 1 月 1 日开始,即 t=0?之后的每个时期都是 t+n?
  • 是的。理想情况下,比例是可变的,因此“t+n”中的“n”可以分配给天、年、季度等……一个会计示例是当前财政季度 = 0,下一个财政季度 = 1 按顺序比较不同会计年度公司的各种指标,并创建一个标准化的预测框架。另一个应用程序是比较债券曲线,我可以比较不同历史点的不同期限集合,或者使用每个期限的平均值作为历史数据的子集。

标签: r time-series


【解决方案1】:

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)

【讨论】:

  • 谢谢。我开始意识到为什么金融界很少有人不超越 Excel。
  • 别自欺欺人;它不仅限于金融界人士。 ;-) 如果您可以举一个您要解决的问题的示例,那么有人可能会提供更好的解决方案。由于chartSeries 不是针对您遇到的问题类型而设计的,请尝试plot.zoo(..., screens=1)。
  • 谢谢!学习“合并”肯定会缩短很多!
猜你喜欢
  • 1970-01-01
  • 2017-04-20
  • 2014-09-29
  • 2013-01-29
  • 2021-02-14
  • 2020-01-12
  • 2020-10-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多