我想使用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"发布于 2020-05-24 01:35:50
编辑以下Bolkers评论:
您可以单独传递参数,而不是作为向量传递,也可以将命名的向量作为输入传递:请参阅docs中的vecpar参数(并在负日志似然函数上使用parnames(nllfun) <- ... )。
传递个别参数:
# 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))传递参数的命名向量:
还请注意,在此示例中,使用日志链接强制参数大于零。在Ben的评论中,“我可能会推荐一个日志链接而不是框约束”--而不是在上面的示例中使用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函数应该返回负日志可能性”,而您没有返回,并且start参数应该是一个命名列表。
https://stackoverflow.com/questions/61980143
复制相似问题