/
forecasting-arima.R
115 lines (104 loc) · 3.59 KB
/
forecasting-arima.R
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#' @name arima_ts
#' @title Make forecasts using AR / ARIMA
#' @aliases arfima_ts, autoarima_one_step
#'
#' @rdname arima_ts
#'
#' @description `arima_ts` fits an arima model using \code{\link{arima}}
#'
#' @inheritParams stats::arima
#' @inheritParams forecast_iterated
#' @inheritParams forecast::forecast
#'
#' @return a data.frame of the mean forecasts, the observed values, and the
#' lower and upper CI levels (if an error occurs, then just NA values)
#'
#' @export
#'
arima_ts <- function(timeseries, num_ahead = 5, level = 95,
order = c(1, 0, 0))
{
f <- function(training, observed, level, order)
{
# make forecasts
arima_model <- stats::arima(training, order = order)
forecasts <- forecast::forecast(arima_model, NROW(observed), level = level)
# return
data.frame(observed = as.numeric(observed),
predicted = as.numeric(forecasts$mean),
lower_CI = as.numeric(forecasts$lower),
upper_CI = as.numeric(forecasts$upper))
}
forecast_iterated(fun = f, timeseries = timeseries, num_ahead = num_ahead,
level = level, order = order)
}
#' @rdname arima_ts
#'
#' @description `arfima_ts` fits a step-wise fractionally differenced
#' auto-arima using \code{\link[forecast]{arfima}}
#'
#' @export
#'
arfima_ts <- function(timeseries, num_ahead = 5, level = 95)
{
f <- function(training, observed, level)
{
# make forecasts
arima_model <- forecast::arfima(training)
forecasts <- forecast::forecast(arima_model, NROW(observed), level = level)
# return
data.frame(observed = as.numeric(observed),
predicted = as.numeric(forecasts$mean),
lower_CI = as.numeric(forecasts$lower),
upper_CI = as.numeric(forecasts$upper))
}
forecast_iterated(fun = f, timeseries = timeseries, num_ahead = num_ahead,
level = level)
}
#' @rdname arima_ts
#'
#' @description `autoarima_one_step` uses \code{\link[forecast]{auto.arima}} to
#' fit an ARIMA model and make a single one-step forecast.
#'
#' @inheritParams hindcast
#'
#' @export
#'
autoarima_one_step <- function(timeseries, level = 95, ...)
{
f <- function(training, observed = NA, level)
{
# make forecasts
arima_model <- forecast::auto.arima(training)
forecasts <- forecast::forecast(arima_model, 1, level = level)
# return
data.frame(observed = as.numeric(observed),
predicted = as.numeric(forecasts$mean),
lower_CI = as.numeric(forecasts$lower),
upper_CI = as.numeric(forecasts$upper))
}
hindcast(fun = f, timeseries = timeseries, level = level, ...)
}
#' @rdname arima_ts
#'
#' @description `arfima_one_step` uses \code{\link[forecast]{arfima}} to
#' fit an fractionally-differentiated ARIMA model and make a single one-step
#' forecast.
#'
#' @export
#'
arfima_one_step <- function(timeseries, level = 95, ...)
{
f <- function(training, observed = NA, level)
{
# make forecasts
arima_model <- forecast::arfima(training)
forecasts <- forecast::forecast(arima_model, 1, level = level)
# return
data.frame(observed = as.numeric(observed),
predicted = as.numeric(forecasts$mean),
lower_CI = as.numeric(forecasts$lower),
upper_CI = as.numeric(forecasts$upper))
}
hindcast(fun = f, timeseries = timeseries, level = level, ...)
}