config-i1 / smooth

The set of functions used for time series analysis and in forecasting.
89 stars 19 forks source link

help("sma") example produces error with forecast #183

Closed technocrat closed 2 years ago

technocrat commented 3 years ago

Under R 4.0.4, Ubuntu 20.10

I came to the example with the same error on my own argument, but found the same problem. I resolved mine by giving an express order argument. Does the example need one?

forecast(ourModel)
#> Error in is.constant(y): 'list' object cannot be coerced to type 'double'

Complete code

library(smooth)
#> Loading required package: greybox
#> Package "greybox", v0.7.0 loaded.
#> This is package "smooth", v3.1.1
library(Mcomp)
#> Loading required package: forecast
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
#> 
#> Attaching package: 'forecast'
#> The following object is masked from 'package:greybox':
#> 
#>     forecast

# SMA of arbitrary order
ourModel <- sma(rnorm(118,100,3),h=18,holdout=TRUE,interval="sp")

summary(ourModel)
#> Time elapsed: 0.15 seconds
#> Model estimated: SMA(56)
#> Initial values were produced using backcasting.
#> 
#> Loss function type: MSE; Loss function value: 9.5667
#> Error standard deviation: 3.1244
#> Sample size: 100
#> Number of estimated parameters: 2
#> Number of degrees of freedom: 98
#> Information criteria:
#>      AIC     AICc      BIC     BICc 
#> 513.6169 513.7406 518.8272 519.1121 
#> 
#> 95% semiparametric prediction interval was constructed
#> 89% of values are in the prediction interval
#> Forecast errors:
#> MPE: 0.1%; sCE: 4%; Bias: 12.2%; MAPE: 2.6%
#> MASE: 0.707; sMAE: 2.6%; sMSE: 0.1%; rMAE: 0.954; rRMSE: 0.964
forecast(ourModel)
#> Error in is.constant(y): 'list' object cannot be coerced to type 'double'
config-i1 commented 3 years ago

Thanks for the report. Can you please try installing smooth v3.1.2 from github via remotes::install_github("config-i1/smooth") to see if the issues persists?

technocrat commented 3 years ago

This works properly under v3.1.2. Thanks for the fix. And confirming my sanity.

hjoshi92 commented 3 years ago

@config-i1 We upgraded "smooth" package in our organization servers and now I am facing this exact issue which was reported here. We are using

Rversion -4.1.0 Most of our forecasting scripts are failing which are using sma() function with smooth_3.1.2. Whereas when we downgrade the smooth_3.0.1 it works fine with forecast() function. Need your immediate help on this.

config-i1 commented 3 years ago

Hi @hjoshi92,

That's weird. Sorry to hear that. Do you get exactly the same error? If yes, then this means that there is a conflict in forecast methods. Use greybox::forecast(model) and see if it works. But this should not appear in the first place, so that's not normal.

Also, can you provide a reproducible code? Including the sequence of loaded packages...

hjoshi92 commented 3 years ago

Hello @config-i1,

Yes I get the exact same error. I also observed that there are 3 packages (i.e forecast, smooth and greybox) having a same function called forecast().

When I tried using

forecast::forecast(ourModel) Error in is.constant(y) : 'list' object cannot be coerced to type 'double'

smooth::forecast(ourModel) Error: 'forecast' is not an exported object from 'namespace:smooth'

greybox::forecast(ourModel) No Error it gives correct output

Also, I observed few points in here, but I am unaware of the reason:

  1. If the input data for the sma() function contains 0's in it, then forecast::forecast() do not work and throws this same error. Whereas the greybox::forecast() provides correct output.
  2. For the models built using smooth package functions we need to use greybox::forecast() for forecasting. Whereas for all the models built using forecast package functions we must use forecast::forecast() during forecasting.

So as per your suggestion my issue got resolved after using greybox::forecast(). Thanks for your help.

config-i1 commented 3 years ago

I'm glad that it worked out. But in the ideal world, you wouldn't need to do greybox::forecast(), it should work with just forecast(). So, just to check, what is the order of packages loaded in that case? Also, if you change the order, does it work?

For example:

library(forecast)
library(greybox)
library(smooth)
forecast(ourModel)

vs

library(greybox)
library(smooth)
library(forecast)
forecast(ourModel)
hjoshi92 commented 3 years ago

@config-i1,

We are loading smooth first and then forecast. We have many models been built from forecast package and in middle of the complete model building function we have sma() from smooth package. So for our case its recommended to use greybox:: or forecast::. We have not loaded greybox package but when smooth is loaded automatically greybox is loaded.

config-i1 commented 3 years ago

Thanks for the info.

I'm not sure why, but I cannot reproduce this on my PC. It works in that order (and in the other one as well). Do you also have fabletools installed? Is it loaded?

config-i1 commented 3 years ago

Hi @hjoshi92,

If possible, can you please check the most recent versions of greybox and smooth by installing them from github using:

remotes::install_github("config-i1/greybox")
remotes::install_github("config-i1/smooth")

Then let me know if the basic commands work (i.e. forecast(ourModel) instead of greybox::forecast(ourModel)).

config-i1 commented 2 years ago

This should be fixed now.