R错误:当尝试使用max估计时,OPTIM中的Hessian出错。可能性

时间:2014-03-26 12:54:48

标签: r optimization mle hessian-matrix

我是一个R noob,可能会反映在不那么密集的代码中 - 所以请耐心等待。 我正在尝试使用max来估计二元正态分布的系数。可能性估计。调用OPTIM函数时,我收到与Hessian相关的错误。我已经尝试了很多调试,但似乎无法摆脱错误。非常感谢您对如何解决这个问题的任何见解。

我使用的数据是{y1,y2,x1,x2},其中y1,y2是二进制变量。我用来模拟数据的代码如下:

x1=rnorm(1000)*2+3
x2=rnorm(1000)-0.5

mu=c(0,0)
sigma=array(c(1,0.5,0.5,1),c(2,2)) # correlation matrix
e=mvrnorm(n = 1000, mu, sigma) #MASS package


z1=1+0.5*x1+x2+e[,1]
y1=1*(z1>=0)

z2=0.8+0.3*x1+1.2*x2+e[,2]
y2=1*(z2>=0)

我想要估计的参数是潜在效用函数z1和z2中的beta,以及方差 - 协方差矩阵中的非对角线元素。

谢谢!

我首先指定错误,然后在错误后提供代码:

首先出现在代码中这一行的错误:

mle = optim(theta.start,logl,x=x,y1=y1,y2=y2,hessian=T)  #Error@Here.  

A)如果我在调用OPTIM的参数中设置了hessian = F,我会得到以下错误和回溯:

Error in array(x, c(length(x), 1L), if (!is.null(names(x))) list(names(x),  : 
  'data' must be of a vector type, was 'NULL' 

6 array(x, c(length(x), 1L), if (!is.null(names(x))) list(names(x), 
    NULL) else NULL) 
5 as.matrix.default(a) 
4 as.matrix(a) 
3 solve.default(mle$hessian) 
2 solve(mle$hessian) 
1 mle.reg(fmla, bvprobitdata) 

B)如果我在调用OPTIM的参数中设置hessian = T,我会得到以下错误和回溯:

Error in solve.default(mle$hessian) : 
  Lapack routine dgesv: system is exactly singular: U[1,1] = 0

3 solve.default(mle$hessian) 
2 solve(mle$hessian) 
1 mle.reg(fmla, bvprobitdata)

现在代码:

 # MLE Estimation of Bivariate Normal with correlation.

    require(Formula)
require(pbivnorm)
#Get probit data
bvprobitdata <- read.csv("/Users/...../yhbi_probitdata.csv", header = TRUE)
head(bvprobitdata,10)

#Bivariate Normal Estimation using MLE

mle.reg = function(fmla,data) {

   # Define the negative log likelihood function
    logl <- function(theta,x,y1,y2){
    y1 <- y1
    y2 <- y2
    x <- x

    #Id <- rep(1,1000)
    #x <- as.matrix(cbind(Id,x1,x2))
    beta1 <- matrix(theta[1:3],3,1)
    beta2 <- matrix(theta[4:6],3,1)
    ro <- theta[7]

    # Calculate CDFs
    temp1 <- as.matrix(cbind((x%*%beta1),(x%*%beta2))) # Create a matrix of the two cross products
    bvCDF <- pbivnorm(temp1,rho=ro) # Bivariate CDF
    xb1CDF <- pnorm(x%*%beta1)   
    Negxb1CDF <- pnorm(-(x%*%beta1))

    # Calculate Log Likelihood  - Temporarily commented out to focus debugging error in Hessian in OPTIM.
    #llik <- y1*y2*bvCDF + y1(1-y2)*log(xb1CDF-bvCDF) + (1-y1)*log(Negxb1CDF) #Calc log likelihood    
    #loglik <- sum(llik) # Sum up the log likelihoods for each observation.    
    #return(-loglik) # -ve Since OPTIM minimizes and we want to maximize loglikelihood.

    return(100)
    }


  # Prepare the data
  fml <- model.frame(fmla, data =data)
  fml
  outcome1 = rownames(attr(terms(fmla),"factors"))[1]
  outcome2 = rownames(attr(terms(fmla),"factors"))[2]
  head(data,10)
  print(outcome2)
  dfrTmp = model.frame(data)

  y1 = as.numeric(as.matrix(data[,match(outcome1,colnames(data))]))
  y2 = as.numeric(as.matrix(data[,match(outcome2,colnames(data))]))
  x = as.matrix(model.matrix(fmla, data=dfrTmp))

  # Define initial values for the parameters
  theta.start = cbind(1,1,1,1,1,1,0.5)
  # Assign names to the parameters
  names(theta.start)[1] = "b10"
  names(theta.start)[2] = "b11"
  names(theta.start)[3] = "b12"
  names(theta.start)[4] = "b20"
  names(theta.start)[5] = "b21"
  names(theta.start)[6] = "b22"
  names(theta.start)[7] = "ro"


  # Calculate the maximum likelihood
  mle = optim(theta.start,logl,x=x,y1=y1,y2=y2,hessian=T)  #Error@Here.  
  out = list(beta=mle$par,vcov=solve(mle$hessian),ll=2*mle$value)
}

print("before call")
fmla <- Formula(y1 | y2 ~x1+x2) #Create model formula
mlebvprobit = mle.reg(fmla,bvprobitdata) #Estimate coefficients for probit 
print("after call")
mlebvprobit

2 个答案:

答案 0 :(得分:0)

您是否尝试过分析可能性? 通常,当参数数量不足时会发生这种情况, 所以粗麻布不能倒置或变性。

答案 1 :(得分:0)

这有点延迟,但你想取负粗麻布的对角线的平方根,因为你的渔信息矩阵会给你模型的SSE。

sqrt(solve(-mle$hessian))