# [R] combining arima and ar function

R. Michael Weylandt michael.weylandt at gmail.com
Thu Dec 1 22:10:21 CET 2011

```I think your attachment got scrubbed so I can't verify this, but i
think the difficulty is that the "inner" apply returns a whole set of
orders, instead of just one and that throws arima off: would this
work?

getOrder <- function(x) ar(na.omit(x), method = "mle")\$order

all.equal(apply(TSX, 2, getOrder), apply(TSX,2, function(x)
ar(na.omit(x),method="mle")\$order) # Verify it works.

apply(TSX,2,function(x)arima(x,order=c(getOrder(x),0,0))\$residuals

Michael

On Thu, Dec 1, 2011 at 4:57 AM, Samir Benzerfa <benzerfa at gmx.ch> wrote:
> Hi everyone
>
>
>
> I've got a problem regarding the arima() and the ar() function for
> autoregressive series. I would simply like to combine them.
>
>
>
> To better understand my question, I first show you how I'm using these two
> functions individually (see file in the attachement).
>
>
>
> 1)      apply(TSX,2, function(x) ar(na.omit(x),method="mle")\$order
> # this function finds the optimal (autoregressive) order for each series
> (briefly speaking: for each series I get a specific number)
>
> 2)      apply(TSX,2,function(x)arima(x,order=c(1,0,0))\$residuals
> # this function puts an autoregressive model of order 1 on each series
>
>
>
>
>
> What I'd like to do, is to combine them, such that the resulting orders
> (numbers) from the first function are used as orders in the second function.
> So in the specific example attached the results from the first function are
> 6,5,1 and these numbers should be used as the order in function two. I tried
> to simply put the two functions together as follows:
>
>
>
> apply(TSX,2,function(x) arima(x,order=c((apply(TSX,2,
> function(x)ar(na.omit(x),method="mle")\$order))),0,0))
>
>
>
> However, I get the error message " 'order' must be a non-negative numeric
> vector of length 3".
>
>
>
> Any hints? I hope you can help me with this issue.
>
>
>
> Many thanks and best regards, S.B.
>
>
>
>
>
>
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help