我正在尝试使用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)
答案 0 :(得分:1)
感谢所有帮助过的人。
只是为了帮助将来偶然发现这篇文章的人:
正常,&#34; bbmle&#34;:
#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
正常,&#34; ExtDist&#34;:
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
制服,&#34; bbmle&#34;:
#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
统一,&#34; ExtDist&#34;:
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
答案 1 :(得分:0)
如果-n*log(1/(b-a))=n*log(b-a)
和a<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
和b
的最大似然估计值分别为min(x)
和max(x)
。
> min(x)
[1] 1.003692
> max(x)
[1] 2.956433