-1

I want to write an R-code to compute the MLEs of (µ; σ2; λ) try initial values λ = 0.5、.... I'm trying to run the following code in R, but I'm getting an error I'm not sure what part of the formula is incorrect. Any help would be greatly appreciated.

library(stats4)
obs.loglik4 = function(y,theta)
{
  mu=theta[1]
  sig2=theta[2]
  lambda = theta[3]
  n = length(y)
  loglik = -0.5*n*log(2*pi)-0.5*n*log(sig2)-
    sum(((y^lambda-1)/lambda-mu)^2)/(2*sig2)+(lambda-1)*sum(log(y))
  return(loglik)
}
mle.4=mle(minuslogl=obs.loglik4,start=c(1,1,0.5))
##Error in mle(minuslogl = obs.loglik4, start = c(1, 1, 0.5)) : 
  Mismatch in length of start values
mle.4=mle(minuslogl=obs.loglik4,start=list(mu=mean(y),sig2=var(y),lambda=0.5))
##Error in l2v(start) : 
  some named values are not arguments to the supplied log-likelihood
user367987
  • 11
  • 3

1 Answers1

2

The log likelihood function should be a function only of the variables you are looking to estimate. I wasn't going to debug your likelihood function, but here is a modification with printing of intermediate results.

library(stats4)
y <-c(0.15, 0.09, 0.18, 0.10, 0.05, 0.12)
nobs <- length(y)
obs.loglik4 = function(mu,sig2,lambda)
{
  n = length(y)
  loglik = -0.5*n*log(2*pi)-0.5*n*log(sig2)-
    sum(((y^lambda-1)/lambda-mu)^2)/(2*sig2)+(lambda-1)*sum(log(y))
  cat( mu, sig2, lambda, loglik, "\n", sep=", ")
  return(loglik)
}
mle.4=mle(minuslogl=obs.loglik4,
          start=list(mu=1,sig2=1, lambda=0.5), nobs=nobs, 
          lower=list(sig2=0))

I had to limit sig2 to be greater than zero, but it ends up at zero anyway and fails.

PhilShea
  • 21
  • 2