Content

\[ \Delta y_{t}= \alpha + \beta t + \gamma y_{t-1} + \delta_{1} \Delta y_{t-1}+ \delta_{2} \Delta y_{t-2} + \epsilon_{t} \]

head(USDJPY)
cat("------------------------\n")
tail(USDJPY)
cat("------------------------\n")
ts_data <- USDJPY$`USD/JPY`
ts_data
cat("------------------------\n")
diff_ts_data <- diff(ts_data)
diff_ts_data
cat("------------------------\n")
length(diff_ts_data)
        Date  USD/JPY
1 2017-04-01 110.0910
2 2017-05-01 112.2436
3 2017-06-01 110.9141
4 2017-07-01 112.4170
5 2017-08-01 109.8270
6 2017-09-01 110.7760
------------------------
         Date  USD/JPY
19 2018-10-01 112.7218
20 2018-11-01 113.3380
21 2018-12-01 112.1994
22 2019-01-01 108.9605
23 2019-02-01 110.4400
24 2019-03-01 111.6533
------------------------
 [1] 110.0910 112.2436 110.9141 112.4170 109.8270 110.7760 112.9148 112.8190 112.9405 110.8710 107.9700 106.0468 107.6562 109.6882 110.0638 111.5210 110.9965 112.0974 112.7218 113.3380 112.1994 108.9605 110.4400 111.6533
------------------------
 [1]  2.1526 -1.3295  1.5029 -2.5900  0.9490  2.1388 -0.0958  0.1215 -2.0695 -2.9010 -1.9232  1.6094  2.0320  0.3756  1.4572 -0.5245  1.1009  0.6244  0.6162 -1.1386 -3.2389  1.4795  1.2133
------------------------
[1] 23
# ここではラグを2次まで取ります。
lag <- 2
# 自己回帰でお世話になるembed{stats}
M_diff <- embed(x = diff_ts_data, dimension = lag + 1)
M_diff
         [,1]    [,2]    [,3]
 [1,]  1.5029 -1.3295  2.1526
 [2,] -2.5900  1.5029 -1.3295
 [3,]  0.9490 -2.5900  1.5029
 [4,]  2.1388  0.9490 -2.5900
 [5,] -0.0958  2.1388  0.9490
 [6,]  0.1215 -0.0958  2.1388
 [7,] -2.0695  0.1215 -0.0958
 [8,] -2.9010 -2.0695  0.1215
 [9,] -1.9232 -2.9010 -2.0695
[10,]  1.6094 -1.9232 -2.9010
[11,]  2.0320  1.6094 -1.9232
[12,]  0.3756  2.0320  1.6094
[13,]  1.4572  0.3756  2.0320
[14,] -0.5245  1.4572  0.3756
[15,]  1.1009 -0.5245  1.4572
[16,]  0.6244  1.1009 -0.5245
[17,]  0.6162  0.6244  1.1009
[18,] -1.1386  0.6162  0.6244
[19,] -3.2389 -1.1386  0.6162
[20,]  1.4795 -3.2389 -1.1386
[21,]  1.2133  1.4795 -3.2389
# 原系列からラグ分を除くための添字の作成(トレンド項(linear trend)として流用)
trend_term <- c((lag + 1):(length(ts_data) - 1))
trend_term
 [1]  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 始めにトレンド項、ドリフト項(constant、定数項)ともに含む場合
result_lm <- summary(lm(M_diff[, 1] ~ ts_data[trend_term] + M_diff[, 2] + M_diff[, 3] + trend_term))
result_lm
cat("------------------------\n")
# ADF検定統計量 = 自己回帰係数(ここではts_dataの係数γ)のt値(但しt分布には従わない)
result_lm$coefficient[2, 1]/result_lm$coefficient[2, 2]
cat("------------------------\n")
# Rには拡張ディッキー=フラー検定関数としてCADFtest{CADFtest}があります
result_adftest <- CADFtest(model = ts_data, max.lag.y = lag, type = "trend")
result_adftest
cat("------------------------\n")
result_adftest %>>% (statistic)

Call:
lm(formula = M_diff[, 1] ~ ts_data[trend_term] + M_diff[, 2] + 
    M_diff[, 3] + trend_term)

Residuals:
    Min      1Q  Median      3Q     Max 
-2.2838 -0.8958  0.1248  0.9857  2.4302 

Coefficients:
                    Estimate Std. Error t value Pr(>|t|)  
(Intercept)         69.40455   25.37431   2.735   0.0147 *
ts_data[trend_term] -0.62736    0.22905  -2.739   0.0146 *
M_diff[, 2]          0.40104    0.23102   1.736   0.1018  
M_diff[, 3]          0.25994    0.23693   1.097   0.2888  
trend_term           0.01554    0.05651   0.275   0.7869  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 1.549 on 16 degrees of freedom
Multiple R-squared:  0.3324,    Adjusted R-squared:  0.1655 
F-statistic: 1.992 on 4 and 16 DF,  p-value: 0.1444

------------------------
[1] -2.739006
------------------------

    ADF test

data:  ts_data
ADF(2) = -2.739, p-value = 0.2325
alternative hypothesis: true delta is less than 0
sample estimates:
     delta 
-0.6273608 

------------------------
   ADF(2) 
-2.739006 
# 続いてドリフト項を含みトレンド項は含まない場合(ドリフト付きランダムウォークモデル仮定)
result_lm <- summary(lm(M_diff[, 1] ~ ts_data[trend_term] + M_diff[, 2] + M_diff[, 3]))
result_lm
cat("------------------------\n")
result_lm$coefficient[2, 1]/result_lm$coefficient[2, 2]
cat("------------------------\n")
result_adftest <- CADFtest(model = ts_data, max.lag.y = lag, type = "drift")
result_adftest
cat("------------------------\n")
result_adftest %>>% (statistic)

Call:
lm(formula = M_diff[, 1] ~ ts_data[trend_term] + M_diff[, 2] + 
    M_diff[, 3])

Residuals:
     Min       1Q   Median       3Q      Max 
-2.15613 -0.85284  0.04881  1.02810  2.29636 

Coefficients:
                    Estimate Std. Error t value Pr(>|t|)  
(Intercept)          69.2542    24.6691   2.807   0.0121 *
ts_data[trend_term]  -0.6242     0.2224  -2.806   0.0122 *
M_diff[, 2]           0.4024     0.2246   1.792   0.0910 .
M_diff[, 3]           0.2506     0.2280   1.099   0.2871  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 1.507 on 17 degrees of freedom
Multiple R-squared:  0.3293,    Adjusted R-squared:  0.2109 
F-statistic: 2.782 on 3 and 17 DF,  p-value: 0.07261

------------------------
[1] -2.805968
------------------------

    ADF test

data:  ts_data
ADF(2) = -2.806, p-value = 0.07441
alternative hypothesis: true delta is less than 0
sample estimates:
     delta 
-0.6241852 

------------------------
   ADF(2) 
-2.805968 
# 最後にトレンド項、ドリフト項ともに含まない場合(ランダムウォークモデル仮定)
result_lm <- summary(lm(M_diff[, 1] ~ ts_data[trend_term] + M_diff[, 2] + M_diff[, 3] - 1))
result_lm
cat("------------------------\n")
result_lm$coefficient[1, 1]/result_lm$coefficient[1, 2]
cat("------------------------\n")
result_adftest <- CADFtest(model = ts_data, max.lag.y = lag, type = "none")
result_adftest
cat("------------------------\n")
result_adftest %>>% (statistic)

Call:
lm(formula = M_diff[, 1] ~ ts_data[trend_term] + M_diff[, 2] + 
    M_diff[, 3] - 1)

Residuals:
    Min      1Q  Median      3Q     Max 
-3.0930 -1.1791  0.4348  1.5408  1.8003 

Coefficients:
                      Estimate Std. Error t value Pr(>|t|)
ts_data[trend_term]  0.0002478  0.0034917   0.071    0.944
M_diff[, 2]          0.1064627  0.2331709   0.457    0.653
M_diff[, 3]         -0.0851595  0.2282309  -0.373    0.713

Residual standard error: 1.771 on 18 degrees of freedom
Multiple R-squared:  0.01875,   Adjusted R-squared:  -0.1448 
F-statistic: 0.1147 on 3 and 18 DF,  p-value: 0.9504

------------------------
[1] 0.07096589
------------------------

    ADF test

data:  ts_data
ADF(2) = 0.070966, p-value = 0.6945
alternative hypothesis: true delta is less than 0
sample estimates:
      delta 
0.000247791 

------------------------
    ADF(2) 
0.07096589 

Asset Management Consulting Corporation