| Title: | Univariate Time Series Forecasting |
|---|---|
| Description: | An engine for univariate time series forecasting using different regression models in an autoregressive way. The engine provides an uniform interface for applying the different models. Furthermore, it is extensible so that users can easily apply their own regression models to univariate time series forecasting and benefit from all the features of the engine, such as preprocessings or estimation of forecast accuracy. |
| Authors: | Maria Pilar Frias-Bustamante [aut] (ORCID: <https://orcid.org/0000-0001-6886-0953>), Francisco Martinez [aut, cre, cph] (ORCID: <https://orcid.org/0000-0002-5206-1898>) |
| Maintainer: | Francisco Martinez <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.3.3 |
| Built: | 2026-05-22 08:37:30 UTC |
| Source: | https://github.com/franciscomartinezdelrio/utsf |
utsf_forecast objectPlot the time series and its associated forecast.
## S3 method for class 'utsf_forecast' autoplot(object, ...)## S3 method for class 'utsf_forecast' autoplot(object, ...)
object |
An object of class |
... |
additional parameter. |
The ggplot object representing a plotting of the time series and
its forecast.
m <- create_model(AirPassengers, lags = 1:12, method = "rf") f <- forecast(m, h = 12) library(ggplot2) autoplot(f)m <- create_model(AirPassengers, lags = 1:12, method = "rf") f <- forecast(m, h = 12) library(ggplot2) autoplot(f)
Build the training examples for a regressive model to forecast a time series using lagged values of the series as autoregressive features.
build_examples(timeS, lags)build_examples(timeS, lags)
timeS |
The time series. |
lags |
An integer vector with the lags used as feature vector in decreasing order. |
A list with two fields: 1) a matrix with the features of the examples and 2) a vector with the targets of the examples
build_examples(ts(1:5), lags = 2:1)build_examples(ts(1:5), lags = 2:1)
This function trains a model from the historical values of a time series using an autoregressive approach: the targets are the historical values and the features of the targets their lagged values.
create_model( timeS, lags = NULL, method = c("knn", "lm", "rt", "mt", "bagging", "rf", "xgboost"), trend = c("additive", "multiplicative", "differences", "none"), nfd = -1, transform_features = TRUE, ... )create_model( timeS, lags = NULL, method = c("knn", "lm", "rt", "mt", "bagging", "rf", "xgboost"), trend = c("additive", "multiplicative", "differences", "none"), nfd = -1, transform_features = TRUE, ... )
timeS |
A time series of class |
lags |
An integer vector, in increasing order, expressing the lags used
as autoregressive variables. If the default value ( |
method |
A string indicating the method used for training and forecasting. Allowed values are:
See details for a brief explanation of the models. It is also possible to use your own regression model, in that case a function explaining how to build your model must be provided, see the vignette for further details. |
trend |
A character indicating the type of preprocessing applied to the time series in order to deal with trending series, see the vignette for details. |
nfd |
In the case that the parameter |
transform_features |
A logical value indicating whether the training features are also transformed if the additive or multiplicative transformation has been used as preprocessing to deal with trending series. |
... |
Parameters for the underlying function that builds the model. If no parameters are provided, the model is normally fitted with its default parameters. See details for the functions used to train the models. |
The functions used to build and train the model are:
KNN: In this case no model is built and the function FNN::knn.reg() is
used to predict the future values of the time series. By default, k is equal
to 3.
Linear models: Function stats::lm() to build the model and the method
stats::predict.lm() associated with the trained model to forecast the future
values of the time series.
Regression trees: Function rpart::rpart() to build the model and the
method rpart::predict.rpart() associated with the trained model to forecast
the future values of the time series.
Model trees: Function Cubist::cubist() to build the model and the
method Cubist::predict.cubist() associated with the trained model to
forecast the future values of the time series. By default, the parameter
committees is set to 5.
Bagging: Function ipred::bagging() to build the model and the
method ipred::predict.regbagg() associated with the trained model to
forecast the future values of the time series.
Random forest: Function ranger::ranger() to build the model and the
method ranger::predict.ranger() associated with the trained model to
forecast the future values of the time series.
Extreme gradient boosting: Function xgboost::xgboost() to build the model and the
method xgboost::predict.xgboost() associated with the trained model to
forecast the future values of the time series.
An S3 object of class utsf, basically a list with, at least, the
following components:
ts |
The time series being forecast. |
features |
A data frame with the features of the training set. The column names of the data frame indicate the autoregressive lags. |
targets |
A vector with the targets of the training set. |
lags |
An integer vector with the autoregressive lags. |
model |
The regression model used recursively to make the forecast. |
## Build model using k-nearest neighbors create_model(AirPassengers, method = "knn") ## Using k-nearest neighbors changing the default k value create_model(AirPassengers, method = "knn", k = 5) ## Using your own regression model # Function to build the regression model my_knn_model <- function(X, y, param) { 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 } create_model(AirPassengers, method = my_knn_model)## Build model using k-nearest neighbors create_model(AirPassengers, method = "knn") ## Using k-nearest neighbors changing the default k value create_model(AirPassengers, method = "knn", k = 5) ## Using your own regression model # Function to build the regression model my_knn_model <- function(X, y, param) { 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 } create_model(AirPassengers, method = my_knn_model)
It uses an object of class utsf to asses the forecasting accuracy of its
associated model on its associated time series applying a rolling origin
evaluation.
efa(model, h, type = c("normal", "minimum"), size = NULL, prop = NULL)efa(model, h, type = c("normal", "minimum"), size = NULL, prop = NULL)
model |
An object of class |
h |
A positive integer. The forecasting horizon. |
type |
A string. Possible values are |
size |
An integer. It is the size of the test set (how many of the last
observations of the time series are used as test set). It can only be used
when the type parameter is |
prop |
A numeric value in the range (0, 1). It is the proportion of the
time series used as test set. It can only be used when the type parameter is
|
A list with four components:
per_horizon |
A matrix with the estimated forecast accuracy per forecasting horizon using several forecasting accuracy measures. |
global |
The average estimated forecast accuracy for all the horizons. It is computed as the mean
of the different rows of the |
test_sets |
A matrix with the test sets used in the evaluation. Each row of the matrix is a test set. |
predictions |
The predictions for the test sets. |
m <- create_model(UKgas, lags = 1:4, method = "rt") efa(m, h = 4, type = "normal", size = 8)m <- create_model(UKgas, lags = 1:4, method = "rt") efa(m, h = 4, type = "normal", size = 8)
Forecasting a time series
## S3 method for class 'utsf' forecast(object, h, PI = FALSE, level = 90, ...)## S3 method for class 'utsf' forecast(object, h, PI = FALSE, level = 90, ...)
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 |
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 |
## 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", k = 5) forecast(m, h = 12) ## Using your own regression model # Function to build the regression model my_knn_model <- function(X, y, param) { 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)## 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", k = 5) forecast(m, h = 12) ## Using your own regression model # Function to build the regression model my_knn_model <- function(X, y, param) { 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)
utsf objectsPredict the class of a new observation based on the model associated with the
utsf object
## S3 method for class 'utsf' predict(object, new_value, ...)## S3 method for class 'utsf' predict(object, new_value, ...)
object |
object of class |
new_value |
a data frame with one row of a new observation. |
... |
further arguments passed to or from other methods. |
a numeric value with the forecast.
It uses an object of class utsf to asses the forecasting accuracy of its
associated model on its associated time series applying rolling origin
evaluation according to different configurations of model parameters.
tune_grid( model, h, tuneGrid, type = c("normal", "minimum"), size = NULL, prop = NULL )tune_grid( model, h, tuneGrid, type = c("normal", "minimum"), size = NULL, prop = NULL )
model |
An object of class |
h |
A positive integer. The forecasting horizon. |
tuneGrid |
A data frame with possible tuning values. The columns are named as the tuning parameters. |
type |
A string. Possible values are |
size |
An integer. It is the size of the test set (how many of the last
observations of the time series are used as test set). It can only be used
when the type parameter is |
prop |
A numeric value in the range (0, 1). It is the proportion of the
time series used as test set. It can only be used when the type parameter is
|
The estimation of forecast accuracy is done with the efa() function. The
best combination of parameters is used to train the model with all the
historical values of the time series and forecast h values ahead.
A list with three components:
tuneGrid |
A data frame with the different combination of parameters and the estimated forecast accuracy of a model trained with those parameters. |
best |
The best combination of parameters according to root mean squared error. |
forecast |
An
object of class |
m <- create_model(UKgas, lags = 1:4, method = "knn") tune_grid(m, h = 4, tuneGrid = expand.grid(k = 1:7), type = "normal", size = 8)m <- create_model(UKgas, lags = 1:4, method = "knn") tune_grid(m, h = 4, tuneGrid = expand.grid(k = 1:7), type = "normal", size = 8)