Title: | Time Series Forecasting Using Nearest Neighbors |
---|---|
Description: | Allows forecasting time series using nearest neighbors regression Francisco Martinez, Maria P. Frias, Maria D. Perez-Godoy and Antonio J. Rivera (2019) <doi:10.1007/s10462-017-9593-z>. When the forecasting horizon is higher than 1, two multi-step ahead forecasting strategies can be used. The model built is autoregressive, that is, it is only based on the observations of the time series. The nearest neighbors used in a prediction can be consulted and plotted. |
Authors: | Francisco Martinez [aut, cre] |
Maintainer: | Francisco Martinez <[email protected]> |
License: | GPL-2 |
Version: | 0.6.0 |
Built: | 2025-01-06 04:02:18 UTC |
Source: | https://github.com/franciscomartinezdelrio/tsfknn |
It uses a knnForecast object to create a ggplot object that plots a time series and its forecast using KNN regression.
## S3 method for class 'knnForecast' autoplot(object, ...)
## S3 method for class 'knnForecast' autoplot(object, ...)
object |
An object of class |
... |
additional parameter, see details. |
Commonly used parameters are:
highlight
. A character string indicating what elements should be highlighted. Possible values are
"none"
, "points"
and "neighbors"
. The default value is "none"
.
faceting
. Logical. This applies only if the highlight
parameter is
set to "neighbors"
. It indicates whether the different nearest neighbors
should be seen in different plots (TRUE
, the default value) or in one
plot.
The ggplot object representing a plotting with the forecast.
pred <- knn_forecasting(USAccDeaths, h = 12, lags = 1:12, k = 2) library(ggplot2) autoplot(pred) autoplot(pred, highlight = "neighbors")
pred <- knn_forecasting(USAccDeaths, h = 12, lags = 1:12, k = 2) library(ggplot2) autoplot(pred) autoplot(pred, highlight = "neighbors")
It allows to see the examples of the model associated to a
knnForecast
object.
knn_examples(forecast)
knn_examples(forecast)
forecast |
A |
A matrix including the features and targets of the examples
associated with the model of a knnForecast
object.
pred <- knn_forecasting(ts(1:8), h = 1, lags = 1:2, k = 2) knn_examples(pred)
pred <- knn_forecasting(ts(1:8), h = 1, lags = 1:2, k = 2) knn_examples(pred)
It applies KNN regression to forecast the future values of a time series.
The lags used as autoregressive variables are set with the lags
parameter. If the user does not set the number of nearest neighbors or
the lags, these values are selected automatically.
knn_forecasting( timeS, h, lags = NULL, k = c(3, 5, 7), msas = c("recursive", "MIMO"), cf = c("mean", "median", "weighted"), transform = c("additive", "multiplicative", "none") )
knn_forecasting( timeS, h, lags = NULL, k = c(3, 5, 7), msas = c("recursive", "MIMO"), cf = c("mean", "median", "weighted"), transform = c("additive", "multiplicative", "none") )
timeS |
A numeric vector or time series of class |
h |
A positive integer. Number of values to forecast. |
lags |
An integer vector in increasing order expressing the lags used as autoregressive variables. |
k |
A positive integer. The k parameter in KNN regression. A vector of k values can also be used. In that case, the forecast is the average of the forecasts produced by the different models with the different k parameters. |
msas |
A string indicating the Multiple-Step Ahead Strategy used when more than one value is predicted. It can be "recursive" or "MIMO" (the default). |
cf |
A string. It indicates the combination function used to aggregate the targets associated with the nearest neighbors. It can be "median", "weighted" or "mean" (the default). |
transform |
A character value indicating whether the training samples
are transformed. If the time series has a trend it is recommended. By
default is |
An object of class "knnForecast"
. The
function summary
can be used to obtain or print a
summary of the results.
An object of class \code{"knnForecast"} is a list containing at least the following components:
call |
the matched call. |
msas |
the Multi-Step Ahead Strategy. |
prediction |
a time series with the forecast. |
model |
an object of class |
pred <- knn_forecasting(USAccDeaths, h = 12, lags = 1:12, k = 2) pred$prediction # To see a time series with the forecasts plot(pred) # To see a plot with the forecast
pred <- knn_forecasting(USAccDeaths, h = 12, lags = 1:12, k = 2) pred$prediction # To see a time series with the forecasts plot(pred) # To see a plot with the forecast
It computes the number of training examples that would have a KNN model with the specified parameters.
n_training_examples(timeS, h, lags, msas = c("MIMO", "recursive"))
n_training_examples(timeS, h, lags, msas = c("MIMO", "recursive"))
timeS |
A numeric vector or time series of class |
h |
A positive integer. Number of values to forecast. |
lags |
An integer vector in increasing order expressing the lags used as autoregressive variables. |
msas |
A string indicating the Multiple-Step Ahead Strategy used when more than one value is predicted. It can be "recursive" or "MIMO" (the default). |
An integer.
n_training_examples(ts(1:10), h = 2, lags = 1:3, msas = "MIMO") n_training_examples(ts(1:10), h = 2, lags = 1:3, msas = "recursive")
n_training_examples(ts(1:10), h = 2, lags = 1:3, msas = "MIMO") n_training_examples(ts(1:10), h = 2, lags = 1:3, msas = "recursive")
It allows to check the new instances and their nearest neighbors used in a prediction associated with a "knnForecast" object.
nearest_neighbors(forecast)
nearest_neighbors(forecast)
forecast |
A |
A list including the new instances used in KNN regression and their nearest neighbors.
pred <- knn_forecasting(UKgas, h = 4, lags = 1:4, k = 2, msas = "MIMO") nearest_neighbors(pred)
pred <- knn_forecasting(UKgas, h = 4, lags = 1:4, k = 2, msas = "MIMO") nearest_neighbors(pred)
It uses a test set generated with the function rolling_origin
and plots its forecast.
## S3 method for class 'knnForecastRO' plot(x, h = NULL, ...)
## S3 method for class 'knnForecastRO' plot(x, h = NULL, ...)
x |
the object obtained from a call to |
h |
an integer. The forecasting horizon. If |
... |
Other plotting parameters to affect the plot. |
Predicted values based on a KNN model for time series forecasting.
## S3 method for class 'knnForecast' predict(object, h, ...)
## S3 method for class 'knnForecast' predict(object, h, ...)
object |
a |
h |
an integer. The forecasting horizon. |
... |
further arguments passed to or from other methods. |
If the models uses the MIMO strategy for multiple-step ahead prediction, the forecasting horizon is fixed to the model forecasting horizon.
a knnForecast
object with the prediction and information
about the KNN model, see the documentation of knn_forecasting
for the structure of knnForecast
objects.
pred <- knn_forecasting(UKgas, h = 4, k = 1, msas = "recursive") new_pred <- predict(pred, h = 6) print(new_pred$prediction) plot(new_pred) # To see a plot with the forecast
pred <- knn_forecasting(UKgas, h = 4, k = 1, msas = "recursive") new_pred <- predict(pred, h = 6) print(new_pred$prediction) plot(new_pred) # To see a plot with the forecast
It uses the model and the time series associated with the knnForecast
object to asses the forecasting accuracy of the model using the last
h
values of the time series to build test sets applying a rolling
origin evaluation.
rolling_origin(knnf, h = NULL, rolling = TRUE)
rolling_origin(knnf, h = NULL, rolling = TRUE)
knnf |
A |
h |
A positive integer. The forecast horizon. If |
rolling |
A logical. If |
This function assesses the forecast accuracy of the model used by the
knnForecast
object. It uses h
different test and training
sets. The first test set consists of the last h
values of the time
series (the training set is formed by the previous values). The next test
set consists of the last values of the time series and so on
(the last test set is formed by the last value of the time series).
A list containing at least the following fields:
test_sets |
a matrix containing the test sets used in the evaluation. Every row contains a different test set. |
predictions |
The predictions for the test sets. |
errors |
The errors for the test sets. |
global_accu |
Different measures of accuracy applied to all the errors. |
h_accu |
Different measures of accuracy applied to all the errors for every forecasting horizon. |
pred <- knn_forecasting(UKgas, h = 4, lags = 1:4, k = 2) ro <- rolling_origin(pred) print(ro$global_accu)
pred <- knn_forecasting(UKgas, h = 4, lags = 1:4, k = 2) ro <- rolling_origin(pred) print(ro$global_accu)