-
Notifications
You must be signed in to change notification settings - Fork 8
manipulations
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"), ...)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: ...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.
# R — functions are in the greybox namespace
library(greybox)# Python
from greybox.xreg import xreg_transformer, xreg_multiplier, xreg_expander, temporal_dummyTransforms each variable in a matrix using a pool of mathematical functions, appending the transformed columns to the original data.
| 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".
Matrix with the original variables followed by transformed columns. For n variables and k functions, the output has n * (k + 1) columns.
# 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 sqrtGenerates cross-products of all pairs of variables. Useful for introducing interactions between dummy and continuous variables.
| 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 |
Matrix with the original variables followed by all pairwise cross-products. For n variables, the output has n + n*(n-1)/2 columns.
# 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)Expands variables with lagged and/or lead values. Negative lags mean past values, positive mean future values.
| 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"
|
| 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"
|
Matrix with lagged/lead columns. For n variables and m lags, the output has n * m columns (zero-lag is excluded).
# 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, lead1Generates dummy (one-hot encoded) variables for seasonal patterns based on frequency.
| 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).
Matrix of shape (n + h, freq) with one-hot encoded dummy columns.
# 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 Function | Python Function |
|---|---|
xregTransformer() |
xreg_transformer() |
xregMultiplier() |
xreg_multiplier() |
xregExpander() |
xreg_expander() |
temporalDummy() |
temporal_dummy() |
- Svetunkov, I. (2023). Statistics for Business Analytics. https://openforecast.org/sba/