[英]Difference between log likelihood by hand and logLike function
我正在嘗試比較 logLik function 給出的對數似然 function 的值和手動計算 Gamma 分布的值。 logLik function給出的值是:
require(fitdistrplus)
x = rgamma(50,shape = 2, scale = 10)
Gamma_fitdist = fitdist(x,"gamma")
logLik(Gamma_fitdistr)
-189.4192
對於對數似然 function “手動”是:
gmll <- function(scale,shape,datta){
a <- scale
b <- shape
n <- length(datta)
sumd <- sum(datta)
sumlogd <- sum(log(datta))
gmll <- n*a*log(b) + n*lgamma(a) + sumd/b - (a-1)*sumlogd
gmll
}
gmll(scale = 10, shape = 2, datta = x)
-246.6081
為什么 logLik function 給我一個不同的值? 謝謝!
您已經顛倒了比例和形狀,並且您的代碼中有幾個符號錯誤。
library(fitdistrplus)
set.seed(666)
x = rgamma(50, shape = 2, scale = 4)
Gamma_fitdist = fitdist(x,"gamma")
logLik(Gamma_fitdist)
# -150.3687
gmll <- function(scale,shape,datta){
a <- shape
b <- scale
n <- length(datta)
sumd <- sum(datta)
sumlogd <- sum(log(datta))
-n*a*log(b) - n*lgamma(a) - sumd/b + (a-1)*sumlogd
}
rate <- Gamma_fitdist$estimate[["rate"]]
shape <- Gamma_fitdist$estimate[["shape"]]
gmll(scale = 1/rate, shape = shape, datta = x)
# -150.3687
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.