knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(fastTS) library(tibble) set.seed(123)
data("LakeHuron") years <- time(LakeHuron) fit <- fastTS(LakeHuron, n_lags_max = 3) fit
predict
do?Let $y_t$ refer to our outcome series, and $\hat y_t^{(k)}$ refer to the $k$-step-ahead prediction for $y_t$.
The predicted value returned at any time point $t$ is the model's prediction for that point $\hat y_t$, given the model and all data up to $t -$ n_ahead
. This means that
The 1-step prediction $\hat y_t^{(1)}$ is computed by using lags of $y_t$ deemed important by the fitting process.
The 2-step prediction $\hat y_t^{(2)}$ is computed by using important lags of $y_t$, but replacing the first lag $y_{t-1}$ with $\hat y_{t-1}^{(1)}$.
The 3-step prediction $\hat y_t^{(3)}$ is computed by replacing the first lag $y_{t-1}$ with $\hat y_{t-1}^{(2)}$ and the second lag $y_{t-2}$ with $\hat y_{t-2}^{(1)}$.
And so on until the $k$-step prediction $\hat y_t^{(k)}$ is similarly computed by replacing lags of $y_t$ with predicted values as necessary.
Here is an example with the LakeHuron
data set.
p1 <- predict(fit, n_ahead = 1) p7 <- predict(fit, n_ahead = 7) predictions <- tibble(years, LakeHuron, p1, p7) head(predictions, 10) tail(predictions)
predict
function returns missing values for the first n_lags_max
observations for 1-step ahead predictions. The prediction process back-fill real values when necessary for early predictions, but resets to NA before returning predictions.r p1[10]
. r p7[10]
.Note: there is a "burn-in" component to fastTS
objects that means the first n_lags_max
observations are back-filled in.
By default, the predict
function does not produce forecasts. In order to get forecasts, we need to set forecast_ahead = TRUE
, which will return forecasted values at the tail end of the returned vector.
p1 <- predict(fit, n_ahead = 1, forecast_ahead = TRUE) predictions <- tibble(time = c(1973), p1) # For 7-step ahead forecasts p7 <- predict(fit, n_ahead = 7, forecast_ahead = TRUE) predictions <- tibble(time = c(1973:1979), p7) predictions
Finally, the return_intermediate
option allows users to collect all of the step-ahead predictions up to $k$:
p1_p7 <- predict(fit, n_ahead = 7, return_intermediate = TRUE) predictions <- tibble(years, LakeHuron, p1_p7) tail(predictions)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.