Package {codez}


Type: Package
Title: Torch Seq2Seq Encoder-Decoder Model for Time-Feature Analysis
Version: 2.0.0
Maintainer: Giancarlo Vercellino <giancarlo.vercellino@gmail.com>
Description: Proposes Seq2seq Time-Feature Analysis using a torch Encoder-Decoder to project into latent space and a Forward Network to predict the next sequence, with dependency-light local support functions, tidy outputs and baseline backtesting helpers.
License: GPL-3
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.3.3
Depends: R (≥ 3.6)
URL: https://rpubs.com/giancarlo_vercellino/codez
Suggests: testthat (≥ 3.0.0), torch
Config/testthat/edition: 3
NeedsCompilation: no
Packaged: 2026-07-17 14:12:18 UTC; gianc
Author: Giancarlo Vercellino [aut, cre, cph]
Repository: CRAN
Date/Publication: 2026-07-17 15:50:21 UTC

codez

Description

Seq2seq Time-Feature Analysis using an Encoder-Decoder to project into latent space and a Forward Network to predict the next sequence.

Usage

codez(
  df,
  seq_len = NULL,
  n_windows = 10,
  latent = NULL,
  smoother = FALSE,
  n_samp = 30,
  autoencoder_layers_n = NULL,
  autoencoder_layers_size = NULL,
  autoencoder_activ = NULL,
  forward_net_layers_n = NULL,
  forward_net_layers_size = NULL,
  forward_net_activ = NULL,
  forward_net_reg_L1 = NULL,
  forward_net_reg_L2 = NULL,
  forward_net_drop = NULL,
  loss_metric = "mae",
  autoencoder_optimizer = NULL,
  forward_net_optimizer = NULL,
  epochs = 100,
  patience = 10,
  holdout = 0.5,
  verbose = FALSE,
  ci = 0.8,
  error_scale = "naive",
  error_benchmark = "naive",
  dates = NULL,
  seed = 42,
  batch_size = 32,
  validation_split = 0.1,
  n_sim = 1000,
  backend = "torch"
)

Arguments

df

A data frame with time features on columns. They could be numeric variables or categorical, but not both.

seq_len

Positive integer. Time-step number of the forecasting sequence. Default: NULL (random selection within 2 to max preset boundary).

n_windows

Positive integer. Number of validation windows to test prediction error. Default: 10.

latent

Positive integer. Dimensions of the latent space for encoding-decoding operations. Default: NULL (random selection within preset boundaries)

smoother

Logical. Perform optimal smoothing using standard loess for each time feature. Default: FALSE

n_samp

Positive integer. Number of samples for random search. Default: 30.

autoencoder_layers_n

Positive integer. Number of layers for the encoder-decoder model. Default: NULL (random selection within preset boundaries)

autoencoder_layers_size

Positive integer. Numbers of nodes for the encoder-decoder model. Default: NULL (random selection within preset boundaries)

autoencoder_activ

String. Activation function to be used by the encoder-decoder model. Implemented functions are: "linear", "relu", "leaky_relu", "selu", "elu", "sigmoid", "tanh", "swish", "gelu". Default: NULL (random selection within standard activations)

forward_net_layers_n

Positive integer. Number of layers for the forward net model. Default: NULL (random selection within preset boundaries)

forward_net_layers_size

Positive integer. Numbers of nodes for the forward net model. Default: NULL (random selection within preset boundaries)

forward_net_activ

String. Activation function to be used by the forward net model. Implemented functions are: "linear", "relu", "leaky_relu", "selu", "elu", "sigmoid", "tanh", "swish", "gelu". Default: NULL (random selection within standard activations)

forward_net_reg_L1

Positive numeric between. Weights for L1 regularization. Default: NULL (random selection within preset boundaries).

forward_net_reg_L2

Positive numeric between. Weights for L2 regularization. Default: NULL (random selection within preset boundaries).

forward_net_drop

Positive numeric between 0 and 1. Value for the dropout parameter for each layer of the forward net model (for example, a neural net with 3 layers may have dropout = c(0, 0.5, 0.3)). Default: NULL (random selection within preset boundaries).

loss_metric

String. Loss function for both models. Available metrics: "mse", "mae", "mape". Default: "mae".

autoencoder_optimizer

String. Optimization method for autoencoder. Implemented methods are: "adam", "adadelta", "adagrad", "rmsprop", "sgd", "nadam", "adamax". Default: NULL (random selection within standard optimizers).

forward_net_optimizer

String. Optimization method for forward net. Implemented methods are: "adam", "adadelta", "adagrad", "rmsprop", "sgd", "nadam", "adamax". Default: NULL (random selection within standard optimizers).

epochs

Positive integer. Default: 100.

patience

Positive integer. Waiting time (in epochs) before evaluating the overfit performance. Default: 10.

holdout

Positive numeric between 0 and 1. Holdout sample for validation. Default: 0.5.

verbose

Logical. Default: FALSE.

ci

Positive numeric. Confidence interval. Default: 0.8

error_scale

String. Scale for the scaled error metrics (for continuous variables). Two options: "naive" (average of naive one-step absolute error for the historical series) or "deviation" (standard error of the historical series). Default: "naive".

error_benchmark

String. Benchmark for the relative error metrics (for continuous variables). Two options: "naive" (sequential extension of last value) or "average" (mean value of true sequence). Default: "naive".

dates

Date. Vector with dates for time features.

seed

Positive integer. Random seed. Default: 42.

batch_size

Positive integer. Mini-batch size for torch training. Default: 32.

validation_split

Positive numeric between 0 and 1. Validation share used by the autoencoder. Default: 0.1.

n_sim

Positive integer. Number of simulated paths generated from validation residuals. Default: 1000.

backend

String. Neural backend. Only "torch" is supported in codez 2.0.

Value

This function returns a list including:

Author(s)

Maintainer: Giancarlo Vercellino giancarlo.vercellino@gmail.com [copyright holder]

Giancarlo Vercellino giancarlo.vercellino@gmail.com

See Also

Useful links:


amzn_aapl_fb data set

Description

A data frame with the close prices for Amazon, Google and Facebook.

Usage

amzn_aapl_fb

Format

A data frame with 4 columns and 1798 rows.

Source

Yahoo Finance


Create a codez plot object

Description

Generic for creating dependency-free plot objects. Use plot() to draw the returned object with base R graphics.

Usage

autoplot(object, ...)

Arguments

object

An object with an autoplot() method.

...

Additional arguments passed to methods.

Value

A lightweight codez plot object when called on a supported codez object.


codez training controls

Description

Create reusable training controls for the codez 2.0 torch backend.

Usage

codez_control(
  batch_size = 32,
  validation_split = 0.1,
  n_sim = 1000,
  backend = "torch",
  epochs = NULL,
  patience = NULL,
  holdout = NULL
)

Arguments

batch_size

Positive integer. Mini-batch size for torch training.

validation_split

Numeric value in [0, 1). Validation share for the autoencoder.

n_sim

Positive integer. Number of simulated paths generated from validation residuals.

backend

String. Neural backend. Only "torch" is supported.

epochs

Optional positive integer overriding codez() epochs.

patience

Optional positive integer overriding early-stopping patience.

holdout

Optional numeric value between 0 and 1 overriding validation holdout.

Value

A list of controls for fit_codez().


Extract tidy forecasts from a codez result

Description

Extract tidy forecasts from legacy codez() output or a fitted codez_model.

Usage

codez_forecast_tidy(result)

Arguments

result

A result returned by codez() or a codez_model.

Value

A data frame with one row per feature and forecast horizon.


codez model methods

Description

S3 helpers for fitted codez 2.0 models.

Usage

## S3 method for class 'codez_model'
predict(object, type = c("forecast", "raw", "plots", "benchmarks"), ...)
## S3 method for class 'codez_model'
summary(object, ...)
## S3 method for class 'codez_model'
autoplot(object, feature = NULL, ...)
## S3 method for class 'codez_model'
plot(x, ...)
## S3 method for class 'codez_model'
as.data.frame(x, row.names = NULL, optional = FALSE, ...)

Arguments

object

A codez_model.

x

A codez_model.

type

Output type for predict().

feature

Optional feature name or names to plot.

...

Additional arguments reserved for future use.

row.names

Optional row names for as.data.frame().

optional

Optional argument passed through by as.data.frame().

Value

predict() and as.data.frame() return forecast data. summary() returns a summary object. autoplot() returns a lightweight codez plot object and plot() draws it with base R graphics.


Fit a codez 2.0 model

Description

fit_codez() is the codez 2.0 workflow wrapper. It keeps codez() as the forecasting engine, then attaches tidy forecasts, rolling baseline comparisons, the training call, and S3 methods.

Usage

fit_codez(df, ..., dates = NULL, control = codez_control())

Arguments

df

A data frame with time features on columns.

...

Arguments passed to codez().

dates

Optional Date vector with one value per row of df.

control

A codez_control() object.

Value

A codez_model object containing the legacy result, tidy forecasts, baseline backtests, controls, call, and backend metadata.

Examples

## Not run: 
model <- fit_codez(
  amzn_aapl_fb[, -1],
  dates = as.Date(amzn_aapl_fb$Date),
  seq_len = 20,
  n_samp = 3,
  n_windows = 5
)
predict(model)
summary(model)
autoplot(model)

## End(Not run)