Re: [R] NAs in optim

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Mon 23 Jan 2006 - 18:02:14 EST

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")
>
> Thank you in advance.
> Hannu Kahra
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

-- 
Brian D. Ripley,                  ripley@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

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Mon Jan 23 18:14:57 2006

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:42:09 EST