/
forecasting-lm.R
35 lines (32 loc) · 1.17 KB
/
forecasting-lm.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
#' @name lm_ts
#' @title Make forecasts using a Linear Model
#'
#' @description Fit a Linear Regression for forecasting the time series as a
#' linear function over time/years
#'
#' @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
#'
lm_ts <- function(timeseries, num_ahead = 5, level = 95)
{
f <- function(training, observed, order, level)
{
# make forecasts
t <- seq_len(length(training))
lm_model <- stats::lm(training ~ t)
# predict function requires a new list of predictor variables as newdata
t_observed <- length(training) + seq_len(length(observed))
forecasts <- stats::predict.lm(lm_model,
newdata = list(t = t_observed),
se.fit = TRUE)
# return
return_forecasts(observed, forecasts, level)
}
forecast_iterated(fun = f, timeseries = timeseries, num_ahead = num_ahead,
order = order, level = level)
}