Akhmad K
Akhmad K

Reputation: 11

Optimisation in R

I'm new to R and I have a problem with optimisation.

d <- c(0, 9.017, -9.017, 0, 9.017, 0, -8.579, -7.849, 0, 0, -7.849, 
    -9.017, 0, -7.849, -7.849, 0, 0, 0, 8.579, 1.168, 8.579, 8.579, 
    -7.849, 0.729, 8.579, 9.017, 0, -0.438)

x <- c(0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 
    1, 1, 0, 1, 1, 1, 0, 1)

log.like<-function (sigma){ 
    theta = pnorm(d,mean=0,sd=sigma) 
    logl = sum(log((theta ^ x) * ((1-theta)^(1-x))))
    -logl
}

optim(0,fn=log.like,method="L-BFGS-B",lower=0,upper=1)

It gives me the following error:

 Error in optim(0, fn = log.like, method = "L-BFGS-B", lower = 0, upper = 1) : 
  L-BFGS-B needs finite values of 'fn'

Upvotes: 1

Views: 1052

Answers (1)

Dason
Dason

Reputation: 62003

> log.like(0)
[1] Inf

You're using 0 as a starting value but that gives an infinite value for your function. This is why the function complains. Choose an actual appropriate starting value and it should work alright. I don't understand why you're setting an upper limit of 1 for the parameter though. You might want to increase that as well.

Upvotes: 5

Related Questions