首先,我使用“rmutil”包来模拟双泊松分布的数据。与泊松分布不同的是,双泊松分布允许过度离散和欠离散,其中均值和方差不一定相等。
这个链接展示了双泊松分布的函数: http://ugrad.stat.ubc.ca/R/library/rmutil/html/DoublePoisson.html 我已经模拟出了一个大小为500的数据集。
以下是参数的真实值:
mean(x) #平均数 [1] 10.986 mean(x)/var(x) #离散度 [1] 0.695784
为了通过MLE获得参数,我使用nlminb函数来最大化对数似然函数。 对数似然函数由“rmutil”包中双分布的密度函数形成。
这个链接展示了双泊松分布的函数: http://ugrad.stat.ubc.ca/R/library/rmutil/html/DoublePoisson.html 我已经模拟出了一个大小为500的数据集。
set.seed(10)
library("rmutil")
nn = 500 #size of data
gam = 0.7 #dispersion parameter
mu = 11
x <- rdoublepois(nn, mu, gam)
head(x)
[1] 11 9 10 13 6 8
mean(x) #mean
mean(x)/var(x) #dispersion
以下是参数的真实值:
mean(x) #平均数 [1] 10.986 mean(x)/var(x) #离散度 [1] 0.695784
为了通过MLE获得参数,我使用nlminb函数来最大化对数似然函数。 对数似然函数由“rmutil”包中双分布的密度函数形成。
logl <- function(par) {
mu.new <- par[1]
gam.new <- par[2]
-sum(ddoublepois(x, mu.new, gam.new, log=TRUE))
}
nlminb(start = c(0.1,0.1), lower = 0, upper = Inf, logl)
出现错误:
ddoublepois(x, mu.new, gam.new)中的错误:s必须为正数
因此,我尝试了另一种方法,输入了双泊松分布函数的方程。
logl2 <- function(par) {
mu.new <- vector() #mean
gam.new <- vector() #dispersion
ddpoi <- vector()
for (i in 1:nn){
ddpoi[i] <- 0.5*log(gam.new[i])-gam.new[i]*mu.new[i]
+x[i]*(log(x[i])-1)-log(factorial(x[i]))
+(gam.new[i])*x[i]*(1+log(mu.new[i]/x[i]))
}
-sum(ddpoi)
}
nlminb(start = c(0.1,0.1), lower = 0, upper = Inf, logl2)
输出结果:
nlminb(start = c(0.1,0.1), lower = 0, upper = Inf, logl2)
$par
[1] 0.1 0.1
$objective
[1] Inf
$convergence
[1] 0
$iterations
[1] 1
$evaluations
function gradient
2 4
$message [1] "X-convergence (3)"
很明显,估计参数为0.1(与初始值相同)表明此代码失败了。
有谁能告诉我如何正确进行双泊松分布的最大似然估计?
提前感谢。