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] , Francisco Martinez [aut, cre, cph] |
Maintainer: | Francisco Martinez <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.0.0.9000 |
Built: | 2024-11-05 10:22:02 UTC |
Source: | https://github.com/franciscomartinezdelrio/utsf |
utsf
objectPlot the time series and its associated forecast.
## S3 method for class 'utsf' autoplot(object, ...)
## S3 method for class 'utsf' autoplot(object, ...)
object |
An object of class |
... |
additional parameter. |
The ggplot
object representing a plotting of the time series and
its forecast.
f <- forecast(AirPassengers, h = 12, lags = 1:12, method = "rf") library(ggplot2) autoplot(f)
f <- forecast(AirPassengers, h = 12, lags = 1:12, method = "rf") 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)
Specify the way in which the estimation of forecast accuracy is done
evaluation(type = "normal", size = NULL, prop = NULL)
evaluation(type = "normal", size = NULL, prop = NULL)
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 |
An S3 object of class evaluation
with the chosen evaluation.
evaluation("normal", size = 10)
evaluation("normal", size = 10)
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. Then, the trained model is used to predict the future values of the series using a recursive strategy.
forecast( timeS, h, lags = NULL, method = "knn", param = NULL, efa = NULL, tuneGrid = NULL, preProcess = NULL )
forecast( timeS, h, lags = NULL, method = "knn", param = NULL, efa = NULL, tuneGrid = NULL, preProcess = NULL )
timeS |
A time series of class |
h |
A positive integer. Number of values to be forecast into the future, i.e., forecast horizon. |
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. |
param |
A list with parameters for the underlying function that builds
the model. If the default value ( |
efa |
It is used to indicate how to estimate the forecast accuracy of the
model using the last observations of the time series as test set. If the
default value ( |
tuneGrid |
A data frame with possible tuning values. The columns are
named the same as the tuning parameters. The estimation of forecast accuracy
is done as explained for the |
preProcess |
A list indicating the preprocessings or transformations.
Currently, the length of the list must be 1 (only one preprocessing). If
|
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.
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.
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.
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. |
pred |
An object of class |
efa |
This component is included if forecast accuracy is estimated. A vector with estimates of forecast accuracy according to different forecast accuracy measures. |
tuneGrid |
This component is included if the tuneGrid parameter has been used. A data frame in which each row contains estimates of forecast accuracy for a combination of tuning parameters. |
## Forecast time series using k-nearest neighbors f <- forecast(AirPassengers, h = 12, method = "knn") f$pred library(ggplot2) autoplot(f) ## Using k-nearest neighbors changing the default k value forecast(AirPassengers, h = 12, method = "knn", param = list(k = 5))$pred ## 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 } forecast(AirPassengers, h = 12, method = my_knn_model)$pred ## Estimating forecast accuracy of the model f <- forecast(UKgas, h = 4, lags = 1:4, method = "rf", efa = evaluation("minimum")) f$efa ## Estimating forecast accuracy of different tuning parameters f <- forecast(UKgas, h = 4, lags = 1:4, method = "knn", tuneGrid = expand.grid(k = 1:5)) f$tuneGrid ## Forecasting a trending series # Without any preprocessing or transformation f <- forecast(airmiles, h = 4, method = "knn", preProcess = list(trend("none"))) autoplot(f) # Applying the additive transformation (default) f <- forecast(airmiles, h = 4, method = "knn") autoplot(f)
## Forecast time series using k-nearest neighbors f <- forecast(AirPassengers, h = 12, method = "knn") f$pred library(ggplot2) autoplot(f) ## Using k-nearest neighbors changing the default k value forecast(AirPassengers, h = 12, method = "knn", param = list(k = 5))$pred ## 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 } forecast(AirPassengers, h = 12, method = my_knn_model)$pred ## Estimating forecast accuracy of the model f <- forecast(UKgas, h = 4, lags = 1:4, method = "rf", efa = evaluation("minimum")) f$efa ## Estimating forecast accuracy of different tuning parameters f <- forecast(UKgas, h = 4, lags = 1:4, method = "knn", tuneGrid = expand.grid(k = 1:5)) f$tuneGrid ## Forecasting a trending series # Without any preprocessing or transformation f <- forecast(airmiles, h = 4, method = "knn", preProcess = list(trend("none"))) autoplot(f) # Applying the additive transformation (default) f <- forecast(airmiles, h = 4, method = "knn") autoplot(f)
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.
This function is used to specify the preprocessing associated with the trend of a time series.
trend(type = "additive", n = -1)
trend(type = "additive", n = -1)
type |
A character indicating the type of preprocessing applied to the
time series. Possible values are: |
n |
An integer specifying the order of first differences to be applied.
If the default (-1) is used, the order of first differences needed by the
time series will be estimated by the |
A list with the selected options
trend("none") # no preprocessing trend("additive") # additive preprocessing trend("differences", 1) # order 1 first differences trend("differences", -1) # number of first differences estimated
trend("none") # no preprocessing trend("additive") # additive preprocessing trend("differences", 1) # order 1 first differences trend("differences", -1) # number of first differences estimated