library(spdep)
example(columbus)
col.listw <- nb2listw(col.gal.nb)
x=seq(1:49)
n<-length(x)
xibar <- (rep(sum(x), n) - x)/(n - 1)
si2 <- ((rep(sum(x^2), n) - x^2)/(n - 1)) - xibar^2
Wi <- sapply(col.listw$weights, sum)
#[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
#[37] 1 1 1 1 1 1 1 1 1 1 1 1 1
Wi2=Wi^2
S1i <- sapply(col.listw$weights, function(x) sum(x^2))
# [1] 0.5000000 0.3333333 0.2500000 0.2500000 0.1428571 0.5000000 0.2500000
# [8] 0.1666667 0.1250000 0.2500000 0.2000000 0.1666667 0.2500000 0.1666667
#[15] 0.1666667 0.1428571 0.3333333 0.2500000 0.3333333 0.1000000 0.3333333
#[22] 0.1666667 0.3333333 0.1428571 0.1428571 0.1666667 0.2500000 0.1111111
#[29] 0.1428571 0.2500000 0.5000000 0.2500000 0.2500000 0.2500000 0.1428571
#[36] 0.2000000 0.1666667 0.1666667 0.5000000 0.2000000 0.3333333 0.5000000
#[43] 0.1666667 0.2000000 0.2500000 0.5000000 0.5000000 0.2500000 0.3333333
lx <- lag.listw(col.listw, x)
res <- (lx - Wi * xibar)
res <- res/sqrt(si2 * (((n - 1) * S1i - Wi^2)/(n - 2)))
res
[1] -2.37332110 -2.90731303 -3.33404263 -3.10096296 -3.52712088 -1.87601018
[7] -2.00517335 -3.07281364 -2.00272695 -1.21686333 -2.32942618 -2.51955856
[13] -2.18641859 -2.01890149 -1.80019026 -1.63006479 -0.93223336 -1.00013383
[19] -0.80158055 -0.07496224 0.52694104 -0.91849373 -0.25298002 -0.48562024
[25] -0.79300074 -0.66119098 0.11454945 1.21862840 0.95192246 0.41343356
[31] 1.01485374 0.89135794 0.38706277 1.11682531 2.06289781 2.24404180
[37] 1.96425487 2.06215004 1.64678694 1.70734475 1.88352479 1.05182914
[43] 3.05170115 3.00125143 2.90624770 1.32511796 1.63837602 3.08496360
[49] 2.70457746
############
但是function lag.listw()
要转成matlab比较麻烦
因为lag.listw()是调用C function