I have a matrix output from the Seasonal package that I filter out the "forecast" column leaving only the time (Month Year) and the "lowerci" and "upperci" entries.
This is done via:
season13201101FL.forecast[,c('lowerci','upperci')]
Sample of the data:
lowerci upperci
Oct 2017 2415.8826 3083.332
Nov 2017 2217.2670 3238.572
Dec 2017 1976.0041 3181.648
Jan 2018 2048.9771 3577.373
Feb 2018 2046.3051 3834.099
This is of "mts" class.
I am using the highcharter library to plot out my values. However, it does not appear to be using both "lowerci" and "upperci" columns even though I am using series.keys
to map.:
hc <- highchart(type = "stock") %>%
hc_add_series(season13201101FL, id = "Original", name = "Original-FL") %>%
hc_add_series(season13201101FL.seasonalData, id = "Seasonally Adjusted-FL", name = "Seasonally Adjusted") %>%
hc_add_series(season13201101FL.forecast[,c('forecast')], id = "Forecast-FL") %>%
hc_add_series(season13201101FL.forecast[,c('lowerci','upperci')], id = "ForecastRange-FL", keys = c('x', 'low', 'high'), type = "arearange")
hc
The resulting chart has the original, seasonally adjusted, and forecast series showing but the forecast range shows up with no "line" connecting the points and only one actual data point per time entry. How to get highcharter to see that this is an arearange
series?
To reproduce use the following as the import CSV as theCSV
:
date count
2008.0027 45778
2008.0874 50460
2008.1667 62162
2008.2514 55999
2008.3333 51571
2008.418 45044
2008.5 46357
2008.5847 48498
2008.6694 45472
2008.7514 47161
2008.8361 41907
2008.918 39131
2009.0027 33810
2009.0877 34469
Then the code is:
library(shiny)
library(highcharter)
library(dplyr)
library(tidyr)
library(seasonal)
seasonData <- ts(theCSV[,-1], frequency = 12, start = c(2008,1));
seasonData.seas <- seas(seasonData);
seasonData.seasonalData <- final(seasonData.seas);
seasonData.forecast <- series(seasonData.seas, "forecast.forecasts");
seasonData.seasComp <- series(seasonData.seas, "seats.seasonal");
hc <- highchart(type = "stock") %>%
hc_add_series(seasonData, id = "Original", name = "Original-FL") %>%
hc_add_series(seasonData.seasonalData, id = "Seasonally Adjusted-FL", name = "Seasonally Adjusted") %>%
hc_add_series(seasonData.forecast[,c('forecast')], id = "Forecast-FL") %>%
hc_add_series(seasonData.forecast[,c('lowerci','upperci')], id = "ForecastRange-FL", keys = c('x', 'low', 'high'), type = "arearange")
hc;
One approach is tranform de forecast to a data frame with values and dates/time values.
To get the datetime
values you can use time
and as.Date
function. Then
use hc_add_series
to add the data.
library(highcharter)
library(dplyr)
library(tidyr)
library(seasonal)
seasonData <- AirPassengers
seasonData.seas <- seas(seasonData);
seasonData.seasonalData <- final(seasonData.seas);
seasonData.forecast <- series(seasonData.seas, "forecast.forecasts");
seasonData.seasComp <- series(seasonData.seas, "seats.seasonal");
time <- seasonData.forecast %>%
stats::time() %>%
zoo::as.Date() %>%
datetime_to_timestamp()
dfforecast <- seasonData.forecast %>%
as.data.frame() %>%
mutate(time = time)
highchart(type = "stock") %>%
hc_add_series(seasonData, id = "Original", name = "Original-FL") %>%
hc_add_series(seasonData.seasonalData, id = "Seasonally Adjusted-FL", name = "Seasonally Adjusted") %>%
hc_add_series(seasonData.forecast[,c('forecast')], id = "Forecast-FL") %>%
hc_add_series(dfforecast, hcaes(x = time, low = lowerci, high = upperci), id = "ForecastRange-FL", type = "arearange")
hc