我正在尝试使用包优化似然函数maxLik
,如下所示。
library(fExtremes)
theta0 <- c(0.1, 1)
theta1 <- c(0.1, 2)
set.seed(20150518)
X <- matrix(0, nrow=1000, ncol=1000)
for(i in 1:1000){
X[i, ] <- c(rgpd(900, xi=-theta0[1], beta=theta0[2]),
rgpd(100, xi=-theta1[1], beta=theta1[2]))
}
library(maxLik)
loglik <- function(param){
shape <- param[1]
scale <- param[2]
sum(dgpd(x, xi=-shape, beta=scale, log=TRUE))
}
scale.mle <- rep(0, 1000)
for(i in 1:1000){
x <- X[i, ]
scale.mle[i] <- as.numeric(maxLik(logLik=loglik, start=c(theta0[1], theta0[2]), fixed=1)$estimate[2])
}
但是,我不断收到以下错误消息:
Error in maxNRCompute(fn = logLikAttr, fnOrig = fn, gradOrig = grad, hessOrig = hess, :
NA in the initial gradient
我该如何解决?甚至可能吗?还有什么其他R
功能可以用来优化这个?