2014年9月23日 過去10年間のドル円レート、ゴールド価格及び10年物米国財務省証券金利

animation
Sys.time()
## [1] "2014-09-23 16:39:29 JST"

Package

#install.packages("knitr",dependencies=T)
#install.packages("rgl",dependencies=T)
#install.packages("quantmod",dependencies=T)
#install.packages("corrplot",dependencies=T)
library(knitr)
library(rgl) # http://cran.r-project.org/web/packages/rgl/rgl.pdf
library(quantmod) # http://cran.r-project.org/web/packages/quantmod/quantmod.pdf
library(corrplot) # http://cran.r-project.org/web/packages/corrplot/corrplot.pdf

Import Data & Plot

username<-Sys.info()['user']
path01<-paste("C:/Users/",username,"/Desktop",sep="")
path02<-"R_output"
path<-file.path(path01,path02)
if(file.exists(path)==F){dir.create(path)}
FirstDate<-as.Date("2004/1/1")
LastDate<-as.Date("2014/9/23")
period<-1 #1:days 2:months
if(period==1){
period<-"days"
DateStyle<-"%Y/%b/%d"
}else
if(period==2){
period<-"months"
DateStyle<-"%Y/%b"  
}
all.data<-data.frame(date=seq(FirstDate,LastDate,by=period))
symbol<-c("DEXJPUS","JPYUSD","FRED","GOLDAMGBD228NLBM","Gold","FRED","DGS10","Treasury.10year","FRED")
for(iii in seq(1,length(symbol),by=3)){
dataset.xts<-getSymbols(symbol[iii],src=symbol[iii+2],auto.assign=FALSE,from=FirstDate,to=LastDate)
dataset<-data.frame(date=index(dataset.xts),dataset.xts,row.names=NULL,stringsAsFactors=F)
dataset<-subset(dataset,dataset[,2]!="." & is.na(dataset[,2])==F) # &,|
names(dataset)[2]<-symbol[iii+1]
all.data<-merge(all.data,dataset,by="date",sort=T) #,all=T 
}
head(all.data,1)
##         date JPYUSD  Gold Treasury.10year
## 1 2004-01-02    107 415.2            4.38
tail(all.data,1)
##            date JPYUSD Gold Treasury.10year
## 2630 2014-09-19    109 1222            2.59
period.date<-paste(format(all.data[,1][1],DateStyle),"-",format(all.data[,1][length(all.data[,1])],DateStyle))

3Dplot

setwd(path)
plot(all.data[,-1],panel=panel.smooth,main="Level")

plot of chunk unnamed-chunk-4

plot(all.data[,-1][-1,]-all.data[,-1][-length(all.data[,1]),],panel=panel.smooth,main="1stDifference")

plot of chunk unnamed-chunk-4

all.data.cor<-cor(all.data[,-1])
corrplot(all.data.cor)

plot of chunk unnamed-chunk-4

plot3d(all.data$JPYUSD,all.data$Gold,all.data$Treasury.10year,main=paste("JPYUSD-Gold-10yearTreauryRate.",period.date,sep=""),xlab=symbol[2],ylab=symbol[5],zlab=symbol[8])
grid3d("x") # ,at=NULL,col="red",lwd=3,lty=2,n=10
grid3d("y")
grid3d("z")
par3d(windowRect=c(0,0,800,600), zoom=1.0)
for(i in 0:360){
rgl.viewpoint(theta=i,phi=i/4)
rgl.snapshot(fmt="png",sprintf("R_image%04d.png",i))
}
##ImageMagick## convert -loop 0 R*.png animation.gif