LoginSignup
0
1

More than 1 year has passed since last update.

経済・ファイナンスデータの計量時系列分析(沖本)5章 Rコード

Last updated at Posted at 2021-08-17

5章

5章のRコードの解答例です。

コードで気になったところ
・とにかく時系列データは初めにADF検定。p116にあるようにDF検定には3つのやり方(背後に置くモデルの種類)が存在するが、書いてるコードはどれなのかちゃんとチェックしてない(要チェック)

理論的には、
・t検定でrho = 1を検定するのではダメな点に注意。これは誤差項の収束性ないことによって中心極限定理が使えなくなることに起因(直接DF分布の導出をするとブラウン運動になっている項の収束速度が中心極限定理よりも遅くなっていることがわかる。ここを考慮してるのがDF分布)
・問題ではDf検定(p=1)のみだが、ADFでも同じようにやる。(字数の選択は全章のAICを使う方法でoK)
・PP検定もDF検定みたいに3種類の状況が考えられるはずだけど、手順は同じなのか?

#5.5

setwd("Okimoto/5")
library(tidyverse)
library(gdata)
library(lubridate)
library(rugarch)
library(stats)
library(tseries)
library(dplyr)
data = read.xls("economicdata.xls")
data$X = ymd(data$X)

data_use = mutate(data,topix = log(topix),exrate = log(exrate), indprod = log(indprod), cpi = log(cpi))
data_use = data_use[,-1]

#ADF検定
for (var_index in (1:length(colnames(data_use)))){
  result = adf.test(data[,var_index],k=3)
  print(colnames(data_use)[var_index])
  print(result)
}
#(Nikkei225以外)P> 0.05 →H_0 単位根は棄却できない

#PP検定
for (var_index in (1:length(colnames(data_use)))){
  result_pp = pp.test(data_use[,var_index])
  print(colnames(data_use)[var_index])
  print(result_pp)
}
#誤差項のheteroscedasticityを考慮しても結果は同じになる。→一階階差でテスト

data_use_diff =   mutate(data_use,topix  = topix - dplyr::lag(topix,n=1),indprod  = indprod - dplyr::lag(indprod,n=1),exrate  = exrate - dplyr::lag(exrate,n=1),cpi  = cpi - dplyr::lag(cpi,n=1),saunemp  = saunemp - dplyr::lag(saunemp,n=1),intrate  = intrate- dplyr::lag(intrate,n=1))
data_use_diff = data_use_diff[-1,] #Nanを抜く
data_use_diff = data_use_diff[,1] #Nikkei225は階差取らなくてもstationary
for (var_index in (1:length(colnames(data_use_diff)))){
  result_pp = pp.test(data_use_diff[,var_index])
  print(colnames(data_use)[var_index])
  print(result_pp)
}


結果

output1

長いので一部割愛、重要な部分のみ
ADF, PP共にnikkei225以外は全て有意水準0.05で「帰無仮説H_0:単位根が存在」を棄却できず。

[1] "nikkei225"

    Augmented Dickey-Fuller Test

data:  data[, var_index]
Dickey-Fuller = -9.5665, Lag order = 3, p-value = 0.01
alternative hypothesis: stationary

[1] "topix"

    Augmented Dickey-Fuller Test

data:  data[, var_index]
Dickey-Fuller = -1.0587, Lag order = 3, p-value = 0.9284
alternative hypothesis: stationary

[1] "indprod"

    Augmented Dickey-Fuller Test

data:  data[, var_index]
Dickey-Fuller = -1.3092, Lag order = 3, p-value = 0.8685
alternative hypothesis: stationary

[1] "exrate"

    Augmented Dickey-Fuller Test

data:  data[, var_index]
Dickey-Fuller = -1.8058, Lag order = 3, p-value = 0.6589
alternative hypothesis: stationary

[1] "cpi"

    Augmented Dickey-Fuller Test

data:  data[, var_index]
Dickey-Fuller = -2.3998, Lag order = 3, p-value = 0.4082
alternative hypothesis: stationary

[1] "saunemp"

    Augmented Dickey-Fuller Test

data:  data[, var_index]
Dickey-Fuller = -1.9879, Lag order = 3, p-value = 0.582
alternative hypothesis: stationary

[1] "intrate"

    Augmented Dickey-Fuller Test

data:  data[, var_index]
Dickey-Fuller = -1.255, Lag order = 3, p-value = 0.8914
alternative hypothesis: stationary

output2

1階の階差の系列では上の単位根過程を棄却できなかった変数も「H_0:一階階差が単位根過程に従う」を有意水準5%で棄却できた。

[1] "topix"

    Phillips-Perron Unit Root Test

data:  data_use_diff[, var_index]
Dickey-Fuller Z(alpha) = -253.61, Truncation lag parameter = 5, p-value = 0.01
alternative hypothesis: stationary

[1] "indprod"

    Phillips-Perron Unit Root Test

data:  data_use_diff[, var_index]
Dickey-Fuller Z(alpha) = -553.4, Truncation lag parameter = 5, p-value = 0.01
alternative hypothesis: stationary

[1] "exrate"

    Phillips-Perron Unit Root Test

data:  data_use_diff[, var_index]
Dickey-Fuller Z(alpha) = -246.95, Truncation lag parameter = 5, p-value = 0.01
alternative hypothesis: stationary

[1] "cpi"

    Phillips-Perron Unit Root Test

data:  data_use_diff[, var_index]
Dickey-Fuller Z(alpha) = -267.34, Truncation lag parameter = 5, p-value = 0.01
alternative hypothesis: stationary

[1] "saunemp"

    Phillips-Perron Unit Root Test

data:  data_use_diff[, var_index]
Dickey-Fuller Z(alpha) = -387.91, Truncation lag parameter = 5, p-value = 0.01
alternative hypothesis: stationary

[1] "intrate"

    Phillips-Perron Unit Root Test

data:  data_use_diff[, var_index]
Dickey-Fuller Z(alpha) = -263.86, Truncation lag parameter = 5, p-value = 0.01
alternative hypothesis: stationary

・今回は一階階差を取ることで定常性を手に入れたが、logを取って定常にするケースも散見される。これはどういう形のモデルを背後に想定しているのかが見えにくいので、一回手を動かして計算してみる必要。

Reference

PPとADF検定しか問題では扱っていないが、その他の検定法についても載っているので参考になりそう。
https://fisproject.jp/2017/11/statistical-test-on-time-series-data/

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1