MLE() 函数 -> if (!all(lower <= start & start <= upper)) { : missing value where TRUE/FALSE needed

问题描述 投票:0回答:1

我试图解决最大似然问题,但我似乎无法让 mle() 函数工作。它总是抱怨 optim() 以某种方式返回非有限值。我尝试将 theta 限制为 >0,以避免计算 log(0) 和负值的自然对数,但无济于事。出现上述错误。有谁知道为什么会发生这种情况?


library(stats4)


neg_loglik <- function(theta, x) {
  n <- length(x)
  a <- 4
  loglik <- n * log(theta) + n * theta * log(a) - (theta + 1) * sum(log(x))
  return(-loglik)
}

x <- c(4.37, 4.3, 5.15, 5.11, 5.15, 4.66, 6.15, 5.72, 5.87, 5.64, 4.05)

fit <- mle(minuslogl = neg_loglik, start = list(theta = 3.6), data = list(x=x), lower = c(theta = .Machine$double.xmin), upper = Inf, log = FALSE)
summary(fit)
theta_hat <- coef(fit)



q_hat <- qgamma(0.75, shape = theta_hat, scale = 1/4)


q_real <- qgamma(0.75, shape = 3.6, scale = 1/4)


desvio <- abs(q_hat - q_real) *100


resultado <- round(desvio, 4)

我也尝试更改 neg_loglik 函数,并将其替换为似然函数,然后再对其应用自然对数。因为,如果没有 ln() 应用于它,对于 theta 或 theta = 0 的负值,似然函数不应该计算 Inf、-Inf 或 NaNs,因为其中没有 log(theta) 的实例,所以我期望 optim不返回非有限值,但它仍然返回。

在第一个引用之前生成的错误:

“optim(start, f, method = method, hessian = TRUE, ...) 中的错误: optim 提供的非有限值”

r mathematical-optimization mle natural-logarithm
1个回答
0
投票
Error in if (!all(lower <= start & start <= upper)) { :
  missing value where TRUE/FALSE needed

可以通过为

x
的参数
neg_loglik
提供初始值来避免此错误,例如。 g.
fit <- mle(minuslogl = neg_loglik, start = list(theta = 3.6, x = 5), …

© www.soinside.com 2019 - 2024. All rights reserved.