Stroring and extracting AICs from an ARIMA model using a nested loop

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

Stroring and extracting AICs from an ARIMA model using a nested loop

R devel mailing list
Hello
I am trying to extract AICs from an ARIMA estimation with different
combinations of p & q ( p =0,1,2,3
and q=0,1.2,3). I have tried using the following code unsucessfully. Can
anyone help?

code:
storage1 <- numeric(16)
for (p in 0:3){

    for (q in 0:3){
 
    storage1[p]  <- arima(x,order=c(p,0,q), method="ML")}
}
storage1$aic

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Stroring and extracting AICs from an ARIMA model using a nested loop

Rui Barradas
Hello,

You can solve the problem in two different ways.

1. Redefine storage1 as a matrix and extract the aic *in* the loop.

storage1 <- matrix(0, 4, 4)
for(p in 0:3){
   for(q in 0:3){
     storage1[p + 1, q + 1] <- arima(etc)$aic
   }
}


2. define storage1 as a list.

storage1 <- vector("list", 16)
i <- 0L
for(p in 0:3){
   for(q in 0:3){
     i <- i + 1L
     storage1[[i]] <- arima(etc)
   }
}

lapply(storage1, '[[', "aic")  # get the aic's.

Maybe sapply is better it will return a vector.


Hope this helps,

Rui Barradas




Às 06:23 de 03/02/20, ismael ismael via R-devel escreveu:

> Hello
> I am trying to extract AICs from an ARIMA estimation with different
> combinations of p & q ( p =0,1,2,3
> and q=0,1.2,3). I have tried using the following code unsucessfully. Can
> anyone help?
>
> code:
> storage1 <- numeric(16)
> for (p in 0:3){
>
>      for (q in 0:3){
>  
>      storage1[p]  <- arima(x,order=c(p,0,q), method="ML")}
> }
> storage1$aic
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Stroring and extracting AICs from an ARIMA model using a nested loop

R devel mailing list
It works!!!
Thank you so much for your help!

Sent from my iPhone

> On Feb 3, 2020, at 3:47 AM, Rui Barradas <[hidden email]> wrote:
>
> Hello,
>
> You can solve the problem in two different ways.
>
> 1. Redefine storage1 as a matrix and extract the aic *in* the loop.
>
> storage1 <- matrix(0, 4, 4)
> for(p in 0:3){
>  for(q in 0:3){
>    storage1[p + 1, q + 1] <- arima(etc)$aic
>  }
> }
>
>
> 2. define storage1 as a list.
>
> storage1 <- vector("list", 16)
> i <- 0L
> for(p in 0:3){
>  for(q in 0:3){
>    i <- i + 1L
>    storage1[[i]] <- arima(etc)
>  }
> }
>
> lapply(storage1, '[[', "aic")  # get the aic's.
>
> Maybe sapply is better it will return a vector.
>
>
> Hope this helps,
>
> Rui Barradas
>
>
>
>
> Às 06:23 de 03/02/20, ismael ismael via R-devel escreveu:
>> Hello
>> I am trying to extract AICs from an ARIMA estimation with different
>> combinations of p & q ( p =0,1,2,3
>> and q=0,1.2,3). I have tried using the following code unsucessfully. Can
>> anyone help?
>> code:
>> storage1 <- numeric(16)
>> for (p in 0:3){
>>     for (q in 0:3){
>>       storage1[p]  <- arima(x,order=c(p,0,q), method="ML")}
>> }
>> storage1$aic
>>    [[alternative HTML version deleted]]
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Stroring and extracting AICs from an ARIMA model using a nested loop

Martin Maechler
>>>>> ismael ismael via R-devel
>>>>>     on Mon, 3 Feb 2020 04:09:24 -0600 writes:

    > It works!!!
    > Thank you so much for your help!

and it was an "R help" question which  Rui  so generously answered.

But this is the R-devel mailing list.
Please do *NOT* misuse it for  R-help questions in the future:

These should go to the R-help mailing list instead!

Best,
Martin Maechler


    >> On Feb 3, 2020, at 3:47 AM, Rui Barradas <[hidden email]> wrote:
    >>
    >> Hello,
    >>
    >> You can solve the problem in two different ways.
    >>
    >> 1. Redefine storage1 as a matrix and extract the aic *in* the loop.
    >>
    >> storage1 <- matrix(0, 4, 4)
    >> for(p in 0:3){
    >> for(q in 0:3){
    >> storage1[p + 1, q + 1] <- arima(etc)$aic
    >> }
    >> }
    >>
    >>
    >> 2. define storage1 as a list.
    >>
    >> storage1 <- vector("list", 16)
    >> i <- 0L
    >> for(p in 0:3){
    >> for(q in 0:3){
    >> i <- i + 1L
    >> storage1[[i]] <- arima(etc)
    >> }
    >> }
    >>
    >> lapply(storage1, '[[', "aic")  # get the aic's.
    >>
    >> Maybe sapply is better it will return a vector.
    >>
    >>
    >> Hope this helps,
    >>
    >> Rui Barradas
    >>
    >>
    >>
    >>
    >> Às 06:23 de 03/02/20, ismael ismael via R-devel escreveu:
    >>> Hello
    >>> I am trying to extract AICs from an ARIMA estimation with different
    >>> combinations of p & q ( p =0,1,2,3
    >>> and q=0,1.2,3). I have tried using the following code unsucessfully. Can
    >>> anyone help?
    >>> code:
    >>> storage1 <- numeric(16)
    >>> for (p in 0:3){
    >>> for (q in 0:3){
    >>> storage1[p]  <- arima(x,order=c(p,0,q), method="ML")}
    >>> }
    >>> storage1$aic
    >>> [[alternative HTML version deleted]]
    >>> ______________________________________________
    >>> [hidden email] mailing list
    >>> https://stat.ethz.ch/mailman/listinfo/r-devel

    > ______________________________________________
    > [hidden email] mailing list
    > https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Stroring and extracting AICs from an ARIMA model using a nested loop

Rui Barradas
Hello,

Inline.

Às 11:04 de 03/02/20, Martin Maechler escreveu:

>>>>>> ismael ismael via R-devel
>>>>>>      on Mon, 3 Feb 2020 04:09:24 -0600 writes:
>
>      > It works!!!
>      > Thank you so much for your help!
>
> and it was an "R help" question which  Rui  so generously answered.
>
> But this is the R-devel mailing list.
> Please do *NOT* misuse it for  R-help questions in the future:
>
> These should go to the R-help mailing list instead!

Yes, and I had noticed it but then, after writing down the answer,
forgot to mention it.
Thanks for the warning.

Rui Barradas

>
> Best,
> Martin Maechler
>
>
>      >> On Feb 3, 2020, at 3:47 AM, Rui Barradas <[hidden email]> wrote:
>      >>
>      >> Hello,
>      >>
>      >> You can solve the problem in two different ways.
>      >>
>      >> 1. Redefine storage1 as a matrix and extract the aic *in* the loop.
>      >>
>      >> storage1 <- matrix(0, 4, 4)
>      >> for(p in 0:3){
>      >> for(q in 0:3){
>      >> storage1[p + 1, q + 1] <- arima(etc)$aic
>      >> }
>      >> }
>      >>
>      >>
>      >> 2. define storage1 as a list.
>      >>
>      >> storage1 <- vector("list", 16)
>      >> i <- 0L
>      >> for(p in 0:3){
>      >> for(q in 0:3){
>      >> i <- i + 1L
>      >> storage1[[i]] <- arima(etc)
>      >> }
>      >> }
>      >>
>      >> lapply(storage1, '[[', "aic")  # get the aic's.
>      >>
>      >> Maybe sapply is better it will return a vector.
>      >>
>      >>
>      >> Hope this helps,
>      >>
>      >> Rui Barradas
>      >>
>      >>
>      >>
>      >>
>      >> Às 06:23 de 03/02/20, ismael ismael via R-devel escreveu:
>      >>> Hello
>      >>> I am trying to extract AICs from an ARIMA estimation with different
>      >>> combinations of p & q ( p =0,1,2,3
>      >>> and q=0,1.2,3). I have tried using the following code unsucessfully. Can
>      >>> anyone help?
>      >>> code:
>      >>> storage1 <- numeric(16)
>      >>> for (p in 0:3){
>      >>> for (q in 0:3){
>      >>> storage1[p]  <- arima(x,order=c(p,0,q), method="ML")}
>      >>> }
>      >>> storage1$aic
>      >>> [[alternative HTML version deleted]]
>      >>> ______________________________________________
>      >>> [hidden email] mailing list
>      >>> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>      > ______________________________________________
>      > [hidden email] mailing list
>      > https://stat.ethz.ch/mailman/listinfo/r-devel
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Stroring and extracting AICs from an ARIMA model using a nested loop

R devel mailing list
In reply to this post by Rui Barradas
 
I am nowaware that I should not post this type of questions on this group. However, Iwould like to have some clarifications related to the response you've alreadyprovided. The code you provided yields accurate results, however I still haveissues grasping the loop process in case 1 & 2.

In case1, the use of "p+1" and "q+1" is still blurry tome? Likewise "0L" and " i + 1L" in case 2.

 

Can youplease provide explanations on the loop mechanisms you've used. 




    Le lundi 3 février 2020 à 03:47:20 UTC−6, Rui Barradas <[hidden email]> a écrit :  
 
 Hello,

You can solve the problem in two different ways.

1. Redefine storage1 as a matrix and extract the aic *in* the loop.

storage1 <- matrix(0, 4, 4)
for(p in 0:3){
  for(q in 0:3){
    storage1[p + 1, q + 1] <- arima(etc)$aic
  }
}


2. define storage1 as a list.

storage1 <- vector("list", 16)
i <- 0L
for(p in 0:3){
  for(q in 0:3){
    i <- i + 1L
    storage1[[i]] <- arima(etc)
  }
}

lapply(storage1, '[[', "aic")  # get the aic's.

Maybe sapply is better it will return a vector.


Hope this helps,

Rui Barradas




Às 06:23 de 03/02/20, ismael ismael via R-devel escreveu:

> Hello
> I am trying to extract AICs from an ARIMA estimation with different
> combinations of p & q ( p =0,1,2,3
> and q=0,1.2,3). I have tried using the following code unsucessfully. Can
> anyone help?
>
> code:
> storage1 <- numeric(16)
> for (p in 0:3){
>
>      for (q in 0:3){

>      storage1[p]  <- arima(x,order=c(p,0,q), method="ML")}
> }
> storage1$aic
>
>     [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
 
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Stroring and extracting AICs from an ARIMA model using a nested loop

Rui Barradas
Hello,

Don't worry, we've seen worst questions :).
Inline.

Às 13:20 de 04/02/20, ismael ismael escreveu:
> I am now aware that I should not post this type of questions on this
> group. However, I would like to have some clarifications related to the
> response you've already provided. The code you provided yields accurate
> results, however I still have issues grasping the loop process in case 1
> & 2.
>
> In case 1, the use of "p+1" and "q+1" is still blurry to me?

1. R indexes starting from 1, both your orders p and q are 0:3. So to
assign the results to the results matrix, add 1 and get indices 1:4.
You could also set the row and column names after, to make it more clear:

dimnames(storage1) <- list(paste0("p", 0:3), paste0("q", 0:3))

2. 0L is an integer, just 0 is a floating-point corresponding to the C
language double.

class(0L)   # "integer"
class(0)    # "numeric"

typeof(0L)  # "integer"
typeof(0)   # "double"

Indices are integers, so I used integers and added 1L every iteration
through the inner loop.

This also means that in point 1. I should have indexed the matrix with p
+ 1L and q + 1L, see the output of

class(0:3)


Hope this helps,

Rui Barradas

Likewise

> "0L" and " i + 1L" in case 2.
>
> Can you please provide explanations on the loop mechanisms you've used.
>
>
>
>
>
> Le lundi 3 février 2020 à 03:47:20 UTC−6, Rui Barradas
> <[hidden email]> a écrit :
>
>
> Hello,
>
> You can solve the problem in two different ways.
>
> 1. Redefine storage1 as a matrix and extract the aic *in* the loop.
>
> storage1 <- matrix(0, 4, 4)
> for(p in 0:3){
>    for(q in 0:3){
>      storage1[p + 1, q + 1] <- arima(etc)$aic
>    }
> }
>
>
> 2. define storage1 as a list.
>
> storage1 <- vector("list", 16)
> i <- 0L
> for(p in 0:3){
>    for(q in 0:3){
>      i <- i + 1L
>      storage1[[i]] <- arima(etc)
>    }
> }
>
> lapply(storage1, '[[', "aic")  # get the aic's.
>
> Maybe sapply is better it will return a vector.
>
>
> Hope this helps,
>
> Rui Barradas
>
>
>
>
> Às 06:23 de 03/02/20, ismael ismael via R-devel escreveu:
>  > Hello
>  > I am trying to extract AICs from an ARIMA estimation with different
>  > combinations of p & q ( p =0,1,2,3
>  > and q=0,1.2,3). I have tried using the following code unsucessfully. Can
>  > anyone help?
>  >
>  > code:
>  > storage1 <- numeric(16)
>  > for (p in 0:3){
>  >
>  >      for (q in 0:3){
>  >
>  >      storage1[p]  <- arima(x,order=c(p,0,q), method="ML")}
>  > }
>  > storage1$aic
>
>  >
>  >     [[alternative HTML version deleted]]
>  >
>  > ______________________________________________
>  > [hidden email] <mailto:[hidden email]> mailing list
>  > https://stat.ethz.ch/mailman/listinfo/r-devel
>
>  >

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Stroring and extracting AICs from an ARIMA model using a nested loop

R devel mailing list
I does help! Thank you for clarifications!


Sent from my iPhone

> On Feb 4, 2020, at 9:36 AM, Rui Barradas <[hidden email]> wrote:
>
> Hello,
>
> Don't worry, we've seen worst questions :).
> Inline.
>
> Às 13:20 de 04/02/20, ismael ismael escreveu:
>> I am now aware that I should not post this type of questions on this group. However, I would like to have some clarifications related to the response you've already provided. The code you provided yields accurate results, however I still have issues grasping the loop process in case 1 & 2.
>> In case 1, the use of "p+1" and "q+1" is still blurry to me?
>
> 1. R indexes starting from 1, both your orders p and q are 0:3. So to assign the results to the results matrix, add 1 and get indices 1:4.
> You could also set the row and column names after, to make it more clear:
>
> dimnames(storage1) <- list(paste0("p", 0:3), paste0("q", 0:3))
>
> 2. 0L is an integer, just 0 is a floating-point corresponding to the C language double.
>
> class(0L)   # "integer"
> class(0)    # "numeric"
>
> typeof(0L)  # "integer"
> typeof(0)   # "double"
>
> Indices are integers, so I used integers and added 1L every iteration through the inner loop.
>
> This also means that in point 1. I should have indexed the matrix with p + 1L and q + 1L, see the output of
>
> class(0:3)
>
>
> Hope this helps,
>
> Rui Barradas
>
> Likewise
>> "0L" and " i + 1L" in case 2.
>> Can you please provide explanations on the loop mechanisms you've used.
>> Le lundi 3 février 2020 à 03:47:20 UTC−6, Rui Barradas <[hidden email]> a écrit :
>> Hello,
>> You can solve the problem in two different ways.
>> 1. Redefine storage1 as a matrix and extract the aic *in* the loop.
>> storage1 <- matrix(0, 4, 4)
>> for(p in 0:3){
>>   for(q in 0:3){
>>     storage1[p + 1, q + 1] <- arima(etc)$aic
>>   }
>> }
>> 2. define storage1 as a list.
>> storage1 <- vector("list", 16)
>> i <- 0L
>> for(p in 0:3){
>>   for(q in 0:3){
>>     i <- i + 1L
>>     storage1[[i]] <- arima(etc)
>>   }
>> }
>> lapply(storage1, '[[', "aic")  # get the aic's.
>> Maybe sapply is better it will return a vector.
>> Hope this helps,
>> Rui Barradas
>> Às 06:23 de 03/02/20, ismael ismael via R-devel escreveu:
>> > Hello
>> > I am trying to extract AICs from an ARIMA estimation with different
>> > combinations of p & q ( p =0,1,2,3
>> > and q=0,1.2,3). I have tried using the following code unsucessfully. Can
>> > anyone help?
>> >
>> > code:
>> > storage1 <- numeric(16)
>> > for (p in 0:3){
>> >
>> >      for (q in 0:3){
>> >
>> >      storage1[p]  <- arima(x,order=c(p,0,q), method="ML")}
>> > }
>> > storage1$aic
>> >
>> >     [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > [hidden email] <mailto:[hidden email]> mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-devel
>> >

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel