# A little example with simulated data (1000 observations)
if (FALSE) {
n=1000 # sample size
e<-(rmvnorm(n,rep(0,3), matrix(c(1,0.5,0.5, 0.5,1,0.5, 0.5,0.5,1),3,3)))
# correlated error term of treatment, response, and outcome equation
x=runif(n,-0.5,0.5) # observed confounder
z1<-(-0.25*x+rnorm(n)>0)*1 # binary instrument for treatment
z2<- -0.25*x+rnorm(n) # continuous instrument for selection
d<-(z1-0.25*x+e[,1]>0)*1 # treatment equation
y_star<- -0.25*x+d+e[,2] # latent outcome
r<-(-0.25*x+z2+d+e[,3]>0)*1 # response equation
y=y_star # observed outcome
y[r==0]=0 # nonobserved outcomes are set to zero
# The true treatment effect is 1
ivnr(y=y,d=d,r=r,z1=z1,z2=z2,x=x,xpar=x,numresprob=4,boot=39)}
Run the code above in your browser using DataLab