Weibull样本上的mle2

我想使用mle2函数为魏布尔形状和比例参数生成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的以下评论:

You can pass the parameters individually rather than as a vector or you can pass a named vector as input instead: see the vecpar argument in the docs (and use parnames(nllfun) <- ... on your negative log-likelihood function).

传递各个参数:

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

重写似然函数以返回负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))

为参数传递命名向量:

Also note in this example that the parameters are forced to be greater than zero by using a log link. From Ben's comment "I would probably recommend a log-link rather than box constraints" -- this is instead of using the lower optimisation parameter in the above example.

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 function form
mle2(x~dweibull(shape=exp(logshape),scale=exp(logscale)), 
     start=list(logshape=0, logscale=0),
     data=list(x=x2))

A couple of comments on your code; from ?bblme help page: "Note that the minuslogl function should return the negative log-likelihood" which yours didn't, and the start parameters should be a named list.

点赞
评论