prophetでガソリン価格の予測
prophetパッケージを利用してみようということで、 とりあえずvignetteにあるものをデータを変えて実行してみた。
ガソリン価格の予測を行う
資源エネルギー庁のHPから給油所小売価格調査(ガソリン、軽油、灯油)の週次データを取得し、 レギュラーガソリン価格の推移を利用して今後のガソリン価格の予測を行う。 当たり前だが、データ依存による影響がみられており、適切なデータ選定が必要なことがわかる。
library(tidyverse) library(prophet) library(magrittr) file_url <- "https://github.com/ogwf/gasoline_value/blob/master/gasoline_prices.xls?raw=true" tmp <- paste0(getwd(), "/gasoline_prices.xls") download.file(url = file_url, destfile = tmp, mode = "wb") gasoline <- readxl::read_xls(path = tmp, sheet = "レギュラー", col_types = "numeric") names(gasoline) %<>% stringr::str_replace_all(pattern = "[:space:]", "") gasoline <- gasoline %>% filter(!is.na(調査日)) %>% mutate(調査日 = 調査日 %>% as.Date(origin = "1900-01-01"))
これまでのガソリン価格の推移はこのような状況である。
ggplot(gasoline, aes(調査日, 全国)) + geom_path()
これをprophetを利用して予測してみると。。。
res <- gasoline %>% transmute(ds = 調査日, y = 全国) %>% prophet()
Initial log joint probability = -9.47537 Optimization terminated normally: Convergence detected: relative gradient magnitude is below tolerance
future <- make_future_dataframe(res, periods = 365) plot(res, predict(res, future))
prophet_plot_components(res, predict(res))
2010年以降は、価格の変動が激しいこともありうまくトレースできておらず、近年は下落傾向となっている。
直近一年間のガソリン価格を使って予測してみる。
近年のガソリン価格の変動が激しくうまく予測できていないようなので、 期間を直近一年間に絞り同様に予測してみる。
直近のガソリン価格の推移は下記の通り。
gasoline %>% arrange(desc(調査日)) %>% slice(1:52) %>% ggplot(aes(調査日, 全国)) + geom_path()
予測結果は
res <- gasoline %>% arrange(desc(調査日)) %>% slice(1:52) %>% transmute(ds = 調査日, y = 全国) %>% prophet()
Initial log joint probability = -2.03264 Optimization terminated normally: Convergence detected: relative gradient magnitude is below tolerance
future <- make_future_dataframe(res, periods = 60) plot(res, predict(res, future))
prophet_plot_components(res, predict(res))