forecast.utsf {utsf} | R Documentation |
Forecasting a time series
Description
Forecasting a time series
Usage
## S3 method for class 'utsf'
forecast(object, h, PI = FALSE, level = 90, ...)
Arguments
object |
an object of class |
h |
A positive integer. Number of values to be forecast into the future, i.e., forecast horizon. |
PI |
If TRUE, prediction intervals are produced using simulation and assuming normally distributed errors. |
level |
Confidence level for predictions intervals. |
... |
Other arguments passed to methods |
Value
an object of class utsf_forecast
with the same components of the
model received as first argument, plus several components:
pred |
The forecast as an |
lower |
Lower limits for prediction interval. |
upper |
Upper limits for prediction interval. |
level |
Confidence value associated with the prediction interval |
Examples
## Forecast time series using k-nearest neighbors
m <- create_model(USAccDeaths, method = "knn")
f <- forecast(m, h = 12)
f$pred
library(ggplot2)
autoplot(f)
## Using k-nearest neighbors changing the default k value
m <- create_model(USAccDeaths, method = "knn", param = list(k = 5))
forecast(m, h = 12)
## Using your own regression model
# Function to build the regression model
my_knn_model <- function(X, y) {
structure(list(X = X, y = y), class = "my_knn")
}
# Function to predict a new example
predict.my_knn <- function(object, new_value) {
FNN::knn.reg(train = object$X, test = new_value, y = object$y)$pred
}
m <- create_model(USAccDeaths, method = my_knn_model)
forecast(m, h = 12)
[Package utsf version 1.3.0 Index]