Skip to content

Commit

Permalink
fix issue #6
Browse files Browse the repository at this point in the history
  • Loading branch information
earowang committed Jul 17, 2014
1 parent d2c6b0a commit 1aef4b9
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 20 deletions.
3 changes: 2 additions & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
v4.4 (xx 2014)
v4.4 (17 July 2014)
* Allowed multiple seasonal objects "msts" in hts() and gts().
* Fixed bug of MASE in accuracy.gts().
* Allowed user's defined forecasting function in forecast.gts().
* Fixed bug of xreg and newxreg in forecast.gts(), when a matrix is passed.

v4.3 (10 June 2014)
* Fixed bug of the arg "include" in plot.gts, when there are not yearly data.
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: hts
Type: Package
Title: Hierarchical and grouped time series
Version: 4.3
Version: 4.4
Depends: forecast (>= 5.0)
Imports: SparseM, parallel, utils
Suggests: testthat
Expand Down
74 changes: 58 additions & 16 deletions R/forecast.gts.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,24 +112,66 @@ forecast.gts <- function(object, h = ifelse(frequency(object) > 1L,
return(out)
}

if (parallel) { # parallel == TRUE
if (is.null(num.cores)) {
num.cores <- detectCores()
if (fmethod == "arima" && !is.null(c(xreg, newxreg))
&& !is.vector(xreg) && !is.vector(newxreg)) {
# Error handling for args xreg and newxreg
nc.y <- ncol(y)
nr.y <- nrow(y)
# the dim of xreg and newreg should be equal to the dim of forecasting ts
check.col <- all(nc.y == c(ncol(xreg), ncol(newxreg)))
check.row <- nr.y == nrow(xreg)
check.row.new <- h == nrow(newxreg)
if (!check.col) {
msg <- sprintf("The args xreg or newxreg should have %i columns.", nc.y)
stop(msg)
} else if (!check.row) {
msg <- sprintf("The arg xreg should have %i rows", nr.y)
stop(msg)
} else if (!check.row.new) {
msg <- sprintf("The arg newxreg should have %i rows", h)
stop(msg)
}
# parallel process not working and conventional forloop will be used instead
# because auto.arima and forecast.Arima use the same arg xreg, and lapply
# cannot be able to tell xreg and newxreg
parallel <- FALSE
pfcasts <- matrix(, nrow = h, ncol = nc.y)
if (keep.fitted) {
fits <- matrix(, nrow = nr.y, ncol = nc.y)
}
if (keep.resid) {
resid <- matrix(, nrow = nr.y, ncol = nc.y)
}
for (i in 1L:nc.y) {
models <- auto.arima(y[, i], lambda = lambda, xreg = xreg[, i], ...)
pfcasts[, i] <- forecast(models, h = h, xreg = newxreg[, i])$mean
if (keep.fitted) {
fits[, i] <- fitted(models)
}
if (keep.resid) {
resid[, i] <- residuals(models)
}
}
} else { # if xreg and newxreg are not used
if (parallel) { # parallel == TRUE
if (is.null(num.cores)) {
num.cores <- detectCores()
}
cl <- makeCluster(num.cores)
loopout <- parSapplyLB(cl = cl, X = y, FUN = function(x) loopfn(x, ...),
simplify = FALSE)
stopCluster(cl = cl)
} else { # parallel = FALSE
loopout <- lapply(y, function(x) loopfn(x, ...))
}
cl <- makeCluster(num.cores)
loopout <- parSapplyLB(cl = cl, X = y, FUN = function(x) loopfn(x, ...),
simplify = FALSE)
stopCluster(cl = cl)
} else { # parallel = FALSE
loopout <- lapply(y, function(x) loopfn(x, ...))
}

pfcasts <- sapply(loopout, function(x) x$pfcasts)
if (keep.fitted) {
fits <- sapply(loopout, function(x) x$fitted)
}
if (keep.resid) {
resid <- sapply(loopout, function(x) x$resid)
pfcasts <- sapply(loopout, function(x) x$pfcasts)
if (keep.fitted) {
fits <- sapply(loopout, function(x) x$fitted)
}
if (keep.resid) {
resid <- sapply(loopout, function(x) x$resid)
}
}

if (is.vector(pfcasts)) { # if h = 1, sapply returns a vector
Expand Down
4 changes: 2 additions & 2 deletions man/forecast.gts.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ Methods for forecasting hierarchical or grouped time series.
\item{FUN}{It allows users to define their own fitted model that can be passed to the \code{forecast} function
to generate base forecasts such as \code{tbats} and \code{stlf}. There is no use in the
arguments \code{positive} and \code{lambda}, if \code{FUN} is being used.}
\item{xreg}{When \code{fmethod = "arima"}, a vector or matrix of external regressors, which must have the same number of rows as the original univariate time series}
\item{newxreg}{When \code{fmethod = "arima"}, a vector or matrix of external regressors, which must have the same number of rows as the original univariate time series}
\item{xreg}{When \code{fmethod = "arima"}, a vector or matrix of external regressors used for modelling, which must have the same number of rows as the original univariate time series}
\item{newxreg}{When \code{fmethod = "arima"}, a vector or matrix of external regressors used for forecasting, which must have the same number of rows as the \code{h} forecast horizon}
\item{...}{Other arguments passing to \code{\link[forecast]{ets}} or \code{\link[forecast]{auto.arima}}}
}
\value{A forecasted hierarchical/grouped time series of class \code{gts}.}
Expand Down

0 comments on commit 1aef4b9

Please sign in to comment.