我编写了以下 R 函数,我需要在两种情况下使用 MLE 估算参数。它在下面给出。
案例1:选择theta1=1
,找到3个参数:lambda, p, theta2
案例2:选择theta1=1, p=1
,找到2个参数:lambda, theta2
我已复制下面的 R 代码。
x <- rpois(100, 5)
MLE <- function(x, p, theta1, theta2, lambda){
numerator <- p*theta1*theta2*dpois(x, lambda)*ppois(x, lambda)^(theta1 -1)*(1-ppois(x, lambda)^theta1)^(theta2-1)
denominator <- (1 - (1-p)*((1-ppois(x, lambda)^theta1)^theta2))^2
result = numerator/denominator
return(result)
}
你能说出来吗?非常感谢你。
我还写了另一个代码来解决上述问题。但它给了我一个错误。我也在下面复制了这段代码。
theta2 <- 1
p <- 1
x <- rpois(200, lambda = 4)
pfun <- function(lambda, theta1){
n <- 200
x.poi <- n*log(p*theta1*theta2) + (theta1-1)*sum(log(ppois(x, lambda)))+
(theta2-1)*sum(log(1-ppois(x,lambda)^theta1))+sum(log(dpois(x,lambda)))-
2*sum(log(1-(1-p)*(1-ppois(x,lambda)^theta1)^theta2))
}
out = optim(par=c(100,10), fn=pfun, data=x, maximum = T)
out
est1 <- mle(minuslog=pfun, start = list(lambda = 2, theta1 = 1),method = "BFGS")
summary(est1)
我用以下方式重写了上面的代码。但它仍然会产生一些错误。我在下面复制了它们。我真的很感激这方面的任何建议。
在这里,我修复了参数 p = 1 并估算了其他三个参数,例如 theta1,theta2,lambda 。
library(maxLik)
extendDistr <- function(x, p, theta1, theta2, lambda, log = FALSE) {
numerator <- p * theta1 * theta2 * dpois(x, lambda) * ppois(x,
lambda)^(theta1 - 1) * (1 - ppois(x, lambda)^theta1)^(theta2 -
1)
denominator <- (1 - (1 - p) * ((1 - ppois(x, lambda)^theta1)^theta2))^2
pdf <- numerator/denominator
if (log)
pdf <- log(pdf)
return(pdf)
}
set.seed(650)
x <- rpois(100, 2)
llf <- function(param) {
theta1 <- param[1]
theta2 <- param[2]
lambda <- param[3]
return(sum(extendDistr(x, p = 1, theta1 = theta1, theta2 = theta2,
lambda = lambda, log = TRUE)))
}
ml <- maxLik(llf, start = c(theta1 = 1, theta2 = 1, lambda = 1))
print(summary(ml))
--------------------------------------------
Maximum Likelihood estimation
Newton-Raphson maximisation, 8 iterations
Return code 3: Last step could not find a value above the current.
Boundary of parameter space?
Consider switching to a more robust optimisation method temporarily.
Log-Likelihood: 115.1142
3 free parameters
Estimates:
Estimate Std. error t value Pr(> t)
theta1 2.59598 NA NA NA
theta2 0.08531 0.00853 10 <2e-16 ***
lambda 0.02565 NA NA NA
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
--------------------------------------------
Warning messages:
1: In sqrt(diag(vc)) : NaNs produced
2: In sqrt(diag(vc)) : NaNs produced