# An ivreg2 function for R

The ivreg2 command is one of the most popular routines in Stata. The reason for this popularity is its simplicity. A one-line ivreg2 command generates not only the instrumental variable regression coefficients and their standard errors, but also a number of other statistics of interest.

I have come across a number of functions in R that calculate instrumental variable regressions. However, none appear to (and correct me if I am wrong) offer an output similar to the ivreg2 command in Stata. The function below is my first attempt to replicate Stata’s ivreg2.

```ivreg2(form,endog,iv,data,digits)
```

There are four required arguments. The ‘form’ argument is the second stage regression, written in the same manner as any regression model in R. The ‘endog’ argument is a character object with the name of the endogenous variable. The user should specify the instrumental variable(s) with the ‘iv’ argument. These instruments should be contained in ‘data’ – a data frame object. Note, the function in its current state only allows of one endogenous variable (which is usually more than enough for the researcher to contend with). Furthermore, make sure that there are no ‘NA’ values in the data frame being passed through the function.

This function performs a 2SLS regression calculating the usual regression output, a weak identification F-statistic, the Wu-Hausman test of endogeneity, and, in the case where there is more than one-instrument, a Sargan test. The weak identification statistic is used to determine whether the instrument(s) is(are) sufficiently correlated with the endogenous variable of interest. The ‘rule-of-thumb’ critical statistic here is ten. A Wu-Hausman test examines the difference between the IV and OLS coefficients. Rejecting the null hypothesis indicates the presence of endogeneity. Finally, the Sargan over-identification test is used in the cases where there are more instruments than endogenous regressors. A rejection of the null in this test means that the instruments are not exclusively affecting the outcome of interest though the endogenous variable.

The code for this function, alongside an example with the well known Mroz data, is shown below.

```> mroz <- read.dta("mroz.dta")
> mroz <- mroz[,c("hours","lwage","educ","age","kidslt6","kidsge6","nwifeinc","exper")]
> ivreg2(form=hours ~ lwage + educ + age + kidslt6 + kidsge6 + nwifeinc,
+       endog="lwage",iv=c("exper"),data=na.omit(mroz))
\$results
Coef    S.E. t-stat p-val
(Intercept) 2478.435 655.207  3.783 0.000
lwage       1772.323 594.185  2.983 0.003
educ        -201.187  69.910 -2.878 1.996
age          -11.229  10.537 -1.066 1.713
kidslt6     -191.659 195.761 -0.979 1.672
kidsge6      -37.732  63.635 -0.593 1.447
nwifeinc      -9.978   7.174 -1.391 1.836

\$weakidtest
First Stage F-test
[1,]             12.965

\$endogeneity
Wu-Hausman F-test p-val
[1,]             36.38     0

\$overid
Sargan test of over-identifying restrictions
[1,] "No test performed. Model is just identified"
```
```ivreg2 <- function(form,endog,iv,data,digits=3){
# library(MASS)
# model setup
r1 <- lm(form,data)
y <- r1\$fitted.values+r1\$resid
x <- model.matrix(r1)
aa <- rbind(endog == colnames(x),1:dim(x)[2])
z <- cbind(x[,aa[2,aa[1,]==0]],data[,iv])
colnames(z)[(dim(z)[2]-length(iv)+1):(dim(z)[2])] <- iv
# iv coefficients and standard errors
z <- as.matrix(z)
pz <- z %*% (solve(crossprod(z))) %*% t(z)
biv <- solve(crossprod(x,pz) %*% x) %*% (crossprod(x,pz) %*% y)
sigiv <- crossprod((y - x %*% biv),(y - x %*% biv))/(length(y)-length(biv))
vbiv <- as.numeric(sigiv)*solve(crossprod(x,pz) %*% x)
res <- cbind(biv,sqrt(diag(vbiv)),biv/sqrt(diag(vbiv)),(1-pnorm(biv/sqrt(diag(vbiv))))*2)
res <- matrix(as.numeric(sprintf(paste("%.",paste(digits,"f",sep=""),sep=""),res)),nrow=dim(res)[1])
rownames(res) <- colnames(x)
colnames(res) <- c("Coef","S.E.","t-stat","p-val")
# First-stage F-test
y1 <- data[,endog]
z1 <- x[,aa[2,aa[1,]==0]]
bet1 <- solve(crossprod(z)) %*% crossprod(z,y1)
bet2 <- solve(crossprod(z1)) %*% crossprod(z1,y1)
rss1 <- sum((y1 - z %*% bet1)^2)
rss2 <- sum((y1 - z1 %*% bet2)^2)
p1 <- length(bet1)
p2 <- length(bet2)
n1 <- length(y)
firststage <- c(fs)
firststage <- matrix(as.numeric(sprintf(paste("%.",paste(digits,"f",sep=""),sep=""),firststage)),ncol=length(firststage))
colnames(firststage) <- c("First Stage F-test")
# Hausman tests
bols <- solve(crossprod(x)) %*% crossprod(x,y)
sigols <- crossprod((y - x %*% bols),(y - x %*% bols))/(length(y)-length(bols))
vbols <- as.numeric(sigols)*solve(crossprod(x))
sigml <- crossprod((y - x %*% bols),(y - x %*% bols))/(length(y))
x1 <- x[,!(colnames(x) %in% "(Intercept)")]
z1 <- z[,!(colnames(z) %in% "(Intercept)")]
pz1 <- z1 %*% (solve(crossprod(z1))) %*% t(z1)
biv1 <- biv[!(rownames(biv) %in% "(Intercept)"),]
bols1 <- bols[!(rownames(bols) %in% "(Intercept)"),]
# Durbin-Wu-Hausman chi-sq test:
# haus <- t(biv1-bols1) %*% ginv(as.numeric(sigml)*(solve(crossprod(x1,pz1) %*% x1)-solve(crossprod(x1)))) %*% (biv1-bols1)
# hpvl <- 1-pchisq(haus,df=1)
# Wu-Hausman F test
resids <- NULL
resids <- cbind(resids,y1 - z %*% solve(crossprod(z)) %*% crossprod(z,y1))
x2 <- cbind(x,resids)
bet1 <- solve(crossprod(x2)) %*% crossprod(x2,y)
bet2 <- solve(crossprod(x)) %*% crossprod(x,y)
rss1 <- sum((y - x2 %*% bet1)^2)
rss2 <- sum((y - x %*% bet2)^2)
p1 <- length(bet1)
p2 <- length(bet2)
n1 <- length(y)
fpval <- 1-pf(fs, p1-p2, n1-p1)
#hawu <- c(haus,hpvl,fs,fpval)
hawu <- c(fs,fpval)
hawu <- matrix(as.numeric(sprintf(paste("%.",paste(digits,"f",sep=""),sep=""),hawu)),ncol=length(hawu))
#colnames(hawu) <- c("Durbin-Wu-Hausman chi-sq test","p-val","Wu-Hausman F-test","p-val")
colnames(hawu) <- c("Wu-Hausman F-test","p-val")
# Sargan Over-id test
ivres <- y - (x %*% biv)
oid <- solve(crossprod(z)) %*% crossprod(z,ivres)
sstot <- sum((ivres-mean(ivres))^2)
sserr <- sum((ivres - (z %*% oid))^2)
rsq <- 1-(sserr/sstot)
sargan <- length(ivres)*rsq
spval <- 1-pchisq(sargan,df=length(iv)-1)
overid <- c(sargan,spval)
overid <- matrix(as.numeric(sprintf(paste("%.",paste(digits,"f",sep=""),sep=""),overid)),ncol=length(overid))
colnames(overid) <- c("Sargan test of over-identifying restrictions","p-val")
if(length(iv)-1==0){
overid <- t(matrix(c("No test performed. Model is just identified")))
colnames(overid) <- c("Sargan test of over-identifying restrictions")
}
full <- list(results=res, weakidtest=firststage, endogeneity=hawu, overid=overid)
return(full)
}

```

# Instrumental Variables without Traditional Instruments

Typically, regression models in empirical economic research suffer from at least one form of endogeneity bias.

The classic example is economic returns to schooling, where researchers want to know how much increased levels of education affect income. Estimation using a simple linear model, regressing income on schooling, alongside a bunch of control variables, will typically not yield education’s true effect on income. The problem here is one of omitted variables – notably unobserved ability. People who are more educated may be more motivated or have other unobserved characteristics which simultaneously affect schooling and future lifetime earnings.

Endogeneity bias plagues empirical research. However, there are solutions, the most common being instrumental variables (IVs). Unfortunately, the exclusion restrictions needed to justify the use of traditional IV methodology may be impossible to find.

So, what if you have an interesting research question, some data, but endogeneity with no IVs. You should give up, right? Wrong. According to Lewbel (forthcoming in Journal of Business and Economic Statistics), it is possible to overcome the endogeneity problem without the use of a traditional IV approach.

Lewbel’s paper demonstrates how higher order moment restrictions can be used to tackle endogeneity in triangular systems. Without going into too much detail (interested readers can consult Lewbel’s paper), this method is like the traditional two-stage instrumental variable approach, except the first-stage exclusion restriction is generated by the control, or exogenous, variables which we know are heteroskedastic (interested practitioners can test for this in the usual way, i.e. a White test).

In the code below, I demonstrate how one could employ this approach in R using the GMM framework outlined by Lewbel. My code only relates to a simple example with one endogenous variable and two exogenous variables. However, it would be easy to modify this code depending on the model.

```rm(list=ls())
library(gmm)
# gmm function for 1 endog variable with 2 hetero exogenous variable
# outcome in the first column of 'datmat', endog variable in second
# constant and exog variables in the next three
# hetero exog in the last two (i.e no constant)
g1 <- function(theta, datmat) {
#set up data
y1 <- matrix(datmat[,1],ncol=1)
y2 <- matrix(datmat[,2],ncol=1)
x1 <- matrix(datmat[,3:5],ncol=3)
z1 <- matrix(datmat[,4:5],ncol=2)
# if the variable in the 4th col was not hetero
# this could be modified so:
# z1 <- matrix(datmat[,5],ncol=1)

#set up moment conditions
in1 <- (y1 -theta[1]*x1[,1]-theta[2]*x1[,2]-theta[3]*x1[,3])
M <- NULL
for(i in 1:dim(z1)[2]){
M <- cbind(M,(z1[,i]-mean(z1[,i])))
}
in2 <- (y2 -theta[4]*x1[,1]-theta[5]*x1[,2]-theta[6]*x1[,3]-theta[7]*y1)
for(i in 1:dim(x1)[2]){M <- cbind(M,in1*x1[,i])}
for(i in 1:dim(x1)[2]){M <- cbind(M,in2*x1[,i])}
for(i in 1:dim(z1)[2]){M <- cbind(M,in2*((z1[,i]-mean(z1[,i]))*in1))}
return(M)
}
# so estimation is easy
# gmm(function(...), data matrix, initial values vector)
# e.g : gmm(g1, x =as.matrix(dat),c(1,1,1,1,1,1,1))
```

I also tested the performance of Lewbel’s GMM estimator in comparison a mis-specified OLS estimator. In the code below, I perform 500 simulations of a triangular system containing an omitted variable. For the GMM estimator, it is useful to have good initial starting values. In this simple example, I use the OLS coefficients. In more complicated settings, it is advisable to use the estimates from the 2SLS procedure outlined in Lewbel’s paper. The distributions of the coefficient estimates are shown in the plot below. The true value, indicated by the vertical line, is one. It is pretty evident that the Lewbel approach works very well. I think this method could be very useful in a number of research disciplines.

```beta1 <- beta2 <- NULL
for(k in 1:500){
#generate data (including intercept)
x1 <- rnorm(1000,0,1)
x2 <- rnorm(1000,0,1)
u <- rnorm(1000,0,1)
s1 <- rnorm(1000,0,1)
s2 <- rnorm(1000,0,1)
ov <- rnorm(1000,0,1)
e1 <- u + exp(x1)*s1 + exp(x2)*s1
e2 <- u + exp(-x1)*s2 + exp(-x2)*s2
y1 <- 1 + x1 + x2 + ov + e2
y2 <- 1 + x1 + x2 + y1 + 2*ov + e1
x3 <- rep(1,1000)
dat <- cbind(y1,y2,x3,x1,x2)

#record ols estimate
beta1 <- c(beta1,coef(lm(y2~x1+x2+y1))[4])
#init values for iv-gmm
init <- c(coef(lm(y2~x1+x2+y1)),coef(lm(y1~x1+x2)))
#record gmm estimate
beta2 <- c(beta2,coef(gmm(g1, x =as.matrix(dat),init))[7])
}

library(sm)
d <- data.frame(rbind(cbind(beta1,"OLS"),cbind(beta2,"IV-GMM")))
d\$beta1 <- as.numeric(as.character(d\$beta1))
sm.density.compare(d\$beta1, d\$V2,xlab=("Endogenous Coefficient"))
title("Lewbel and OLS Estimates")
legend("topright", levels(d\$V2),lty=c(1,2,3),col=c(2,3,4),bty="n")
abline(v=1)
```