earowang / hts

Hierarchical and Grouped Time Series
http://pkg.earo.me/hts
110 stars 36 forks source link

A typo in the code causes error when calling forecast.gts() with nonnegative=T parameter #58

Closed apantovic closed 4 years ago

apantovic commented 4 years ago

Hi,

I have created a grouped time series with 2 groups and 3 levels in each. When I pass this data to forecast.gts function error pops up saying:

Error in forecast.gts(gtsdf, h = 5, method = "comb", fmethod = "arima", : object 'fcasts' not found

the code for creating ts:

gtsdata <- gts(tsdata, characters=list(c(6,2,3),c(2,1,1)))
modelFcsts<-forecast.gts(gtsdata, h=5, method="comb", fmethod="arima", weights='mint', covariance='shr', keep.fitted = T, keep.resid = T, nonnegative = T) 

I've been digging a bit through the source code and I think the issue is in line 281 in [https://github.com/earowang/hts/blob/master/R/forecast-gts.R] where it should state: pfcasts[pfcasts < 0] <- 0

Fully reproducible example on the public data that can be used to recreate problem is here (I've used the example from [https://otexts.com/fpp2/gts.html] with small adjustment to get negative forecasts):

library(fpp2)
library(hts)

prisonNeg<-prison
prisonNeg[,1]<-seq(nrow(prisonNeg), 1, -1)         # just changing this column to get negative predictions from the model
prison.gts <- gts(prisonNeg, characters = c(3,1,9),  gnames = c("State", "Gender", "Legal",  "State*Gender", "State*Legal",  "Gender*Legal"))
modelFcsts<-forecast.gts(prison.gts, h=5, method="comb", fmethod="arima", weights='mint', covariance='shr', keep.fitted = T, keep.resid = T, nonnegative = T) 
robjhyndman commented 4 years ago

Thanks for the PR. Now resolved.