robjhyndman / forecast

Forecasting Functions for Time Series and Linear Models
http://pkg.robjhyndman.com/forecast
1.11k stars 341 forks source link

Information criteria (AIC or BIC) for hw() object is missing #895

Closed espher1987 closed 2 years ago

espher1987 commented 2 years ago

Hello to everyone and thanks for reading this I'm trying to fit a short time series with holtwinters method, so far i dont get problem with my model but AIC and BIC information is missing, but model can be fitted, maybe i'm missing something here. Please check this:

library(magrittr)
library(AER)
#> Loading required package: car
#> Loading required package: carData
#> Loading required package: lmtest
#> Loading required package: zoo
#> 
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#> 
#>     as.Date, as.Date.numeric
#> Loading required package: sandwich
#> Loading required package: survival
library(forecast)
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo

data("USMacroG")

model_a <- USMacroG[, 5] %>%
  window(
    x = ., start = c(1990, 1),
    end = c(1992, 4)
  ) %>%
  hw(h = 4)

summary(model_a)
#> 
#> Forecast method: Holt-Winters' additive method
#> 
#> Model Information:
#> Holt-Winters' additive method 
#> 
#> Call:
#>  hw(y = ., h = 4) 
#> 
#>   Smoothing parameters:
#>     alpha = 0.428 
#>     beta  = 0.8805 
#>     gamma = 1 
#> 
#>   Initial states:
#>     l = 5014.175 
#>     b = 4.7062 
#>     s = -18.375 18.525 12.425 -12.575
#> 
#>   sigma:  32.5718
#> Error measures:
#>                    ME    RMSE      MAE       MPE      MAPE      MASE       ACF1
#> Training set 8.675513 32.5718 21.26129 0.1691399 0.4139905 0.2414341 0.06516707
#> 
#> Forecasts:
#>         Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
#> 1993 Q1       5339.828 5298.086 5381.571 5275.989 5403.668
#> 1993 Q2       5330.953 5262.207 5399.699 5225.815 5436.091
#> 1993 Q3       5328.631 5214.281 5442.981 5153.747 5503.514
#> 1993 Q4       5463.459 5291.721 5635.198 5200.808 5726.111

AIC(model_a$model)
#> Warning in structure(object$loglik, df = length(object$par) + 1, class = "logLik"): Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
#>   Consider 'structure(list(), *)' instead.
#> numeric(0)
BIC(model_a$model)
#> Warning in structure(object$loglik, df = length(object$par) + 1, class = "logLik"): Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
#>   Consider 'structure(list(), *)' instead.
#> numeric(0)

if i remove the window(), all works fine:

USMacroG[, 5] %>%
  hw(h = 4) %>%
  summary()
#> 
#> Forecast method: Holt-Winters' additive method
#> 
#> Model Information:
#> Holt-Winters' additive method 
#> 
#> Call:
#>  hw(y = ., h = 4) 
#> 
#>   Smoothing parameters:
#>     alpha = 0.9588 
#>     beta  = 0.0523 
#>     gamma = 1e-04 
#> 
#>   Initial states:
#>     l = 1172.9373 
#>     b = 8.1072 
#>     s = 2.672 -0.5016 -0.4488 -1.7217
#> 
#>   sigma:  30.8205
#> 
#>      AIC     AICc      BIC 
#> 2493.433 2494.361 2523.296 
#> 
#> Error measures:
#>                    ME     RMSE      MAE       MPE      MAPE      MASE
#> Training set 3.533763 30.21014 21.01538 0.1191288 0.6564725 0.1929858
#>                     ACF1
#> Training set -0.01874399
#> 
#> Forecasts:
#>         Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
#> 2001 Q1       6675.493 6635.995 6714.992 6615.086 6735.901
#> 2001 Q2       6722.587 6666.416 6778.758 6636.681 6808.493
#> 2001 Q3       6768.363 6698.223 6838.503 6661.093 6875.633
#> 2001 Q4       6817.361 6734.524 6900.198 6690.673 6944.049

Created on 2021-11-22 by the reprex package (v2.0.1)

AIC or BIC cant be calculated in first model.

If i try to fit a ets, with window(), i get:

model_a <- USMacroG[, 5] %>%
  window(
    x = ., start = c(1990, 1),
    end = c(1992, 4)
  ) %>%
  ets(h = 4)
#> Error in ets(., h = 4): No model able to be fitted

So i don't know what happens!

Thanks for this amazing package!.

robjhyndman commented 2 years ago

When you use window() here, you end up with a data set of 12 observations. There are 8 degrees of freedom for the model, leaving only 4 degrees of freedom for the errors. The function is set to use a heuristic, non-optimized approach for the initial states whenever there are four or fewer degrees of freedom for the errors. This means that the likelihood is never computed (only the MSE), and so the AIC cannot be calculated.

espher1987 commented 2 years ago

does ets() and hw() use complete different methods? Why hw() fit a model but ets() don't? as far i understand, a hw model can be fitted using ets() function, so... is this the way should work?

robjhyndman commented 2 years ago

hw() usually calls ets() to fit a model. But if the data set is too small for the model to be estimated, it uses a heuristic approach instead.