[R] how to avoid NaN in optim()

Berend Hasselman bhh at xs4all.nl
Thu Sep 30 21:16:33 CEST 2010



arindam fadikar wrote:
> 
> 
> loglik <- function(par)
>   {
>       p=par[1]
>       q=par[2]
>       r <- 1 - p - q
>       if (c(p,q,r) > rep(0,3) && c(p,q,r) < rep(1,3) )
>         {
>           -(2 * nO * log (r) + nA * log (p^2 + 2 * p * r)
>             + nB * log (q^2 + 2 * q * r)
>             + nAB * (log(2) +log(p) +log(q)))
>        }
>       else
>         NA
>   }
> loglik
> }
> .....
> 

Extending the tests in the if in loglik to 

      if (c(p,q,r) > rep(0,3) && c(p,q,r) < rep(1,3) && (p^2 + 2*p*r)>0 &&
(q^2 + 2*q*r)>0) 

would also help.

/Berend

-- 
View this message in context: http://r.789695.n4.nabble.com/how-to-avoid-NaN-in-optim-tp2738093p2746635.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list