我想使用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 useparnames(nllfun) <- ...
on your negative log-likelihood function).传递各个参数:
重写似然函数以返回负LL
估算:命名启动参数(还设置了较低的参数限制以避免警告)
为参数传递命名向量:
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.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 thestart
parameters should be a named list.