* *******************************
* Instrumental variables exercise
* *******************************
* Set the seed so our work is replicable
set seed 12345
* create a matrix of correlations (for x1,x2,x3)
matrix C = (1, 0.7, 0.5 \ 0.7, 1, 0.5 \ ///
0.5, 0.5, 1)
* Create a matrix of means (for x1,x3,x3)
matrix mu = (3,2,2)
* Createa a matrix of standard deviations (for x1,x2,x3)
matrix sd = (0.5,2,1)
* Draw three random variables from a multivariate distribution
drawnorm x1 x2 x3, n(100) means(mu) sds(sd) corr(C)
* Draw one more random variable from a normal distribution
drawnorm x4, n(100) means(1) sds(3)
/* The x1 that we observe actually has some funny ingredients
x1 is equal to the random data we drew above, plus some x3
and some x4. What you see below is the final data generating
process for the x1 that we observe (the x1 in our dset). */
replace x1 = x1 + x3 + x4
* Draw some "unobservable" stuff for the DGP of y
gen u = rnormal()
* Create the dependent variable y
gen y = 5 + 2*x1 - 4*x2 + u
* Can we recover the true effect of x1 on y? (=2)