Returns a threedx
model applied to time series y
after learning the
optimal set of parameters by minimizing a provided loss function. Use
predict.threedx()
to generate a forecast based on the fitted model.
Usage
learn_weights(
y,
period_length,
alphas_grid,
loss_function,
penalize = FALSE,
loss_increase = 1
)
Arguments
- y
The time series to be forecasted as numeric vector (not as
ts()
object)- period_length
The presumed length of
y
's seasonal period; for example,12L
for monthly observations,7L
for daily observations, ...- alphas_grid
A data frame of possible parameter combinations to generate the weights of the final model. The optimal parameter set will be chosen based on the minimization of
loss_function
. The expected columns are numeric and calledalpha
,alpha_seasonal
,alpha_seasonal_decay
. At least one row must be provided. All values must be between 0 and 1. Use, for example,list_sampled_alphas()
orlist_edge_alphas()
to generate this data frame, or generate it in any way you like.- loss_function
A function with first argument
y_hat
and optionally more arguments. Usually, to compute a loss, at least an additionaly
argument is required to compute errors. Must be able to handle additional parameters via...
to allow for potential future changes in the set of arguments passed toloss_function
bylearn_weights()
. For examples, seeloss_mae()
orloss_mae_with_observation_weight()
. It can be assumed that the argumentsy_hat
andy
passed bylearn_weights()
are numeric vectors of equal length. The providedloss_function
must return a numeric scalar value.- penalize
Logical,
FALSE
by default. IfTRUE
, will try to pick a set of parameters that are simpler while not increasing the loss too much. The allowed increase in loss in percentage points is defined via theloss_increase
. A model is simpler if more of its parameters are equal to exactly zero or one, as these correspond to the edge cases.- loss_increase
A non-negative scalar value by which the loss may be increased compared to the best possible loss, in percentage points. This argument is ignored unless
penalize = TRUE
. The default of1
corresponds to a range of up to a 1 percentage point increase in loss.
Value
A fitted model object of class threedx
, which is a list of:
A numeric vector
weights
of the same length as the inputy
, assigning a weight to each index of the past observations. The weights sum to 1. These weights are the fitted weights used to produce forecasts.A numeric scalar
alpha
, the optimal paramater for the exponential smoothing component chosen during model trainingA numeric scalar
alpha_seasonal
, the optimal paramater for the seasonal exponential smoothing component chosen during model trainingA numeric scalar
alpha_seasonal_decay
, the optimal paramater for the seasonal exponential decay smoothing component chosen during model trainingA numeric vector
fitted
containing fitted values for each index ofy
; the up toperiod_length
-first observations may be missing.A numeric vector
residuals
containing the residuals for the training data as computed byy - fitted
, thus the up toperiod_length
-first observations may be missing.A numeric vector
y
, the inputy
A scalar
n
, the number of observations provided viay
A scalar
period_length
, the inputperiod_length
A function
loss_function
, the providedloss_function
A scalar
loss
, the value computed by the providedloss_function
based on the inputy
and the fitted values (ignoring the initial missing values) for the loss minimizing set of parameters reported inalpha
,alpha_seasonal
,alpha_seasonal_decay
A logical
penalize
, identical to the provided function argumentA scalar
loss_increase
, identical to the provided function argumentA list
full
containing intermediate results observed during model optimization for all other parameter combinations provided viaalphas_grid
Examples
set.seed(9284)
y <- stats::rpois(n = 55, lambda = pmax(0.1, 1 + 10 * sinpi((5 + 1:55 )/ 6)))
alphas_grid <- list_sampled_alphas(
n_target = 1000L,
include_edge_cases = TRUE
)
model <- learn_weights(
y = y,
alphas_grid = alphas_grid,
period_length = 12L,
loss_function = loss_mae
)
if (require("ggplot2")) {
autoplot(model)
}
#> Loading required package: ggplot2
model_penalized <- learn_weights(
y = y,
alphas_grid = alphas_grid,
period_length = 12L,
loss_function = loss_mae,
penalize = TRUE,
loss_increase = 10
)
model$full$best_alphas
#> alpha alpha_seasonal alpha_seasonal_decay
#> 1 0.03240655 0.8265107 0.173151
model_penalized$full$best_alphas
#> alpha alpha_seasonal alpha_seasonal_decay
#> 1 0 0.9146342 0
if (require("ggplot2")) {
autoplot(model_penalized)
}