Search for best ARIMA model

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Search for best ARIMA model

Schweitzer, Markus
Hello,

I have a several time series, which I would like to check for their best
fitted Arima model (I am checking for the lowest aic value).
Which lets me raise two questions:

1) is there are more efficient way, than using 6 for-loops?
2) sometimes the system cannot calculate  with given parameters - is
there a more efficient solution than I found?

I hope, you can help me to make this calculation quicker since I have to
run this function 450 times...
Thank you very much in advance,

Markus


arima.estim <- function(TS) {
        best.model <- arima(TS, order = c(1, 0, 0), seasonal =
list(order = c(0, 0, 0), period = frequency(TS)) )

# Start value
# I continue with brute force- p, q, r, s are nested from 0 to 3 and i
and j are nested from 0 to 2. p and  q are not both allowed to be 0.

for (p in 0:3){
  for( q in 0:3){
    if(p==0 && q==0) {}
      else {
        for(r in 0:3) {
          for(s in 0:3) {
            for (j in 0:2) {
              for(i in 0:2) {
     
# test, if series works
    if(inherits(try(arima(TS, order = c(p, i, q), seasonal = list(order
= c(r,  j, s), period = frequency(TS)) ), TRUE), 'try-error')){
       
        print(c(p,i,q))} #shows, which parameters didn't work -> will be
removed by
       
         else    {
                 tmp <- arima(TS, order = c(p, i, q), seasonal =
list(order = c(r,  j, s), period = frequency(TS)))     # calculate again
:(

          if(best.model$aic > tmp$aic)
          {
                  best.model <- tmp
          }
              }
                 }
                 }    
                 }
                 } } } }
                 
                 best.model}

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Search for best ARIMA model

Spencer Graves
      I just got 166 hits from RSiteSearch("best fit arima") and 86 from
RSiteSearch("best fit arima").  Have you tried that?

      If that does not get you what you want, I might try 'expand.grid'
plus some hand massage if necessary to create the list of alternative
models I wanted to consider.  Then I might add a column "AIC" of NAs to
that using "cbind" to create a data.frame with all the alternatives with
the result.  Then I might start with something like "fit0 <- arima(lh,
order = c(0,0,0))" and use 'update(fit0, order=...)' to evaluate each
one, storing only the AIC.  Then 'which(aic==max(aic))' would identify
the best fitting alternative(s).  If you haven't already, I suggest you
review Venables and Ripley (2002) Modern Applied Statistics with S, 4th
ed. (Springer) on 'expand.grid', 'cbind', and 'update';  if you don't
already have this book, I highly recommend it.

      Hope this helps.
      Spencer Graves

Schweitzer, Markus wrote:

> Hello,
>
> I have a several time series, which I would like to check for their best
> fitted Arima model (I am checking for the lowest aic value).
> Which lets me raise two questions:
>
> 1) is there are more efficient way, than using 6 for-loops?
> 2) sometimes the system cannot calculate  with given parameters - is
> there a more efficient solution than I found?
>
> I hope, you can help me to make this calculation quicker since I have to
> run this function 450 times...
> Thank you very much in advance,
>
> Markus
>
>
> arima.estim <- function(TS) {
> best.model <- arima(TS, order = c(1, 0, 0), seasonal =
> list(order = c(0, 0, 0), period = frequency(TS)) )
>
> # Start value
> # I continue with brute force- p, q, r, s are nested from 0 to 3 and i
> and j are nested from 0 to 2. p and  q are not both allowed to be 0.
>
> for (p in 0:3){
>   for( q in 0:3){
>     if(p==0 && q==0) {}
>       else {
>         for(r in 0:3) {
>           for(s in 0:3) {
>             for (j in 0:2) {
>               for(i in 0:2) {
>      
> # test, if series works
>     if(inherits(try(arima(TS, order = c(p, i, q), seasonal = list(order
> = c(r,  j, s), period = frequency(TS)) ), TRUE), 'try-error')){
>        
> print(c(p,i,q))} #shows, which parameters didn't work -> will be
> removed by
>        
> else    {
>         tmp <- arima(TS, order = c(p, i, q), seasonal =
> list(order = c(r,  j, s), period = frequency(TS)))     # calculate again
> :(
>
>           if(best.model$aic > tmp$aic)
>           {
>                   best.model <- tmp
>           }
>               }
>                  }
>                  }    
>                  }
>                  } } } }
>                  
>                  best.model}
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Search for best ARIMA model

esmetaman
In reply to this post by Schweitzer, Markus
Hi, to create best ARIMA model, I use the package forecast.

You can search it in the following URL:

http://www-personal.buseco.monash.edu.au/~hyndman/Rlibrary/forecast/

You should use the method "best.arima"

further information, here: http://www-personal.buseco.monash.edu.au/~hyndman/Rlibrary/forecast/forecast/best.arima.html

Best Regards.

Schweitzer, Markus wrote
Hello,

I have a several time series, which I would like to check for their best
fitted Arima model (I am checking for the lowest aic value).
Which lets me raise two questions:

1) is there are more efficient way, than using 6 for-loops?
2) sometimes the system cannot calculate  with given parameters - is
there a more efficient solution than I found?

I hope, you can help me to make this calculation quicker since I have to
run this function 450 times...
Thank you very much in advance,

Markus


arima.estim <- function(TS) {
        best.model <- arima(TS, order = c(1, 0, 0), seasonal =
list(order = c(0, 0, 0), period = frequency(TS)) )

# Start value
# I continue with brute force- p, q, r, s are nested from 0 to 3 and i
and j are nested from 0 to 2. p and  q are not both allowed to be 0.

for (p in 0:3){
  for( q in 0:3){
    if(p==0 && q==0) {}
      else {
        for(r in 0:3) {
          for(s in 0:3) {
            for (j in 0:2) {
              for(i in 0:2) {
     
# test, if series works
    if(inherits(try(arima(TS, order = c(p, i, q), seasonal = list(order
= c(r,  j, s), period = frequency(TS)) ), TRUE), 'try-error')){
       
        print(c(p,i,q))} #shows, which parameters didn't work -> will be
removed by
       
         else    {
                 tmp <- arima(TS, order = c(p, i, q), seasonal =
list(order = c(r,  j, s), period = frequency(TS)))     # calculate again
:(

          if(best.model$aic > tmp$aic)
          {
                  best.model <- tmp
          }
              }
                 }
                 }    
                 }
                 } } } }
                 
                 best.model}

        [[alternative HTML version deleted]]

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.