sum(na.rm=TRUE) works and gives a solution, but in that case I will loose
observations, since if any caw[i,j], mom[i,j] or me[i,j] is a NA, retp[i]
will be a NA in the following

retp[i] <- (caw[i,j]+1/24*(b1*mom[i,j] + b2*me[i,j] + b3*btm[i,j]))*ret[i,j]

caw[i,j] is the weight of stock j at date i in a benchmark portfolio;  b1,
b2 and b3 are the coefficients to be estimated; mom[i,j], me[i,j] and
btm[i,j]  are  the characteristics  of  stock  j  at  date  i; and ret[i,j]
is the return on stock j at date i. Rather than estimating one weight for
each stock, I estimate weights as a single function of characteristics that
applies to all 24 stocks.

Prof. Ripley is right; I have NAs in my data using optim, not in optim
itself.

On 1/23/06, Prof Brian Ripley <ripley@stats.ox.ac.uk> wrote:
>
> 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
>

	[[alternative HTML version deleted]]


