Learn R Programming

PortfolioTesteR (version 0.1.4)

roll_fit_predict: Rolling fit/predict for tabular features (pooled / per-symbol / per-group)

Description

Rolling fit/predict for tabular features (pooled / per-symbol / per-group)

Usage

roll_fit_predict(
  features_list,
  label,
  fit_fn,
  predict_fn,
  is_periods = 156L,
  oos_periods = 4L,
  step = 4L,
  group = c("pooled", "per_symbol", "per_group"),
  group_map = NULL,
  na_action = c("omit", "zero")
)

Value

wide panel of scores: Date + symbols.

Arguments

features_list

list of wide panels (each: Date + symbols).

label

wide panel of future returns (same symbol set as features).

fit_fn

function (X, y) -> model trained on in-sample stacked rows.

predict_fn

function (model, Xnew) -> numeric scores.

is_periods, oos_periods, step

ints; in-sample length, out-of-sample length, and step size for the rolling window.

group

one of "pooled", "per_symbol", "per_group".

group_map

optional data.frame(Symbol, Group) if group = "per_group".

na_action

"omit" or "zero" for feature NA handling.

Examples

Run this code
# \donttest{
data(sample_prices_weekly); data(sample_prices_daily)
mom <- panel_lag(calc_momentum(sample_prices_weekly, 12), 1)
vol <- panel_lag(align_to_timeframe(
  calc_rolling_volatility(sample_prices_daily, 20),
  sample_prices_weekly$Date, "forward_fill"), 1)
Y <- make_labels(sample_prices_weekly, horizon = 4, type = "log")
fit_lm  <- function(X,y){ Xc <- cbind(1,X); list(coef=stats::lm.fit(Xc,y)$coefficients) }
pred_lm <- function(m,X){ as.numeric(cbind(1,X) %*% m$coef) }
S <- roll_fit_predict(list(mom=mom, vol=vol), Y, fit_lm, pred_lm, 52, 4, 4)
head(S)
# }

Run the code above in your browser using DataLab