1 Median House Price Example

library(sp)
library(sf)
library(ggplot2)
library(mapview)

Here is some data visualisation

load("datasets/gmel2.Rdata")
mapView(gmel2, zcol = "price")

2 Specify W

library(spdep)
library(spatialreg)
gmel2.sp = as(gmel2, "Spatial")
# argument: queen = TRUE by default
swpoly = poly2nb(gmel2.sp)
# Style: whether to standardise  
## "B": basic binary coding
## "W": row standardised (sums over all links to n)
colb = nb2listw(swpoly, style = "B")
colw = nb2listw(swpoly, style = "W")
apply(listw2mat(colb), 1, sum)
##  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 
##  6  3  6  7  6  5  5  4  5  5  4  5  6  6  4  4  7  3  7  4  7  4  6  5  5  3 
## 27 28 29 30 31 
##  6  2  2  4  6
apply(listw2mat(colw), 1, sum)
##  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 
##  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 
## 27 28 29 30 31 
##  1  1  1  1  1
# Construct W via distance
centre_tmp = st_centroid(gmel2$geometry)
centre = st_coordinates(centre_tmp)
n = dim(gmel2)[1]
dist_mat = as.matrix(dist(centre, diag = TRUE, upper = TRUE))
matW = 1/dist_mat
W1 = mat2listw(matW)
## Warning in mat2listw(matW): style is M (missing); style should be set to a valid
## value

3 Spatial Lag Model

Before 2020, the function lagsarlm is in the package spdep. Now it is moved to spatialreg package, and I suggest you to use spatialreg::lagsarlm to make sure you are using lagsarlm function from spatialreg package

## This is a demonstration on how to use function "lagsarlm"
## Not really about how to analyze melbourne housing in depth. 
mlag = spatialreg::lagsarlm(log(price)~dis+off + inc, data=gmel2, 
                listw=colw) 
# Just like lm output. distance and income are significant, while offense is not.
summary(mlag)
## 
## Call:spatialreg::lagsarlm(formula = log(price) ~ dis + off + inc, 
##     data = gmel2, listw = colw)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -0.2935279 -0.0820879 -0.0071624  0.0669417  0.5123931 
## 
## Type: lag 
## Coefficients: (asymptotic standard errors) 
##                Estimate  Std. Error z value Pr(>|z|)
## (Intercept)  3.57678039  1.66157026  2.1527 0.031346
## dis         -0.00817046  0.00291656 -2.8014 0.005088
## off         -0.00075528  0.00074882 -1.0086 0.313157
## inc          0.00047283  0.00015485  3.0534 0.002263
## 
## Rho: 0.70341, LR test value: 16.502, p-value: 4.8604e-05
## Asymptotic standard error: 0.12285
##     z-value: 5.7256, p-value: 1.0306e-08
## Wald statistic: 32.783, p-value: 1.0306e-08
## 
## Log likelihood: 10.3016 for lag model
## ML residual variance (sigma squared): 0.026197, (sigma: 0.16185)
## Number of observations: 31 
## Number of parameters estimated: 6 
## AIC: -8.6032, (AIC for lm: 5.8986)
## LM test for residual autocorrelation
## test value: 0.75033, p-value: 0.38637
## beta estimates and asymptotic standard error
mlag$coefficients
mlag$rest.se
##   (Intercept)           dis           off           inc 
##  3.5767803911 -0.0081704594 -0.0007552777  0.0004728276 
##  (Intercept)          dis          off          inc 
## 1.6615702586 0.0029165631 0.0007488227 0.0001548541
## rho estimates and asymptotic standard error
mlag$rho
mlag$rho.se
##       rho 
## 0.7034077 
## [1] 0.1228528
## sigma^2 estimates
mlag$s2

## AIC value
AIC(mlag)

## The loglikelihood value
mlag$LL
logLik(mlag)

## the covariance matrix of the estimator ()
vcov(mlag)
## [1] 0.0261966
## [1] -8.603195
##         [,1]
## [1,] 10.3016
## 'log Lik.' 10.3016 (df=6)
##                       rho   (Intercept)           dis           off
## rho          1.509281e-02 -2.012913e-01  1.937843e-04  5.873773e-06
## (Intercept) -2.012913e-01  2.760816e+00 -3.051004e-03 -1.993186e-04
## dis          1.937843e-04 -3.051004e-03  8.506340e-06  8.484201e-07
## off          5.873773e-06 -1.993186e-04  8.484201e-07  5.607354e-07
## inc         -4.419485e-06  1.979093e-05  1.287583e-07  3.879847e-08
##                       inc
## rho         -4.419485e-06
## (Intercept)  1.979093e-05
## dis          1.287583e-07
## off          3.879847e-08
## inc          2.397980e-08