複数時系列データの結合と原系列と一次階差の単位根検定

> library(tseries)
> FirstDate="2013/9/1"
> LastDate="2014/8/31"
> path="C:/Users/username/Desktop/R_Folder"
> setwd(path)
> AllData01<-data.frame(date=seq(as.Date(FirstDate),as.Date(LastDate),by="days"),dummy=1)
> AllData02<-AllData01
> TSeriesDataFileList<-dir(path)
> for(iii in 1:length(TSeriesDataFileList))
+ {
+  dataset<-read.table(file=TSeriesDataFileList[iii],header=TRUE,sep=",")
+  dataset[,1]<-as.Date(dataset[,1])
+  for(ccc in 2:ncol(dataset))
+  {
+   #以下は非数値データ行の削除及びデータの実数化。オリジナルデータに合わせて適宜変更。
+   dataset<-subset(dataset,dataset[,ccc]!=".")
+   dataset<-subset(dataset,is.na(dataset[,ccc])==F)
+   dataset<-subset(dataset,as.Date(FirstDate)<=dataset[,1])
+   dataset<-subset(dataset,as.Date(LastDate)>=dataset[,1])
+   dataset[,ccc]<-as.double(as.character(dataset[,ccc]))
+   #日付列名の統一
+   colnames(dataset)[1]<-"date"
+   #単位根検定 H0:単位根有り
+   p<-adf.test(dataset[,ccc],k=1)$p.value
+   cat(TSeriesDataFileList[iii],",",colnames(dataset)[ccc],",単位根検定,p値=",p,"\n",sep="")
+   p<-adf.test(diff(dataset[,ccc]),k=1)$p.value
+   cat(TSeriesDataFileList[iii],",",colnames(dataset)[ccc],",一次階差",",単位根検定,p値=",p,"\n","\n",sep="")
+  }
+  #print(head(dataset,3))
+  #cat("\n","\n")
+  AllData01<-merge(AllData01,dataset,by="date",all=T,sort=T) #欠損はNAとして処理
+  AllData02<-merge(AllData02,dataset,by="date",sort=T) #全系列に数値データが存在する日付のみ
+ }
DEXJPUS.csv,VALUE,単位根検定,p値=0.5720206
DEXJPUS.csv,VALUE,一次階差,単位根検定,p値=0.01

GOLDAMGBD228NLBM.csv,VALUE,単位根検定,p値=0.1160808
GOLDAMGBD228NLBM.csv,VALUE,一次階差,単位根検定,p値=0.01

nikkei_stock_average_daily_en.csv,Close,単位根検定,p値=0.266896
nikkei_stock_average_daily_en.csv,Close,一次階差,単位根検定,p値=0.01

nikkei_stock_average_daily_en.csv,Open,単位根検定,p値=0.18935
nikkei_stock_average_daily_en.csv,Open,一次階差,単位根検定,p値=0.01

nikkei_stock_average_daily_en.csv,High,単位根検定,p値=0.2332964
nikkei_stock_average_daily_en.csv,High,一次階差,単位根検定,p値=0.01

nikkei_stock_average_daily_en.csv,Low,単位根検定,p値=0.1994597
nikkei_stock_average_daily_en.csv,Low,一次階差,単位根検定,p値=0.01

SP500.csv,VALUE,単位根検定,p値=0.02618246
SP500.csv,VALUE,一次階差,単位根検定,p値=0.01

 警告メッセージ: 
1: In adf.test(diff(dataset[, ccc]), k = 1) :
  p-value smaller than printed p-value
2: In adf.test(diff(dataset[, ccc]), k = 1) :
  p-value smaller than printed p-value
3: In adf.test(diff(dataset[, ccc]), k = 1) :
  p-value smaller than printed p-value
4: In adf.test(diff(dataset[, ccc]), k = 1) :
  p-value smaller than printed p-value
5: In adf.test(diff(dataset[, ccc]), k = 1) :
  p-value smaller than printed p-value
6: In adf.test(diff(dataset[, ccc]), k = 1) :
  p-value smaller than printed p-value
7: In adf.test(diff(dataset[, ccc]), k = 1) :
  p-value smaller than printed p-value
> TSeriesDataFileList
[1] "DEXJPUS.csv"                       "GOLDAMGBD228NLBM.csv"             
[3] "nikkei_stock_average_daily_en.csv" "SP500.csv"                        
> head(AllData01,10)
         date dummy VALUE.x VALUE.y    Close     Open     High      Low   VALUE
1  2013-09-01     1      NA      NA       NA       NA       NA       NA      NA
2  2013-09-02     1      NA 1391.25 13572.92 13438.07 13613.48 13407.53      NA
3  2013-09-03     1   99.43 1391.25 13978.44 13748.68 13978.44 13748.68 1639.77
4  2013-09-04     1   99.61 1403.75 14053.87 13875.17 14056.88 13843.61 1653.08
5  2013-09-05     1  100.08 1391.75 14064.82 14140.20 14156.50 13981.52 1655.08
6  2013-09-06     1   99.04 1368.25 13860.81 14088.41 14099.13 13834.52 1655.17
7  2013-09-07     1      NA      NA       NA       NA       NA       NA      NA
8  2013-09-08     1      NA      NA       NA       NA       NA       NA      NA
9  2013-09-09     1   99.55 1386.00 14205.23 14141.67 14251.46 14117.68 1671.71
10 2013-09-10     1  100.22 1373.00 14423.36 14318.72 14441.81 14296.78 1683.99
> head(AllData02,10)
         date dummy VALUE.x VALUE.y    Close     Open     High      Low   VALUE
1  2013-09-03     1   99.43 1391.25 13978.44 13748.68 13978.44 13748.68 1639.77
2  2013-09-04     1   99.61 1403.75 14053.87 13875.17 14056.88 13843.61 1653.08
3  2013-09-05     1  100.08 1391.75 14064.82 14140.20 14156.50 13981.52 1655.08
4  2013-09-06     1   99.04 1368.25 13860.81 14088.41 14099.13 13834.52 1655.17
5  2013-09-09     1   99.55 1386.00 14205.23 14141.67 14251.46 14117.68 1671.71
6  2013-09-10     1  100.22 1373.00 14423.36 14318.72 14441.81 14296.78 1683.99
7  2013-09-11     1  100.08 1365.25 14425.07 14511.74 14561.46 14422.72 1689.13
8  2013-09-12     1   99.24 1340.25 14387.27 14397.90 14455.37 14321.57 1683.42
9  2013-09-13     1   99.38 1308.25 14404.67 14316.70 14439.93 14233.12 1687.99
10 2013-09-17     1   99.16 1317.25 14311.67 14456.99 14474.53 14311.67 1704.76
> 
> 

library(tseries)
FirstDate="2013/9/1"
LastDate="2014/8/31"
path="C:/Users/username/Desktop/R_Folder"
setwd(path)
AllData01<-data.frame(date=seq(as.Date(FirstDate),as.Date(LastDate),by="days"),dummy=1)
AllData02<-AllData01
TSeriesDataFileList<-dir(path)
for(iii in 1:length(TSeriesDataFileList))
{
 dataset<-read.table(file=TSeriesDataFileList[iii],header=TRUE,sep=",")
 dataset[,1]<-as.Date(dataset[,1])
 for(ccc in 2:ncol(dataset))
 {
  #以下は非数値データ行の削除及びデータの実数化。オリジナルデータに合わせて適宜変更。
  dataset<-subset(dataset,dataset[,ccc]!=".")
  dataset<-subset(dataset,is.na(dataset[,ccc])==F)
  dataset<-subset(dataset,as.Date(FirstDate)<=dataset[,1])
  dataset<-subset(dataset,as.Date(LastDate)>=dataset[,1])
  dataset[,ccc]<-as.double(as.character(dataset[,ccc]))
  #日付列名の統一
  colnames(dataset)[1]<-"date"
  #単位根検定 H0:単位根有り
  p<-adf.test(dataset[,ccc],k=1)$p.value	
  cat(TSeriesDataFileList[iii],",",colnames(dataset)[ccc],",単位根検定,p値=",p,"\n",sep="")
  p<-adf.test(diff(dataset[,ccc]),k=1)$p.value	
  cat(TSeriesDataFileList[iii],",",colnames(dataset)[ccc],",一次階差",",単位根検定,p値=",p,"\n","\n",sep="")
 }
 #print(head(dataset,3))
 #cat("\n","\n")
 AllData01<-merge(AllData01,dataset,by="date",all=T,sort=T) #欠損はNAとして処理
 AllData02<-merge(AllData02,dataset,by="date",sort=T) #全系列に数値データが存在する日付のみ
}
TSeriesDataFileList
head(AllData01,10)
head(AllData02,10)