JWW
JWW

Reputation: 81

Error in mle Estimation, even in simple example

I am trying to estimate a model with mle and therefore started with an simple example, which is also explained here:

Unfortunately, even when reproducing this code I always get the error: Error in optim(start, f, method = method, hessian = TRUE, ...) : initial value in 'vmmin' is not finite

My Code looks like

library(stats4)

N=100
x <- runif(N)
y <- 5 * x + 3 + rnorm(N)

LL <- function(beta0, beta1, mu, sigma) {
  R = y - x * beta1 - beta0
  R = dnorm(R, mu, sigma, log = TRUE)
  -sum(log(R))
}
fit <- mle(LL, start = list(beta0 = 5.9, beta1 = 2.8, sigma=1), fixed = list(mu=0))
fit

I derived the starting values from the linear ols model, but changing these a bit up- or down did not solve the error. Does anyone has an idea, whats wrong here?

Upvotes: 0

Views: 52

Answers (1)

user10307643
user10307643

Reputation:

You told dnorm to return the log of the probability with the option log = TRUE, then took again the log of the result. That's wrong: either remove the option, or better, remove the call to log afterwards.

It's better to keep the option because the values of dnorm can be very small, and rounded to zero if they don't fit the floating point numbers used by R, which will lead to log(0) (usually the minimum positive number is 2^-1074 or 4.940656e-324, whose log is "only" -744.4401). To account for this, you should write down the log of the likelihood, simplify the formula and only then code the resulting expression. That's basically what the option log = TRUE does for you.

Upvotes: 1

Related Questions