Changing world, Changing JGB term structure

November 10, 2011

(This article was first published on My Life as a Mock Quant in English, and kindly contributed to R-bloggers)

Writing the article “How much does “Beta” change depending on time?“, I learned how to create an animation by using R language. Then, I would like to continue do that in this article.
In this article, I visualize time series of JGB term structure Ministry of Finance Japan publishes because I’m japanese!
You can download these data from here. You can get daily yield curve data, but I visualized these as monthly data for simplicity.
You can easily understand how JGB term structure behave and Japanese yield gradually go down.
The source code to create it is below.
(To run, you need to install xts, animation package, and ImageMagick)
#Source of JGB curve
source.jgb <- NULL
source.jgb[[length(source.jgb) + 1]] <- ""
source.jgb[[length(source.jgb) + 1]] <- ""
source.jgb[[length(source.jgb) + 1]] <- ""
source.jgb[[length(source.jgb) + 1]] <- ""
source.jgb[[length(source.jgb) + 1]] <- ""
source.jgb[[length(source.jgb) + 1]] <- ""
#From Japanese era To ChristianEra
ToChristianEra <- function(x)
era <- substr(x, 1, 1)
year <- as.numeric(substr(x, 2, nchar(x)))
if(era == "H"){
year <- year + 1988
}else if(era == "S"){
year <- year + 1925
#Down load yield curve and convert to xts object
GetJGBYield <- function(source.url)
jgb <- read.csv(source.url, stringsAsFactors = FALSE)
#Extract date only <- strsplit(jgb[, 1], "\\.")
#stop warning
warn.old <- getOption("warn")
options(warn = -1)
#From Japanese era To ChristianEra <- lapply(, function(x)c(ToChristianEra(x[1]), x[2:length(x)]))
#From date string to date object
jgb[, 1] <- as.Date(sapply(, function(x)Reduce(function(y,z)paste(y,z, sep="-"),x)))
#Convert data from string to numeric
jgb[, -1] <- apply(jgb[, -1], 2, as.numeric)
options(warn = warn.old)
#Down load JBG yield
jgb.list <- lapply(source.jgb, GetJGBYield)
#convert one xts object
jgb.xts <- Reduce(rbind, jgb.list)
#Interpolate(nearest value)
coredata(jgb.xts) <- na.locf(t(na.locf(t(coredata(jgb.xts)))))
#to monthly
jgb.xts <- jgb.xts[endpoints(jgb.xts, on="months",k = 1)]
#Label for x-axis
label.term <- paste(gsub("X", "", colnames(jgb.xts)), "Y", sep="")
#The range of y
y.max <- c(min(jgb.xts), max(jgb.xts))
#plot one image
Snap <- function(val){
term.structure <- coredata(val) <- index(val)
plot(t(term.structure),type="l",lwd=3, col = 2, xlab = "Term", ylab = "Rate", ylim = y.max)
axis(1, 1:length(label.term), label.term)
text(0.5, y.max[2], as.character(, pos = 4)
#save as animation
for(i in 1:nrow(jgb.xts)){Snap(jgb.xts[i])}
},interval = 0.005)
Created by Pretty R at

To leave a comment for the author, please follow the link and comment on their blog: My Life as a Mock Quant in English. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)