# NOT RUN {
## This illustration was made possible by Samuele Centorrino
## <samuele.centorrino@univ-tlse1.fr>
set.seed(42)
n <- 1000
## For trimming the plot (trim .5% from each tail)
trim <- 0.005
## The DGP is as follows:
## 1) y = phi(z) + u
## 2) E(u|z) != 0 (endogeneity present)
## 3) Suppose there exists an instrument w such that z = f(w) + v and
## E(u|w) = 0
## 4) We generate v, w, and generate u such that u and z are
## correlated. To achieve this we express u as a function of v (i.e. u =
## gamma v + eps)
v <- rnorm(n,mean=0,sd=0.27)
eps <- rnorm(n,mean=0,sd=0.05)
u <- -0.5*v + eps
w <- rnorm(n,mean=0,sd=1)
## In Darolles et al (2011) there exist two DGPs. The first is
## phi(z)=z^2 and the second is phi(z)=exp(-abs(z)) (which is
## discontinuous and has a kink at zero).
fun1 <- function(z) { z^2 }
fun2 <- function(z) { exp(-abs(z)) }
z <- 0.2*w + v
## Generate two y vectors for each function.
y1 <- fun1(z) + u
y2 <- fun2(z) + u
## You set y to be either y1 or y2 (ditto for phi) depending on which
## DGP you are considering:
y <- y1
phi <- fun1
## Sort on z (for plotting)
ivdata <- data.frame(y,z,w,u,v)
ivdata <- ivdata[order(ivdata$z),]
rm(y,z,w,u,v)
attach(ivdata)
model.ivderiv <- crsivderiv(y=y,z=z,w=w)
ylim <-c(quantile(model.ivderiv$phi.prime,trim),
quantile(model.ivderiv$phi.prime,1-trim))
plot(z,model.ivderiv$phi.prime,
xlim=quantile(z,c(trim,1-trim)),
main="",
ylim=ylim,
xlab="Z",
ylab="Derivative",
type="l",
lwd=2)
rug(z)
# }
# NOT RUN {
# }
# NOT RUN {
<!-- % end dontrun -->
# }
Run the code above in your browser using DataLab