目的
- Rの"quantmod"パッケージを使って、FREDからデータを呼び出し、データセットを作成する。
- 作ったデータセットをHodrick-Prescott Filterでトレンド除去し、整理する。
背景
- 日本の政府統計は使いづらい…(PDF形式、出所がバラバラ、etc…)
- 米国のデータは…使いやすい…!(FRBのセントルイス連銀が提供するFREDというデータベース)
- できればローカルでデータセットを持たずに政府統計を分析したい!
- 推計期間の更新を簡単にしたい!
- quantmodパッケージの知名度はイマイチ…?
読み込むデータ
以下の5変数を用いる(1998Q1~2008Q2)。
- $Y$… Real Gross Domestic Product
- $C$… Real Personal Consumption Expenditures
- $I$… Real Gross Private Domestic Investment
- $W$… Employed full time: Median usual weekly real earnings
- $N$… 以下の2つのデータを掛けて作成(雇用量 = 生産年齢人口 × 生産年齢人口雇用率)
コード
まず、HP Filterで使える、"mFilter"パッケージを呼び出します。
(必要に応じてインストールしてください)
データのコード名(例:GDPC1)は、FREDページでの系列名の隣に書いてあります。
library(mFilter)
次に、推計期間を定義し、tickerを作成します。
data <- new.env()
# 推計開始時期と終了時期
date.start <- "1998-01-01"
date.end <- "2008-06-30"
#"y_obs", "c_obs", "i_obs", "w_obs", "n_obs"
#QUARTERLY
tickers <- c("GDPC1", #Real Gross Domestic Product #Y
"PCECC96", #Real Personal Consumption Expenditures #C
"GPDIC1", #Real Gross Private Domestic Investment #I
"LES1252881600Q", #Employed full time: Median usual weekly real earnings
#: Wage and salary workers: 16 years and over #W
"LFWA64TTUSQ647N", #Working Age Population: Aged 15-64: All Persons #N1
"LREM64TTUSQ156S") #Employment Rate: Aged 15-64: All Persons #N2
次に、quantmodパッケージを使ってデータを呼び出します。
# FREDのデータベースにアクセス
library("quantmod")
getSymbols( tickers
, src = "FRED"
, from = date.start
, to = date.end
, env = data
)
#階層分け
dtx1 <- data$GDPC1
x1 <- dtx1[paste(date.start,date.end,sep="/")]
dtx2 <- data$PCECC96
x2 <- dtx2[paste(date.start,date.end,sep="/")]
dtx3 <- data$GPDIC1
x3 <- dtx3[paste(date.start,date.end,sep="/")]
dtx4 <- data$LES1252881600Q
x4 <- dtx4[paste(date.start,date.end,sep="/")]
# 生産年齢人口*雇用率
dtxa <- data$LFWA64TTUSQ647N
xa <- dtxa[paste(date.start,date.end,sep="/")]
dtxb <- data$LREM64TTUSQ156S
xb <- dtxb[paste(date.start,date.end,sep="/")]
x5 <- xa*xb
次に、HP Filterをかけます。
四半期データなので、$\lambda = 1600$とします。
また、データの定常性を確保するため、自然対数をとっています。
#HPフィルター
hpf1 <- hpfilter(log(x1),freq = 1600)
hpf2 <- hpfilter(log(x2),freq = 1600)
hpf3 <- hpfilter(log(x3),freq = 1600)
hpf4 <- hpfilter(log(x4),freq = 1600)
hpf5 <- hpfilter(log(x5),freq = 1600)
結果を出力します。
以下ではサイクル成分のみを抽出していますが、
例えばhpf1$trend
などと入力すると、トレンド成分も見られます。
(詳細はView(hpf1)
等で確認してください。)
結果の出力先、形式等は適宜変更してください。
# 結果
out <- xts(cbind(hpf1$cycle, hpf2$cycle, hpf3$cycle, hpf4$cycle, hpf5$cycle)
, index(x1, x2, x3, x4, x5))
colnames(out) <- c("y_obs", "c_obs", "i_obs", "w_obs", "n_obs")
# 結果の表示
head(out)
# 結果の出力
write.csv(out, "C:/cat/mcmc.csv", row.names = F)
出力結果は、以下のようにExcel(csv)ファイルで出力されます。
最後に、結果を描画します(場合によっては不要)
# 結果の描画
par(mfrow = c(2, 1), mar = c(3, 2, 2, 1))
colnames(out) <- c("real GDP", "real consumption", "real investment",
"real wage", "employment")
plot(out[,"real GDP"], t= "n", main = "real GDP", col = "steelblue")
plot(out[,"real consumption"], t= "n", main = "real consumption", col = "steelblue")
plot(out[,"real investment"], t= "n", main = "real investment", col = "steelblue")
plot(out[,"real wage"], t= "n", main = "real wage", col = "steelblue")
plot(out[,"employment"], t= "n", main = "employment", col = "steelblue")
結果は以下のように表示されます。
横軸が時系列、縦軸が系列のトレンドからの乖離率を示しています。
全コードはこちらからご覧になれます。
ご参考(RBCモデルへの接続)
この記事で作成したデータは、こちらで用いることを目的としたものです。
(DynareによるRBCモデルのMCMC推計)
また、quantmodでは、FRED以外にも、様々なデータベースにアクセスできます。
参考… quantmodのdocumentation