ts_mlp: MLP

View source: R/ts_mlp.R

ts_mlpR Documentation

MLP

Description

Create a time series prediction object based on a Multilayer Perceptron (MLP) regressor.

It wraps the nnet package to train a single-hidden-layer neural network on sliding-window inputs. Use ts_regsw utilities to project inputs/outputs.

Usage

ts_mlp(
  preprocess = NA,
  input_size = NA,
  input_map = ts_lagmap(),
  size = NA,
  decay = 0.01,
  maxit = 1000
)

Arguments

preprocess

Normalization preprocessor (e.g., ts_norm_gminmax()).

input_size

Integer. Number of lagged inputs used by the model.

input_map

Lag-selection strategy object created by ts_lagmap().

size

Integer. Number of hidden neurons.

decay

Numeric. L2 weight decay (regularization) parameter.

maxit

Integer. Maximum number of training iterations.

Details

The MLP is a universal function approximator capable of learning non-linear mappings from lagged inputs to next-step values. For stability, consider normalizing inputs (e.g., ts_norm_gminmax()). Hidden size and weight decay control capacity and regularization respectively.

Value

A ts_mlp object (S3) inheriting from ts_regsw.

References

  • D. E. Rumelhart, G. E. Hinton, and R. J. Williams (1986). Learning representations by back-propagating errors. Nature 323, 533–536.

  • W. N. Venables and B. D. Ripley (2002). Modern Applied Statistics with S. Fourth Edition. Springer. (for the nnet package)

Examples

# Example: MLP on sliding windows with min–max normalization
# Load package and dataset
library(daltoolbox)
library(tspredit)
data(tsd)
ts <- ts_data(tsd$y, 10)
ts_head(ts, 3)

samp <- ts_sample(ts, test_size = 5)
io_train <- ts_projection(samp$train)
io_test <- ts_projection(samp$test)

# Prepare projection (X, y)
samp <- ts_sample(ts, test_size = 5)
io_train <- ts_projection(samp$train)
io_test <- ts_projection(samp$test)

# Define and fit the MLP
imap <- ts_lagmap("even")
model <- ts_mlp(ts_norm_gminmax(), input_size = 4, input_map = imap,
 size = 4, decay = 0)
model <- fit(model, x=io_train$input, y=io_train$output)

# Predict 5 steps ahead
prediction <- predict(model, x = io_test$input[1,], steps_ahead = 5)
prediction <- as.vector(prediction)
output <- as.vector(io_test$output)

# Evaluate
ev_test <- evaluate(model, output, prediction)
ev_test

tspredit documentation built on May 15, 2026, 1:07 a.m.