Skip to main content
Corrected typos and removed a sentence that was dated and no one would follow.
Source Link
mmgm
  • 445
  • 2
  • 7

I went to look into the code in detail and actually my answer was not correct. Sorry i hasted and i should not have.

The residuals that each of them calculating are different. Here is why:

Now what Prof. LeSage does ieis:

#------------------ GENERATE INSTRUMENTS
#get some instrumental variables 
z0 <- w%*%rvariable 
z1 <- w%*%w%*%rvariable


#check to see if there is a minimum of correlation ... it shoudl
cor(z0, w%*%y)
cor(z1, w%*%y)

The instruments work because rvariable is exogenous. So as long as w is exougenousexogenous we have a game!

I went to look into the code in detail and actually my answer was not correct. Sorry i hasted and i should not have.

The residuals that each of them calculating are different. Here is why:

Now what Prof. LeSage does ie:

#------------------ GENERATE INSTRUMENTS
#get some instrumental variables 
z0 <- w%*%rvariable 
z1 <- w%*%w%*%rvariable


#check to see if there is a minimum of correlation ... it shoudl
cor(z0, w%*%y)
cor(z1, w%*%y)

The instruments work because rvariable is exogenous. So as long as w is exougenous we have a game!

The residuals that each of them calculating are different. Here is why:

Now what Prof. LeSage does is:

#------------------ GENERATE INSTRUMENTS
#get some instrumental variables 
z0 <- w%*%rvariable 
z1 <- w%*%w%*%rvariable


#check to see if there is a minimum of correlation
cor(z0, w%*%y)
cor(z1, w%*%y)

The instruments work because rvariable is exogenous. So as long as w is exogenous we have a game!

formatted everything to make it clearer
Source Link
Seb
  • 571
  • 3
  • 15

y = pWy + xb + e$y = \rho Wy + xb + e$ with e ~ n(0,1)$e \sim n(0,1)$

y = (I - pW)^-1(xb + e)$y = (I - \rho W)^{-1}(xb + e)$

y - (I-p_hat * W)^-1 * xb_hat = (I-pW)^-1*e$y - (I-\hat{\rho} \cdot W)^{-1} \cdot x\hat{b} = (I-\rho W)^{-1}\cdot e$

y - p_hatWy = xb + e$y - \hat{\rho}\cdot W \cdot y = xb + e$

Estimating, xb$xb$ and calculation the residuals, what Bivand is doing is giving you e$e$ instead of (I-pW)^-1*e$(I-\rho W)^{-1}\cdot e$

A cautionary note here is that depending on the structure of w$W$ the effect might not be identifiable so the strategy of using the random graph generator might be bogus some times !

y = pWy + xb + e with e ~ n(0,1)

y = (I - pW)^-1(xb + e)

y - (I-p_hat * W)^-1 * xb_hat = (I-pW)^-1*e

y - p_hatWy = xb + e

Estimating, xb and calculation the residuals, what Bivand is doing is giving you e instead of (I-pW)^-1*e

A cautionary note here is that depending on the structure of w the effect might not be identifiable so the strategy of using the random graph generator might be bogus some times !

$y = \rho Wy + xb + e$ with $e \sim n(0,1)$

$y = (I - \rho W)^{-1}(xb + e)$

$y - (I-\hat{\rho} \cdot W)^{-1} \cdot x\hat{b} = (I-\rho W)^{-1}\cdot e$

$y - \hat{\rho}\cdot W \cdot y = xb + e$

Estimating, $xb$ and calculation the residuals, what Bivand is doing is giving you $e$ instead of $(I-\rho W)^{-1}\cdot e$

A cautionary note here is that depending on the structure of $W$ the effect might not be identifiable so the strategy of using the random graph generator might be bogus some times !

added 295 characters in body; Post Made Community Wiki
Source Link
mmgm
  • 445
  • 2
  • 7
#------------------ GENERATE SAMPLE DATA
rm(list=ls())   #clean
require(igraph) #random graphs
require(AER)    #get ivreg ...


n<-700   #700 locations
p=0.2
g <- erdos.renyi.game(n=n, p.or.m=p, type="gnp", directed=F, loops=F)
graph.density(g) 

w <- get.adjacency(g) #get an adjacency matrix
w <- w/rowSums(w)     #row standardize because of eigen vectors and eigen values
sum(rowSums(w)==0) 

rho <- 0.5
intercept   <- rep(1,n)
rvariable   <- runifrnorm(n)
y <- solve(diag(n) - rho*w) %*% ( 2*intercept + 3*rvariable + rnorm(n))
#------------------ GENERATE INSTRUMENTS
#get some instrumental variables 
z0 <- w%*%rvariable 
z1 <- w%*%w%*%rvariable
z2 <- w%*%w%*%w%*%rvariable

#check to see if there is a minimum of correlation ... it shoudl
cor(z0, w%*%y)
cor(z1, w%*%y)
cor(z2, w%*%y)
#------------------ NOW ONTO ESTIMATION

#The wrong way ...
summary(out<-lm(y ~ rvariable)) 
confint(out)

#The not so bad, but still very wrong way
summary(out<-lm(y ~ w%*%y + rvariable)) 
confint(out)

#ok now this should do it  ... not perfect beacuse 2sls is not efficient. 
#I am doing it this way because i did not want to generate random maps...
#Plus random graphs are easily available !

summary(out<-ivreg( y ~ w%*%y + rvariable, instruments=~ z0 + z1 + z2 + rvariable )) 
confint(out)

In the end you should see the residuals difference == 0 !

A cautionary note here is that depending on the structure of w the effect might not be identifiable so the strategy of using the random graph generator might be bogus some times !

Anyway I hope this really solved your question

#------------------ GENERATE SAMPLE DATA
rm(list=ls())   #clean
require(igraph) #random graphs
require(AER)    #get ivreg ...


n<-700   #700 locations
p=0.2
g <- erdos.renyi.game(n=n, p.or.m=p, type="gnp", directed=F, loops=F)
graph.density(g) 

w <- get.adjacency(g) #get an adjacency matrix
w <- w/rowSums(w)     #row standardize because of eigen vectors and eigen values
sum(rowSums(w)==0) 

rho <- 0.5
intercept   <- rep(1,n)
rvariable   <- runif(n)
y <- solve(diag(n) - rho*w) %*% ( 2*intercept + 3*rvariable + rnorm(n))
#------------------ GENERATE INSTRUMENTS
#get some instrumental variables 
z0 <- w%*%rvariable 
z1 <- w%*%w%*%rvariable
z2 <- w%*%w%*%w%*%rvariable

#check to see if there is a minimum of correlation ... it shoudl
cor(z0, w%*%y)
cor(z1, w%*%y)
cor(z2, w%*%y)
#------------------ NOW ONTO ESTIMATION

#The wrong way ...
summary(out<-lm(y ~ rvariable)) 
confint(out)

#The not so bad, but still very wrong way
summary(out<-lm(y ~ w%*%y + rvariable)) 
confint(out)

#ok now this should do it  ... not perfect beacuse 2sls is not efficient. 
#I am doing it this way because i did not want to generate random maps...
#Plus random graphs are easily available !

summary(out<-ivreg( y ~ w%*%y + rvariable, instruments=~ z0 + z1 + z2 + rvariable )) 
confint(out)
#------------------ GENERATE SAMPLE DATA
rm(list=ls())   #clean
require(igraph) #random graphs
require(AER)    #get ivreg ...


n<-700   #700 locations
p=0.2
g <- erdos.renyi.game(n=n, p.or.m=p, type="gnp", directed=F, loops=F)
graph.density(g) 

w <- get.adjacency(g) #get an adjacency matrix
w <- w/rowSums(w)     #row standardize because of eigen vectors and eigen values
sum(rowSums(w)==0) 

rho <- 0.5
intercept   <- rep(1,n)
rvariable   <- rnorm(n)
y <- solve(diag(n) - rho*w) %*% ( 2*intercept + 3*rvariable + rnorm(n))
#------------------ GENERATE INSTRUMENTS
#get some instrumental variables 
z0 <- w%*%rvariable 
z1 <- w%*%w%*%rvariable


#check to see if there is a minimum of correlation ... it shoudl
cor(z0, w%*%y)
cor(z1, w%*%y)
#------------------ NOW ONTO ESTIMATION

#The wrong way ...
summary(out<-lm(y ~ rvariable)) 
confint(out)

#The not so bad, but still very wrong way
summary(out<-lm(y ~ w%*%y + rvariable)) 
confint(out)

#ok now this should do it  ... not perfect beacuse 2sls is not efficient. 
#I am doing it this way because i did not want to generate random maps...
#Plus random graphs are easily available !

summary(out<-ivreg( y ~ w%*%y + rvariable, instruments=~ z0 + z1 + rvariable )) 
confint(out)

In the end you should see the residuals difference == 0 !

A cautionary note here is that depending on the structure of w the effect might not be identifiable so the strategy of using the random graph generator might be bogus some times !

Anyway I hope this really solved your question

deleted 43 characters in body
Source Link
mmgm
  • 445
  • 2
  • 7
Loading
deleted 150 characters in body
Source Link
mmgm
  • 445
  • 2
  • 7
Loading
deleted 150 characters in body
Source Link
mmgm
  • 445
  • 2
  • 7
Loading
deleted 150 characters in body
Source Link
mmgm
  • 445
  • 2
  • 7
Loading
deleted 150 characters in body
Source Link
mmgm
  • 445
  • 2
  • 7
Loading
deleted 21 characters in body
Source Link
mmgm
  • 445
  • 2
  • 7
Loading
added 14 characters in body
Source Link
mmgm
  • 445
  • 2
  • 7
Loading
added 155 characters in body
Source Link
mmgm
  • 445
  • 2
  • 7
Loading
added 5 characters in body
Source Link
mmgm
  • 445
  • 2
  • 7
Loading
Source Link
mmgm
  • 445
  • 2
  • 7
Loading