
####################################################
# LATE estimation under attrition and non-response # 
####################################################

# Function "attrition.late": Semiparametric LATE estimation based on probit specifications for the propensity scores
############################################
# INPUT
# y1: outcome in period 1
# y2: outcome in period 2
# d: binary treatment
# z: binary instrument
# x0 baseline covariates (pre-instrument)
# x1: covariates in period 1 (may include outcomes of period 1)
# r1: binary response indicator for period 1
# r2: binary response indicator for period 2
# weightmax: Trimming rule based on the maximum relative weight a single observation may obtain in estimation - observations with higher weights are discarded 
#
# OUTPUT
# lateuncor1: LATE without attrition correction in period 1
# lateuncor2: LATE without attrition correction in period 2
# late1: LATE based on Theorem 1 (LI) in period 1
# late2: LATE based on Theorem 1 (LI) in period 2
# late1t: LATE based on Theorem 1 (LI) in period 1 with trimming
# late2t: LATE based on Theorem 1 (LI) in period 2 with trimming
# latemar1: LATE based on Theorem 2 (MAR) in period 1
# latemar2: LATE based on Theorem 2 (MAR) in period 2
# latemar1t: LATE based on Theorem 2 (MAR) in period 1 with trimming
# latemar2t: LATE based on Theorem 2 (MAR) in period 2 with trimming

# Function:
attrition.late<-function(y1,y2,d,z,x0,x1,r1,r2, weightmax=0.1){
pz<-fitted.values(glm(z~x0,family=binomial(probit)))
temp1<-glm(r1~cbind(x0,d),family=binomial(probit))
pr1<-pnorm(cbind(1,x0,1)%*%(temp1$coef))
temp2<-glm(r2~cbind(x0,x1,r1,d),family=binomial(probit))
pr2<-pnorm(cbind(1,x0,x1,1,1)%*%(temp2$coef))
temp3<-glm(z~cbind(x0,d),family=binomial(probit))
pz0<-pnorm(cbind(1,x0,1)%*%(temp3$coef))
temp4<-glm(z~cbind(x0,r1,d),family=binomial(probit))
pz01<-pnorm(cbind(1,x0,1,1)%*%(temp4$coef))
temp5<-glm(z~cbind(x0,x1,r1,d),family=binomial(probit))
pz1<-pnorm(cbind(1,x0,x1,1,1)%*%(temp5$coef))
temp6<-glm(z~cbind(x0,x1,r2,d),family=binomial(probit))
pz12<-pnorm(cbind(1,x0,x1,1,1)%*%(temp6$coef))

temp7<-glm(r1~cbind(x0,z,d),family=binomial(probit))
pr1z1<-pnorm(cbind(1,x0,1,1)%*%(temp7$coef))
pr1z0<-pnorm(cbind(1,x0,0,1)%*%(temp7$coef))

temp8<-glm(r2~cbind(x0,x1,r1,z,d),family=binomial(probit))
pr2z1<-pnorm(cbind(1,x0,x1,1,1,1)%*%(temp8$coef))
pr2z0<-pnorm(cbind(1,x0,x1,1,0,1)%*%(temp8$coef))


ggg=(mean(d/pz*(z-pz)/(1-pz)))
weightz1<- (r1*d*z*1/(pz*pr1*((pz01-pz)/(pz0-pz))*ggg))
relweightz1<-weightz1/sum(weightz1)
indz1a<-1-(relweightz1>weightmax)

weightz0<-(r1*d*(1-z)*1/((1-pz)*pr1*((pz01-pz)/(pz0-pz))*ggg))
relweightz0<-weightz0/sum(weightz0)
relweightz0[is.na(relweightz0)]=0
indz0a<-1-(relweightz0>weightmax) 

y11<-(y1*r1*(d/pz)*(z-pz)/(1-pz)*(1/pr1)*((pz0-pz)/(pz01-pz)) )*(1/mean(d/pz*(z-pz)/(1-pz)))
y11t<-mean(y11[(indz1a*indz0a)==1])
y11<-mean(y11)

weightz1<- (r2*r1*d*z*1/(pz*pr1*((pz01-pz)/(pz0-pz))*pr2*((pz12-pz)/(pz1-pz))*ggg))
relweightz1<-weightz1/sum(weightz1)
indz1a<-1-(relweightz1>weightmax)

weightz0<-(r2*r1*d*(1-z)*1/((1-pz)*pr1*((pz01-pz)/(pz0-pz))*pr2*((pz12-pz)/(pz1-pz))*ggg))
relweightz0<-weightz0/sum(weightz0)
relweightz0[is.na(relweightz0)]=0
indz0a<-1-(relweightz0>weightmax)

y12<-(y2*r1*r2*(d/pz)*(z-pz)/(1-pz)*(1/pr1)*((pz0-pz)/(pz01-pz)) *(1/pr2)*((pz1-pz)/(pz12-pz)) )*(1/mean(d/pz*(z-pz)/(1-pz)))
y12t<-mean(y12[(indz1a*indz0a)==1])
y12<-mean(y12)

weightz1<-((r1*d*z/pz*(1/pr1z1))/mean(d/pz*(z-pz)/(1-pz) ))
relweightz1<-weightz1/sum(weightz1)
indz1a<-1-(relweightz1>weightmax)

weightz0<-((r1*d*(1-z)/(1-pz)*(1/pr1z0))/mean(d/pz*(z-pz)/(1-pz) ))
relweightz0<-weightz0/sum(weightz0)
relweightz0[is.na(relweightz0)]=0
indz0a<-1-(relweightz0>weightmax)

y11mar<-(y1*r1*d*z/pz*(1/pr1z1))/mean(d/pz*(z-pz)/(1-pz) )-(y1*r1*d*(1-z)/(1-pz)*(1/pr1z0))/mean(d/pz*(z-pz)/(1-pz))
y11mart<-mean(y11mar[(indz1a*indz0a)==1])
y11mar<-mean(y11mar)

weightz1<-((r1*r2*d*z/pz*(1/pr1z1)*(1/pr2z1))/mean(d/pz*(z-pz)/(1-pz)))
relweightz1<-weightz1/sum(weightz1)
indz1a<-1-(relweightz1>weightmax)

weightz0<-((r1*r2*d*((1-z)/(1-pz)*(1/pr1z0)*(1/pr2z0)))/mean(d/pz*(z-pz)/(1-pz)))
relweightz0<-weightz0/sum(weightz0)
relweightz0[is.na(relweightz0)]=0
indz0a<-1-(relweightz0>weightmax)

y12mar<-(y2*r1*r2*d*(z/pz*(1/pr1z1)*(1/pr2z1)-(1-z)/(1-pz)*(1/pr1z0)*(1/pr2z0)))/mean(d/pz*(z-pz)/(1-pz))
y12mart<-mean(y12mar[(indz1a*indz0a)==1])
y12mar<-mean(y12mar)

pz<-fitted.values(glm(z~x0,family=binomial(probit)))
pr1<-pnorm(cbind(1,x0,0)%*%(temp1$coef))
pr2<-pnorm(cbind(1,x0,x1,1,0)%*%(temp2$coef))
pz0<-pnorm(cbind(1,x0,0)%*%(temp3$coef))
pz01<-pnorm(cbind(1,x0,1,0)%*%(temp4$coef))
pz1<-pnorm(cbind(1,x0,x1,1,0)%*%(temp5$coef))
pz12<-pnorm(cbind(1,x0,x1,1,0)%*%(temp6$coef))
pr1z1<-pnorm(cbind(1,x0,1,0)%*%(temp7$coef))
pr1z0<-pnorm(cbind(1,x0,0,0)%*%(temp7$coef))
pr2z1<-pnorm(cbind(1,x0,x1,1,1,0)%*%(temp8$coef))
pr2z0<-pnorm(cbind(1,x0,x1,1,0,0)%*%(temp8$coef))
ggg=(mean((1-d)/pz*(z-pz)/(1-pz)))
weightz1<- (-r1*(1-d)*z*1/(pz*pr1*((pz01-pz)/(pz0-pz))*ggg))
relweightz1<-weightz1/sum(weightz1)
indz1a<-1-(relweightz1>weightmax)

weightz0<-(-r1*(1-d)*(1-z)*1/((1-pz)*pr1*((pz01-pz)/(pz0-pz))*ggg))
relweightz0<-weightz0/sum(weightz0)
relweightz0[is.na(relweightz0)]=0
indz0a<-1-(relweightz0>weightmax)

y01<-(y1*r1*((1-d)/pz)*(z-pz)/(1-pz)*(1/pr1)*((pz0-pz)/(pz01-pz)) )*(1/mean((1-d)/pz*(z-pz)/(1-pz)))
y01t<-mean(y01[(indz1a*indz0a)==1])
y01<-mean(y01)

weightz1<-(-r2*r1*(1-d)*z*1/(pz*pr1*((pz01-pz)/(pz0-pz))*pr2*((pz12-pz)/(pz1-pz))*ggg))
relweightz1<-weightz1/sum(weightz1)
indz1a<-1-(relweightz1>weightmax)

weightz0<-(-r2*r1*(1-d)*(1-z)*1/((1-pz)*pr1*((pz01-pz)/(pz0-pz))*pr2*((pz12-pz)/(pz1-pz))*ggg))
relweightz0<-weightz0/sum(weightz0)
relweightz0[is.na(relweightz0)]=0
indz0a<-1-(relweightz0>weightmax)

y02<-(y2*r1*r2*((1-d)/pz)*(z-pz)/(1-pz)*(1/pr1)*((pz0-pz)/(pz01-pz)) *(1/pr2)*((pz1-pz)/(pz12-pz)) )*(1/mean((1-d)/pz*(z-pz)/(1-pz)))
y02t<-mean(y02[(indz1a*indz0a)==1])
y02<-mean(y02)

weightz1<-((-r1*(1-d)*(z/pz*(1/pr1z1)))/mean((1-d)/pz*(z-pz)/(1-pz)))
relweightz1<-weightz1/sum(weightz1)
indz1a<-1-(relweightz1>weightmax)

weightz0<-((-r1*(1-d)*((1-z)/(1-pz)*(1/pr1z0)))/mean((1-d)/pz*(z-pz)/(1-pz)))
relweightz0<-weightz0/sum(weightz0)
relweightz0[is.na(relweightz0)]=0
indz0a<-1-(relweightz0>weightmax)

y01mar<-(y1*r1*(1-d)*(z/pz*(1/pr1z1)-(1-z)/(1-pz)*(1/pr1z0)))/mean((1-d)/pz*(z-pz)/(1-pz))
y01mart<-mean(y01mar[(indz1a*indz0a)==1])
y01mar<-mean(y01mar)

weightz1<-((-r1*r2*(1-d)*(z/pz*(1/pr1z1)*(1/pr2z1)))/mean((1-d)/pz*(z-pz)/(1-pz)))
relweightz1<-weightz1/sum(weightz1)
indz1a<-1-(relweightz1>weightmax)

weightz0<-((-r1*r2*(1-d)*((1-z)/(1-pz)*(1/pr1z0)*(1/pr2z0)))/mean((1-d)/pz*(z-pz)/(1-pz)))
relweightz0<-weightz0/sum(weightz0)
relweightz0[is.na(relweightz0)]=0
indz0a<-1-(relweightz0>weightmax)

y02mar<-(y2*r1*r2*(1-d)*(z/pz*(1/pr1z1)*(1/pr2z1)-(1-z)/(1-pz)*(1/pr1z0)*(1/pr2z0)))/mean((1-d)/pz*(z-pz)/(1-pz))
y02mart<-mean(y02mar[(indz1a*indz0a)==1])
y02mar<-mean(y02mar)

lateuncor1=(mean(y1[r1==1 & z==1])-mean(y1[r1==1 & z==0]))/(mean(d[r1==1 & z==1])-mean(d[r1==1 & z==0]))
lateuncor2=(mean(y2[r2==1 & z==1])-mean(y2[r2==1 & z==0]))/(mean(d[r2==1 & z==1])-mean(d[r2==1 & z==0]))

late1<-y11-y01
late2<-y12-y02
late1t<-y11t-y01t
late2t<-y12t-y02t

latemar1<-y11mar-y01mar
latemar2<-y12mar-y02mar
latemar1t<-y11mart-y01mart
latemar2t<-y12mart-y02mart

list (lateuncor1=lateuncor1, lateuncor2=lateuncor2, late1=late1, late2=late2, late1t=late1t, late2t=late2t, latemar1=latemar1, latemar2=latemar2, latemar1t=latemar1t, latemar2t=latemar2t)
}


# Function "bootstrap.late": Bootstrap procedure
############################################
# INPUT
# y1: outcome in period 1
# y2: outcome in period 2
# d: binary treatment
# z: binary instrument
# x0 baseline covariates (pre-instrument)
# x1: covariates in period 1 (may include outcomes of period 1)
# r1: binary response indicator for period 1
# r2: binary response indicator for period 2
# boot: number of bootstrap replications
# weightmax: Trimming rule based on the maximum relative weight a single observation may obtain in estimation - observations with higher weights are discarded 
#
# OUTPUT
# se1uncor: bootstrap standard error of the LATE without attrition correction in period 1
# se2uncor: bootstrap standard error of the LATE without attrition correction in period 2
# rboot1uncor: bootstrap estimates of the LATE without attrition correction in period 1
# rboot2uncor: bootstrap estimates of the LATE without attrition correction in period 2
# se1: bootstrap standard error of the LATE based on Theorem 1 (LI) in period 1
# se2: bootstrap standard error of the LATE based on Theorem 1 (LI) in period 2
# rboot1: bootstrap estimates of the LATE based on Theorem 1 (LI) in period 1
# rboot2: bootstrap estimates of the LATE based on Theorem 1 (LI) in period 2
# se1t: bootstrap standard error of the LATE based on Theorem 1 (LI) in period 1 with trimming
# se2t: bootstrap standard error of the LATE based on Theorem 1 (LI) in period 2 with trimming
# rboot1t: bootstrap estimates of the LATE based on Theorem 1 (LI) in period 1 with trimming
# rboot2t: bootstrap estimates of the LATE based on Theorem 1 (LI) in period 2 with trimming
# se1mar: bootstrap standard error of the LATE based on Theorem 2 (MAR) in period 1
# se2mar: bootstrap standard error of the LATE based on Theorem 2 (MAR) in period 2
# rboot1mar: bootstrap estimates of the LATE based on Theorem 2 (MAR) in period 1
# rboot2mar: bootstrap estimates of the LATE based on Theorem 2 (MAR) in period 2
# se1mart: bootstrap standard error of the LATE based on Theorem 2 (MAR) in period 1 with trimming
# se2mart: bootstrap standard error of the LATE based on Theorem 2 (MAR) in period 2 with trimming
# rboot1mart: bootstrap estimates of the LATE based on Theorem 2 (MAR) in period 1 with trimming
# rboot2mart: bootstrap estimates of the LATE based on Theorem 2 (MAR) in period 2 with trimming

# Function:
bootstrap.late<-function(y1,y2,d,z,x0,x1,r1,r2,boot=999, weightmax=0.1){
obs<-length(y1)
x1<-as.matrix(x1)
x0<-as.matrix(x0)
rboot1<-c()
rboot2<-c()
rboot1t<-c()
rboot2t<-c()
rboot1mar<-c()
rboot2mar<-c()
rboot1mart<-c()
rboot2mart<-c()
rboot1uncor<-c()
rboot2uncor<-c()
while(length(rboot1)<boot){
sboot<-sample(1:obs,obs,TRUE)
if (dim(x0)[2]==1) x0b<-x0[sboot]
if (dim(x0)[2]>1)  x0b<-x0[sboot,]
if (dim(x1)[2]==1) x1b<-x1[sboot]
if (dim(x1)[2]>1)  x1b<-x1[sboot,]
db<-d[sboot]
y1b<-y1[sboot]
y2b<-y2[sboot]
zb<-z[sboot]
r1b<-r1[sboot]
r2b<-r2[sboot]
temp<-attrition.late(y1b,y2b,db,zb,x0b,x1b,r1b,r2b, weightmax=weightmax)
rboot1<-c(rboot1,temp$late1)
rboot2<-c(rboot2,temp$late2)
rboot1t<-c(rboot1t,temp$late1t)
rboot2t<-c(rboot2t,temp$late2t)
rboot1mar<-c(rboot1mar,temp$latemar1)
rboot2mar<-c(rboot2mar,temp$latemar2)
rboot1mart<-c(rboot1mart,temp$latemar1t)
rboot2mart<-c(rboot2mart,temp$latemar2t)
rboot1uncor<-c(rboot1uncor,temp$lateuncor1)
rboot2uncor<-c(rboot2uncor,temp$lateuncor2)
}

list(se1uncor=sd(rboot1uncor), se2uncor=sd(rboot2uncor), rboot1uncor=rboot1uncor, rboot2uncor=rboot2uncor,  se1=sd(rboot1), se2=sd(rboot2), rboot1=rboot1, rboot2=rboot2, se1t=sd(rboot1t), se2t=sd(rboot2t), rboot1t=rboot1t, rboot2t=rboot2t, se1mar=sd(rboot1mar), se2mar=sd(rboot2mar), rboot1mar=rboot1mar, rboot2mar=rboot2mar, se1mart=sd(rboot1mart), se2mart=sd(rboot2mart), rboot1mart=rboot1mart, rboot2mart=rboot2mart)
}

######################################################################################
# Application to college data from  Angrist, Lang, and Oreopoulos (2009):            #
# "Incentives and Services for College Achievement: Evidence from a Randomized Trial"# 
######################################################################################
# load data
library(foreign)
library(xtable)
data=read.dta("C:\\star_college_data.dta")

# define variables
attach(data)
z<-treatass
d<-treatment1
y1<-yy
y2<-yy2
r1<-response1
r2<-response2
x0<-cbind(gpa0_1, gpa0, fallgrade, mathsci, bamore, lastmin12, firstchoice, age171819, dadeduc3,  compsurv)
x1<-cbind(yy, goodst, credity1)

# estimate effects
est<-attrition.late(y1,y2,d,z,x0,x1,r1,r2, weightmax=0.1)
boot<-bootstrap.late(y1,y2,d,z,x0,x1,r1,r2,boot=1999, weightmax=0.1)
#results first period
results1<-cbind(c(est$lateuncor1,boot$se1uncor,2*(min( mean((boot$rboot1uncor-est$lateuncor1)<=est$lateuncor1) , mean((boot$rboot1uncor-est$lateuncor1)>est$lateuncor1) ) )),c(est$late1,boot$se1, 2*(min( mean((boot$rboot1-est$late1)<=est$late1) , mean((boot$rboot1-est$late1)>est$late1) ) )),            c(est$late1t,boot$se1t,2*(min( mean((boot$rboot1t-est$late1t)<=est$late1t) , mean((boot$rboot1t-est$late1t)>est$late1t) ) )),  c(est$latemar1,boot$se1mar,2*(min( mean((boot$rboot1mar-est$latemar1)<=est$latemar1) , mean((boot$rboot1mar-est$latemar1)>est$latemar1) ) )),           c(est$latemar1t,boot$se1mart,2*(min( mean((boot$rboot1mart-est$latemar1t)<=est$latemar1t) , mean((boot$rboot1mart-est$latemar1t)>est$latemar1t) ) ))   )
#results second period
results2<-cbind(c(est$lateuncor2,boot$se2uncor,2*(min( mean((boot$rboot2uncor-est$lateuncor2)<=est$lateuncor2) , mean((boot$rboot2uncor-est$lateuncor2)>est$lateuncor2) ) )), c(est$late2,boot$se2,2*(min( mean((boot$rboot2-est$late2)<=est$late2) , mean((boot$rboot2-est$late2)>est$late2) ) ) ),c(est$late2t,boot$se2t,      2*(min( mean((boot$rboot2t-est$late2t)<=est$late2t) , mean((boot$rboot2t-est$late2t)>est$late2t) ) )),       c(est$latemar2,boot$se2mar,2*(min( mean((boot$rboot2mar-est$latemar2)<=est$latemar2) , mean((boot$rboot2mar-est$latemar2)>est$latemar2) ) )),            c(est$latemar2t,boot$se2mart,2*(min( mean((boot$rboot2mart-est$latemar2t)<=est$latemar2t) , mean((boot$rboot2mart-est$latemar2t)>est$latemar2t) ) ))   ) 

# all results
results<-cbind(results1, results2)
rownames(results)<-c("effect", "s.e.", "bootstrap quantile-based p-val")
colnames(results)<-c("Wald t1", "LI t1", "LI trim t1", "MAR t1", "MAR trim t1", "Wald t2", "LI t2", "LI trim t2", "MAR t2", "MAR trim t2")
results

# export as latex table
xtable(cbind(results), digits=3)

