The loglikelihood function looks like this. I wrote to this a function in R, this return of negative one times the loglikelihood. I generated a vector with rWeibull with parameters shape=1.5, scale=0.5. But when i call the nlm function with my loglikelihood function, the estimated values for the parameters are: 2.124180 and 4.003675. What wrong with my R code? The code:
vec<-rWeibull(n=1000, params=list(shape=1.5, scale=0.5))
weibull_loglik<-function(parm){
n<-length(vec)
gamma<-parm[1]
lambda<-parm[2]
loglik<-n*log(gamma/lambda)+(gamma-1)*sum(vec-log((lambda^(gamma-1))))-sum((vec/lambda)^gamma)
return(-loglik)}
weibull<-nlm(weibull_loglik,parm<-c(1,1), hessian=TRUE)
weibull$estimate
Your log-likelihood is not correct. I get correct estimates when I use dweibull(..., log=TRUE)
:
vec <- rweibull(n=1000, shape=1.5, scale=0.5)
weibull_loglik <- function(parm){
n <- length(vec)
gamma <- parm[1]
lambda <- parm[2]
loglik <- sum(dweibull(vec, shape=gamma, scale=lambda, log=TRUE))
return(-loglik)
}
weibull <- nlm(weibull_loglik, p = c(1,1), hessian=TRUE)
> weibull$estimate
[1] 1.5547181 0.5116119