[R] NA return to NLM routine

Mac Gaulin gaulinmp at gmail.com
Fri Nov 30 23:55:56 CET 2012


Hello,

I am trying to understand a small quirk I came across in R. The
following code results in an error:

k <- c(2, 1, 1, 5, 5)
f <- c(1, 1, 1, 3, 2)
loglikelihood <- function(theta,k,f){
    if( theta<1 && theta>0 )
        return(-1*sum(log(choose(k,f))+f*log(theta)+(k-f)*log(1-theta)))
    return(NA)
}
nlm(loglikelihood ,0.5, k, f )

Running this code results in:
Error in nlm(logliklihood, 0.5, k, f) :
  invalid function value in 'nlm' optimizer

However if the line return(NA) is changed to return(-NA) or even
return(1*NA) or return(1/0), the code works. Is this expected
behavior? The nlm help file says not NA or not NaN are acceptable
values but I don't understand what that means.

Thank you for your time,
Mac Gaulin



More information about the R-help mailing list