5
\$\begingroup\$

Consider the following reproducible example:

# note that lh is a standard ts dataset that ships with R 
lh
# fit an R model   
ar.mle<-ar(lh,method="mle")

# now get the min AIC, this is the relevant line:
ar.mle$aic[ar.mle$aic==min(ar.mle$aic)]

This works fine and gives back the smallest AIC value and it's index, which is the suggested AR order. I feel I am repeating myself in this last line of code. Is there an easier way to obtain index and value? I know I could use partial autocorrelations to determine the level, too. This is not a stats question, but an R indexing question.

\$\endgroup\$
1
  • \$\begingroup\$ Try which([blahblah], arr.ind=TRUE) \$\endgroup\$ Commented Aug 13, 2011 at 14:14

2 Answers 2

13
\$\begingroup\$

perhaps the function which.min() would do the trick?

which.min(ar.mle$aic)

it won't shorten your code all that much:

ar.mle$aic[which.min(ar.mle$aic)]
\$\endgroup\$
1
  • \$\begingroup\$ nice start tim :) \$\endgroup\$
    – ran2
    Commented Aug 14, 2011 at 7:38
1
\$\begingroup\$

Well, it really doesn't return an index and value, what you get is a named value.

> ans <- ar.mle$aic[which.min(ar.mle$aic)]
> names(ans)
[1] "3"

The index itself is actually 4 (and is an integer); R indexes from 1.

> which.min(ar.mle$aic)
3
4
> which.min(ar.mle$aic) == 4
   3
TRUE

(yes, this idea of tagging names to values is rather weird, coming from any other language)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.