Relative time series

I am looking for a standardized method for organizing data in relative time. Applications include accounting data, such as FY1, FY2, etc ... and economic data, such as the structure of interest rate terms using 1 year, 2 years, 3 years, etc.

I would like to be able to compare a dataset of time series that are current and several historical sets of time series that represent similar situations or historical norms. I looked at xts, but it looks like I need to use an absolute reference to the time.

Ultimately, I would like to use Quantmod functions or graphic functions with the equivalent ability to visualize data. Since chartSeries requires a time series object, does anyone know how to do this? It would be helpful even in the right direction. Thank.

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)

Problem: I would like to be able to choose a time period such as September 2007 and get each yield curve for the curve against the current yield curve. I am sure that I could use several pages of the first and last functions, but that would be more work than creating it in Excel.

+3
source share
1 answer

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)
+3

Source: https://habr.com/ru/post/1760939/


All Articles