Skip to content

Commit

Permalink
Merge pull request #271 from davidtedfordholt/parallel_forecasting
Browse files Browse the repository at this point in the history
implement future.apply in forecast.mdl_df
  • Loading branch information
mitchelloharawild authored Sep 23, 2020
2 parents db3fece + 2bf4b80 commit b24fe54
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
3 changes: 3 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ Authors@R:
role = "ctb"),
person(given = "George",
family = "Athanasopoulos",
role = "ctb"),
person(given = "David",
family = "Holt",
role = "ctb"))
Description: Provides tools, helpers and data structures for
developing models and time series functions for 'fable' and extension
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
allowing unbalanced hierarchies to be reconciled.
* Produce unique names for unnamed features used with `features()` (#258).
* Documentation improvements
* Performance improvements
* Performance improvements, including using `future.apply()` to parallelize
`forecast()` when the `future` package is attached (#268).

## Breaking changes

Expand Down
37 changes: 31 additions & 6 deletions R/forecast.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,38 @@ forecast.mdl_df <- function(object, new_data = NULL, h = NULL,
object <- bind_new_data(object, new_data)
}

# Evaluate forecasts
object <- dplyr::mutate_at(as_tibble(object), vars(!!!mdls),
forecast, object[["new_data"]],
h = h, point_forecast = point_forecast, ...,
key_data = key_data(object))
object <- tidyr::pivot_longer(object, !!mdls, names_to = ".model", values_to = ".mdl")

object <- tidyr::pivot_longer(object, !!mdls, names_to = ".model", values_to = ".fc")
# Evaluate forecasts
if(is_attached("package:future")){
require_package("future.apply")

object[[".fc"]] <- future.apply::future_mapply(
FUN = forecast,
object[[".mdl"]],
MoreArgs = list(
h = h,
point_forecast = point_forecast,
...,
key_data = key_data(object)
),
SIMPLIFY = FALSE,
future.globals = FALSE
)
}
else{
object[[".fc"]] <- mapply(
FUN = forecast,
object[[".mdl"]],
MoreArgs = list(
h = h,
point_forecast = point_forecast,
...,
key_data = key_data(object)
),
SIMPLIFY = FALSE
)
}

# Combine and re-construct fable
fbl_attr <- attributes(object$.fc[[1]])
Expand Down

0 comments on commit b24fe54

Please sign in to comment.