In-class Exercise 5

Published

February 6, 2023

Modified

April 3, 2023

1 Load R Packages

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

2 Importing Data

studyArea <- st_read(dsn = "data",
                     layer = "study_area") %>%
  st_transform(crs = 3829)
Reading layer `study_area' from data source 
  `C:\deadline2359\IS415-GAA\In-class_Ex\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 = "data",
                  layer = "stores") %>%
  st_transform(crs = 3829)
Reading layer `stores' from data source 
  `C:\deadline2359\IS415-GAA\In-class_Ex\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

3 Visualising sf Layers

tmap_mode("view")
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))

4 Local Colocation Quotients (LCLQ)

nb <- include_self(
  st_knn(st_geometry(stores), 6) # includes itself; hence surround neighbours = 5
)

wt <- st_kernel_weights(nb,
                        stores,
                        "gaussian",
                        adaptive = TRUE)
FamilyMart <- stores %>% 
  filter(Name == "Family Mart")
A <- FamilyMart$Name
FamilyMart
Simple feature collection with 563 features and 4 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 346891.5 ymin: 2767412 xmax: 356451.1 ymax: 2775447
Projected CRS: Hu Tzu Shan 1950 / UTM zone 51N
First 10 features:
          Name  CompNum      lat      lng                 geometry
1  Family Mart 16080660 25.04065 121.5022 POINT (348063.3 2770528)
2  Family Mart 16082885 25.04251 121.5768   POINT (355596 2770652)
3  Family Mart 16090111 25.05628 121.5407   POINT (351962 2772217)
4  Family Mart 16093150 25.03333 121.5548 POINT (353358.8 2769660)
5  Family Mart 16095713 25.02866 121.5392 POINT (351784.6 2769159)
6  Family Mart 16098747 25.05705 121.5255 POINT (350433.4 2772319)
7  Family Mart 16434609 25.03642 121.5016 POINT (347995.4 2770060)
8  Family Mart 16435369 25.04579 121.5717 POINT (355076.8 2771021)
9  Family Mart 16435564 25.06487 121.5228 POINT (350166.9 2773188)
10 Family Mart 16438035 25.04535 121.5756 POINT (355472.4 2770968)
SevenEleven <- stores %>% 
  filter(Name == "7-Eleven")
B <- SevenEleven$Name
LCLQ <- local_colocation(A, B, nb, wt, 49) # 49 simulations
LCLQ_stores <- cbind(stores, LCLQ) # appending 
# cannot do relational join as LCLQ doesn't have unique identifier
head(LCLQ_stores, 5)
Simple feature collection with 5 features and 6 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 348063.3 ymin: 2769159 xmax: 355596 ymax: 2772217
Projected CRS: Hu Tzu Shan 1950 / UTM zone 51N
         Name  CompNum      lat      lng X7.Eleven p_sim_7.Eleven
1 Family Mart 16080660 25.04065 121.5022  0.998818           0.08
2 Family Mart 16082885 25.04251 121.5768  0.998818           0.04
3 Family Mart 16090111 25.05628 121.5407        NA             NA
4 Family Mart 16093150 25.03333 121.5548        NA             NA
5 Family Mart 16095713 25.02866 121.5392        NA             NA
                  geometry
1 POINT (348063.3 2770528)
2   POINT (355596 2770652)
3   POINT (351962 2772217)
4 POINT (353358.8 2769660)
5 POINT (351784.6 2769159)
tmap_mode("view")
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))