Skip to content

Commit

Permalink
Merge pull request #89 from ellisp/matrix_xreg
Browse files Browse the repository at this point in the history
Fix messy function call
  • Loading branch information
dashaub committed Nov 19, 2018
2 parents 2608917 + b02df3b commit e291d40
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions pkg/DESCRIPTION
Expand Up @@ -19,6 +19,7 @@ Imports:
doParallel (>= 1.0.10),
foreach (>= 1.4.3),
ggplot2 (>= 2.2.0),
purrr (>= 0.2.5),
zoo (>= 1.7)
Suggests:
GMDH,
Expand Down
12 changes: 7 additions & 5 deletions pkg/R/hybridModel.R
Expand Up @@ -158,19 +158,20 @@ hybridModel <- function(y, models = "aefnst",
# Parallel processing won't be used by default since the benefit only occurs on long series
# with large frequency
currentModel <- NULL
fitModels <- foreach::foreach(currentModel = expandedModels,
fitModels <- foreach::foreach(modelCode = expandedModels,
.packages = c("forecast", "forecastHybrid")) %dopar% {
# thetam() currently does not handle arguments
if(currentModel == "f"){
if(modelCode == "f"){
fitModel <- thetam(y)
} else{ # All other models handle lambda and additional arguments
argsAdditional <- modelArguments[[currentModel]]
argsAdditional <- modelArguments[[modelCode]]
if(is.null(argsAdditional)){
argsAdditional <- list(lambda = lambda)
} else if(is.null(argsAdditional$lambda)){
argsAdditional$lambda <- lambda
}
fitModel <- do.call(getModel(currentModel), c(list(y), argsAdditional))
currentModel <- purrr::partial(getModel(modelCode), y = y)
fitModel <- do.call(currentModel, argsAdditional)
}
fitModel
}
Expand All @@ -192,7 +193,8 @@ hybridModel <- function(y, models = "aefnst",
} else if(is.null(argsAdditional$lambda)){
argsAdditional$lambda <- lambda
}
modelResults[[modelName]] <- do.call(getModel(modelCode), c(list(y = y), argsAdditional))
currentModel <- purrr::partial(getModel(modelCode), y = y)
modelResults[[modelName]] <- do.call(currentModel, argsAdditional)
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/inst/NEWS.md
@@ -1,5 +1,8 @@
# Version 4.0.15 [Unreleased]
* The `xreg` argument passed in should now be a matrix instead of a dataframe for consistency with "forecast" v8.5.
* Fix messy function call in #27. This results in `hybridModel` objects that use far less memory and that print more cleanly to the console. For example, previously `hm <- hybridModel(wineind); format(object.size(hm), units = "auto")` produced a 5.8 Mb object but now it is only 314.8 Kb.
* Adds "purrr" to imports.


# Version 3.0.14 [2018-07-22]
* Parallel support added to `hybridModel()`. This can be controlled by setting `parallel = TRUE` and setting `num.cores`. By default this is not enabled since the performance improvement typically only occurs when fitting `auto.arima` and `tbats` models on long series with large frequency (e.g. `taylor`).
Expand Down
7 changes: 7 additions & 0 deletions pkg/tests/testthat/test-hybridModel.R
Expand Up @@ -90,6 +90,13 @@ if(require(forecast) & require(testthat)){
dat <- ts(rnorm(52 * 3), f = 52)
expect_warning(hm <- hybridModel(y = dat, models = "fs"), NA)
expect_true(length(forecast(hm)$mean) == 52 * 2)
# Test for messy function call
hm <- hybridModel(wineind)
# Less than orignal, messy function call
expect_true(format(object.size(hm)) < "6035456 bytes")
# No worse than improved function call
# Disable for now because fails on r-devel
#expect_true(format(object.size(hm)) <= "322352 bytes")
})

test_that("Testing model matching", {
Expand Down

0 comments on commit e291d40

Please sign in to comment.