I would like to replace the exponential log likelihood
explik <- function(x) {
sum( dexp(data, 1/x, log=TRUE))
}
with the gamma negative log-likelihood
gammalike <- function(x) {
-sum(dgamma(data, shape=x, scale=2, log=TRUE))
}
in the following function and script
likeqn <- function(mu) {
muhat <- mean(data)
maxlik <- explik(muhat)
explik(mu) - maxlik + 0.5 * 3.841
}
source("explik.R")
source("likeqn.R")
# Input data
data <- c(63, 130, 88, 120, 330, 188, 270, 222, 189, 116)
# Find the MLE of an exponential distribution
muhat <- mean(data)
# Solve likelihood equation numerically to find likelihood based CI
vlikeqn <- Vectorize(likeqn)
CIlower <- uniroot(vlikeqn, c(10,muhat) )$root
CIupper <- uniroot(vlikeqn, c(muhat,500) )$root
cbind(CIlower, muhat, CIupper)
# Plot log-likelihood function
x <- seq(80, 350)
vexplik <- Vectorize(explik)
plot(x, vexplik(x), type="l", xlab=expression(mu), ylab="log-likelihood")
I have tried to replace the log-lik functions however the result does not seem reasonable.