1971年1月以降のドル円レート(monthly)と60,25,12カ月ごとのトレンド(傾き)

EXJPUS.csv_EXJPUS
                 [,1]
2014-04-01         NA
2014-05-01         NA
2014-06-01         NA
2014-07-01         NA
2014-08-01         NA
2014-09-01 -0.4424647

Call:
lm(formula = dataset.xts[, ccc] ~ dataset.xts[, ncol(dataset.xts)])

Residuals:
    Min      1Q  Median      3Q     Max 
-68.611 -29.470   7.189  23.799  79.002 

Coefficients:
                                   Estimate Std. Error t value Pr(>|t|)    
(Intercept)                      281.499912   2.943074   95.65   <2e-16 ***
dataset.xts[, ncol(dataset.xts)]  -0.442465   0.009696  -45.63   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 33.67 on 523 degrees of freedom
Multiple R-squared:  0.7993,	Adjusted R-squared:  0.7989 
F-statistic:  2083 on 1 and 523 DF,  p-value: < 2.2e-16
#conditions
#FirstDate<-as.Date("2014/1/1")
#LastDate<-as.Date("2014/9/5")
width<-c(60,25,12)
period<-2 #1:days 2:months
if(period==1){
period<-"days"
DateStyle<-"%Y/%b/%d"
}else
if(period==2){
period<-"months"
DateStyle<-"%Y/%b"  
}
#path
username<-Sys.info()['user']
path01<-paste("C:/Users/",username,"/Desktop/R_Data_Read/",sep="")
path02<-paste("C:/Users/",username,"/Desktop/R_Graph/",sep="")
path03<-paste("C:/Users/",username,"/Desktop/R_Data_Write/",sep="")
setwd(path01)
#package
library(urca)
library(tseries)
library(forecast)
library(MASS)
library(lmtest)
library(systemfit)
library(xts)
#analytics
for(iii in 1:length(dir(path01))){
dataset<-read.table(file=dir(path01)[iii],header=TRUE,sep=",")
dataset[,1]<-as.Date(dataset[,1])
colnames(dataset)[1]<-"date" #lower case
#dataset<-subset(dataset,FirstDate<=dataset[,1])
#dataset<-subset(dataset,LastDate>=dataset[,1])
dataset<-subset(dataset,dataset[,c(2:ncol(dataset))]!=".")
dataset<-subset(dataset,is.na(dataset[,c(2:ncol(dataset))])==F)
dataset[,c(2:ncol(dataset))]<-as.double(as.character(dataset[,c(2:ncol(dataset))]))
#data.frame to xts
dataset.xts<-as.xts(dataset[,-1],order.by=as.Date(dataset$date))
colnames(dataset.xts)<-colnames(dataset)[ c(2:ncol(dataset)) ]
dataset.xts<-cbind(dataset.xts,dummyID=seq(1:length(dataset[,1])))
setwd(path02)
for(ccc in 1:(ncol(dataset.xts)-1)){
png(file=paste(dir(path01)[iii],"_",colnames(dataset.xts)[ccc],".png",sep=""),width=1400,height=700)
par(mfrow=c(2,2),mar=c(5,5,5,5),ps=22,cex.axis=1,cex.lab=1,cex.main=1,cex.sub=1)
#slope
slope<-rollapply(dataset.xts,length(dataset.xts[,ccc]),function(z)lm(z[,ccc]~z[,ncol(dataset.xts)])$coe[2],by.column=FALSE)
corr<-lm(dataset.xts[,ccc]~dataset.xts[,ncol(dataset.xts)])
print(tail(slope))
print(summary(corr))
#plot
Date<-index(dataset.xts)
plot(Date,dataset.xts[,ccc],type="l",xaxt="n",ylab=colnames(dataset.xts)[ccc],main=colnames(dataset.xts)[ccc])
axis.Date(side=1,Date,format="%Y/%m")
for(ggg in 1:length(width)){
slope1<-rollapply(dataset.xts,width[ggg],function(z)lm(z[,ccc]~z[,ncol(dataset.xts)])$coe[2],by.column=FALSE)
plot(Date,slope1,type="h",xaxt="n",ylab="correlation-slope",main=paste("Trend(slope) of ",colnames(dataset.xts)[ccc],".width=",width[ggg],period,sep=""))
axis.Date(side=1,Date,format="%Y/%m")
}
dev.off()
}
}