相对时间序列 [英] Relative Time Series
问题描述
我正在寻找一种按相对时间排列数据的标准化方法.应用包括FY1、FY2等会计数据和使用1年、2年、3年等的利率期限结构等经济数据……
I am looking for a standardized method for arranging data in relative time. Applications include accounting data such as FY1,FY2,etc... and economic data such as the term structure of interest rates using the 1 year, 2 year, 3 year, etc...
我希望能够比较一组当前的时间序列数据和几个代表类似情况或历史规范的历史时间序列集.我在看 xts,但看起来我需要使用绝对时间参考.
I would like to be able to compare a set of time series data that is current and several historic time series sets that represent similar situations or historic norms. I was looking at xts but it looks like I need to use an absolute time reference.
我最终想使用 Quantmod 的图表函数或具有等效功能的图形来可视化数据.由于 chartSeries 需要一个时间序列对象,有谁知道如何做到这一点?即使是在正确方向上的一点也会有帮助.谢谢.
I would eventually like to use Quantmod's charting functions or graphs with equivalent capability to visualize the data. Since chartSeries requires a time series object, does anyone know how to do this? Even a point in the right direction would be helpful. Thanks.
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 中构建它要多得多.
Problem: I would like to be able to select a time period such as September of 2007 and grab each daily yield curve to plot against the current yield curve. I'm sure I could use several pages of first and last functions but that would be more work than building it in Excel.
推荐答案
xts
需要一个明确的时间索引,但它基于 zoo
,它没有这样的要求.所以 zoo
将允许你做这样的事情,只要索引是有序的:
xts
requires an explicit time index but it's based on zoo
, which has no such requirement. So zoo
will allow you to do something like this, as long as the index is ordered:
> 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
对象.我怀疑这是否能回答您的问题,但我希望它能给您一些想法.
The downside is that you won't be able to use those objects with quantmod::chartSeries
because it requires an xts
object. I doubt this answers your question, but I hope it gives you some ideas.
编辑以合并 OP 的示例:
EDIT to incorporate OP's example:
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)
这篇关于相对时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!