我现在正在进行线性回归,其中误差项用普通随机变量和β随机变量分解(更多细节参见例如http://www.eea-esem.com/files/papers/eea-esem/2012/405/Cardak-Johnston-Martin_20111110.pdf)。问题是,当我在R中使用maxLik执行MLE时,它会返回错误并且进程停止。这是R中的代码:
#######################DGP################
library(maxLik)
set.seed(1357)
n=1000
x1 = matrix(rnorm(n,3,1.5),n,1)
x2 = matrix(rnorm(n,1,2.5),n,1)
x3 = sample(c(0,1), n, replace = TRUE)
X = cbind(1,x1,x2)
b = matrix(c(1,2,-3),1,3)
y = X%*%t(b)+rnorm(n,0,2)
g0 = 2
g1 = 1
g2 = 1.5
g3 = 2.4
#######################
p = exp(g0+g1*x3)
q = exp(g2+g3*x3)
#cbind(p,q)
eta = rep(0,n)
for (i in 1:n){
eta[i] = rbeta(1,shape1=p[i],shape2=q[i])}
plot(density(eta))
y2 = y+eta*2
summary(lm(y~X-1))
ols = lm(y2~X-1)
summary(ols)
#######################Model specification################
normbeta = function(u,nu,sigma2,p,q,k){
eta = (u-nu)/k
ff= dnorm(nu,0,sqrt(sigma2))*dbeta(eta,shape1=p,shape2=q)/k
return(ff)
}
obj = function(u,sigma2,p,q,k){
integrate(normbeta,u=u,lower=u-k,upper=u,sigma2=sigma2,p=p,q=q,k=k)$value
}
objv = Vectorize(obj)
############MLE#################
loglik = function(b){
a = split(b,rep(1:7,c(3,1,1,1,1,1,1)))
beta = a[[1]]
sigma2 = a[[2]]^2
k2 = a[[3]]^2
k=sqrt(k2)
g0 = a[[4]]
g1 = a[[5]]
g2 = a[[6]]
g3 = a[[7]]
p = exp(g0+g1*x3)
q = exp(g2+g3*x3)
u = y-X%*%beta
l = objv(u=u,sigma2=sigma2,p=p,q=q,k=k)
return(log(l))
}
a = list()
a[[1]] = coef(ols)
a[[2]] = sqrt(deviance(ols)/df.residual(ols))
a[[3]] = c(1,2,1,1.5,2.4)
b = unlist(a)
sum(loglik(b))
MLE = maxLik(logLik=loglik,print.level=2,start=b,method="BHHH")
summary(MLE)
报告错误:
----- Initial parameters: -----
fcn value: -2331.714
parameter initial gradient free
X1 2.042012 -338.04135 1
X2 1.953121 -1008.58628 1
X3 -2.969475 -310.82131 1
2.008750 212.29245 1
1.000000 -161.51965 1
2.000000 -75.76826 1
1.000000 -29.25573 1
1.500000 75.10844 1
2.400000 29.20714 1
Condition number of the (active) hessian: 1.657308e+14
Error in integrate(normbeta, u = u, lower = u - k, upper = u, sigma2 = sigma2, :
non-finite function value
In addition: Warning message:
In dbeta(eta, shape1 = p, shape2 = q) : NaNs produced
我认为错误可能来自dbeta(eta, shape1 = p, shape2 = q)
,β密度参数p和q应该是正数。但是我已经用exp()
函数约束了它们。谁知道这里发生了什么?非常感谢。