#################
# load packages #
#################

library(foreign)
library(xtable)
library(mvtnorm)
library(sampleSelection)
library(tmle)
library(locfit)
library(SuperLearner)

#############
# Functions #
#############

ipw<-function(y,d,r,x,psd,psr){
xx<-cbind(d,x)
xx<-xx[r==1,]
coeff<-lm(y[r==1]~xx)$coef
eff.ipw<-sum(r*y*d/(psr*psd))/sum(r*d/(psr*psd))-sum(r*y*(1-d)/(psr*(1-psd)))/sum(r*(1-d)/(psr*(1-psd)))
eff.aipw<-mean( r/psr* (y*d/psd-y*(1-d)/(1-psd)-(d-psd)/(psd*(1-psd))*( (1-psd)*(cbind(1,1,x)%*%coeff)+psd*(cbind(1,0,x)%*%coeff) ) ) )
eff.reg<-mean(r/psr*((cbind(1,1,x)%*%coeff)-(cbind(1,0,x)%*%coeff)))
list(eff.ipw=eff.ipw, eff.aipw=eff.aipw, eff.reg=eff.reg)
}

attrition.est.kern<-function(y1,y2,d,z,x0,x1,r1,r2, alpha=0.7, kern="gauss", weightmax=c(0.15, 0.1, 0.05, 0.02, 0.01)){

pz<-fitted.values(locfit(z~x0,deg=0, alpha=alpha, kern=kern))
temp1<-locfit(r1[d==1]~x0[d==1],deg=0,kern=kern , alpha=alpha)
pr1<-predict(temp1,x0)
temp2<-locfit(r2[r1==1 & d==1]~x0[r1==1 & d==1]+x1[r1==1 & d==1],deg=0,kern=kern)
pr2<-predict(temp2,cbind(x0,x1))
temp3<-locfit(z[d==1]~x0[d==1],deg=0,kern=kern, alpha=alpha)
pz0<-predict(temp3,x0)
temp4<-locfit(z[r1==1 & d==1]~x0[r1==1 & d==1],deg=0,kern=kern, alpha=alpha)
pz01<-predict(temp4,x0)
temp5<-locfit(z[r1==1 & d==1]~x0[r1==1 & d==1]+x1[r1==1 & d==1],deg=0,kern=kern, alpha=alpha)
pz1<-predict(temp5,cbind(x0,x1))
temp6<-locfit(z[r2==1 & r1==1 & d==1]~x0[r2==1 & r1==1 & d==1]+x1[r2==1 & r1==1 & d==1],deg=0,kern=kern, alpha=alpha)
pz12<-predict(temp6,cbind(x0,x1))
temp7<-locfit(r1[z==1 & d==1]~x0[z==1 & d==1],deg=0,kern=kern, alpha=alpha)
pr1z1<-predict(temp7,x0)
temp7a<-locfit(r1[z==0 & d==1]~x0[z==0 & d==1],deg=0,kern=kern, alpha=alpha)
pr1z0<-predict(temp7a,x0)
temp8<-locfit(r2[r1==1 & z==1 & d==1]~x0[r1==1 & z==1 & d==1]+x1[r1==1 & z==1 & d==1],deg=0,kern=kern, alpha=alpha)
pr2z1<-predict(temp8,cbind(x0,x1))
temp8a<-locfit(r2[r1==1 & z==0 & d==1]~x0[r1==1 & z==0 & d==1]+x1[r1==1 & z==0 & d==1],deg=0,kern=kern, alpha=alpha)
pr2z0<-predict(temp8a,cbind(x0,x1))

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[1]); indz1b<-1-(relweightz1>weightmax[2]); indz1c<-1-(relweightz1>weightmax[3]); indz1d<-1-(relweightz1>weightmax[4]); indz1e<-1-(relweightz1>weightmax[5])

weightz0<-(r1*d*(1-z)*1/((1-pz)*pr1*((pz01-pz)/(pz0-pz))*ggg))
relweightz0<-weightz0/sum(weightz0)
indz0a<-1-(relweightz0>weightmax[1]); indz0b<-1-(relweightz0>weightmax[2]); indz0c<-1-(relweightz0>weightmax[3]); indz0d<-1-(relweightz0>weightmax[4]); indz0e<-1-(relweightz0>weightmax[5])

y11<-(y1*r1*(d/pz)*(z-pz)/(1-pz)*(1/pr1)*((pz0-pz)/(pz01-pz)) )*(1/mean(d/pz*(z-pz)/(1-pz)))
y11t<-c(mean(y11[(indz1a*indz0a)==1]), mean(y11[(indz1b*indz0b)==1]), mean(y11[(indz1c*indz0c)==1]), mean(y11[(indz1d*indz0d)==1]), mean(y11[(indz1e*indz0e)==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[1]); indz1b<-1-(relweightz1>weightmax[2]); indz1c<-1-(relweightz1>weightmax[3]); indz1d<-1-(relweightz1>weightmax[4]); indz1e<-1-(relweightz1>weightmax[5])

weightz0<-(r2*r1*d*(1-z)*1/((1-pz)*pr1*((pz01-pz)/(pz0-pz))*pr2*((pz12-pz)/(pz1-pz))*ggg))
relweightz0<-weightz0/sum(weightz0)
indz0a<-1-(relweightz0>weightmax[1]); indz0b<-1-(relweightz0>weightmax[2]); indz0c<-1-(relweightz0>weightmax[3]); indz0d<-1-(relweightz0>weightmax[4]); indz0e<-1-(relweightz0>weightmax[5])

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<-c(mean(y12[(indz1a*indz0a)==1]), mean(y12[(indz1b*indz0b)==1]), mean(y12[(indz1c*indz0c)==1]), mean(y12[(indz1d*indz0d)==1]), mean(y12[(indz1e*indz0e)==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[1]); indz1b<-1-(relweightz1>weightmax[2]); indz1c<-1-(relweightz1>weightmax[3]); indz1d<-1-(relweightz1>weightmax[4]); indz1e<-1-(relweightz1>weightmax[5])

weightz0<-((r1*d*(1-z)/(1-pz)*(1/pr1z0))/mean(d/pz*(z-pz)/(1-pz) ))
relweightz0<-weightz0/sum(weightz0)
indz0a<-1-(relweightz0>weightmax[1]); indz0b<-1-(relweightz0>weightmax[2]); indz0c<-1-(relweightz0>weightmax[3]); indz0d<-1-(relweightz0>weightmax[4]); indz0e<-1-(relweightz0>weightmax[5])

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<-c(mean(y11mar[(indz1a*indz0a)==1]), mean(y11mar[(indz1b*indz0b)==1]), mean(y11mar[(indz1c*indz0c)==1]), mean(y11mar[(indz1d*indz0d)==1]), mean(y11mar[(indz1e*indz0e)==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[1]); indz1b<-1-(relweightz1>weightmax[2]); indz1c<-1-(relweightz1>weightmax[3]); indz1d<-1-(relweightz1>weightmax[4]); indz1e<-1-(relweightz1>weightmax[5])

weightz0<-((r1*r2*d*((1-z)/(1-pz)*(1/pr1z0)*(1/pr2z0)))/mean(d/pz*(z-pz)/(1-pz)))
relweightz0<-weightz0/sum(weightz0)
indz0a<-1-(relweightz0>weightmax[1]); indz0b<-1-(relweightz0>weightmax[2]); indz0c<-1-(relweightz0>weightmax[3]); indz0d<-1-(relweightz0>weightmax[4]); indz0e<-1-(relweightz0>weightmax[5])

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<-c(mean(y12mar[(indz1a*indz0a)==1]), mean(y12mar[(indz1b*indz0b)==1]), mean(y12mar[(indz1c*indz0c)==1]), mean(y12mar[(indz1d*indz0d)==1]), mean(y12mar[(indz1e*indz0e)==1]) )
y12mar<-mean(y12mar)

pz<-fitted.values(locfit(z~x0,deg=0,kern=kern, alpha=alpha))

temp1<-locfit(r1[d==0]~x0[d==0],deg=0,kern=kern, alpha=alpha)
pr1<-predict(temp1,x0)

temp2<-locfit(r2[r1==1 & d==0]~x0[r1==1 & d==0]+x1[r1==1 & d==0],deg=0,kern=kern, alpha=alpha)
pr2<-predict(temp2,cbind(x0,x1))

temp3<-locfit(z[d==0]~x0[d==0],deg=0,kern=kern, alpha=alpha)
pz0<-predict(temp3,x0)

temp4<-locfit(z[r1==1 & d==0]~x0[r1==1 & d==0],deg=0,kern=kern, alpha=alpha)
pz01<-predict(temp4,x0)

temp5<-locfit(z[r1==1 & d==0]~x0[r1==1 & d==0]+x1[r1==1 & d==0],deg=0,kern=kern, alpha=alpha)
pz1<-predict(temp5,cbind(x0,x1))

temp6<-locfit(z[r2==1 & r1==1 & d==0]~x0[r2==1 & r1==1 & d==0]+x1[r2==1 & r1==1 & d==0],deg=0,kern=kern, alpha=alpha)
pz12<-predict(temp6,cbind(x0,x1))

temp7<-locfit(r1[z==1 & d==0]~x0[z==1 & d==0],deg=0,kern=kern, alpha=alpha)
pr1z1<-predict(temp7,x0)

temp7a<-locfit(r1[z==0 & d==0]~x0[z==0 & d==0],deg=0,kern=kern, alpha=alpha)
pr1z0<-predict(temp7a,x0)

temp8<-locfit(r2[r1==1 & z==1 & d==0]~x0[r1==1 & z==1 & d==0]+x1[r1==1 & z==1 & d==0],deg=0,kern=kern, alpha=alpha)
pr2z1<-predict(temp8,cbind(x0,x1))

temp8a<-locfit(r2[r1==1 & z==0 & d==0]~x0[r1==1 & z==0 & d==0]+x1[r1==1 & z==0 & d==0],deg=0,kern=kern, alpha=alpha)
pr2z0<-predict(temp8a,cbind(x0,x1))

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[1]); indz1b<-1-(relweightz1>weightmax[2]); indz1c<-1-(relweightz1>weightmax[3]); indz1d<-1-(relweightz1>weightmax[4]); indz1e<-1-(relweightz1>weightmax[5])

weightz0<-(-r1*(1-d)*(1-z)*1/((1-pz)*pr1*((pz01-pz)/(pz0-pz))*ggg))
relweightz0<-weightz0/sum(weightz0)
indz0a<-1-(relweightz0>weightmax[1]); indz0b<-1-(relweightz0>weightmax[2]); indz0c<-1-(relweightz0>weightmax[3]); indz0d<-1-(relweightz0>weightmax[4]); indz0e<-1-(relweightz0>weightmax[5])

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<-c(mean(y01[(indz1a*indz0a)==1]), mean(y01[(indz1b*indz0b)==1]), mean(y01[(indz1c*indz0c)==1]), mean(y01[(indz1d*indz0d)==1]), mean(y01[(indz1e*indz0e)==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[1]); indz1b<-1-(relweightz1>weightmax[2]); indz1c<-1-(relweightz1>weightmax[3]); indz1d<-1-(relweightz1>weightmax[4]); indz1e<-1-(relweightz1>weightmax[5])

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)
indz0a<-1-(relweightz0>weightmax[1]); indz0b<-1-(relweightz0>weightmax[2]); indz0c<-1-(relweightz0>weightmax[3]); indz0d<-1-(relweightz0>weightmax[4]); indz0e<-1-(relweightz0>weightmax[5])

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<-c(mean(y02[(indz1a*indz0a)==1]), mean(y02[(indz1b*indz0b)==1]), mean(y02[(indz1c*indz0c)==1]), mean(y02[(indz1d*indz0d)==1]), mean(y02[(indz1e*indz0e)==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[1]); indz1b<-1-(relweightz1>weightmax[2]); indz1c<-1-(relweightz1>weightmax[3]); indz1d<-1-(relweightz1>weightmax[4]); indz1e<-1-(relweightz1>weightmax[5])

weightz0<-((-r1*(1-d)*((1-z)/(1-pz)*(1/pr1z0)))/mean((1-d)/pz*(z-pz)/(1-pz)))
relweightz0<-weightz0/sum(weightz0)
indz0a<-1-(relweightz0>weightmax[1]); indz0b<-1-(relweightz0>weightmax[2]); indz0c<-1-(relweightz0>weightmax[3]); indz0d<-1-(relweightz0>weightmax[4]); indz0e<-1-(relweightz0>weightmax[5])

y01mar<-(y1*r1*(1-d)*(z/pz*(1/pr1z1)-(1-z)/(1-pz)*(1/pr1z0)))/mean((1-d)/pz*(z-pz)/(1-pz))
y01mart<-c(mean(y01mar[(indz1a*indz0a)==1]), mean(y01mar[(indz1b*indz0b)==1]), mean(y01mar[(indz1c*indz0c)==1]), mean(y01mar[(indz1d*indz0d)==1]), mean(y01mar[(indz1e*indz0e)==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[1]); indz1b<-1-(relweightz1>weightmax[2]); indz1c<-1-(relweightz1>weightmax[3]); indz1d<-1-(relweightz1>weightmax[4]); indz1e<-1-(relweightz1>weightmax[5])

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)
indz0a<-1-(relweightz0>weightmax[1]); indz0b<-1-(relweightz0>weightmax[2]); indz0c<-1-(relweightz0>weightmax[3]); indz0d<-1-(relweightz0>weightmax[4]); indz0e<-1-(relweightz0>weightmax[5])

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<-c(mean(y02mar[(indz1a*indz0a)==1]), mean(y02mar[(indz1b*indz0b)==1]), mean(y02mar[(indz1c*indz0c)==1]), mean(y02mar[(indz1d*indz0d)==1]), mean(y02mar[(indz1e*indz0e)==1]) )
y02mar<-mean(y02mar)

late1<-y11-y01
late2<-y12-y02
late1t<-y11t-y01t
late2t<-y12t-y02t

latemar1<-y11mar-y01mar
latemar2<-y12mar-y02mar
latemar1t<-y11mart-y01mart
latemar2t<-y12mart-y02mart

late1uncor<-(mean(y1[r1==1]*z[r1==1]/pz[r1==1])-mean(y1[r1==1]*(1-z[r1==1])/(1-pz[r1==1])))/(mean(d[r1==1]*z[r1==1]/pz[r1==1])-mean(d[r1==1]*(1-z[r1==1])/(1-pz[r1==1])) )   
late2uncor<-(mean(y2[r2==1]*z[r2==1]/pz[r2==1])-mean(y2[r2==1]*(1-z[r2==1])/(1-pz[r2==1])))/(mean(d[r2==1]*z[r2==1]/pz[r2==1])-mean(d[r2==1]*(1-z[r2==1])/(1-pz[r2==1])) )   

list (late1uncor=late1uncor, late2uncor=late2uncor, y11=y11, y01=y01, y12=y12, y02=y02, y11t=y11t, y01t=y01t, y12t=y12t, y02t=y02t, late1=late1, late2=late2, late1t=late1t, late2t=late2t, latemar1=latemar1, latemar2=latemar2, latemar1t=latemar1t, latemar2t=latemar2t)
}

####################
#### Simulation ####
####################


# Simulation parameters
#############################################
alpha=0.68 #1.35, 0.68  determining share of compliers
gamma=0 #0, 0.5   effect of being a complier on response
beta=0.5 #0, 0.5   effect of z on response
delta1=0.25 #0, 0.25  sel on unobservables in first period
delta2=0.25 #0, 0.25  sel on unobservables in second period
kappa=0.5 #0, 0.5 effect heterogeneity
attr.abs=1 # attrition absorbing?
sigma <- matrix(c(1,0.5,0.5,  0.5,1,0.5,  0.5,0.5,1),3,3) # error covairance matrix
n<-5000  # sample size
repetitions<-1000 # number of simulations
##############################

# Simulation starts here
##############################################################
mc_est1<-c(); mc_est2<-c(); mc_est1mar<-c(); mc_est2mar<-c()
mc_est1t01<-c(); mc_est2t01<-c(); mc_est1mart01<-c(); mc_est2mart01<-c()
mc_est1t05<-c(); mc_est2t05<-c(); mc_est1mart05<-c(); mc_est2mart05<-c()
mc_est1t10<-c(); mc_est2t10<-c(); mc_est1mart10<-c(); mc_est2mart10<-c()
mc_est1tp5<-c(); mc_est2tp5<-c(); mc_est1martp5<-c(); mc_est2martp5<-c()
mc_est1tp10<-c(); mc_est2tp10<-c(); mc_est1martp10<-c(); mc_est2martp10<-c()
y1naive<-c(); y2naive<-c(); late1<-c(); late2<-c(); sel1<-c(); sel2<-c(); comp<-c(); true1<-c(); true2<-c(); mcr1<-c(); mcr2<-c()
mc.ipw1<-c(); mc.ipw2<-c(); mc.aipw1<-c(); mc.aipw2<-c(); mc.reg1<-c(); mc.reg2<-c(); mc.ipw1.sel<-c(); mc.ipw2.sel<-c(); mc.aipw1.sel<-c()
mc.aipw2.sel<-c(); mc.reg1.sel<-c(); mc.reg2.sel<-c(); mcx1z1<-c(); mcx1z0<-c(); mc.gcomp1<-c(); mc.gcomp2<-c(); mc.mar.mle1<-c(); mc.mar.mle2<-c()
i=1
while(length(mcr1)<repetitions){
set.seed(i)
e<-(rmvnorm(n,rep(0,3),sigma))
x0<-runif(n,0,1)
z<-(0.25*x0+rnorm(n)>0)*1
u<-e[,1]
d<-(alpha*z-0.25*x0+u>0.5)*1
y1_star<-0.5*x0+0.5*d+kappa*d*u+e[,2]
r1<-(0.25*x0+0.25*d+beta*z+gamma*((-0.25*x0+u>0.5-alpha)&(-0.25*x0+u<=0.5))+delta1*y1_star+rnorm(n)>0)*1
y1=y1_star
y1[r1==0]=0
x1<-(0.5*y1_star+0.5*rnorm(n))
y2_star<-0.5*x0+x1+d+kappa*d*u+e[,3]
r2<-((0.25*x0+0.25*x1+0.25*d+beta*z+gamma*((-0.25*x0+u>0.5-alpha)&(-0.25*x0+u<=0.5))+delta2*y2_star+rnorm(n)>0)*1)
if (attr.abs==1) r2<-r1*r2
y2=y2_star
y2[r2==0]=0
y1naive<-c(y1naive,mean(y1[r1*d==1])-mean(y1[r1*(1-d)==1]))
y2naive<-c(y2naive,mean(y2[r2*d==1])-mean(y2[r2*(1-d)==1]))
temp<-attrition.est.kern(y1,y2,d,z,x0,x1,r1,r2, alpha=0.7)
mc_est1<-c(mc_est1,temp$late1) 
mc_est2<-c(mc_est2,temp$late2)
mc_est1mar<-c(mc_est1mar,temp$latemar1) 
mc_est2mar<-c(mc_est2mar,temp$latemar2)
mc_est1t01<-c(mc_est1t01,temp$late1t[1]); mc_est2t01<-c(mc_est2t01,temp$late2t[1]); mc_est1mart01<-c(mc_est1mart01,temp$latemar1t[1]); mc_est2mart01<-c(mc_est2mart01,temp$latemar2t[1])
mc_est1t05<-c(mc_est1t05,temp$late1t[2]); mc_est2t05<-c(mc_est2t05,temp$late2t[2]); mc_est1mart05<-c(mc_est1mart05,temp$latemar1t[2]); mc_est2mart05<-c(mc_est2mart05,temp$latemar2t[2])
mc_est1t10<-c(mc_est1t10,temp$late1t[3]); mc_est2t10<-c(mc_est2t10,temp$late2t[3]); mc_est1mart10<-c(mc_est1mart10,temp$latemar1t[3]); mc_est2mart10<-c(mc_est2mart10,temp$latemar2t[3])
mc_est1tp5<-c(mc_est1tp5,temp$late1t[4]); mc_est2tp5<-c(mc_est2tp5,temp$late2t[4]); mc_est1martp5<-c(mc_est1martp5,temp$latemar1t[4]); mc_est2martp5<-c(mc_est2martp5,temp$latemar2t[4])
mc_est1tp10<-c(mc_est1tp10,temp$late1t[5]); mc_est2tp10<-c(mc_est2tp10,temp$late2t[5]); mc_est1martp10<-c(mc_est1martp10,temp$latemar1t[5]); mc_est2martp10<-c(mc_est2martp10,temp$latemar2t[5])

late1<-c(late1, temp$late1uncor )
late2<-c(late2, temp$late2uncor )
b<-(selection(r1~x0+d+z, y1~d+x0))
b<-coef( summary(b), part="outcome") 
sel1<-c(sel1,b[2,1])
b<-(selection(r2[r1==1]~x0[r1==1]+d[r1==1]+z[r1==1], y2[r1==1]~d[r1==1]+x0[r1==1]))
#b<-(selection(r2[r1==1]~x0[r1==1]+x1[r1==1]+d[r1==1], y2[r1==1]~d[r1==1]+x0[r1==1]))
b<-coef( summary(b), part="outcome") 
sel2<-c(sel2,b[2,1])
comp<-c(comp,mean(d[z==1])-mean(d[z==0]))
true1<-c(true1,mean(0.5+kappa*u[(-0.25*x0+u>0.5-alpha)&(-0.25*x0+u<=0.5)]   ))
true2<-c(true2,mean(1.25+(1.5)*kappa*u[(-0.25*x0+u>0.5-alpha)&(-0.25*x0+u<=0.5)] ))
ps=fitted.values(glm(d~x0,family=binomial(probit)))
tempp<-tmle(Y=y1, A=d, W=as.matrix(x0), Z=NULL, Delta = r1,  family="gaussian")
mc.gcomp1<-c(mc.gcomp1,mean(tempp$Qinit$Q[,2]-tempp$Qinit$Q[,1]))
mc.mar.mle1<-c(mc.mar.mle1,tempp$estimates$ATE$psi)
ggg<-as.matrix(cbind(x0,x1,z))
ggg<-ggg[r1==1,]
A<-d[r1==1]
Y<-y2[r1==1]
Delta<-r2[r1==1]
tempp<-tmle(Y=Y, A=A, W=ggg[,c("x0","x1")],  Z=NULL, Delta =Delta, Qform=Y~A+x0, gform=A~x0, g.Deltaform=Delta~A+x0+x1, family="gaussian")
mc.gcomp2<-c(mc.gcomp2,mean(tempp$Qinit$Q[,2]-tempp$Qinit$Q[,1]))
mc.mar.mle2<-c(mc.mar.mle2,tempp$estimates$ATE$psi)
temp<-ipw(y=y1,d=d,r=r1,x=x0,psd=ps,psr=fitted.values(glm(r1~cbind(d,x0),family=binomial(probit))))
mc.ipw1=c(mc.ipw1,temp$eff.ipw)
mc.aipw1=c(mc.aipw1,temp$eff.aipw)
mc.reg1<-c(mc.reg1,temp$eff.reg)
temp<-ipw(y=y2[r1==1],d=d[r1==1],r=r2[r1==1],x=x0[r1==1],psd=fitted.values(glm(d[r1==1]~x0[r1==1],family=binomial(probit))),psr=fitted.values(glm(r2[r1==1]~cbind(d[r1==1],x0[r1==1],x1[r1==1]),family=binomial(probit))))
mc.ipw2=c(mc.ipw2,temp$eff.ipw)
mc.aipw2=c(mc.aipw2,temp$eff.aipw)
mc.reg2<-c(mc.reg2,temp$eff.reg)
psr=fitted.values(glm(r1~cbind(d,x0,z)))
psd=fitted.values(glm(d~cbind(x0,psr),family=binomial(probit)))
temp<-ipw(y=y1,d=d,r=r1,x=cbind(x0),psd=psd,psr=psr)
mc.ipw1.sel=c(mc.ipw1.sel,temp$eff.ipw)
mc.aipw1.sel=c(mc.aipw1.sel,temp$eff.aipw)
mc.reg1.sel<-c(mc.reg1.sel,temp$eff.reg)
psr=fitted.values(glm(r2[r1==1]~cbind(d[r1==1],x0[r1==1],z[r1==1])))
psd=fitted.values(glm(d[r1==1]~cbind(x0[r1==1],psr),family=binomial(probit)))
temp<-ipw(y=y2[r1==1],d=d[r1==1],r=r2[r1==1],x=cbind(x0[r1==1]),psd=psd,psr=psr)
mc.ipw2.sel=c(mc.ipw2.sel,temp$eff.ipw)
mc.aipw2.sel=c(mc.aipw2.sel,temp$eff.aipw)
mc.reg2.sel<-c(mc.reg2.sel,temp$eff.reg)
mcr1<-c(mcr1, mean(r1))
mcr2<-c(mcr2, mean(r2))
mcx1z1<-c(mcx1z1,mean(x1[0.0*x0+u>0.5 & z==1]))
mcx1z0<-c(mcx1z0,mean(x1[0.0*x0+u>0.5 & z==0]))
i=i+1
}

# Results
#######################################
effect1<-mean(true1)
effect2<-mean(true2)
mse.naive1<-(mean(y1naive-effect1))^2+var(y1naive); mse.naive2<-(mean(y2naive-effect2))^2+var(y2naive)
mse.late1<-(mean(late1-effect1))^2+var(late1); mse.late2<-(mean(late2-effect2))^2+var(late2)
mse.li1<-(mean(mc_est1-effect1))^2+var(mc_est1); mse.li2<-(mean(mc_est2-effect2))^2+var(mc_est2)
mse.mar1<-(mean(mc_est1mar-effect1))^2+var(mc_est1mar); mse.mar2<-(mean(mc_est2mar-effect2))^2+var(mc_est2mar)
mse.li1t01<-(mean(mc_est1t01-effect1))^2+var(mc_est1t01); mse.li2t01<-(mean(mc_est2t01-effect2))^2+var(mc_est2t01)
mse.mar1t01<-(mean(mc_est1mart01-effect1))^2+var(mc_est1mart01); mse.mar2t01<-(mean(mc_est2mart01-effect2))^2+var(mc_est2mart01)
mse.li1t05<-(mean(mc_est1t05-effect1))^2+var(mc_est1t05); mse.li2t05<-(mean(mc_est2t05-effect2))^2+var(mc_est2t05)
mse.mar1t05<-(mean(mc_est1mart05-effect1))^2+var(mc_est1mart05); mse.mar2t05<-(mean(mc_est2mart05-effect2))^2+var(mc_est2mart05)
mse.li1t10<-(mean(mc_est1t10-effect1))^2+var(mc_est1t10); mse.li2t10<-(mean(mc_est2t10-effect2))^2+var(mc_est2t10)
mse.mar1t10<-(mean(mc_est1mart10-effect1))^2+var(mc_est1mart10); mse.mar2t10<-(mean(mc_est2mart10-effect2))^2+var(mc_est2mart10)
mse.li1tp5<-(mean(mc_est1tp5-effect1))^2+var(mc_est1tp5); mse.li2tp5<-(mean(mc_est2tp5-effect2))^2+var(mc_est2tp5)
mse.mar1tp5<-(mean(mc_est1martp5-effect1))^2+var(mc_est1martp5); mse.mar2tp5<-(mean(mc_est2martp5-effect2))^2+var(mc_est2martp5)
mse.li1tp10<-(mean(mc_est1tp10-effect1))^2+var(mc_est1tp10); mse.li2tp10<-(mean(mc_est2tp10-effect2))^2+var(mc_est2tp10)
mse.mar1tp10<-(mean(mc_est1martp10-effect1))^2+var(mc_est1martp10); mse.mar2tp10<-(mean(mc_est2martp10[1*is.na(mc_est2martp10)==0]-effect2))^2+var(mc_est2martp10[1*is.na(mc_est2martp10)==0])
mse.sel1<-(mean(sel1-effect1))^2+var(sel1); mse.sel2<-(mean(sel2-effect2))^2+var(sel2)
mse.tmle1<-(mean(mc.mar.mle1-effect1))^2+var(mc.mar.mle1); mse.tmle2<-(mean(mc.mar.mle2-effect2))^2+var(mc.mar.mle2)
mse.ipw1<-(mean(mc.ipw1-effect1))^2+var(mc.ipw1); mse.ipw2<-(mean(mc.ipw2-effect2))^2+var(mc.ipw2)
mse.aipw1<-(mean(mc.aipw1-effect1))^2+var(mc.aipw1); mse.aipw2<-(mean(mc.aipw2-effect2))^2+var(mc.aipw2)
mse.ipw1.sel<-(mean(mc.ipw1.sel-effect1))^2+var(mc.ipw1.sel); mse.ipw2.sel<-(mean(mc.ipw2.sel-effect2))^2+var(mc.ipw2.sel)
mse.gcomp1<-(mean(mc.gcomp1-effect1))^2+var(mc.gcomp1); mse.gcomp2<-(mean(mc.gcomp2-effect2))^2+var(mc.gcomp2)

sd1<-c(sd(y1naive), sd(late1), sd(mc_est1), sd(mc_est1t01), sd(mc_est1t05), sd(mc_est1t10), sd(mc_est1tp5), sd(mc_est1tp10), sd(mc_est1mar), sd(mc_est1mart01), sd(mc_est1mart05), sd(mc_est1mart10), sd(mc_est1martp5), sd(mc_est1martp10), sd(mc.gcomp1), sd(mc.mar.mle1), sd(mc.ipw1), sd(mc.aipw1), sd(sel1))
sd2<-c(sd(y2naive), sd(late2), sd(mc_est2),  sd(mc_est2t01), sd(mc_est2t05), sd(mc_est2t10), sd(mc_est2tp5), sd(mc_est2tp10),  sd(mc_est2mar), sd(mc_est2mart01), sd(mc_est2mart05), sd(mc_est2mart10), sd(mc_est2martp5), sd(mc_est2martp10[1*is.na(mc_est2martp10)==0]), sd(mc.gcomp2), sd(mc.mar.mle2), sd(mc.ipw2), sd(mc.aipw2), sd(sel1))
bias1<-c((mean(y1naive-effect1)), (mean(late1-effect1)), (mean(mc_est1-effect1)), (mean(mc_est1t01-effect1)), (mean(mc_est1t05-effect1)), (mean(mc_est1t10-effect1)), (mean(mc_est1tp5-effect1)), (mean(mc_est1tp10-effect1)), (mean(mc_est1mar-effect1)), (mean(mc_est1mart01-effect1)), (mean(mc_est1mart05-effect1)), (mean(mc_est1mart10-effect1)), (mean(mc_est1martp5-effect1)), (mean(mc_est1martp10-effect1)), (mean(mc.gcomp1-effect1)), (mean(mc.mar.mle1-effect1)), (mean(mc.ipw1-effect1)), (mean(mc.aipw1-effect1)), (mean(sel1-effect1)) )
bias2<-c((mean(y2naive-effect2)), (mean(late2-effect2)), (mean(mc_est2-effect2)), (mean(mc_est2t01-effect2)), (mean(mc_est2t05-effect2)), (mean(mc_est2t10-effect2)), (mean(mc_est2tp5-effect2)), (mean(mc_est2tp10-effect2)), (mean(mc_est2mar-effect2)), (mean(mc_est2mart01-effect2)), (mean(mc_est2mart05-effect2)), (mean(mc_est2mart10-effect2)), (mean(mc_est2martp5-effect2)), (mean(mc_est2martp10[1*is.na(mc_est2martp10)==0]-effect2)), (mean(mc.gcomp2-effect2)), (mean(mc.mar.mle2-effect2)), (mean(mc.ipw2-effect2)), (mean(mc.aipw2-effect2)), (mean(sel2-effect2)) )
rmse1<-c(sqrt(mse.naive1),sqrt(mse.late1),sqrt(mse.li1),sqrt(mse.li1t01),sqrt(mse.li1t05),sqrt(mse.li1t10),sqrt(mse.li1tp5),sqrt(mse.li1tp10),sqrt(mse.mar1),sqrt(mse.mar1t01),sqrt(mse.mar1t05),sqrt(mse.mar1t10),sqrt(mse.mar1tp5),sqrt(mse.mar1tp10),sqrt(mse.gcomp1), sqrt(mse.tmle1), sqrt(mse.ipw1), sqrt(mse.aipw1), sqrt(mse.sel1) )
rmse2<-c(sqrt(mse.naive2),sqrt(mse.late2),sqrt(mse.li2), sqrt(mse.li2t01), sqrt(mse.li2t05), sqrt(mse.li2t10), sqrt(mse.li2tp5), sqrt(mse.li2tp10), sqrt(mse.mar2),sqrt(mse.mar2t01),sqrt(mse.mar2t05),sqrt(mse.mar2t10),sqrt(mse.mar2tp5),sqrt(mse.mar2tp10), sqrt(mse.gcomp2), sqrt(mse.tmle2), sqrt(mse.ipw2), sqrt(mse.aipw2), sqrt(mse.sel2) )
results<-cbind(bias1,sd1,rmse1,bias2,sd2,rmse2)

# Display results
###################################
results
#for export to LATEX
xtable(results,digits=3)
# mean response rates
mean(mcr1)
mean(mcr2)
# true LATEs
mean(true1)
mean(true2)
# share of compliers
mean(comp)
