RでX-13ARIMA-SEATSを使う(2019年6月版)

自分用の備忘録です。CRANにあるx13binaryが使えるようになったので、自分でX-13ARIMA-SEATSをコンパイルする必要がなくなりました。

[インストール]GNU Rで二つのパッケージをインストール

  1. install.packages("seasonal"); install.packages("x13binary")

[GNU Rのパッケージseasonalからx13asを実行する]

  1. パッケージの読み込み
    library(x13binary); library(seasonal)
  2. checkX13()を実行して、"Congratulations! 'seasonal' should work fine!"と表示されれば問題なし
  3. m <- seas(AirPassengers)
  4. plot(m)
  5. 季節調整済み系列(s11)の取り出し
    m$series$s11
  6. seasonalの使い方についてvignettesを参照されたい
    https://cran.ism.ac.jp/web/packages/seasonal/vignettes/seas.pdf

[注意すべき点]

  1. X-13ARIMA-SEATSから季節調整済み系列はs11となった模様(X-12-ARIMAのd11。プログラムする人はお気をつけ下さい)
    http://www.census.gov/ts/x13as/docX13ASHTML.pdf

[s10やs11等のまとめ]
s10: 季節成分(seasonal component)
s11: 季節調整値(seasonal adjustment component)
s12: トレンド成分(trend component)
s13: 不規則成分(rregular component)
詳しくは以下のTable7.13を参照:
https://www.census.gov/ts/x13as/docX13AS.pdf

Rでxlsxを読み書きする

以下のサイトが非常に分かりやすかったです。
R で openxlsx パッケージを用いて xlsx ファイルを扱う - Qiita
http://qiita.com/zero310/items/71b6e5bc0731680d19df

Rで解析:セル体裁!大きなデータも大丈夫!エクセル操作の「openxlsx」パッケージ
http://www.karada-good.net/analyticsr/r-338

マイナス金利に関するまとめ(2016/09/23初稿、09/26修正)

以下、自分用の覚書:

概況:

以下、「超過準備に対するマイナス金利」をマイナス金利政策と呼ぶ。

日本語によるまとめ:

英語によるまとめ:

日銀:

デンマーク国立銀行

欧州中央銀行

スウェーデン中央銀行

スイス国立銀行

ハンガリー国立銀行

GNU Rで月次データを四半期に集約する

時系列解析で月次データを四半期に集約する(3ヶ月の合計や平均を計算する)ことはよくある。以下はstats, zoo, xtsを使った場合。

まず準備:

# 2010年1月から12月までの月次データを生成する
rm(list=ls())
dataLength <- 12
set.seed(1) # to make it reproducible
data1 <- rnorm(dataLength)

statsのtsオブジェクトに対してstatsのaggregateを使う場合:

dataMonthTs <- ts(data1, start=c(2010,1), frequency=12)
dataQuartTs <- aggregate(dataMonthTs, nfrequency = 4, FUN = sum)
dataQuartTs

以上のやり方はzooもxtsもなかった10年以上前にはよく使われていた。ただし、最近は以下のようにzooやxtsを使うほうが一般的かもしれない。

zooオブジェクトに対してzooのaggregateを使う場合:

library(zoo)
dateZoo <- seq(as.Date("2010-01-01"), as.Date("2010-12-01"), by = "month")
dataMonthZoo <- zoo(data1, dateZoo)
dataQuartZoo <- aggregate(dataMonthZoo, as.yearqtr, sum)
dataQuartZoo

zooオブジェクトに対してxtsのapply.quarterlyを使う場合:

library(xts)
dataQuartXts <- apply.quarterly(dataMonthZoo, sum)
dataQuartXts

# ただし、四半期の日付が2010-03-01 2010-06-01等となって日付がふさわしくない。
# たとえば、以下のようにして日付2010-01-01 2010-04-01等と直す。
time(dataQuartZoo) <- as.Date(as.yearqtr(time(dataQuartZoo)))
dataQuartZoo
#それ以外だとto.quarterlyを使う方法も考えられる。

その他にもRmetricsのtimeSeriesの関数を使うなどのやり方もあると思います。よりエレガントなやり方があったらコメントで教えて下さい。

[参考]
R - Set the first month of each quarter as the index after applying apply.quarterly() - Stack Overflow
http://stackoverflow.com/questions/19352828/set-the-first-month-of-each-quarter-as-the-index-after-applying-apply-quarterly
Kobe.R: Kobe.R #10 xts 時系列の簡単操作
http://kobexr.blogspot.jp/2014/10/kober-10-xts.html

Emacsでjulia-mode

ESS (Emacs Speaks Statistics) にはすでにjulia-modeが入っているはずなのですが、OS Xでうまく動かないので、しょうがなくJulia-lang公式のjulia-mode.elを入れてみました。

  1. Julia-lang公式のgithubから julia-mode.el をダウンロード(ページの「RAW」をクリックしてローカル[たとえば~/bin/julia]に保存)
  2. ~/.emacsに以下のように記述

(add-to-list 'load-path "~/bin/julia")
(require 'julia-mode)

ESS (Emacs Speaks Statistics) をgitからインストール

  1. cd ~/bin/
  2. git clone https://github.com/emacs-ess/ESS.git
  3. cd ess
  4. make
  5. ~/.emacsに以下のように記述*1

(add-to-list 'load-path "~/bin/ess/lisp")
(load "ess-site")

以上でemacs上でM-RでRを起動できるようになる*2

*1:~/.emacs.d/init.elに書いておくのが現在の正しいお作法かもしれません。でも矢野がemacs使い始めた25年前にはそんな便利な仕組みはなかったので、今でも~/.emacsを使っています・・・

*2:OS Xの場合:makeでwgetを使う必要があるので、homebrewの場合はbrew install wgetしておくこと。