In-Class Exercise 5: Local Colocation Quotients

Calculating and Visualising the Local Colocation of 7-Eleven and Family Mart Convenience Store Chains in Taipei
In-Class Exercise
sf
tidyverse
tmap
Author

Teo Ren Jie

Published

February 6, 2023

1 Getting Started

1.1 Installing and Loading Packages

Firstly, the code below will check if pacman has been installed. If it has not been installed, R will download and install it, before activating it for use during this session.

if (!require('pacman', character.only = T)){
  install.packages('pacman')
}
Loading required package: pacman
library('pacman')

Next, pacman assists us by helping us load R packages that we require, sf, tidyverse and tmap.

pacman::p_load(tidyverse, tmap, sf, sfdep)

1.2 Importing Data

studyArea <- st_read(dsn = "In-Class_Ex05/data", layer = "study_area") %>%
  st_transform(crs = 3829)
Reading layer `study_area' from data source 
  `C:\renjie-teo\IS415-GAA\exercises\In-Class_Ex05\data' using driver `ESRI Shapefile'
Simple feature collection with 7 features and 7 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 121.4836 ymin: 25.00776 xmax: 121.592 ymax: 25.09288
Geodetic CRS:  TWD97
stores <- st_read(dsn = "In-Class_Ex05/data", layer = "stores") %>%
  st_transform(crs = 3829)
Reading layer `stores' from data source 
  `C:\renjie-teo\IS415-GAA\exercises\In-Class_Ex05\data' using driver `ESRI Shapefile'
Simple feature collection with 1409 features and 4 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 121.4902 ymin: 25.01257 xmax: 121.5874 ymax: 25.08557
Geodetic CRS:  TWD97

1.3 Visualising SF Layers

tmap_mode("view")
tmap mode set to interactive viewing
tm_shape(studyArea) +
  tm_polygons() +
tm_shape(stores) +
  tm_dots(col = "Name",
          size = 0.01,
          border.col = "black",
          border.lwd = 0.5) +
tm_view(set.zoom.limits = c(12,16))
Note

The polygon layers should always be above the dots. The geospatial layers are rendered in sequence, hence if polygons are rendered after the dots (ie. dots are ontop of the polygon code), the dots might be coverd by the polygon line/fill.

2 Local Colocation Quotients (LCLQ)

nb <- include_self(
  st_knn(st_geometry(stores), 6))
wt <- st_kernel_weights(nb, stores, "gaussian")
Warning in spdep::knearneigh(pnts, k): knearneigh: identical points found
Warning in spdep::knearneigh(pnts, k): knearneigh: kd_tree not available for
identical points

The variable nb stores the values of its neighbours. The number 6 defines 6 points including itself and 5 other neighbours.

The variable wt will balance weights according to distance to centre point. The further points will get lower weights.

FamilyMart <- stores %>%
  filter(Name == "Family Mart")
A <- FamilyMart$Name

SevenEleven <- stores %>%
  filter(Name == "7-Eleven")
B <- SevenEleven$Name

LCLQ <- local_colocation(A, B, nb, wt, 49) #49 = 50 simulations and immediately see p value

LCLQ_stores <- cbind(stores, LCLQ)

tmap_mode("view")
tmap mode set to interactive viewing
tmap_mode("view")
tmap mode set to interactive viewing
tm_shape(studyArea) +
  tm_polygons() +
tm_shape(LCLQ_stores) +
  tm_dots(col = "X7.Eleven",
          size = 0.01,
          border.col = "black",
          border.lwd = 0.5) +
tm_view(set.zoom.limits = c(12,16))