2010-08-22 22 views
3

我正在尋找一種在相對時間內安排數據的標準化方法。應用包括諸如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中編譯它更有效。

+0

我不知道你想做什麼。你能提供一個例子嗎? – 2010-08-22 15:15:36

+0

by'relative time'by you'you start,例如,1980年1月1日是t = 0嗎?之後的每個時期是t + n? – 2010-08-22 19:11:36

+0

是的。理想情況下,規模將是可變的,以便「t + n」中的「n」可以分配給天,年,季度等等。會計實例將是當前財政季度= 0,下一財政季度= 1比較不同會計年度公司的各種指標,併爲預測創建標準化框架。另一個應用是比較債券曲線,我可以比較歷史上不同點的各種期限組合,或者對歷史數據子集的每個成熟期使用平均值。 – ProbablePattern 2010-08-22 21:34:50

回答

3

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對象。我懷疑這回答你的問題,但我希望它給你一些想法。

EDIT納入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) 
+0

謝謝。我開始意識到爲什麼這麼少的金融人士不會超越Excel。 – ProbablePattern 2010-08-24 20:53:08

+0

不要自欺欺人;它不僅限於金融領域的人士。 ;-)如果你能舉一個你想解決的問題的例子,有人可能會提供更好的解決方案。由於'chartSeries'不是針對您所遇到的問題而設計的,請嘗試'plot.zoo(...,screens = 1)'。 – 2010-08-24 21:18:40

+0

例如,謝謝你。我希望我的編輯幫助。 – 2010-08-25 21:01:03