1

我正在尝试使用 R 找到给定均匀分布 X ~ UNIF(1,3) 的最大似然估计量 a_hat 和 b_hat。下面是我的代码及其输出:

##Example: Uniform Distribution
x<-runif(100,1,3)
n<-length(x)
ll<-function(a,b){

  -sum(1/(b-a)^n,log=TRUE)

}
m0<-mle2(ll,start=list(a=1,b=2))
summary(m0)

> summary(m0)

最大似然估计

Call:
mle2(minuslogl = ll, start = list(a = 1, b = 2))

Coefficients:
Estimate Std. Error z value Pr(z)
a   1.5159         NA      NA    NA
b   1.4841         NA      NA    NA

-2 log L: -1.542595e+150 
Warning message:
In sqrt(diag(object@vcov)) : NaNs produced 

我无法解释为什么我的系数与原始值如此偏离。我很确定我正在使用正确的似然函数来进行均匀分布,但我可能在某个地方的语法也错了。我在用library(bbmle)

4

2 回答 2

2

感谢所有帮助过的人。

  1. 我联系了我的教授,结果发现我不能将“bbmle”用于不可区分的分布。
  2. 在这种情况下,log(constant=1/ba) 不可微分以获得最大值。
  3. 还有另一个名为“ ExtDist ”的 R 包,它为所有分布(到目前为止对我来说,包括统一)输出 MLE 非常好,但不提供它们的标准错误,事实上“bbmle”确实如此

只是为了帮助将来可能偶然发现这篇文章的任何人:

正常,“bbmle”:

#Comparison of mentioned packages
  #Example for normal distribution
  set.seed(123)
  library("bbmle")
  x<-rnorm(100,1,3) #mean=1, sd = 3
  n<-length(x)
  ll<-function(a,b){
    -sum(dnorm(x,a,b,log=TRUE)) 
  }
  m0<-mle2(ll,start=list(a=1,b=2))
  summary(m0)

结果:

Maximum likelihood estimation

 Call:
 mle2(minuslogl = ll, start = list(a = 1, b = 2))

 Coefficients:
   Estimate Std. Error z value     Pr(z)    
 a  1.27122    0.27247  4.6655 3.079e-06 ***
 b  2.72473    0.19267 14.1421 < 2.2e-16 ***
 ---
 Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

 -2 log L: 484.2609 

正常,“ExtDist”:

library("ExtDist")
  m1<-eNormal(X=x,method = "unbiased.MLE")
  m1

结果:

 Parameters for the Normal distribution. 
 (found using the  unbiased.MLE method.)

  Parameter     Type Estimate      S.E.
       mean location 1.271218 0.2738448
         sd    scale 2.738448 0.1946130

统一,“bbmle”:

#Example for uniform distribution
  set.seed(123)
  x<-runif(100,1,3) #minimum =1, maximum = 3
  range(x)  #To know beforehand the original minimum and maximum before the      package estimates
 [1] 1.00125 2.98854
  n<-length(x)
  ll<-function(a,b){ 
    -sum(dunif(x,a,b,log=TRUE))   
  }
 m3<-mle2(ll,start=list(a=1,b=2))
 Error in optim(par = c(1, 2), fn = function (p)  : 
   initial value in 'vmmin' is not finite
  summary(m3)

错误信息:

 Error in optim(par = c(1, 2), fn = function (p)  : 
 initial value in 'vmmin' is not finite

统一,“ExtDist”:

 m4<-eUniform(X=x,method = "unbiased.MLE")
  m4

 Parameters for the Uniform distribution. 
 (found using the  numerical.MLE method.)

  Parameter     Type Estimate
          a boundary 1.001245
          b boundary 2.988544
于 2017-11-16T18:38:31.687 回答
1

负对数似然是-n*log(1/(b-a))=n*log(b-a)ifa<min(x)b>max(x)。如果不满足这些约束条件,则可能性为0

您可以使用以下命令指定约束method = "L-BFGS-B"

library(bbmle)
x <- runif(100,1,3)
n <- length(x)
ll <- function(a,b){
  n*log(b-a)
}

m0 <- mle2(ll, start=list(a=0, b=4), 
           lower=c(a=-Inf, b=max(x)), upper=c(a=min(x), b=Inf), 
           method="L-BFGS-B")

你得到:

Warning message:
In mle2(ll, start = list(a = 0, b = 4), lower = c(a = -Inf, b = max(x)),  :
  some parameters are on the boundary: variance-covariance calculations based on Hessian may be unreliable


> m0

Coefficients:
       a        b 
1.003692 2.956433 

Log-likelihood: -66.92 

结果是正确的。和的最大似然估计分别是a和。bmin(x)max(x)

> min(x)
[1] 1.003692
> max(x)
[1] 2.956433
于 2017-11-14T09:51:16.163 回答