R语言 如何在使用MLE函数时消 debugging 误

uqxowvwt  于 2023-05-11  发布在  其他
关注(0)|答案(2)|浏览(132)

我尝试在r中使用mle函数运行MLE。它工作正常,但我突然遇到了以下错误。有人能帮忙吗?

Error in if (!all(lower[isfixed] <= fixed[isfixed] & fixed[isfixed] <=  : 
  missing value where TRUE/FALSE needed

为了给予你更多的背景知识,下面是我的代码:
1.负似然函数

likelihood.func <- function(theta, kappa, sigma, rt){

  n <- NROW(rt)  
  y <- rt[2:n,]
  dt <- 1/12  
  mu <- rt[1:(n-1),]* exp(-kappa*dt) + theta* (1- exp(-kappa*dt))  
  sd <- sqrt((sigma^2)*(1 - exp(-2*kappa*dt))/(2*kappa))  
  pdf_yt <- dnorm(y, mu, sd, log=FALSE)  
  
  -sum(log(pdf_yt))  
}
  1. MLE命令
theta.est <- 0.04
kappa.est <- 0.5
sigma.est <- 0.02

library(stats4)
bound.lower <- parameters.est * 0.1
bound.upper <- parameters.est * 2

est.mle<-mle(likelihood.func, start= list(theta=theta.est, kappa=kappa.est, sigma=sigma.est),
method="L-BFGS-B", lower = bound.lower, upper = bound.upper, fixed=list(rt = rt))

summary(est.mle)
svmlkihl

svmlkihl1#

如果你把rt作为固定参数去掉,它会起作用吗?

#Here I make a function that uses the likelihood function's first three parameters

LL2 = function(theta, kappa, sigma){
  likelihood.func(theta,kappa,sigma,rt)
}

#Then I use it in my mle
est.mle <- mle(minuslogl = LL2, 
               start = list(theta=theta.est, kappa=kappa.est, sigma=sigma.est), 
               method = "L-BFGS-B", 
#               fixed = list(rt = rt2),
               lower = bound.lower, 
               upper = bound.upper
               )
zlwx9yxi

zlwx9yxi2#

您的变量rt可能是一个具有1列的矩阵(返回值),但fixed参数似乎接受一个“类似于向量”的变量(抱歉,没有得到正确的术语)。
更改函数中的以下行:
y <- rt[2:n,]变为y <- rt[2:n,1]
mu <- rt[1:(n-1),] ...变为mu <- rt[1:(n-1),1] ...
这很奇怪,别问我为什么。

相关问题