R/forecasting-nnet.R

Defines functions nnet_ts

Documented in nnet_ts

#' @name nnet_ts
#' @title Make forecasts using neural network time series model
#'
#' @description `nnet_ts` fits a neural network time series model using 
#'   \code{\link[tsDyn]{nnetTs}}
#'
#' @inheritParams tsDyn::nnetTs
#' @inheritParams forecast_iterated
#' @inheritParams forecast::forecast
#' @param ... arguments to be passed to \code{\link[tsDyn]{predict.nlar}}
#'
#' @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
#'
nnet_ts <- function(timeseries, num_ahead = 5, level = 95,
                    m = 1, size = 1, ...)
{
    f <- function(training, observed, order, level, ...)
    {
        # make forecasts
        nnet_model <- tsDyn::nnetTs(training, m = m, size = size)

        nnet_out <- tsDyn:::predict.nlar(nnet_model, n.ahead = num_ahead,
                            ci = 0.5 + level/200, # adjust for implementation issue
                            ...)

        if (is.list(nnet_out) && all(c("pred", "se") %in% names(nnet_out)))
        {
            forecasts <- list(mean = nnet_out$pred,
                              lower = nnet_out$se[, 1],
                              upper = nnet_out$se[, 2]
            )
        } else {
            forecasts <- list(mean = nnet_out, lower = NA, upper = NA)
        }

        # 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,
                   order = order, level = level, ...)
}
ha0ye/MATSS-forecasting documentation built on Nov. 28, 2020, 10:16 a.m.