我尝试在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))
}
- 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)
2条答案
按热度按时间svmlkihl1#
如果你把rt作为固定参数去掉,它会起作用吗?
zlwx9yxi2#
您的变量
rt
可能是一个具有1列的矩阵(返回值),但fixed
参数似乎接受一个“类似于向量”的变量(抱歉,没有得到正确的术语)。更改函数中的以下行:
y <- rt[2:n,]
变为y <- rt[2:n,1]
mu <- rt[1:(n-1),] ...
变为mu <- rt[1:(n-1),1] ...
这很奇怪,别问我为什么。