【问题标题】:Difference between log likelihood by hand and logLike function手动对数似然和 logLike 函数之间的区别
【发布时间】:2021-04-07 01:43:59
【问题描述】:

我正在尝试比较 logLik 函数给出的对数似然函数的值和手动计算 Gamma 分布的值。 logLik函数给出的值是:

require(fitdistrplus)

x = rgamma(50,shape = 2, scale = 10)
Gamma_fitdist = fitdist(x,"gamma")
logLik(Gamma_fitdistr)
-189.4192

对于“手动”的对数似然函数是:

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 函数给我一个不同的值?谢谢!

【问题讨论】:

    标签: r statistics log-likelihood


    【解决方案1】:

    您已经颠倒了比例和形状,并且您的代码中有几个符号错误。

    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
    

    【讨论】:

      猜你喜欢
      • 2018-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-29
      • 2013-02-09
      • 2014-06-08
      • 2020-03-25
      • 2017-07-12
      相关资源
      最近更新 更多