forked from business-science/modeltime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsnip-seasonal_reg.R
668 lines (556 loc) · 20.8 KB
/
parsnip-seasonal_reg.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
#' General Interface for Multiple Seasonality Regression Models (TBATS, STLM)
#'
#' `seasonal_reg()` is a way to generate a _specification_ of an
#' Seasonal Decomposition model
#' before fitting and allows the model to be created using
#' different packages. Currently the only package is `forecast`.
#'
#' @param mode A single character string for the type of model.
#' The only possible value for this model is "regression".
#' @param seasonal_period_1 (required) The primary seasonal frequency.
#' Uses `"auto"` by default.
#' A character phrase of "auto" or time-based phrase of "2 weeks"
#' can be used if a date or date-time variable is provided.
#' See Fit Details below.
#' @param seasonal_period_2 (optional) A second seasonal frequency.
#' Is `NULL` by default.
#' A character phrase of "auto" or time-based phrase of "2 weeks"
#' can be used if a date or date-time variable is provided.
#' See Fit Details below.
#' @param seasonal_period_3 (optional) A third seasonal frequency.
#' Is `NULL` by default.
#' A character phrase of "auto" or time-based phrase of "2 weeks"
#' can be used if a date or date-time variable is provided.
#' See Fit Details below.
#'
#'
#' @details
#' The data given to the function are not saved and are only used
#' to determine the _mode_ of the model. For `seasonal_reg()`, the
#' mode will always be "regression".
#'
#' The model can be created using the `fit()` function using the
#' following _engines_:
#'
#' - "tbats" - Connects to `forecast::tbats()`
#' - "stlm_ets" - Connects to `forecast::stlm()`, `method = "ets"`
#' - "stlm_arima" - Connects to `forecast::stlm()`, `method = "arima"`
#'
#'
#' @section Engine Details:
#'
#' The standardized parameter names in `modeltime` can be mapped to their original
#' names in each engine:
#'
#' ```{r echo = FALSE}
#' # parsnip::convert_args("seasonal_reg")
#' tibble::tribble(
#' ~ "modeltime", ~ "forecast::stlm", ~ "forecast::tbats",
#' "seasonal_period_1, seasonal_period_2, seasonal_period_3", "msts(seasonal.periods)", "msts(seasonal.periods)"
#' ) %>% knitr::kable()
#' ```
#'
#' Other options can be set using `set_engine()`.
#'
#' The engines use `forecast::stlm()`.
#'
#' Function Parameters:
#' ```{r echo = FALSE}
#' str(forecast::stlm)
#' ```
#'
#' __tbats__
#'
#' - __Method:__ Uses `method = "tbats"`, which by default is auto-TBATS.
#' - __Xregs:__ Univariate. Cannot accept Exogenous Regressors (xregs). Xregs are ignored.
#'
#' __stlm_ets__
#'
#' - __Method:__ Uses `method = "stlm_ets"`, which by default is auto-ETS.
#' - __Xregs:__ Univariate. Cannot accept Exogenous Regressors (xregs). Xregs are ignored.
#'
#'
#' __stlm_arima__
#'
#' - __Method:__ Uses `method = "stlm_arima"`, which by default is auto-ARIMA.
#' - __Xregs:__ Multivariate. Can accept Exogenous Regressors (xregs).
#'
#'
#'
#' @section Fit Details:
#'
#' __Date and Date-Time Variable__
#'
#' It's a requirement to have a date or date-time variable as a predictor.
#' The `fit()` interface accepts date and date-time features and handles them internally.
#'
#' - `fit(y ~ date)`
#'
#' _Seasonal Period Specification_
#'
#' The period can be non-seasonal (`seasonal_period = 1 or "none"`) or
#' yearly seasonal (e.g. For monthly time stamps, `seasonal_period = 12`, `seasonal_period = "12 months"`, or `seasonal_period = "yearly"`).
#' There are 3 ways to specify:
#'
#' 1. `seasonal_period = "auto"`: A seasonal period is selected based on the periodicity of the data (e.g. 12 if monthly)
#' 2. `seasonal_period = 12`: A numeric frequency. For example, 12 is common for monthly data
#' 3. `seasonal_period = "1 year"`: A time-based phrase. For example, "1 year" would convert to 12 for monthly data.
#'
#'
#' __Univariate (No xregs, Exogenous Regressors):__
#'
#' For univariate analysis, you must include a date or date-time feature. Simply use:
#'
#' - Formula Interface (recommended): `fit(y ~ date)` will ignore xreg's.
#' - XY Interface: `fit_xy(x = data[,"date"], y = data$y)` will ignore xreg's.
#'
#' __Multivariate (xregs, Exogenous Regressors)__
#'
#' - The `tbats` engine _cannot_ accept Xregs.
#' - The `stlm_ets` engine _cannot_ accept Xregs.
#' - The `stlm_arima` engine _can_ accept Xregs
#'
#' The `xreg` parameter is populated using the `fit()` or `fit_xy()` function:
#'
#' - Only `factor`, `ordered factor`, and `numeric` data will be used as xregs.
#' - Date and Date-time variables are not used as xregs
#' - `character` data should be converted to factor.
#'
#' _Xreg Example:_ Suppose you have 3 features:
#'
#' 1. `y` (target)
#' 2. `date` (time stamp),
#' 3. `month.lbl` (labeled month as a ordered factor).
#'
#' The `month.lbl` is an exogenous regressor that can be passed to the `seasonal_reg()` using
#' `fit()`:
#'
#' - `fit(y ~ date + month.lbl)` will pass `month.lbl` on as an exogenous regressor.
#' - `fit_xy(data[,c("date", "month.lbl")], y = data$y)` will pass x, where x is a data frame containing `month.lbl`
#' and the `date` feature. Only `month.lbl` will be used as an exogenous regressor.
#'
#' Note that date or date-time class values are excluded from `xreg`.
#'
#'
#'
#' @seealso [fit.model_spec()], [set_engine()]
#'
#' @examples
#' library(dplyr)
#' library(parsnip)
#' library(rsample)
#' library(timetk)
#' library(modeltime)
#'
#' # Data
#' taylor_30_min
#'
#' # Split Data 80/20
#' splits <- initial_time_split(taylor_30_min, prop = 0.8)
#'
#' # ---- STLM ETS ----
#'
#' # Model Spec
#' model_spec <- seasonal_reg() %>%
#' set_engine("stlm_ets")
#'
#' # Fit Spec
#' model_fit <- model_spec %>%
#' fit(log(value) ~ date, data = training(splits))
#' model_fit
#'
#'
#' # ---- STLM ARIMA ----
#'
#' # Model Spec
#' model_spec <- seasonal_reg() %>%
#' set_engine("stlm_arima")
#'
#' # Fit Spec
#' model_fit <- model_spec %>%
#' fit(log(value) ~ date, data = training(splits))
#' model_fit
#'
#' @export
seasonal_reg <- function(mode = "regression",
seasonal_period_1 = NULL, seasonal_period_2 = NULL, seasonal_period_3 = NULL) {
args <- list(
seasonal_period_1 = rlang::enquo(seasonal_period_1),
seasonal_period_2 = rlang::enquo(seasonal_period_2),
seasonal_period_3 = rlang::enquo(seasonal_period_3)
)
parsnip::new_model_spec(
"seasonal_reg",
args = args,
eng_args = NULL,
mode = mode,
method = NULL,
engine = NULL
)
}
#' @export
print.seasonal_reg <- function(x, ...) {
cat("Seasonal Regression Model Specification (", x$mode, ")\n\n", sep = "")
parsnip::model_printer(x, ...)
if(!is.null(x$method$fit$args)) {
cat("Model fit template:\n")
print(parsnip::show_call(x))
}
invisible(x)
}
#' @export
#' @importFrom stats update
update.seasonal_reg <- function(object, parameters = NULL,
seasonal_period_1 = NULL, seasonal_period_2 = NULL, seasonal_period_3 = NULL,
fresh = FALSE, ...) {
parsnip::update_dot_check(...)
if (!is.null(parameters)) {
parameters <- parsnip::check_final_param(parameters)
}
args <- list(
seasonal_period_1 = rlang::enquo(seasonal_period_1),
seasonal_period_2 = rlang::enquo(seasonal_period_2),
seasonal_period_3 = rlang::enquo(seasonal_period_3)
)
args <- parsnip::update_main_parameters(args, parameters)
if (fresh) {
object$args <- args
} else {
null_args <- purrr::map_lgl(args, parsnip::null_value)
if (any(null_args))
args <- args[!null_args]
if (length(args) > 0)
object$args[names(args)] <- args
}
parsnip::new_model_spec(
"seasonal_reg",
args = object$args,
eng_args = object$eng_args,
mode = object$mode,
method = NULL,
engine = object$engine
)
}
#' @export
#' @importFrom parsnip translate
translate.seasonal_reg <- function(x, engine = x$engine, ...) {
if (is.null(engine)) {
message("Used `engine = 'tbats'` for translation.")
engine <- "tbats"
}
x <- parsnip::translate.default(x, engine, ...)
x
}
# FIT - TBATS -----
#' Low-Level tbats function for translating modeltime to forecast
#'
#' @param x A dataframe of xreg (exogenous regressors)
#' @param y A numeric vector of values to fit
#' @param period_1 (required) First seasonal frequency. Uses "auto" by default. A character phrase
#' of "auto" or time-based phrase of "2 weeks" can be used if a date or date-time variable is provided.
#' @param period_2 (optional) First seasonal frequency. Uses "auto" by default. A character phrase
#' of "auto" or time-based phrase of "2 weeks" can be used if a date or date-time variable is provided.
#' @param period_3 (optional) First seasonal frequency. Uses "auto" by default. A character phrase
#' of "auto" or time-based phrase of "2 weeks" can be used if a date or date-time variable is provided.
#' @param use.parallel `TRUE/FALSE` indicates whether or not to use parallel processing.
#' @param ... Additional arguments passed to `forecast::tbats()`
#'
#' @export
tbats_fit_impl <- function(x, y, period_1 = "auto", period_2 = NULL, period_3 = NULL, use.parallel = length(y) > 1000, ...) {
# X & Y
# Expect outcomes = vector
# Expect predictor = data.frame
outcome <- y
predictor <- x
if (is.null(period_1) || period_1 == "none" || period_1 <=1) {
glubort("The 'seasonal_period_1' must be greater than 1 (i.e. have seasonality). Try increasing the seasonality.")
}
# INDEX & PERIOD
# Determine Period, Index Col, and Index
index_tbl <- parse_index_from_data(predictor)
period_1 <- parse_period_from_index(index_tbl, period_1)
if (!is.null(period_2)) period_2 <- parse_period_from_index(index_tbl, period_2)
if (!is.null(period_3)) period_3 <- parse_period_from_index(index_tbl, period_3)
idx_col <- names(index_tbl)
idx <- timetk::tk_index(index_tbl)
# XREGS - NOT USED FOR TBATS METHOD
# Clean names, get xreg recipe, process predictors
# xreg_recipe <- create_xreg_recipe(predictor, prepare = TRUE)
# xreg_matrix <- juice_xreg_recipe(xreg_recipe, format = "matrix")
if (ncol(predictor) > 1) {
message("External regressors (xregs) detected. TBATS is a univariate method. Ignoring xregs.")
}
# FIT
outcome <- forecast::msts(outcome,
seasonal.periods = c(period_1, period_2, period_3))
fit_tbats <- forecast::tbats(y = outcome, use.parallel = use.parallel, ...)
# RETURN
new_modeltime_bridge(
class = "tbats_fit_impl",
# Models
models = list(
model_1 = fit_tbats
),
# Data - Date column (matches original), .actual, .fitted, and .residuals columns
data = tibble::tibble(
!! idx_col := idx,
.actual = as.numeric(y),
.fitted = as.numeric(fit_tbats$fitted.values),
.residuals = as.numeric(y) - as.numeric(fit_tbats$fitted.values)
),
# Preprocessing Recipe (prepped) - Used in predict method
extras = list(),
# Description - Convert tbats model parameters to short description
desc = get_tbats_description(fit_tbats)
)
}
#' @export
print.tbats_fit_impl <- function(x, ...) {
model <- x$models$model_1
print(model)
invisible(x)
}
# PREDICT - TBATS ----
# - auto.arima produces an Arima model
#' @export
predict.tbats_fit_impl <- function(object, new_data, ...) {
tbats_predict_impl(object, new_data, ...)
}
#' Bridge prediction function for ARIMA models
#'
#' @inheritParams parsnip::predict.model_fit
#' @param ... Additional arguments passed to `forecast::forecast()`
#'
#' @export
tbats_predict_impl <- function(object, new_data, ...) {
# PREPARE INPUTS
model <- object$models$model_1
h_horizon <- nrow(new_data)
# XREG
# NOT REQUIRED FOR ETS.
# xreg_recipe <- object$extras$xreg_recipe
# xreg_matrix <- bake_xreg_recipe(xreg_recipe, new_data, format = "matrix")
# PREDICTIONS
preds_forecast <- forecast::forecast(model, h = h_horizon, ...)
# Return predictions as numeric vector
preds <- as.numeric(preds_forecast$mean)
return(preds)
}
# FIT - STLM ETS -----
#' Low-Level stlm function for translating modeltime to forecast
#'
#' @param x A dataframe of xreg (exogenous regressors)
#' @param y A numeric vector of values to fit
#' @param period_1 (required) First seasonal frequency. Uses "auto" by default. A character phrase
#' of "auto" or time-based phrase of "2 weeks" can be used if a date or date-time variable is provided.
#' @param period_2 (optional) First seasonal frequency. Uses "auto" by default. A character phrase
#' of "auto" or time-based phrase of "2 weeks" can be used if a date or date-time variable is provided.
#' @param period_3 (optional) First seasonal frequency. Uses "auto" by default. A character phrase
#' of "auto" or time-based phrase of "2 weeks" can be used if a date or date-time variable is provided.
#' @param ... Additional arguments passed to `forecast::stlm()`
#'
#' @export
stlm_ets_fit_impl <- function(x, y, period_1 = "auto", period_2 = NULL, period_3 = NULL, ...) {
# X & Y
# Expect outcomes = vector
# Expect predictor = data.frame
outcome <- y
predictor <- x
if (is.null(period_1) || period_1 == "none" || period_1 <=1) {
glubort("The 'seasonal_period_1' must be greater than 1 (i.e. have seasonality). Try increasing the seasonality.")
}
# INDEX & PERIOD
# Determine Period, Index Col, and Index
index_tbl <- parse_index_from_data(predictor)
period_1 <- parse_period_from_index(index_tbl, period_1)
if (!is.null(period_2)) period_2 <- parse_period_from_index(index_tbl, period_2)
if (!is.null(period_3)) period_3 <- parse_period_from_index(index_tbl, period_3)
idx_col <- names(index_tbl)
idx <- timetk::tk_index(index_tbl)
# XREGS - NOT USED FOR ETS METHOD
# Clean names, get xreg recipe, process predictors
# xreg_recipe <- create_xreg_recipe(predictor, prepare = TRUE)
# xreg_matrix <- juice_xreg_recipe(xreg_recipe, format = "matrix")
if (ncol(predictor) > 1) {
message("External regressors (xregs) detected. STLM + ETS is a univariate method. Ignoring xregs.")
}
# FIT
outcome <- forecast::msts(outcome, seasonal.periods = c(period_1, period_2, period_3))
fit_stlm <- forecast::stlm(y = outcome, method = "ets", ...)
# RETURN
new_modeltime_bridge(
class = "stlm_ets_fit_impl",
# Models
models = list(
model_1 = fit_stlm
),
# Data - Date column (matches original), .actual, .fitted, and .residuals columns
data = tibble::tibble(
!! idx_col := idx,
.actual = as.numeric(fit_stlm$x),
.fitted = as.numeric(fit_stlm$fitted),
.residuals = as.numeric(fit_stlm$residuals)
),
# Preprocessing Recipe (prepped) - Used in predict method
extras = list(),
# Description - Convert arima model parameters to short description
desc = stringr::str_c("SEASONAL DECOMP: ", fit_stlm$model$method)
)
}
#' @export
print.stlm_ets_fit_impl <- function(x, ...) {
model <- x$models$model_1$model
cat(x$desc)
# cat("\n")
# print(model$call)
cat("\n\n")
print(
tibble::tibble(
aic = model$aic,
bic = model$bic,
aicc = model$aicc,
loglik = model$loglik,
mse = model$mse
)
)
invisible(x)
}
# PREDICT - STLM ETS ----
# - auto.arima produces an Arima model
#' @export
predict.stlm_ets_fit_impl <- function(object, new_data, ...) {
stlm_ets_predict_impl(object, new_data, ...)
}
#' Bridge prediction function for ARIMA models
#'
#' @inheritParams parsnip::predict.model_fit
#' @param ... Additional arguments passed to `forecast::forecast()`
#'
#' @export
stlm_ets_predict_impl <- function(object, new_data, ...) {
# PREPARE INPUTS
model <- object$models$model_1
h_horizon <- nrow(new_data)
# XREG
# NOT REQUIRED FOR ETS.
# xreg_recipe <- object$extras$xreg_recipe
# xreg_matrix <- bake_xreg_recipe(xreg_recipe, new_data, format = "matrix")
# PREDICTIONS
preds_forecast <- forecast::forecast(model, h = h_horizon, ...)
# Return predictions as numeric vector
preds <- as.numeric(preds_forecast$mean)
return(preds)
}
# FIT - STLM ARIMA -----
#' Low-Level stlm function for translating modeltime to forecast
#'
#' @param x A dataframe of xreg (exogenous regressors)
#' @param y A numeric vector of values to fit
#' @param period_1 (required) First seasonal frequency. Uses "auto" by default. A character phrase
#' of "auto" or time-based phrase of "2 weeks" can be used if a date or date-time variable is provided.
#' @param period_2 (optional) First seasonal frequency. Uses "auto" by default. A character phrase
#' of "auto" or time-based phrase of "2 weeks" can be used if a date or date-time variable is provided.
#' @param period_3 (optional) First seasonal frequency. Uses "auto" by default. A character phrase
#' of "auto" or time-based phrase of "2 weeks" can be used if a date or date-time variable is provided.
#' @param ... Additional arguments passed to `forecast::stlm()`
#'
#' @export
stlm_arima_fit_impl <- function(x, y, period_1 = "auto", period_2 = NULL, period_3 = NULL, ...) {
# X & Y
# Expect outcomes = vector
# Expect predictor = data.frame
outcome <- y
predictor <- x
if (is.null(period_1) || period_1 == "none" || period_1 <=1) {
glubort("The 'seasonal_period_1' must be greater than 1 (i.e. have seasonality). Try increasing the seasonality.")
}
# INDEX & PERIOD
# Determine Period, Index Col, and Index
index_tbl <- parse_index_from_data(predictor)
period_1 <- parse_period_from_index(index_tbl, period_1)
if (!is.null(period_2)) period_2 <- parse_period_from_index(index_tbl, period_2)
if (!is.null(period_3)) period_3 <- parse_period_from_index(index_tbl, period_3)
idx_col <- names(index_tbl)
idx <- timetk::tk_index(index_tbl)
# XREGS - NOT USED FOR ETS METHOD
# Clean names, get xreg recipe, process predictors
xreg_recipe <- create_xreg_recipe(predictor, prepare = TRUE, one_hot = FALSE)
xreg_matrix <- juice_xreg_recipe(xreg_recipe, format = "matrix")
# FIT
outcome <- forecast::msts(outcome, seasonal.periods = c(period_1, period_2, period_3))
if (!is.null(xreg_matrix)) {
fit_stlm <- forecast::stlm(outcome, method = "arima", xreg = xreg_matrix, ...)
} else {
fit_stlm <- forecast::stlm(outcome, method = "arima", ...)
}
# RETURN
new_modeltime_bridge(
class = "stlm_arima_fit_impl",
# Models
models = list(
model_1 = fit_stlm
),
# Data - Date column (matches original), .actual, .fitted, and .residuals columns
data = tibble::tibble(
!! idx_col := idx,
.actual = as.numeric(fit_stlm$x),
.fitted = as.numeric(fit_stlm$fitted),
.residuals = as.numeric(fit_stlm$residuals)
),
# Preprocessing Recipe (prepped) - Used in predict method
extras = list(
xreg_recipe = xreg_recipe
),
# Description - Convert arima model parameters to short description
desc = stringr::str_c("SEASONAL DECOMP: ", get_arima_description(fit_stlm$model))
)
}
#' @export
print.stlm_arima_fit_impl <- function(x, ...) {
model <- x$models$model_1$model
cat(x$desc)
# cat("\n")
# print(model$call)
cat("\n\n")
print(model)
# print(
# tibble::tibble(
# aic = model$aic,
# bic = model$bic,
# aicc = model$aicc,
# loglik = model$loglik,
# mse = model$mse
# )
# )
invisible(x)
}
# PREDICT - STLM ARIMA ----
# - auto.arima produces an Arima model
#' @export
predict.stlm_arima_fit_impl <- function(object, new_data, ...) {
stlm_arima_predict_impl(object, new_data, ...)
}
#' Bridge prediction function for ARIMA models
#'
#' @inheritParams parsnip::predict.model_fit
#' @param ... Additional arguments passed to `forecast::forecast()`
#'
#' @export
stlm_arima_predict_impl <- function(object, new_data, ...) {
# PREPARE INPUTS
model <- object$models$model_1
h_horizon <- nrow(new_data)
# XREG
# NOT REQUIRED FOR ETS.
xreg_recipe <- object$extras$xreg_recipe
xreg_matrix <- bake_xreg_recipe(xreg_recipe, new_data, format = "matrix")
# PREDICTIONS
if (!is.null(xreg_matrix)) {
preds_forecast <- forecast::forecast(model, h = h_horizon, xreg = xreg_matrix, ...)
} else {
preds_forecast <- forecast::forecast(model, h = h_horizon, ...)
}
# Return predictions as numeric vector
preds <- as.numeric(preds_forecast$mean)
return(preds)
}