Yoshi Nishikawa Blog

医学となにかのインタラクティブ

計量経済分析ことはじめ~時系列データを正しく扱う~

Rによる計量経済分析を読んだ。このシリーズはとても勉強になる。

書籍のHPはこちら

キーワードと、キーとなるパッケージ・関数を列挙しておく。
時系列データを扱う時の問題点がわかりやすく解説されていて、よかった。

クロスセクションデータの回帰分析

最小二乗推定量 (OLS:ordinary least squares estimator)
決定係数 (coefficient of determination)
BLUE (the best linear unbiased estimator)

不均一分散

実世界のデータを扱う際に標準的仮定が満たされない場合がある。其の1つが不均一分散。

不均一分散の検定は以下で実行できる。

  1. Breusch-Pagan 検定

  2. White 検定

  3. Goldfeld-Quandt 検定: データを2分割して、それらを同じモデルを回帰した場合に誤差項の分散が等しくなるかどうかを検定

bptest(), gqtest()の説明書を見ながら、以下を実行。

library(lmtest)

## generate a regressor
x <- rep(c(-10,10), 100)

## generate heteroskedastic and homoskedastic disturbances
err1 <- rnorm(100, sd=rep(c(1,2), 50))  
err2 <- rnorm(100) 

## generate a linear relationship
y1 <- 1 + x + err1
y2 <- 1 + x + err2

## perform studentized Breusch-Pagan test
bptest(y1 ~ x)
bptest(y2 ~ x)

## perform Breusch-Pagan test
bptest(y1 ~ x, studentize = F) #studentize is a logical number
bptest(y2 ~ x, studentize = F)

## perform Goldfeld-Quandt test
gqtest(y1 ~ x, point=0.5) #point is interpreated to be a breakpoint.
gqtest(y2 ~ x, point=0.5)

時系列データの回帰分析

系列相関

時系列データでは、系列相関という問題が発生する。ある時点の回帰分析の誤差項と、其の隣の誤差項が相関している状態のことを指す。時系列データは、各時点毎に観測されたものであり、観測値の順番に意味があるので、注意が必要だ。

研究者は、このことをしっかり意識する必要がある。

系列相関の検定

  1. Durbin-Watson検定
  2. Breusch-Godfrey検定
library(lmtest)
## perform Durbin-Watson test
dwtest(y1 ~ x)

## perform Breusch-Godfrey test for first-order serial correlation:
bgtest(y1 ~ x)
## or for fourth-order serial correlation
bgtest(y1 ~ x, order = 4)

系列相関がある場合の推定

#FGLSを用いる方法
library(nlme)
res <- gls(y1~x, corr=corARMA(p=1))
summary(res)

#Newey-Westの修正を行う
library(sandwich)
coeftest(lm(y1~x), vcov=NeweyWest)

Stationary time series analysis (定常時系列分析)

定常過程 (Stationary process): 期待値・分散・共分散が時点tに依存せず、一定である。
independent and identically distributed; IID

モデル化には、自己回帰と移動平均を考慮して考える。

  1. Autoregressive model (自己回帰): p次のAR modelはAR(p)と記述される。

  2. Moving average model (移動平均

  3. Autoregressive moving average model (自己回帰移動平均

  4. Autoregressive integrated moving average model (自己回帰和分移動平均

ARCHとGARCH

時期によって分散の大きさがことなることを考慮する場合に用いる。

  1. (Generalized) autoregressive conditional heteroskedasticity model

GARCH(1,1)でモデリングしたいときは以下。

library(fGarch)
GARCH <- garchFit(formula = ~ garch(1, 1), data = data, 
         trace = FALSE, include.mean =TRUE)
summary(GARCH)

多変量時系列

複数の時系列データ(Aの株価とBの株価など)が互いに影響を及ぼしうるときに使う。

VAR vector autoregressive model

定常過程かどうか

library(tseries)
adf.test(data)
pp.test(data)

モデルの次数決定

library(vars)
VARselect(data, lag.max=5, type="const")
vars.res <- VAR(data, p=1, type="const")
summary(vars.res)
causality(vars.res, cause="A")
causality(vars.res, cause="B")

非定常時系列

単位根検定: ある時系列がランダム・ウォークに従っているかどうかの検定
見せかけの回帰: ランダム・ウォークに従っている者同士を回帰すると、見せかけの回帰(spurious regression)を来すことあり。 共和分分析: ランダム・ウォークに従っている者同士でも、共和分関係にあるものであれば、回帰にも意味がある。

library(tseries)
adf.test(data) #Augmented Dickey-Fuller
pp.test(data) #Phillips-Perron

library(urca)
ca.po(data) #Philips-Ouliaris
ca.jo(data) #Johansen

パネル

複数の同じ経済主体に関する複数時点でのデータである。

fixed effectとrandom effectの推定結果の検定: Hausman検定

library(plm)
phtest(res.fixed, res.random)