空间计量经济学——计算残差

机器算法验证 计量经济学 空间的 残差
2022-03-18 22:19:39

考虑由 James P LeSage 用 SAR 表示的空间计量经济学模型:

y=ρWy+Xβ+ϵ

我使用来自www.spatial-econometrics.comR的 LeSage 的软件包spdep和计量经济学工具箱但是,在比较这两种计算残差的方式时,存在差异。Roger Bivand的软件包通过以下方式计算它们R

y˙=yρWy

然后线性回归y˙X矩阵。从此拟合中获得残差。( lm(y-rWy~X-1)。然而 LeSage 以这种方式计算残差(我最初认为这是正确的方式):

y(ρWy)1Xb

所以这是我的问题:

  1. 为什么两位作者计算残差的方式不同?
  2. 是否有人反对计算它们的一种或另一种方式?

谢谢你的帮助!

1个回答

他们每个人计算的残差是不同的。原因如下:

模型如下:

y=ρWy+xb+een(0,1)

现在,如果我们玩弄它,我们会得到:

y=(IρW)1(xb+e)

现在 LeSage 教授所做的是:

y(Iρ^W)1xb^=(IρW)1e

所以你得到的是具有自动相关性的残差。

另一方面,通过变换 y:

yρ^Wy=xb+e

估计,xb并计算残差,Bivand 所做的是给你e代替(IρW)1e

首选哪一个将取决于您的应用程序!

这里有一些代码来证明它。我没有直接使用 SPDEP,因为我不确定如何创建随机地图……但没关系,代码很简单:

#------------------ 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))

根据 SAR LAG 模型生成数据后,我们将通过 2SLS 估计它(我告诉过你我们可以)。

#------------------ 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)

这些工具之所以有效,是因为rvariable是外生的。所以只要w是外生的,我们就有一个游戏!

#------------------ 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)

现在真正重要的是残差的计算:

#residuals LeSage way
y_hat0    <- solve(diag(n) - coef(out)[2]*w ) %*% ( coef(out)[1]*intercept + coef(out)[3]*rvariable )
u_hat0    <- y - y_hat0

#residuals BiVand way
y_tilda   <- y - coef(out)[2]*w%*%y
summary(out_biv   <- lm( y_tilda ~ rvariable ))
#ok they are not the same due to rounding error ...
coef(out)[3] == coef(out_biv)[2]; round(coef(out)[3],5) == round(coef(out_biv)[2],5)

u_hat1 <- residuals(out_biv)
u_hat1 <- solve(diag(n) - coef(out)[2]*w)%*%u_hat1

#If we give Bivand some taste of autocorrelation it is the same as LeSage ...
round( u_hat0 - u_hat1, 5)

最后你应该看到残差差 == 0 !

这里需要注意的是,取决于W效果可能无法识别,因此使用随机图生成器的策略有时可能是虚假的!

无论如何,我希望这真的解决了你的问题