6  Exercises Ib

\[ \newcommand{\Exp}{\mathrm{E}} \newcommand\given[1][]{\:#1\vert\:} \newcommand{\Cov}{\mathrm{Cov}} \newcommand{\Var}{\mathrm{Var}} \newcommand{\rank}{\mathrm{rank}} \newcommand{\bm}[1]{\boldsymbol{\mathbf{#1}}} \]

Required packages

pkgs <- c("sf", "mapview", "areal", "spdep", "spatialreg", "tmap", "viridisLite",
          "ggplot2", "ggthemes", "gridExtra") # note: load spdep first, then spatialreg
lapply(pkgs, require, character.only = TRUE)

Session info

R version 4.6.0 (2026-04-24 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 26200)

Matrix products: default
  LAPACK version 3.12.1

locale:
[1] LC_COLLATE=German_Germany.utf8  LC_CTYPE=German_Germany.utf8   
[3] LC_MONETARY=German_Germany.utf8 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.utf8    

time zone: Europe/Berlin
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
 [1] gridExtra_2.3     ggthemes_5.2.0    ggplot2_4.0.3    
 [4] viridisLite_0.4.3 tmap_4.4          spatialreg_1.4-3 
 [7] Matrix_1.7-5      spdep_1.4-2       spData_2.3.5     
[10] areal_0.1.8       mapview_2.11.4    sf_1.1-1         

loaded via a namespace (and not attached):
 [1] tidyselect_1.2.1       dplyr_1.2.1           
 [3] farver_2.1.2           S7_0.2.2              
 [5] fastmap_1.2.0          leaflegend_1.2.8      
 [7] leaflet_2.2.3          TH.data_1.1-5         
 [9] XML_3.99-0.23          digest_0.6.39         
[11] lifecycle_1.0.5        LearnBayes_2.15.2     
[13] survival_3.8-6         terra_1.9-27          
[15] magrittr_2.0.5         compiler_4.6.0        
[17] rlang_1.2.0            tools_4.6.0           
[19] data.table_1.18.4      knitr_1.51            
[21] htmlwidgets_1.6.4      sp_2.2-1              
[23] classInt_0.4-11        RColorBrewer_1.1-3    
[25] multcomp_1.4-30        abind_1.4-8           
[27] KernSmooth_2.23-26     purrr_1.2.2           
[29] withr_3.0.2            leafsync_0.1.0        
[31] grid_4.6.0             stats4_4.6.0          
[33] cols4all_0.10          e1071_1.7-17          
[35] leafem_0.2.5           colorspace_2.1-2      
[37] spacesXYZ_1.6-0        scales_1.4.0          
[39] MASS_7.3-65            cli_3.6.6             
[41] mvtnorm_1.3-7          rmarkdown_2.31        
[43] generics_0.1.4         otel_0.2.0            
[45] rstudioapi_0.18.0      tmaptools_3.3         
[47] DBI_1.3.0              proxy_0.4-29          
[49] stringr_1.6.0          splines_4.6.0         
[51] stars_0.7-2            parallel_4.6.0        
[53] s2_1.1.11              base64enc_0.1-6       
[55] marginaleffects_0.32.0 vctrs_0.7.3           
[57] boot_1.3-32            sandwich_3.1-1        
[59] jsonlite_2.0.0         crosstalk_1.2.2       
[61] maptiles_0.11.0        units_1.0-1           
[63] glue_1.8.1             lwgeom_0.2-16         
[65] codetools_0.2-20       stringi_1.8.7         
[67] gtable_0.3.6           deldir_2.0-4          
[69] raster_3.6-32          tibble_3.3.1          
[71] logger_0.4.2           pillar_1.11.1         
[73] htmltools_0.5.9        satellite_1.0.6       
[75] R6_2.6.1               wk_0.9.5              
[77] microbenchmark_1.5.0   evaluate_1.0.5        
[79] lattice_0.22-9         png_0.1-9             
[81] backports_1.5.1        class_7.3-23          
[83] Rcpp_1.1.1-1.1         coda_0.19-4.1         
[85] nlme_3.1-169           xfun_0.57             
[87] zoo_1.8-15             pkgconfig_2.0.3       

Reload data from pervious session

load("_data/msoa2_spatial.RData")

6.1 General Exercises

6) Please use the msoa.spdf and calculate a neighbours weights matrix of the nearest 10 neighbours (see spdep::knearneigh()), and create a listw object using row normalization.

7) OPTIONAL: Can you create a map containing the MSOA unit “City of London” (this is the most central MSOA around Tower Bridge, its code is MSOA11CD = “E02000001”). Can you also identify its ten nearest neighbours (the ten MSOA surrounding MSOA11CD = “E02000001”) and also plot them all together?

8) Please calculate the queens neighbours and make a listw object that includes the second order neighbours (the neighbours of the neighbours). See the function nblag().

9) Please calculate the inverse distance weighted neighbours with a 5km cutoff point (create the listw object using nb2listwdist() and minmax normalisation.

10) Chose any characteristic from the data (e.g. ethnic groups or house prices) and calculate global Moran’s I for it.

11) Produce a LISA cluster map for the characteristic you have chosen.

12) Transform the queens neighbours listw object into a matrix format.

13) What do you get when you multiply a variable (data column) such as the home owner rate with your weights matrix?