ts_forec: Calculate point forecasts and prediction intervals for a time...

Description Usage Arguments Details Value References Examples

View source: R/ts_forec.R

Description

Point forecasts and prediction intervals of all forecasting methods in methods are calculated for each time series in dataset.

Usage

1
ts_forec(dataset, methods, level = c(80, 90), parallel = FALSE, num.cores = 2)

Arguments

dataset

a list containing the time series. See details for the required format.

methods

a list of strings with the names of the functions that calculate point forecasts and prediction intervals for time series.

level

the confidence levels for prediction intervals, such as 80, 90.

parallel

logical. If TRUE then the calculations are conducted in parallel.

num.cores

the specified amount of parallel processes to be used if parallel = TRUE.

Details

dataset must be a list with each element having the following format:

x

a time series object ts with the historical data.

h

the amount of future time steps to forecast.

methods a list of strings with the names of the functions that calculate point forecasts and prediction intervals for time series. The functions must exist and take as parameters (x, h, level), where x is the ts object with the input series, h is the amount of future time steps to forecast and level denotes confidence levels for prediction intervals. The output of these functions must be a list with forecs(point forecasts), fitted(fitted values), pil(lower bounds of prediction intervals) and piu(upper bounds of prediction intervals)

Value

A list with the elements having the following structure

x

a time series object ts with the historical data.

h

the amount of future time steps to forecast.

f

a matrix with F rows and n columns. Each row contains the fitted values of each method in methods.

ff

a matrix with F rows and h columns. Each row contains the forecasts of each method in methods.

lower

a list with each element being the matrix of lower bounds for certain confidence level.

upper

a list with each element being the matrix of upper bounds for certain confidence level.

References

Montero-Manso et al. (2018).

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
auto_arima_fun <- function(x, h, level) {
  model <- forecast::auto.arima(x, stepwise=FALSE, approximation=FALSE)
  forecs <- forecast::forecast(model, h=h)$mean
  fitted <- forecast::forecast(model, h=h)$fitted
  pil <- forecast::forecast(model, h=h, level=level)$lower
  piu <- forecast::forecast(model, h=h, level=level)$upper
  list(forecs=forecs, fitted=fitted, pil=pil, piu=piu)
}
ets_fun <- function(x, h, level) {
  # for ets method, residuals != x - fitted
  model <- forecast::ets(x)
  forecs <- forecast::forecast(model, h=h)$mean
  fitted <- forecast::forecast(model, h=h)$fitted
  pil <- forecast::forecast(model, h=h, level=level)$lower
  piu <- forecast::forecast(model, h=h, level=level)$upper
  list(forecs=forecs, fitted=fitted, pil=pil, piu=piu)
}
tbats_fun <- function(x, h, level) {
  # for tbats method, residuals != mean - fitted
  model <- forecast::tbats(x, use.parallel=FALSE)
  forecs <- forecast::forecast(model, h=h)$mean
  fitted <- forecast::forecast(model, h=h)$fitted
  pil <- forecast::forecast(model, h=h, level=level)$lower
  piu <- forecast::forecast(model, h=h, level=level)$upper
  list(forecs=forecs, fitted=fitted, pil=pil, piu=piu)
}
create_example_list <- function() {
  methods_list <- list("auto_arima_fun")
  methods_list <- append(methods_list, "ets_fun")
  methods_list <- append(methods_list, "tbats_fun")
  methods_list
}
methods <- create_example_list()
forec_results <- ts_forec(Mcomp::M3[1:5], methods, level = c(80, 90))

xqnwang/fuma documentation built on May 29, 2021, 6:38 a.m.