I need your help. I am having problems to estimate the parameters of the following data in R using optim. I am always getting a error message of the the following type:
Warning: NaNs producedError in optim(par = c(0, 1, -2), nll, method = "L-BFGS-B", upper = c(Inf, :
L-BFGS-B needs finite values of 'fn'
Could you please take a look and tell what i am doing wrong? Thanks
The code I am using is the following:
X <- rlnorm3(n=1000, meanlog = 0, sdlog = 1, threshold = -2)
nll <- function(theta) {
N <- length(X)
m <- theta[1]
s <- theta[2]
a <- theta[3]
e_1 <- log(X - a) - m
e_2 <- log(X - a)
nll <- -0.5 * N * log(2 * pi) - N * log(s) - (t(e_1) %*% e_1) / 2 * s ^ 2 - (t(e_2) %*% e_2)
return(-nll)
}
optim(par=c(0, 1, -2), nll, method="L-BFGS-B", upper = c(Inf, Inf, min(X)))