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.

asked Aug 12, 2011 at 20:40
\$\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)]
answered Aug 12, 2011 at 20:49
\$\endgroup\$
1
  • \$\begingroup\$ nice start tim :) \$\endgroup\$ 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)

answered Aug 12, 2011 at 20:56
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.