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

animation


Sys.time()
## [1] "2014-09-22 12:56:30 JST"

Package

#install.packages("knitr", dependencies=T)
#install.packages("rgl", dependencies=T)
#install.packages("quantmod", dependencies=T)
#install.packages("animation", 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(animation)

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/19")
period<-1 #1:days 2:months
if(period==1){
period<-"days"
DateStyle<-"%Y/%b/%d"
}else
if(period==2){
period<-"months"
DateStyle<-"%Y/%b"  
}
AllData<-data.frame(date=seq(FirstDate,LastDate,by=period),dummy=1)
symbol<-c("DEXJPUS","FRED","GOLDAMGBD228NLBM","FRED","DGS10","FRED")
for(iii in seq(1,length(symbol),by=2)){
dataset.xts<-getSymbols(symbol[iii],src=symbol[iii+1],auto.assign=FALSE,from=FirstDate,to=LastDate)
dataset<-data.frame(date=index(dataset.xts),dataset.xts,row.names=NULL,stringsAsFactors=F,header=T)
dataset<-subset(dataset,dataset[,2]!=".")
dataset<-subset(dataset,is.na(dataset[,2])==F)
AllData<-merge(AllData,dataset,by="date",sort=T) #,all=T 
}
AllData<-subset(AllData,select=-dummy)
head(AllData,1)
##         date DEXJPUS header.x GOLDAMGBD228NLBM header.y DGS10 header
## 1 2004-01-02     107     TRUE            415.2     TRUE  4.38   TRUE
tail(AllData,1)
##            date DEXJPUS header.x GOLDAMGBD228NLBM header.y DGS10 header
## 2625 2014-09-12   107.2     TRUE             1237     TRUE  2.62   TRUE

3Dplot

setwd(path)
JPYUSD<-AllData$DEXJPUS
Gold<-AllData$GOLDAMGBD228NLBM
Treasury.10year<-AllData$DGS10
plot3d(JPYUSD,Gold,Treasury.10year,main=paste("JPYUSD-Gold-10yearTreauryRate.",FirstDate,"-",LastDate,sep=""))
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