他们每个人计算的残差是不同的。原因如下:
模型如下:
y=ρWy+xb+e和e∼n(0,1)
现在,如果我们玩弄它,我们会得到:
y=(I−ρW)−1(xb+e)
现在 LeSage 教授所做的是:
y−(I−ρ^⋅W)−1⋅xb^=(I−ρW)−1⋅e
所以你得到的是具有自动相关性的残差。
另一方面,通过变换 y:
y−ρ^⋅W⋅y=xb+e
估计,xb并计算残差,Bivand 所做的是给你e代替(I−ρW)−1⋅e
首选哪一个将取决于您的应用程序!
这里有一些代码来证明它。我没有直接使用 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效果可能无法识别,因此使用随机图生成器的策略有时可能是虚假的!
无论如何,我希望这真的解决了你的问题