Title: | Visualizing Collections of Time Series Forecasts |
---|---|
Description: | A way of visualizing collections of time series and, optionally their future values, forecasts for their future values and prediction intervals for the forecasts. A web-based GUI can be used to display the information in a collection of time series. |
Authors: | Maria Pilar Frias-Bustamante [aut] , Francisco Martinez [aut, cre, cph] |
Maintainer: | Francisco Martinez <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.1 |
Built: | 2024-12-31 03:11:51 UTC |
Source: | https://github.com/franciscomartinezdelrio/vctsfr |
This function checks that an object holding a collection of time series,
their future values and their forecasts has the correct format. This kind of
objects are used in function plot_collection()
. A collection of time series
should be a list compounded of objects of class ts_info
, which are built
using the ts_info()
function.
check_time_series_collection(collection)
check_time_series_collection(collection)
collection |
a list representing a collection of time series as
described in |
a character string with value "OK"
if the object is properly
formatted. Otherwise, the character string indicates the first error found
in the object's format.
c <- list(ts_info(USAccDeaths), ts_info(ldeaths)) check_time_series_collection(c)
c <- list(ts_info(USAccDeaths), ts_info(ldeaths)) check_time_series_collection(c)
Launches the web-based GUI for visualizing a collection of time series in a web browser.
GUI_collection(collection)
GUI_collection(collection)
collection |
a list with the collection of time series. Each component
of the list must have been built with the |
The vctsfr package provides a Shiny-based GUI to visualize collections of time series and their forecasts. The main features of the GUI are:
It allows you to easily navigate through the different series.
You can select which forecasting methods are displayed.
In the case you display a single forecasting method with associated prediction intervals, you can select the prediction interval to display.
Forecasting accuracy measures are displayed.
Nothing
# create a collection of two time series and visualize them c <- list(ts_info(USAccDeaths), ts_info(ldeaths)) GUI_collection(c)
# create a collection of two time series and visualize them c <- list(ts_info(USAccDeaths), ts_info(ldeaths)) GUI_collection(c)
The object created represents a prediction interval for the forecast of the future values of a time series.
pi_info(level, lpi, upi)
pi_info(level, lpi, upi)
level |
a number in the interval (0, 100) indicating the level of the prediction interval. |
lpi |
a time series of class |
upi |
a time series of class |
An object of class pi_info
. It is a list containing all the
information supplied to the function.
prediction_info()
which uses this function to specify prediction
intervals.
if (require("forecast")) { time_series <- ts(rnorm(40)) f <- meanf(time_series, level = 95) info <- pi_info(95, f$lower, f$upper) }
if (require("forecast")) { time_series <- ts(rnorm(40)) f <- meanf(time_series, level = 95) info <- pi_info(95, f$lower, f$upper) }
ggplot
object associated with a time series belonging to a
collection.Apart from the time series, future values and forecasts for the
future values form part of the ggplot
object.
plot_collection(collection, number, methods = NULL, level = NULL, sdp = TRUE)
plot_collection(collection, number, methods = NULL, level = NULL, sdp = TRUE)
collection |
a list with the collection of time series. Each component
of the list must have been built with the |
number |
an integer. The number of the time series. It should be a value
between 1 and |
methods |
NULL (default) or a character vector indicating the names of the forecasting methods to be displayed. |
level |
NULL (default) or a number in the interval (0, 100) indicating the level of the prediction interval to be shown. This parameter in considered only when just one forecasting method is plotted and the forecasting method has a prediction interval with the specified level. |
sdp |
logical. Should data points be shown in the plot? (default value
|
The collection
parameter must be a list. Each component of the list stores
a time series and, optionally, its future values, forecasts for the future
values and prediction intervals for the forecasts. Each component should have
been created using the ts_info()
function.
In the example section you can see an example of a collection of time series.
If the collection
parameter is not specified correctly, a proper message is
shown.
The ggplot
object representing the time series and its forecast.
ts_info()
function to see how to build the components of the
collection
parameter.
# create a collection of two time series and plot both time series c <- list(ts_info(USAccDeaths), ts_info(ldeaths)) plot_collection(c, number = 1) plot_collection(c, number = 2, sdp = FALSE) # create a collection of one time series with future values and forecasts if (require(forecast)) { c <- vector(2, mode = "list") timeS <- window(USAccDeaths, end = c(1977, 12)) f <- window(USAccDeaths, start = c(1978, 1)) ets_fit <- ets(timeS) ets_pred <- forecast(ets_fit, h = length(f), level = 90) mean_pred <- meanf(timeS, h = length(f), level = 90) c[[1]] <- ts_info(timeS, future = f, prediction_info("ES", ets_pred$mean, pi_info(90, ets_pred$lower, ets_pred$upper)), prediction_info("Mean", mean_pred$mean, pi_info(90, mean_pred$lower, mean_pred$upper)) ) timeS <- ts(rnorm(30, sd = 3)) f <- rnorm(5, sd = 3) rw <- rwf(timeS, h = length(f), level = 80) mean <- meanf(timeS, h = length(f), level = 90) c[[2]] <- ts_info(timeS, future = f, prediction_info("Random Walk", rw$mean, pi_info(80, rw$lower, rw$upper)), prediction_info("Mean", mean$mean, pi_info(90, mean$lower, mean$upper)) ) plot_collection(c, number = 1) } if (require("forecast")) plot_collection(c, number = 2) if (require("forecast")) plot_collection(c, number = 2, methods = "Mean") # just plot a forecasting method if (require("forecast")) plot_collection(c, number = 2, methods = "Random Walk", level = 80)
# create a collection of two time series and plot both time series c <- list(ts_info(USAccDeaths), ts_info(ldeaths)) plot_collection(c, number = 1) plot_collection(c, number = 2, sdp = FALSE) # create a collection of one time series with future values and forecasts if (require(forecast)) { c <- vector(2, mode = "list") timeS <- window(USAccDeaths, end = c(1977, 12)) f <- window(USAccDeaths, start = c(1978, 1)) ets_fit <- ets(timeS) ets_pred <- forecast(ets_fit, h = length(f), level = 90) mean_pred <- meanf(timeS, h = length(f), level = 90) c[[1]] <- ts_info(timeS, future = f, prediction_info("ES", ets_pred$mean, pi_info(90, ets_pred$lower, ets_pred$upper)), prediction_info("Mean", mean_pred$mean, pi_info(90, mean_pred$lower, mean_pred$upper)) ) timeS <- ts(rnorm(30, sd = 3)) f <- rnorm(5, sd = 3) rw <- rwf(timeS, h = length(f), level = 80) mean <- meanf(timeS, h = length(f), level = 90) c[[2]] <- ts_info(timeS, future = f, prediction_info("Random Walk", rw$mean, pi_info(80, rw$lower, rw$upper)), prediction_info("Mean", mean$mean, pi_info(90, mean$lower, mean$upper)) ) plot_collection(c, number = 1) } if (require("forecast")) plot_collection(c, number = 2) if (require("forecast")) plot_collection(c, number = 2, methods = "Mean") # just plot a forecasting method if (require("forecast")) plot_collection(c, number = 2, methods = "Random Walk", level = 80)
Create a ggplot
object with a time series and, optionally, some future
values of the time series and several forecast for those future values.
plot_predictions(ts, future = NULL, predictions = NULL, sdp = TRUE)
plot_predictions(ts, future = NULL, predictions = NULL, sdp = TRUE)
ts |
a time series of class |
future |
NULL (default) or a time series of class |
predictions |
NULL (default) or a named list containing the predictions
for the future values. Each component of the list should contain a vector or
an object of class |
sdp |
logical. Should data points be shown? (default value |
If future
or the forecasts in the prediction
list are vectors
then they are supposed to start after the last data of the time series.
The ggplot
object representing the time series and its forecast.
# plot a time series, its future values and two forecasts ts <- window(USAccDeaths, end = c(1977, 12)) f <- window(USAccDeaths, start = c(1978, 1)) prediction1 <- rep(mean(ts), 12) prediction2 <- as.vector(window(ts, start = c(1977, 1))) p <- list(Mean = prediction1, Naive = prediction2) plot_predictions(ts, future = f, predictions = p)
# plot a time series, its future values and two forecasts ts <- window(USAccDeaths, end = c(1977, 12)) f <- window(USAccDeaths, start = c(1978, 1)) prediction1 <- rep(mean(ts), 12) prediction2 <- as.vector(window(ts, start = c(1977, 1))) p <- list(Mean = prediction1, Naive = prediction2) plot_predictions(ts, future = f, predictions = p)
Create a ggplot
object associated with a time series and, optionally, its
future values, a forecast for its future values and a prediction interval of
the forecast.
plot_ts( ts, future = NULL, prediction = NULL, method = NULL, lpi = NULL, upi = NULL, level = NULL, sdp = TRUE )
plot_ts( ts, future = NULL, prediction = NULL, method = NULL, lpi = NULL, upi = NULL, level = NULL, sdp = TRUE )
ts |
a time series of class |
future |
NULL (default) or a time series of class |
prediction |
NULL (default) or a time series of class |
method |
NULL (default) a character string with the name of the method used to forecast the future values of the time series. This name will appear in the legend. |
lpi |
NULL (default) or a time series of class |
upi |
NULL (default) or a time series of class |
level |
NULL (default) a number in the interval (0, 100) indicating the level of the prediction interval. |
sdp |
logical. Should data points be shown? (default value |
If future
or prediction
are vectors then they are supposed to
start after the last data of the time series.
The ggplot
object representing the time series and its forecast.
library(ggplot2) plot_ts(USAccDeaths) # plot a time series # plot a time series, not showing data points plot_ts(USAccDeaths, sdp = FALSE) # plot a time series, its future values and a prediction ts <- window(USAccDeaths, end = c(1977, 12)) f <- window(USAccDeaths, start = c(1978, 1)) p <- ts(window(USAccDeaths, start = c(1976, 1), end = c(1976, 12)), start = c(1978, 1), frequency = 12 ) plot_ts(ts, future = f, prediction = p) # plot a time series and a prediction plot_ts(USAccDeaths, prediction = rep(mean(USAccDeaths), 12), method = "Mean") # plot a time series, a prediction and a prediction interval if (require(forecast)) { timeS <- window(USAccDeaths, end = c(1977, 12)) f <- window(USAccDeaths, start = c(1978, 1)) ets_fit <- ets(timeS) p <- forecast(ets_fit, h = length(f), level = 90) plot_ts(timeS, future = f, prediction = p$mean, method = "ES", lpi = p$lower, upi = p$upper, level = 90 ) }
library(ggplot2) plot_ts(USAccDeaths) # plot a time series # plot a time series, not showing data points plot_ts(USAccDeaths, sdp = FALSE) # plot a time series, its future values and a prediction ts <- window(USAccDeaths, end = c(1977, 12)) f <- window(USAccDeaths, start = c(1978, 1)) p <- ts(window(USAccDeaths, start = c(1976, 1), end = c(1976, 12)), start = c(1978, 1), frequency = 12 ) plot_ts(ts, future = f, prediction = p) # plot a time series and a prediction plot_ts(USAccDeaths, prediction = rep(mean(USAccDeaths), 12), method = "Mean") # plot a time series, a prediction and a prediction interval if (require(forecast)) { timeS <- window(USAccDeaths, end = c(1977, 12)) f <- window(USAccDeaths, start = c(1978, 1)) ets_fit <- ets(timeS) p <- forecast(ets_fit, h = length(f), level = 90) plot_ts(timeS, future = f, prediction = p$mean, method = "ES", lpi = p$lower, upi = p$upper, level = 90 ) }
The object created contains a forecast and, optionally, prediction intervals for the forecast.
prediction_info(name, forecast, ...)
prediction_info(name, forecast, ...)
name |
a character indicating the name of the method used to forecast. |
forecast |
a time series of class |
... |
prediction intervals for the forecast. These prediction intervals
must have been built with the |
an object of class pred_info
. A list with the information supplied
to the function.
pi_info()
for how to create prediction intervals.
if (require("forecast")) { time_series <- ts(rnorm(40)) f <- meanf(time_series, level = 95) info <- prediction_info("mean", f$mean, pi_info(95, f$lower, f$upper)) }
if (require("forecast")) { time_series <- ts(rnorm(40)) f <- meanf(time_series, level = 95) info <- prediction_info("mean", f$mean, pi_info(95, f$lower, f$upper)) }
The information about the time series is compounded of the time series and, optionally, its future values and forecasts for those future values (and prediction intervals for those forecasts).
ts_info(historical, ..., future = NULL, name = NULL)
ts_info(historical, ..., future = NULL, name = NULL)
historical |
a time series of class |
... |
forecasts for the future values of the time series. A forecast
must have been built with the |
future |
NULL (default) or a time series of class |
name |
NULL (default) or a character string with information about the time series. Typically, its name. |
An object of class ts_info
. It is a list containing all the
information supplied to the function.
prediction_info()
for how to create forecasts.
# only information about a time series info <- ts_info(USAccDeaths) # Information about a time series and its future values info2 <- ts_info(ts(rnorm(50)), future = rnorm(10)) # Information about a time series, its future values and a forecast if (require("forecast")) { t <- ts(rnorm(50)) f <- rnorm(10) mf <- meanf(t, level = 95) info3 <- ts_info(t, future = f, prediction_info("mean", mf$mean, pi_info(95, mf$lower, mf$upper) ) ) }
# only information about a time series info <- ts_info(USAccDeaths) # Information about a time series and its future values info2 <- ts_info(ts(rnorm(50)), future = rnorm(10)) # Information about a time series, its future values and a forecast if (require("forecast")) { t <- ts(rnorm(50)) f <- rnorm(10) mf <- meanf(t, level = 95) info3 <- ts_info(t, future = f, prediction_info("mean", mf$mean, pi_info(95, mf$lower, mf$upper) ) ) }