R/heck5twosteprobVcov.R

Defines functions heck5twosteprobVcov

Documented in heck5twosteprobVcov

heck5twosteprobVcov <-
function(y1vec, y2vec, x1Matr, x2Matr, eststage1, eststage2, eststage2sigma, weights = rep(1,nrow(y1vec)), t.c = 1.345)
{
  n <- sum(y1vec==0)
  sumheck <- 0 #
  for(i in 1:n)  # sandwich estimator of the first term
  {
    sumheck=sumheck+PsiMest(x2Matr[i,],y2vec[i],eststage2,eststage2sigma,t.c,weights[i])%*%t(PsiMest(x2Matr[i,],y2vec[i],eststage2,eststage2sigma,t.c,weights[i]))
  }
  
  s1 = matrix(0,length(eststage2)-1,length(eststage1$coeff)) 
  s2 = 0   
  for(i in 1:n)
  {
    if((y2vec[i]-t(x2Matr[i,])%*%as.vector(eststage2))/eststage2sigma<(-t.c))
    {
      s1=s1 + 0
      s2=s2 - t.c*weights[i]*drop(dLambdadSM5(x1Matr[i,],eststage1$coeff))*(x1Matr[i,])
    }
    else if(((y2vec[i]-t(x2Matr[i,])%*%as.vector(eststage2)))/eststage2sigma>(t.c))
    {
      s1=s1 + 0
      s2=s2 + t.c*weights[i]*drop(dLambdadSM5(x1Matr[i,],eststage1$coeff))*(x1Matr[i,])
    }
    else {
      s1=s1 + (x2Matr[i,1:dim(x2Matr)[2]-1])%*%t(x1Matr[i,])*eststage2[dim(x2Matr)[2]]*weights[i]*drop(dLambdadSM5(x1Matr[i,],eststage1$coeff))/eststage2sigma
      s2=s2 + (x2Matr[i,dim(x2Matr)[2]]*eststage2[dim(x2Matr)[2]])*weights[i]*c(dLambdadSM5(x1Matr[i,],eststage1$coeff))/eststage2sigma*x1Matr[i,]
    }
  }
  xdx <- rbind(s1,s2)
  term2 <- xdx%*%vcov(eststage1)%*%t(xdx)  
  M2inv <- solve(MmatrM(x2Matr, y2vec, eststage2, eststage2sigma, t.c, weights))
  result <- M2inv%*%(sumheck+term2)%*%M2inv   # asymptotic variance for Heckman-M-estimator
  return(result)
}

Try the ssmrob package in your browser

Any scripts or data that you put into this service are public.

ssmrob documentation built on Aug. 20, 2021, 5:08 p.m.