# [R] NAs in optim

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Jan 23 08:02:14 CET 2006

```This is NAs in your own code, not in optim!  Please use a truthful
subject.

Consider the use of sum(na.rm=TRUE) rather than your inner loop.

On Sun, 22 Jan 2006, Hannu Kahra wrote:

> Hi,
>
> I am trying to maximize a utility function using optim. I have a problem
> with optim, since some of the values in the caw, mom, me and btm matrixes in
> the code bellow are missing. Is there a handy way just to skip the missing
> values in the loop?
>
> g <- 5
> retp <- NULL
> object <- function (x)
> {
>    b1 <- x[1]
>    b2 <- x[2]
>    b3 <- x[3]
>    for(i in 1:nrow(ret)){
>        for(j in 1:ncol(ret)){
>        retp[i] <- (caw[i,j]+1/24*(b1*mom[i,j] + b2*me[i,j] +
> b3*btm[i,j]))*ret[i,j]
>        }
>    }
>    util <- (1+retp)^(1-g)/(1-g)
>    return(-sum(util))
> }
> result <- optim(c(2.0,-1.0,3.5
> ),object,control=list(maxit=500),hessian=TRUE,method="BFGS")
>
> Hannu Kahra
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
>

--
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

```