##generate data where regressor is correlated with error:
set.seed(123) #for reproducibility
n <- 100
z1 <- rnorm(n) #instrument
eps <- rnorm(n) #ensures cor(z,eps)=0
x1 <- 0.5*z1 + 0.5*eps #ensures cor(x,eps) is strong
y <- 0.4 + 0.8*x1 + eps #the dgp
cor(x1, eps) #check correlatedness of regressor
cor(z1, eps) #check uncorrelatedness of instrument
x <- cbind(1,x1) #regressor matrix
z <- cbind(1,z1) #matrix with instruments
##efficient gmm estimation:
mymod <- gmm(y, x, z)
mymod$coefficients
##ols (for comparison):
mymod <- ols(y,x)
mymod$coefficients
Run the code above in your browser using DataLab