Skip to content

manipulations

Ivan Svetunkov edited this page Jun 24, 2026 · 2 revisions

Manipulations — Variable Transformation Functions

Function signatures

R

xregExpander(xreg, lags=c(-frequency(xreg):frequency(xreg)),
             silent=TRUE, gaps=c("auto","NAs","zero","naive","extrapolate"))
xregTransformer(xreg, functions=c("log","exp","inv","sqrt","square"), silent=TRUE)
xregMultiplier(xreg, silent=TRUE)
temporaldummy(object, type=c("month","quarter","week","day",
              "hour","halfhour","minute","second"), ...)

Python

def xreg_expander(
    xreg, lags=None, silent: bool = True,
    gaps: Literal["auto", "NAs", "zero", "naive", "extrapolate"] = "auto",
) -> np.ndarray: ...
def xreg_transformer(xreg, functions=None, silent: bool = True) -> np.ndarray: ...
def xreg_multiplier(xreg, silent: bool = True) -> np.ndarray: ...
def temporal_dummy(x, freq: int | None = None, h: int = 0) -> np.ndarray: ...

Overview

The xreg module provides functions for transforming and expanding exogenous (predictor) variables before passing them to regression models. These include non-linear transformations, cross-products (interactions), lag/lead expansion, and temporal dummy variables.

Import

# R — functions are in the greybox namespace
library(greybox)
# Python
from greybox.xreg import xreg_transformer, xreg_multiplier, xreg_expander, temporal_dummy

Functions

xreg_transformer — Non-linear Transformations

Transforms each variable in a matrix using a pool of mathematical functions, appending the transformed columns to the original data.

Parameters

Parameter R Python Type Default Description
Data xreg xreg matrix / ndarray Variables to transform
Functions functions functions character / list[str] All 5 Pool of functions to apply
Silent silent silent logical / bool TRUE / True Print progress

Available functions: "log", "exp", "inv", "sqrt", "square".

Returns

Matrix with the original variables followed by transformed columns. For n variables and k functions, the output has n * (k + 1) columns.

Example

# R
x <- cbind(rnorm(100, 10, 2), rnorm(100, 5, 1))
x_transformed <- xregTransformer(x, functions=c("log", "sqrt"))
# Result: 6 columns (2 original + 2 log + 2 sqrt)
# Python
import numpy as np
from greybox.xreg import xreg_transformer

x = np.column_stack([np.random.normal(10, 2, 100),
                     np.random.normal(5, 1, 100)])
x_transformed = xreg_transformer(x, functions=["log", "sqrt"])
# Result: shape (100, 6) — 2 original + 2 log + 2 sqrt

xreg_multiplier — Cross-products (Interactions)

Generates cross-products of all pairs of variables. Useful for introducing interactions between dummy and continuous variables.

Parameters

Parameter R Python Type Default Description
Data xreg xreg matrix / ndarray Variables (must have >= 2 columns)
Silent silent silent logical / bool TRUE / True Print progress

Returns

Matrix with the original variables followed by all pairwise cross-products. For n variables, the output has n + n*(n-1)/2 columns.

Example

# R
x <- cbind(c(1, 2, 3), c(4, 5, 6), c(7, 8, 9))
x_mult <- xregMultiplier(x)
# Result: 6 columns (3 original + 3 cross-products: x1*x2, x1*x3, x2*x3)
# Python
from greybox.xreg import xreg_multiplier

x = np.array([[1, 4, 7], [2, 5, 8], [3, 6, 9]])
x_mult = xreg_multiplier(x)
# Result: shape (3, 6)

xreg_expander — Lag/Lead Expansion

Expands variables with lagged and/or lead values. Negative lags mean past values, positive mean future values.

Parameters

Parameter R Python Type Default Description
Data xreg xreg vector/matrix / ndarray Variables to expand
Lags lags lags integer vector / list[int] c(-1, 0, 1) / [-1, 0, 1] Lags (negative) and leads (positive). Zero is ignored.
Silent silent silent logical / bool TRUE / True Print progress
Gaps gaps gaps character / str "auto" How to fill gaps: "NAs", "zero", "naive", "extrapolate", "auto"

Gap-filling methods

Method Description
"NAs" Leave missing values as NaN
"zero" Fill with zeros
"naive" Repeat the first/last valid value
"extrapolate" Linear extrapolation from valid values
"auto" Automatically choose between "extrapolate" and "naive"

Returns

Matrix with lagged/lead columns. For n variables and m lags, the output has n * m columns (zero-lag is excluded).

Example

# R
x <- 1:10
x_expanded <- xregExpander(x, lags=c(-1, -2, 1))
# Python
from greybox.xreg import xreg_expander

x = np.arange(1, 11)
x_expanded = xreg_expander(x, lags=[-1, -2, 1])
# Result: shape (10, 3) — lag1, lag2, lead1

temporal_dummy — Temporal Dummy Variables

Generates dummy (one-hot encoded) variables for seasonal patterns based on frequency.

Parameters

Parameter R Python Type Default Description
Data x x dates / array-like Time index or date vector
Frequency freq freq integer / int NULL / None (defaults to 12) Seasonal frequency
Horizon h h integer / int 0 Number of future periods to generate dummies for

Common frequencies: 12 (monthly), 4 (quarterly), 7 (daily within week), 24 (hourly within day).

Returns

Matrix of shape (n + h, freq) with one-hot encoded dummy columns.

Example

# R
dates <- seq(as.Date("2020-01-01"), by="month", length.out=24)
dummies <- temporalDummy(dates, freq=12)
# Result: 24 × 12 matrix
# Python
import pandas as pd
from greybox.xreg import temporal_dummy

dates = pd.date_range("2020-01-01", periods=24, freq="M")
dummies = temporal_dummy(dates, freq=12)
# Result: shape (24, 12)

# With forecast horizon
dummies = temporal_dummy(dates, freq=12, h=6)
# Result: shape (30, 12) — 24 in-sample + 6 future

R vs Python Function Names

R Function Python Function
xregTransformer() xreg_transformer()
xregMultiplier() xreg_multiplier()
xregExpander() xreg_expander()
temporalDummy() temporal_dummy()

References

Clone this wiki locally