【问题标题】:Error in using optim to maximise the likelihood in r使用 optim 来最大化 r 中的可能性的错误
【发布时间】:2016-07-20 16:37:46
【问题描述】:

所以,我有这些功能:

funk1 <- function(a,x,l,r) {
x^2*exp(-(l*(1-exp(-r*a))/r))}

funk2 <- function(x,l,r) {
sapply(x, function (s) {
integrate(funk1, lower = 0, upper = s, x=s, l=l, r=r)$value })}  

用来解释y中的数据,

z <- data.frame(ts = 1:100,
            y = funk2(1:100, l = 1, r = 1) + rpois(100, 1:100))

我希望使用 optim 来最大化似然性,所以我定义了似然函数:

LL_funk <- function(l,r) { 
n=nrow(z)
R = sum((funk2(ts,l,r) - y)^2)
logl = -((n/2)*log(R))
return(-logl)
} 

我尝试使用 optim 来适应

fit <- optim(par=c(0.5,0.5), fn= LL_funk, method="Nelder-Mead")

但我得到一个错误:

 Error in integrate(funk1, lower = 0, upper = s, x = s, l = l, r = r) : 
 a limit is missing 

我不知道为什么?我可以运行 nls 拟合 funk2(x,l,r) 到 y

nls(y ~ funk2(ts,l,r), data = z, start = list(l = 0.5, r = 0.5))

这意味着 funk2 正在工作。我想这是我设计的 LL 函数的问题,我无法弄清楚!请帮忙!

【问题讨论】:

    标签: r mathematical-optimization non-linear-regression mle


    【解决方案1】:

    是的!您的功能有两个问题。这对我有用:

    LL_funk <- function(params) { 
      n=nrow(z)
      l = params[1]
      r = params[2]
      R = sum((funk2(z$ts,l,r) - z$y)^2)
      logl = -((n/2)*log(R))
      return(-logl)
    }
    

    以前的问题:

    • LL_funk 只接受 1 个参数,即参数向量。
    • 在分配 Rtsy 的 LHS 中,实际上并未引用数据集中的列。

    【讨论】:

    • 非常感谢!不知道 LL 函数只接受一个参数。第二个问题是拼写错误。
    • @ZheyuanLi 是的,我应该开始这样做,非常小心。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-25
    • 1970-01-01
    相关资源
    最近更新 更多