[英]I would like to replace the exponential function with the gamma function in calculating the 95% likelihood based confidence interval in R
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在以下 function 和脚本中
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.我试图替换 log-like 函数,但结果似乎不合理。
The distribution Expo(lambda) with rate lambda, is Gamma(1, 1/lambda) (shape-scale).具有速率 lambda 的分布 Expo(lambda) 是 Gamma(1, 1/lambda)(形状尺度)。
gammalike <- function(x) {
sum(dgamma(data, shape=1, scale=x, log=TRUE))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.