クラスに依存せずに時系列データを扱えるtsbox
R-bloggersで見つけたあまり注目されなそうなパッケージを拾ってみます。時系列解析系のパッケージはなんでも一度は入れてみる派です。
Rでは時系列データを扱うためのクラスが乱立しています。
伝統的なものとしてはts
やzoo
、その後一時期注目されたxts
、最近はtidyverse全盛のため、tibbletime
やtsibble
、もしくはtbl
、data.frame
でそのまま扱うということが多いのではないかと思います。
このtsbox
は、クラス間の変換とクラスに依存しない時系列データを扱うための便利関数がまとめられたパッケージとなっています。
基本的な使い方
まずは、簡単な例を見てみます。
fdeaths
とmdeaths
データは、イギリスの1974年から1979年の月次での肺疾患で亡くなった人数の男女別のデータになっています。クラスはどちらもts
です。
ts
クラス同士の結合。
> library(tidyverse) > library(tsbox) > > deaths_mts <- ts_c(fdeaths, mdeaths) > deaths_mts fdeaths mdeaths Jan 1974 901 2134 Feb 1974 689 1863 Mar 1974 827 1877 Apr 1974 677 1877 May 1974 522 1492 Jun 1974 406 1249 Jul 1974 441 1280 Aug 1974 393 1131 Sep 1974 387 1209 Oct 1974 582 1492 Nov 1974 578 1621 Dec 1974 666 1846 Jan 1975 830 2103 ...
tbl
クラスへの変換。縦持ちのデータになってくれます。
> deaths_tbl <- deaths_mts %>% + ts_tbl() > deaths_tbl # A tibble: 144 x 3 id time value <chr> <date> <dbl> 1 fdeaths 1974-01-01 901 2 fdeaths 1974-02-01 689 3 fdeaths 1974-03-01 827 4 fdeaths 1974-04-01 677 5 fdeaths 1974-05-01 522 6 fdeaths 1974-06-01 406 7 fdeaths 1974-07-01 441 8 fdeaths 1974-08-01 393 9 fdeaths 1974-09-01 387 10 fdeaths 1974-10-01 582 # … with 134 more rows
横持ちのデータへの変換。あまり使うことはなさそうです。
> deaths_tbl %>% + ts_wide() # A tibble: 72 x 3 time fdeaths mdeaths <date> <dbl> <dbl> 1 1974-01-01 901 2134 2 1974-02-01 689 1863 3 1974-03-01 827 1877 4 1974-04-01 677 1877 5 1974-05-01 522 1492 6 1974-06-01 406 1249 7 1974-07-01 441 1280 8 1974-08-01 393 1131 9 1974-09-01 387 1209 10 1974-10-01 582 1492 # … with 62 more rows
tsibble
クラスへの変換。パッケージは個別に読み込む必要があります。
> library(tsibble) > deaths_tbl %>% + ts_tsibble() # A tsibble: 144 x 3 [1D] # Key: id [2] id time value <chr> <date> <dbl> 1 fdeaths 1974-01-01 901 2 fdeaths 1974-02-01 689 3 fdeaths 1974-03-01 827 4 fdeaths 1974-04-01 677 5 fdeaths 1974-05-01 522 6 fdeaths 1974-06-01 406 7 fdeaths 1974-07-01 441 8 fdeaths 1974-08-01 393 9 fdeaths 1974-09-01 387 10 fdeaths 1974-10-01 582 # … with 134 more rows
ggplot2
で可視化が可能です。
> deaths_tbl %>% + ts_ggplot() + + theme_bw() + + theme(legend.position = "top")
以上のようにクラス間の変換や可視化を簡単に行うことができます。
便利な関数
実務での利用の場合、上のようなts
クラスオブジェクトのデータがあるわけでもなく、SQLでデータを抽出して、readr::read_tsv
などでRに読み込んで...その後に、可視化して、モデリングして...という流れが圧倒的に多いので、簡単にクラス間の変換ができるという恩恵を受ける場面は少ないのではないかと考えられます。
そのため、このパッケージを利用するメインの動機になるのは、次のような便利関数を用いるためになると思います。deaths_tbl
をtsvファイルなどから読み込んだ後のデータと考えておきます。
まずは、差分を取ります。
> deaths_tbl %>% + ts_diff() # A tibble: 144 x 3 id time value <chr> <date> <dbl> 1 fdeaths 1974-01-01 NA 2 fdeaths 1974-02-01 -212 3 fdeaths 1974-03-01 138 4 fdeaths 1974-04-01 -150 5 fdeaths 1974-05-01 -155 6 fdeaths 1974-06-01 -116 7 fdeaths 1974-07-01 35 8 fdeaths 1974-08-01 -48 9 fdeaths 1974-09-01 -6 10 fdeaths 1974-10-01 195 # … with 134 more rows
変化率を計算するのも簡単です。
> deaths_tbl %>% + ts_pc() # A tibble: 144 x 3 id time value <chr> <date> <dbl> 1 fdeaths 1974-01-01 NA 2 fdeaths 1974-02-01 -23.5 3 fdeaths 1974-03-01 20.0 4 fdeaths 1974-04-01 -18.1 5 fdeaths 1974-05-01 -22.9 6 fdeaths 1974-06-01 -22.2 7 fdeaths 1974-07-01 8.62 8 fdeaths 1974-08-01 -10.9 9 fdeaths 1974-09-01 -1.53 10 fdeaths 1974-10-01 50.4 # … with 134 more rows
forecast
を用いた予測もできます。
> library(forecast) > deaths_forecast_tbl <- deaths_tbl %>% + ts_forecast() > deaths_forecast_tbl # A tibble: 48 x 3 id time value <chr> <date> <dbl> 1 fdeaths 1980-01-01 789. 2 fdeaths 1980-02-01 812. 3 fdeaths 1980-03-01 746. 4 fdeaths 1980-04-01 592. 5 fdeaths 1980-05-01 479. 6 fdeaths 1980-06-01 413. 7 fdeaths 1980-07-01 394. 8 fdeaths 1980-08-01 355. 9 fdeaths 1980-09-01 365. 10 fdeaths 1980-10-01 443. # … with 38 more rows
元系列と予測系列の可視化することも簡単です。
> ts_ggplot(deaths_tbl, deaths_forecast_tbl) + + theme_bw() + + theme(legend.position = "top")
以上のように、時系列データを扱う際によく行う処理や可視化などをだいぶ省力化してくれるのではないかと思います。
他にもts_dygraphs()
は、R MarkdownでHTML文書に分析結果をまとめてレポーティングするという場合にも使えるのではないでしょうか?