create_model {utsf}R Documentation

Train an univariate time series forecasting model

Description

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.

Usage

create_model(
  timeS,
  lags = NULL,
  method = c("knn", "lm", "rt", "mt", "bagging", "rf"),
  param = NULL,
  preProcess = NULL
)

Arguments

timeS

A time series of class ts or a numeric vector.

lags

An integer vector, in increasing order, expressing the lags used as autoregressive variables. If the default value (NULL) is provided, a suitable vector is chosen.

method

A string indicating the method used for training and forecasting. Allowed values are:

  • "knn": k-nearest neighbors (the default)

  • "lm": linear regression

  • "rt": regression trees

  • "mt": model trees

  • "bagging"

  • "rf": random forests.

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 (NULL) is provided, the model is fitted with its default parameters. See details for the functions used to train the models.

preProcess

A list indicating the preprocessings or transformations. Currently, the length of the list must be 1 (only one preprocessing). If NULL the additive transformation is applied to the series. The element of the list is created with the trend() function.

Details

The functions used to build and train the model are:

Value

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.

Examples

## 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", param = list(k = 5))

## 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
}
create_model(AirPassengers, method = my_knn_model)


[Package utsf version 1.3.0 Index]