いっかくのデータサイエンティストをいく

1からプログラミングとデータサイエンスを独習したい

【時系列】共和分

沖本本5章から。

共和分

単位根の持つデータ同士で回帰分析した場合、見せかけの回帰になってしまうことが多いです。もうあきらめるしかないのか。。。 そんなことはありません!見せかけではない場合があります! それは共和分を持っているときです。 共和分の概念自体は単純でf:id:imakoto0323:20181204131239p:plainf:id:imakoto0323:20181204131311p:plainが線形結合すなわち

f:id:imakoto0323:20181204131404p:plain

の場合に単位根を持たなくなったとき、両者は共和分の関係にあるといいます。共和分の関係の場合、回帰分析ができます

共和分検定

共和分の検定にはEngle-Granger検定があります。 この検定の考え方はシンプルで ①OLSで回帰分析 ②残差が単位根がなければ共和分ありとみなす この検定の帰無仮説は「共和分関係がない」です。

Rで実装

#パッケージ
library(vars)


#ランダムウォークを生成
n_sample <- 400 #サンプルサイズ
set.seed(10)
rw <- cumsum(rnorm(n=n_sample))
x_co <- 0.6*rw + rnorm(n=n_sample)
y_co <- 0.4*rw + rnorm(n=n_sample)

#ADF(単位根)検定
summary(ur.df(y_co, type = "none" ))
############################################### 
# Augmented Dickey-Fuller Test Unit Root Test # 
############################################### 

Test regression none 


Call:
  lm(formula = z.diff ~ z.lag.1 - 1 + z.diff.lag)

Residuals:
  Min      1Q  Median      3Q     Max 
-4.0281 -0.8282 -0.0794  0.8101  3.4312 

Coefficients:
  Estimate Std. Error t value Pr(>|t|)    
z.lag.1    -0.012165   0.009448  -1.288    0.199    
z.diff.lag -0.453423   0.044548 -10.178   <2e-16 ***
  ---
  Signif. codes:  0***0.001**0.01*0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.236 on 396 degrees of freedom
Multiple R-squared:  0.2162, Adjusted R-squared:  0.2122 
F-statistic: 54.62 on 2 and 396 DF,  p-value: < 2.2e-16


Value of test-statistic is: -1.2875 
#単位根を持つという帰無仮説は棄却できない
Critical values for test statistics: 
  1pct  5pct 10pct
tau1 -2.58 -1.95 -1.62

summary(ur.df(x_co, type = "none" ))
############################################### 
# Augmented Dickey-Fuller Test Unit Root Test # 
############################################### 

Test regression none 


Call:
  lm(formula = z.diff ~ z.lag.1 - 1 + z.diff.lag)

Residuals:
  Min      1Q  Median      3Q     Max 
-4.6304 -1.0182 -0.0304  0.8609  4.3365 

Coefficients:
  Estimate Std. Error t value Pr(>|t|)    
z.lag.1    -0.007435   0.007294  -1.019    0.309    
z.diff.lag -0.418492   0.045650  -9.167   <2e-16 ***
  ---
  Signif. codes:  0***0.001**0.01*0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.436 on 396 degrees of freedom
Multiple R-squared:  0.1803, Adjusted R-squared:  0.1762 
F-statistic: 43.57 on 2 and 396 DF,  p-value: < 2.2e-16


Value of test-statistic is: -1.0194 
#単位根を持つという帰無仮説は棄却できない
Critical values for test statistics: 
  1pct  5pct 10pct
tau1 -2.58 -1.95 -1.62

#データを1つにまとめる
df <- data.frame(
  y_co = y_co,
  x_co = x_co,
)

#ts型に変換
ts_df <- ts(df)

#共和分検定
summary(ca.po(ts_df, demean = "none"))
######################################## 
# Phillips and Ouliaris Unit Root Test # 
######################################## 

Test of type Pu 
detrending of series none 


Call:
  lm(formula = z[, 1] ~ z[, -1] - 1)

Residuals:
  Min         1Q     Median         3Q        Max 
-2.580e-15 -3.550e-16  4.000e-18  3.650e-16  5.676e-14 

Coefficients:
  Estimate Std. Error    t value Pr(>|t|)    
z[, -1]x_co  6.667e-01  1.593e-17  4.184e+16   <2e-16 ***
  z[, -1]z    -6.667e-01  8.722e-17 -7.644e+15   <2e-16 ***
  ---
  Signif. codes:  0***0.001**0.01*0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 3.133e-15 on 398 degrees of freedom
Multiple R-squared:      1,  Adjusted R-squared:      1 
F-statistic: 8.822e+32 on 2 and 398 DF,  p-value: < 2.2e-16


Value of test-statistic is: -1.357322e+16 
#帰無仮説を棄却できない
Critical values of Pu are:
  10pct    5pct    1pct
critical values 26.7022 32.9392 46.4097

これまでのVAR~単位根までの過程についてこちらに詳しくまとめられています。

tjo.hatenablog.com