威布尔样本上的 mle2

mle2 on Weibull sample

我想使用 mle2 函数生成 weibull 形状和比例参数的 mles。我写了下面的代码,但是得到了错误:

那么哪个组件是 NULL,我应该更改为数字?我获取mles的代码还有其他问题吗?

x2<- rweibull(n, shape = 1, scale = 1.5)
library(bbmle)
loglik2 <- function(theta, x){
  shape<- theta[1]
  scale<- theta[2]
  K<- length(theta)
  n<- length(x2)
  out<- rep(0,K)
  for(k in 1:K){
    out[k] <- sum(dweibull(x2, shape, scale, log=TRUE))   
  }
  return(out)
}
theta.start<- c(1, 1.4)
(mod <- mle2(loglik2,start=list(theta.start),data=list(x2)))
Error in validObject(.Object) : 
  invalid class “mle2” object: invalid object for slot "fullcoef" in class "mle2": got class "NULL", should be or extend class "numeric"

编辑以下 Ben Bolkers 评论:

您可以单独传递参数,而不是作为向量或 您可以改为传递命名向量作为输入:请参阅文档中的 vecpar 参数(并在负对数似然函数上使用 parnames(nllfun) <- ...)。

传递单个参数:

# some example data
library(bbmle)
set.seed(1)
n = 1000
x2 = rweibull(n, shape = 1, scale = 1.5)

将似然函数重写为return负LL

loglik2 = function(shape, scale, x)
  -sum(dweibull(x, shape=shape, scale=scale, log=TRUE))   

估计:命名启动参数(同时设置下限参数以避免警告)

mle2(loglik2, start=list(shape=1, scale=1),
     method="L-BFGS-B",lower=list(shape=0, scale=0),
     data=list(x=x2))
#Coefficients:
#   shape    scale 
#1.007049 1.485067 

# you can also use the formula notation 
mle2(x~dweibull(shape=shape, scale=scale),
     start=list(shape=1, scale=1),
     method="L-BFGS-B",lower=list(shape=0, scale=0),
     data=list(x=x2))

为参数传递命名向量:

另请注意,在此示例中,使用对数 link 强制参数大于零。来自 Ben 的评论“我可能会推荐一个 log-link 而不是框约束”——这不是在上面的例子中使用 lower 优化参数.

loglik2 = function(theta, x)
  -sum(dweibull(x, shape=exp(theta[1]), scale=exp(theta[2]), log=TRUE))   

# set the parameter names & set `vecpar` to TRUE
parnames(loglik2) = c("shape", "scale")
m = mle2(loglik2, 
         start=list(shape=0, scale=0), 
         data=list(x=x2), vecpar=TRUE)
exp(coef(m)) # exponentiate to get coefficients

# or the formula notation
mle2(x~dweibull(shape=exp(logshape),scale=exp(logscale)), 
     start=list(logshape=0, logscale=0),
     data=list(x=x2))

对您的代码的一些评论;来自 ?bblme 帮助页面: “注意 minuslogl 函数应该 return 负对数似然 ” 而你的函数没有,start 参数应该是一个命名列表。