Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/gluonts/ext/r_forecast/R/univariate_forecast_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,49 @@ arima <- function(ts, params) {


fourier.arima.xreg <- function(ts, params, xreg_in, xreg_out){

if (missing(xreg_in)){
fourier.arima(ts, params)
} else {

fourier.frequency.low.periods <- 4
fourier.ratio.threshold.low.periods <- 18
fourier.frequency.high.periods <- 52
fourier.ratio.threshold.high.periods <- 2
fourier.order <- 4

period <- frequency(ts)
len_ts <- length(ts)
fourier_ratio <- len_ts / period

fourier <- FALSE

if ((period > fourier.frequency.low.periods
&& fourier_ratio > fourier.ratio.threshold.low.periods)
|| (period >= fourier.frequency.high.periods
&& fourier_ratio > fourier.ratio.threshold.high.periods)) {
# When the period is high, auto.arima becomes unstable
# per Rob's suggestion, we use Fourier series instead
# cf. https://robjhyndman.com/hyndsight/longseasonality/
fourier <- TRUE
}

if (fourier == TRUE) {
K <- min(fourier.order, floor(frequency(ts) / 2))
seasonal <- FALSE
xreg <- forecast::fourier(ts, K=K)
xreg_in <- as.matrix(xreg_in, xreg)
model <- forecast::auto.arima(ts, seasonal = seasonal, xreg = xreg_in, trace=TRUE)

xreg <- forecast::fourier(ts, K=K, h=params$prediction_length)
xreg_out <- as.matrix(xreg_out, xreg)

handleModel(model, params, xreg_out)
} else {
model <- forecast::auto.arima(ts, xreg = xreg_in, trace=TRUE)
handleModel(model, params, xreg_out)
}

fourier.frequency.low.periods <- 4
fourier.ratio.threshold.low.periods <- 18
fourier.frequency.high.periods <- 52
Expand Down
1 change: 0 additions & 1 deletion src/gluonts/ext/r_forecast/_univariate_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ def _get_r_forecast(self, data: Dict) -> Dict:
import rpy2.robjects.numpy2ri

rpy2.robjects.numpy2ri.activate()

data["feat_dynamic_real"] = np.transpose(data["feat_dynamic_real"])
nrow, ncol = data["feat_dynamic_real"].shape
xreg_in = self._robjects.r.matrix(
Expand Down
3 changes: 1 addition & 2 deletions test/ext/r_forecast/test_r_multi_seasonality.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ def test_compare_arimas():

assert fourier_arima_eval_metrics["MASE"] < arima_eval_metrics["MASE"]


## Below shows improvement in metric when proper x_regressors are included #
## Below shows improvement in metric when proper x_regressors are included #

dataset_xreg = [
{
Expand Down