Search code examples
rintegralgmm

GMM in R with integral of normal CDF


I'm trying to run a GMM estimation which includes the integration of the normal CDF, for which parameters exist in both the function and the integration interval. The gist of the codes as follows:

g1 <- function(b,x){
  e <- b[1] +b[2]*x$x1
  r <- e + b[3]*x$x2
  n <- b[4]+b[5]*x$x3
  ncdf <- function(z){
    return((pnorm((log(z)-b[6]*log(e))/(b[7]/x$x4)))^n)
  }
  m1 <- x$y-integrate(ncdf,-Inf,r)/ncdf(r)
  f <- cbind(m1)
  return(f)
}

init = rep(0,7)

res<-gmm(g1,bids,init)

For slight variations, I'm either getting

Error in integrate(ncdf, -Inf, r) : 'bound' must be of length one 

or

Error in integrate(ncdf, -Inf, r) : a limit is NA or NaN

Help would be appreciated. Thanks in advance.


Solution

  • So I was able to resolve it by using Vectorize():

    g1 <- function(b,x){
      e <- b[1] +b[2]*x$x1
      r <- e + b[3]*x$x2
      n <- b[4]+b[5]*x$x3
      ncdf <- function(a,b,c,d){
        return((pnorm((a-b)/c))^d)
      }
    
      intn <- function(z){
        return(integrate(ncdf,-Inf,z,b=m,c=s,d=n)$value)
      }
    
      vint <- Vectorize(intn)
    
      d <- b[6]*x$x6
      s <- b[7]*x$x7
    
      m1 <- x$y-vint(r)/ncdf(r,m,s,n)
      f <- cbind(m1)
      return(f)
    }