『経済・ファイナンスデータの計量時系列分析』章末問題をRで解く－第5章単位根過程－

<a href="https://www.amazon.co.jp/%E7%B5%8C%E6%B8%88%E3%83%BB%E3%83%95%E3%82%A1%E3%82%A4%E3%83%8A%E3%83%B3%E3%82%B9%E3%83%87%E3%83%BC%E3%82%BF%E3%81%AE%E8%A8%88%E9%87%8F%E6%99%82%E7%B3%BB%E5%88%97%E5%88%86%E6%9E%90-%E7%B5%B1%E8%A8%88%E3%83%A9%E3%82%A4%E3%83%96%E3%83%A9%E3%83%AA%E3%83%BC-%E6%B2%96%E6%9C%AC-%E7%AB%9C%E7%BE%A9/dp/4254127928/ref=as_li_ss_tl?ie=UTF8&qid=1478165293&sr=8-1&keywords=%E7%B5%8C%E6%B8%88%E3%83%BB%E3%83%95%E3%82%A1%E3%82%A4%E3%83%8A%E3%83%B3%E3%82%B9%E3%83%87%E3%83%BC%E3%82%BF%E3%81%AE%E8%A8%88%E9%87%8F%E6%99%82%E7%B3%BB%E5%88%97%E5%88%86%E6%9E%90&linkCode=ll1&tag=aokikenichi-22&linkId=b4fb2bc3209ec5a8b2d381d8955f2486">『経済・ファイナンスデータの計量時系列分析』</a>
<a href="https://www.amazon.co.jp/%E7%B5%8C%E6%B8%88%E3%83%BB%E3%83%95%E3%82%A1%E3%82%A4%E3%83%8A%E3%83%B3%E3%82%B9%E3%83%87%E3%83%BC%E3%82%BF%E3%81%AE%E8%A8%88%E9%87%8F%E6%99%82%E7%B3%BB%E5%88%97%E5%88%86%E6%9E%90-%E7%B5%B1%E8%A8%88%E3%83%A9%E3%82%A4%E3%83%96%E3%83%A9%E3%83%AA%E3%83%BC-%E6%B2%96%E6%9C%AC-%E7%AB%9C%E7%BE%A9/dp/4254127928/ref=as_li_ss_il?ie=UTF8&qid=1478165293&sr=8-1&keywords=%E7%B5%8C%E6%B8%88%E3%83%BB%E3%83%95%E3%82%A1%E3%82%A4%E3%83%8A%E3%83%B3%E3%82%B9%E3%83%87%E3%83%BC%E3%82%BF%E3%81%AE%E8%A8%88%E9%87%8F%E6%99%82%E7%B3%BB%E5%88%97%E5%88%86%E6%9E%90&linkCode=li2&tag=aokikenichi-22&linkId=53c071200e6bc21d9723dd3a5f926476" target="_blank"><img border="0" src="//ws-fe.amazon-adsystem.com/widgets/q?_encoding=UTF8&ASIN=4254127928&Format=_SL160_&ID=AsinImage&MarketPlace=JP&ServiceVersion=20070822&WS=1&tag=aokikenichi-22" ></a><img src="https://ir-jp.amazon-adsystem.com/e/ir?t=aokikenichi-22&l=li2&o=9&a=4254127928" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />
の章末問題で「コンピュータを用いて」とあるものをRで解いています。

* [サポートサイト（データダウンロード）](http://www.geocities.jp/tatsuyoshi_okimoto/books/tsa.html)

# 5.2.

```r
data<-read.table("economicdata.txt",header=T)
data.ts<-ts(data=data[,2:7],start=c(1975, 1), frequency=12)
```

以下すべてトレンドがあるので「場合3」の筈だが、解答例と異なる

- topix

```r
plot(stl(data.ts[,1], s.window="per"), main="topix")
```
![Rplot07.png](https://qiita-image-store.s3.amazonaws.com/0/116049/4b6ed573-4514-262a-4c10-a5111ddef7ff.png)

- exrate

```r
plot(stl(data.ts[,2], s.window="per"), main="exrate")
```
![Rplot08.png](https://qiita-image-store.s3.amazonaws.com/0/116049/b6176a52-a28e-37c0-aa43-e894c3fa19bf.png)


- indprod(本書ではGDP)

```r
plot(stl(data.ts[,3], s.window="per"), main="indprod(GDP)")
```
![Rplot09.png](https://qiita-image-store.s3.amazonaws.com/0/116049/9c8d6801-b8d8-044f-9804-da99badbf24e.png)

- cpi

```r
plot(stl(data.ts[,4], s.window="per"), main="cpi")
```
![Rplot10.png](https://qiita-image-store.s3.amazonaws.com/0/116049/aa681d9d-4c1b-e186-341d-a3ac4734c4c5.png)

- saunemp(失業率)

```r
plot(stl(data.ts[,5], s.window="per"), main="saunemp")
```
![Rplot11.png](https://qiita-image-store.s3.amazonaws.com/0/116049/bbae17f2-c012-d531-97e3-fe06e911e3c4.png)

- intrate(コールレート)

```r
plot(stl(data.ts[,6], s.window="per"), main="intrate")
```
![Rplot12.png](https://qiita-image-store.s3.amazonaws.com/0/116049/bd4a0d14-4d72-213b-3612-459b94e4821d.png)

# 5.3.
- テキストの値と合わないが、差分を取って帰無仮説が棄却されたので、単位根過程
-- 差分系列で分析を行う

```r
library(tseries)
adf.test(data.ts[,5],alternative="stationary")
```

```r
	Augmented Dickey-Fuller Test

data:  data.ts[, 5]
Dickey-Fuller = -1.5663, Lag order = 7, p-value = 0.76
alternative hypothesis: stationary

```

```r
adf.test(data.ts[,5],alternative="explosive")
```

```r
	Augmented Dickey-Fuller Test

data:  data.ts[, 5]
Dickey-Fuller = -1.5663, Lag order = 7, p-value = 0.24
alternative hypothesis: explosive
```

```r
adf.test(diff(data.ts[,5]),alternative="stationary")
```

```r
	Augmented Dickey-Fuller Test

data:  diff(data.ts[, 5])
Dickey-Fuller = -5.3633, Lag order = 7, p-value = 0.01
alternative hypothesis: stationary

Warning message:
In adf.test(diff(data.ts[, 5]), alternative = "stationary") :
  p-value smaller than printed p-value
```

```r
adf.test(diff(data.ts[,5]),alternative="explosive")
```
```r
	Augmented Dickey-Fuller Test

data:  diff(data.ts[, 5])
Dickey-Fuller = -5.3633, Lag order = 7, p-value = 0.99
alternative hypothesis: explosive
```

# 5.5.
- トレンドの有無でalternative=c("stationaly", "explosiv")を指定する必要があるが、以下は"stationaly"で実行

## (1)
- topix

```r
adf.test(log(data.ts[,1]))
```

```r
	Augmented Dickey-Fuller Test

data:  log(data.ts[, 1])
Dickey-Fuller = -1.2425, Lag order = 7, p-value = 0.8966
alternative hypothesis: stationary
```

```r
adf.test(log(data.ts[,1]), alternative="explosiv")
```

```r
	Augmented Dickey-Fuller Test

data:  log(data.ts[, 1])
Dickey-Fuller = -1.2425, Lag order = 7, p-value = 0.1034
alternative hypothesis: explosive
```

```r
pp.test(log(data.ts[,1]))
```

```r
	Phillips-Perron Unit Root Test

data:  log(data.ts[, 1])
Dickey-Fuller Z(alpha) = -2.6978, Truncation lag parameter = 5, p-value =
0.9476
alternative hypothesis: stationary
```

- exrate

```r
adf.test(log(data.ts[,2]))
```

```r
	Augmented Dickey-Fuller Test

data:  log(data.ts[, 2])
Dickey-Fuller = -1.748, Lag order = 7, p-value = 0.6833
alternative hypothesis: stationary
```

```r
pp.test(log(data.ts[,2]))
```

```r
	Phillips-Perron Unit Root Test

data:  log(data.ts[, 2])
Dickey-Fuller Z(alpha) = -7.2163, Truncation lag parameter = 5, p-value =
0.7062
alternative hypothesis: stationary
```

- indprod(本書ではGDP)

```r
adf.test(log(data.ts[,3]))
```

```r
	Augmented Dickey-Fuller Test

data:  log(data.ts[, 3])
Dickey-Fuller = -2.2418, Lag order = 7, p-value = 0.4749
alternative hypothesis: stationary
```

```r
pp.test(log(data.ts[,3]))
```

```r
	Phillips-Perron Unit Root Test

data:  log(data.ts[, 3])
Dickey-Fuller Z(alpha) = -4.6404, Truncation lag parameter = 5, p-value =
0.8504
alternative hypothesis: stationary
```

- cpi

```r
adf.test(log(data.ts[,4]))
```

```r
	Augmented Dickey-Fuller Test

data:  log(data.ts[, 4])
Dickey-Fuller = -3.8057, Lag order = 7, p-value = 0.01899
alternative hypothesis: stationary
```

```r
pp.test(log(data.ts[,4]))
```

```r
	Phillips-Perron Unit Root Test

data:  log(data.ts[, 4])
Dickey-Fuller Z(alpha) = -4.1101, Truncation lag parameter = 5, p-value =
0.8801
alternative hypothesis: stationary
```

- saunemp(失業率)

```r
adf.test(data.ts[,5])
```

```r
	Augmented Dickey-Fuller Test

data:  data.ts[, 5]
Dickey-Fuller = -1.5663, Lag order = 7, p-value = 0.76
alternative hypothesis: stationary
```

```r
pp.test(data.ts[,5])
```

```r
	Phillips-Perron Unit Root Test

data:  data.ts[, 5]
Dickey-Fuller Z(alpha) = -3.4958, Truncation lag parameter = 5, p-value =
0.9117
alternative hypothesis: stationary
```

- intrate(コールレート)

```r
adf.test(data.ts[,6])
```

```r
	Augmented Dickey-Fuller Test

data:  data.ts[, 6]
Dickey-Fuller = -3.7402, Lag order = 7, p-value = 0.02226
alternative hypothesis: stationary
```

```r
pp.test(data.ts[,6])
```

```r
	Phillips-Perron Unit Root Test

data:  data.ts[, 6]
Dickey-Fuller Z(alpha) = -15.324, Truncation lag parameter = 5, p-value =
0.2523
alternative hypothesis: stationary
```
- cpi, intrateのadfのみ帰無仮説を棄却

## (2)

- topix

```r
adf.test(diff(log(data.ts[,1])))
```

```r
	Augmented Dickey-Fuller Test

data:  diff(log(data.ts[, 1]))
Dickey-Fuller = -6.3141, Lag order = 7, p-value = 0.01
alternative hypothesis: stationary

Warning message:
In adf.test(diff(log(data.ts[, 1]))) : p-value smaller than printed p-value
```

```r
pp.test(diff(log(data.ts[,1])))
```

```r
	Phillips-Perron Unit Root Test

data:  diff(log(data.ts[, 1]))
Dickey-Fuller Z(alpha) = -253.61, Truncation lag parameter = 5, p-value = 0.01
alternative hypothesis: stationary

Warning message:
In pp.test(diff(log(data.ts[, 1]))) : p-value smaller than printed p-value
```

- exrate

```r
adf.test(diff(log(data.ts[,2])))
```

```r
	Augmented Dickey-Fuller Test

data:  diff(log(data.ts[, 2]))
Dickey-Fuller = -5.8527, Lag order = 7, p-value = 0.01
alternative hypothesis: stationary

Warning message:
In adf.test(diff(log(data.ts[, 2]))) : p-value smaller than printed p-value
```

```r
pp.test(diff(log(data.ts[,2])))
```

```r
	Phillips-Perron Unit Root Test

data:  diff(log(data.ts[, 2]))
Dickey-Fuller Z(alpha) = -246.95, Truncation lag parameter = 5, p-value = 0.01
alternative hypothesis: stationary

Warning message:
In pp.test(diff(log(data.ts[, 2]))) : p-value smaller than printed p-value
```

- indprod(本書ではGDP)

```r
adf.test(diff(log(data.ts[,3])))
```

```r
	Augmented Dickey-Fuller Test

data:  diff(log(data.ts[, 3]))
Dickey-Fuller = -5.1424, Lag order = 7, p-value = 0.01
alternative hypothesis: stationary

Warning message:
In adf.test(diff(log(data.ts[, 3]))) : p-value smaller than printed p-value
```

```r
pp.test(diff(log(data.ts[,3])))
```

```r
	Phillips-Perron Unit Root Test

data:  diff(log(data.ts[, 3]))
Dickey-Fuller Z(alpha) = -553.4, Truncation lag parameter = 5, p-value = 0.01
alternative hypothesis: stationary

Warning message:
In pp.test(diff(log(data.ts[, 3]))) : p-value smaller than printed p-value
```

- cpi

```r
adf.test(diff(log(data.ts[,4])))
```

```r
	Augmented Dickey-Fuller Test

data:  diff(log(data.ts[, 4]))
Dickey-Fuller = -4.6986, Lag order = 7, p-value = 0.01
alternative hypothesis: stationary

Warning message:
In adf.test(diff(log(data.ts[, 4]))) : p-value smaller than printed p-value
```

```r
pp.test(diff(log(data.ts[,4])))
```

```r
	Phillips-Perron Unit Root Test

data:  diff(log(data.ts[, 4]))
Dickey-Fuller Z(alpha) = -267.34, Truncation lag parameter = 5, p-value = 0.01
alternative hypothesis: stationary

Warning message:
In pp.test(diff(log(data.ts[, 4]))) : p-value smaller than printed p-value
```

- saunemp(失業率)

```r
adf.test(diff(data.ts[,5]))
```

```r
	Augmented Dickey-Fuller Test

data:  diff(data.ts[, 5])
Dickey-Fuller = -5.3633, Lag order = 7, p-value = 0.01
alternative hypothesis: stationary

Warning message:
In adf.test(diff(data.ts[, 5])) : p-value smaller than printed p-value
```

```r
pp.test(diff(data.ts[,5]))
```

```r
	Phillips-Perron Unit Root Test

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

Warning message:
In pp.test(diff(data.ts[, 5])) : p-value smaller than printed p-value
```

- intrate(コールレート)

```r
adf.test(diff(data.ts[,6]))
```

```r
	Augmented Dickey-Fuller Test

data:  diff(data.ts[, 6])
Dickey-Fuller = -5.9193, Lag order = 7, p-value = 0.01
alternative hypothesis: stationary

Warning message:
In adf.test(diff(data.ts[, 6])) : p-value smaller than printed p-value
```

```r
pp.test(diff(data.ts[,6]))
```

```r
	Phillips-Perron Unit Root Test

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

Warning message:
In pp.test(diff(data.ts[, 6])) : p-value smaller than printed p-value
```

- すべて、p-value=0.01 となり、5or1%の危険率で帰無仮説が棄却される。

次は、[『経済・ファイナンスデータの計量時系列分析』章末問題をRで解く－第6章見せかけの回帰と共和分－](http://qiita.com/aokikenichi/items/825011ee4601273ba81c)