PacktPublishing / Hands-On-Time-Series-Analysis-with-R

Hands-On-Time-Series-Analysis-with-R
MIT License
104 stars 88 forks source link

arima_search error - hessian setting? #1

Open zjachc opened 2 years ago

zjachc commented 2 years ago

Hi Rami,

Onto the right repo.

Here's the block from the script: My suspicion is that the problem is derived from hessian = TRUE.

data(USgas)

ts_plot(USgas, title = "US Monthly Natural Gas consumption", Ytitle = "Billion Cubic Feet", Xtitle = "Year")

USgas_split <- ts_split(USgas, sample.out = 12)

train <- USgas_split$train test <- USgas_split$test

par(mfrow=c(1,2)) acf(train, lag.max = 60) pacf(train, lag.max = 60)

USgas_d12 <- diff(train, 12)

ts_plot(USgas_d12, title = "US Monthly Natural Gas consumption - First Seasonal Difference", Ytitle = "Billion Cubic Feet (First Difference)", Xtitle = "Year")

USgas_d12_1 <- diff(diff(USgas_d12, 1))

ts_plot(USgas_d12_1, title = "US Monthly Natural Gas consumption - First Seasonal and Non-Seasonal Differencing", Ytitle = "Billion Cubic Feet (Difference)", Xtitle = "Year")

par(mfrow=c(1,2)) acf(USgas_d12_1, lag.max = 60) pacf(USgas_d12_1, lag.max = 60)

p <- q <- P <- Q <- 0:2

arima_grid <- expand.grid(p,q,P,Q) names(arima_grid) <- c("p", "q", "P", "Q") arima_grid$d <- 1 arima_grid$D <- 1

arima_grid$k <- rowSums(arima_grid)

library(dplyr)

arima_grid <- arima_grid %>% filter(k <= 7)

arima_search <- lapply(1:nrow(arima_grid), function(i){ md <- NULL md <- arima(train, order = c(arima_grid$p[i], 1, arima_grid$q[i]), seasonal = list(order = c(arima_grid$P[i], 1, arima_grid$Q[i])))

results <- data.frame(p = arima_grid$p[i], d = 1, q = arima_grid$q[i], P = arima_grid$P[i], D = 1, Q = arima_grid$Q[i], AIC = md$aic) }) %>% bind_rows() %>% arrange(AIC)

Error in optim(init[mask], armafn, method = optim.method, hessian = TRUE, : non-finite finite-difference value [2] Called from: optim(init[mask], armafn, method = optim.method, hessian = TRUE, control = optim.control, trans = as.logical(transform.pars))

zjachc commented 2 years ago

Also - when I change the arima method to "CSS" the AIC comes back NA but it does work.

RamiKrispin commented 2 years ago

Hi @zjachc

This error related to the ARIMA's optimization algorithm and the version of the TSstudio package:

ARIMA's optimization algorithm

The reason you are getting this error is related to ARIMA's optimization algorithm that cannot converge into a result, this typically will occur for ARIMA models with higher-order (i.e., k - the sum of q, p, d, Q, P, D). In the example above the grid table is set to k <= 7, you may want to change it k<7 and it should work:

arima_grid <- arima_grid %>% filter(k <= 6)

arima_search <- lapply(1:nrow(arima_grid), function(i){
  md <- NULL
  md <- arima(train, 
              order = c(arima_grid$p[i], 1, arima_grid$q[i]), seasonal = list(order = c(arima_grid$P[i], 1, arima_grid$Q[i])))

  results <- data.frame(p = arima_grid$p[i], d = 1, q = arima_grid$q[i],
                        P = arima_grid$P[i], D = 1, Q = arima_grid$Q[i],
                        AIC = md$aic)
}) %>% bind_rows() %>% arrange(AIC)

TSstudio version

On a side note, what version of TSstudio package are you using? The version that is used in the book is 0.1.4 which may affect the results as the USgas series gets updated on every release (most recent version - 0.1.6) and may trigger different results. To install the version used in the book:

remotes::install_version(package = "TSstudio", version = "0.1.4")

Hope this will fix it, Rami

zjachc commented 2 years ago

Hi Rami,

Thank you very much for the help!

I'm using 0.1.6 for TSstudio, but will download the archived and give it a shot.

Best,

Zach

chathuracse commented 12 months ago

I am new to ARIMA modelling.

I have two issues in SARIMA case study up to now.

  1. I wonder USgas_d12_1 <- diff(diff(USgas_d12, 1)) gives the first-order non-seasonal differencing or second-order non-seasonal differencing since diff() has been used twice.
  2. k has been defined as 7. How this information can be derived because p,q and P,Q lag orders are not yet known? According to my understanding grid method is used to determine the p,q,P and Q orders.

Thank you,

Chathura