[R] probl with optimization

fc502 at york.ac.uk fc502 at york.ac.uk
Sun Apr 29 17:54:00 CEST 2007


hi everyone!

i've got a problem in optimizing the following function

fun <- function(theta){
 theta <- rbind(beta,lambda)
 fun <- sum(exp(h(beta)%*%lambda))
 fun
}

where h(beta) is

h <- function(beta,...){
 g1 <- matrix(0,b,M)
 g2 <- matrix(0,b,M)
 h1 <- matrix(0,b,1)
 h2 <- matrix(0,b,1)
 for(f in 1:M){
  for(F in 1:b){
   g1[F,f] <- exp(mu-beta*(z1[F,f]+z2[F,f])+3*z2[F,f])-1
   g2[F,f] <- z2[F,f]*(exp(mu-beta*(z1[F,f]+z2[F,f])+3*z2[F,f])-1)
  }
 }
 for(q in 1:b){
  h1[q] <- (1/M)*sum(g1[q,])
  h2[q] <- (1/M)*sum(g2[q,])
 }
 h <- matrix(0, b,2)
 h[,1] <- h1
 h[,2] <- h2
 h
}

i tried to optimize fun with optim and nlminb but i get an error:
Error in beta * (z1[F, f] + z2[F, f]) : 
        non-numeric argument to binary operator

any help will be extremely appreciated

best

federico



More information about the R-help mailing list