A simple example

library(AER)
data("CigarettesSW")
rprice  <- with(CigarettesSW, price/cpi)
tdiff   <- with(CigarettesSW, (taxs - tax)/cpi)
packs   <- CigarettesSW$packs

Estimate using OLS.

lm(packs ~ rprice)
##
## Call:
## lm(formula = packs ~ rprice)
##
## Coefficients:
## (Intercept)       rprice  
##     222.209       -1.044

Now using instrumental variables.

ivreg(packs ~ rprice | tdiff)
##
## Call:
## ivreg(formula = packs ~ rprice | tdiff)
##
## Coefficients:
## (Intercept)       rprice  
##     219.576       -1.019

Now using the lm function.

# first stage
lms1 <- lm(rprice ~ tdiff)

# manually obtain fitted values
lmXhat <- lms1$coefficients[2]*tdiff + lms1$coefficients[1]

# estimate second stage using Xhat
(lms2 <- lm(packs ~ lmXhat) )
##
## Call:
## lm(formula = packs ~ lmXhat)
##
## Coefficients:
## (Intercept)       lmXhat  
##     219.576       -1.019

Now using a neural network

library(nnet)
set.seed(123)

# first stage
nns1 <- nnet(rprice ~ tdiff, size=0, skip=TRUE, linout=TRUE)
## # weights:  2
## initial  value 1123401.708750
## final  value 14467.562948
## converged
# manually obtain fitted values
nnXhat <- nns1$fitted.values

# estimate second stage using Xhat
nns2 <- nnet(packs ~ nnXhat, size=0, skip=TRUE, linout=TRUE)
## # weights:  2
## initial  value 335265.176965
## final  value 48851.806790
## converged
summary(nns2)
## a 1-0-1 network with 2 weights
## options were - skip-layer connections  linear output units
##   b->o  i1->o
## 219.58  -1.02

Compare output.

lms2$coefficients - nns2$wts
##   (Intercept)        lmXhat
##  4.880515e-05 -4.206591e-07

Compare estimates.

library(ggplot2)
qplot(lms2$fitted.values - nns2$fitted.values)

plot of chunk qplot

Updated: