patternpythonModerate
Is there a better way to get the index of a minimum?
Viewed 0 times
thewayminimumbettergetindexthere
Problem
Consider the following reproducible example:
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.
# 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.
Solution
perhaps the function which.min() would do the trick?
it won't shorten your code all that much:
which.min(ar.mle$aic)it won't shorten your code all that much:
ar.mle$aic[which.min(ar.mle$aic)]Code Snippets
which.min(ar.mle$aic)ar.mle$aic[which.min(ar.mle$aic)]Context
StackExchange Code Review Q#4083, answer score: 13
Revisions (0)
No revisions yet.