Jacob Feldman
Jacob Feldman

Reputation: 1

Error Using Optim Command in R

logl = function(theta, x, y)
{
b0 <- theta[1]
b1 <- theta[2]
sig.hat <- theta[3] 
b2 <- theta[4]
n = length(x)
logl <- .5*n*log(2*pi)-.5*n*(sig.hat) - (1/(2*(exp(sig.hat))))*sum((y - (b0+b1*x+b2*(x^2))^2))
}
theta.start = c(.015,.02,3,1)
optim(theta.start, fn=logl, x=x, y=y, control=list(fnscale=-1), hessian = T)

This gives me an error:

Error in optim(theta.start, fn = logl, x = x, y = y, control = list(fnscale = -1),  : 
  non-finite finite-difference value [1]

I got this error before when I accidentally had R try to take the log of a negative number, but now I can't tell what the issue is?

Upvotes: 0

Views: 117

Answers (1)

Feng Mai
Feng Mai

Reputation: 3109

When you set fnscale = -1 it becomes a maximization problem. A quick glance of the objective function seems to suggest that your problem is unbounded, i.e. you can keep increasing b0 to increase logl.

Upvotes: 3

Related Questions