In-Class Exercise 5: 2nd Order Spatial Point Patterns Analysis

Published

February 6, 2023

Modified

April 3, 2023

1 Install relevant packages

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

We only focus on local colocation quotient of the sfdep package

2 Import studyArea

studyArea <- st_read(dsn = "data",
                     layer = "study_area") %>%
  st_transform(crs = 3829)#National Projection System of Taiwan
Reading layer `study_area' from data source 
  `C:\guga-nesh\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

3 Import stores data

stores <- st_read(dsn = "data",
                  layer = "stores") %>%
  st_transform(crs = 3829)
Reading layer `stores' from data source 
  `C:\guga-nesh\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

4 Plot the values imported

# plotting functional and non-functional points in the area of study
tmap_mode("view")
tm_shape(studyArea) +  #always display the polygon first
  tm_polygons() +
tm_shape(stores) + 
  tm_dots(col = "Name", #colour coded for 7-Elevent and Family Mart
          size = 0.01,
          border.col = "black",
          border.lwd = 0.5) +
  tm_view(set.zoom.limits = c(12, 16))

5 Perform LCLQ calculation

# neighbourhood list
nb <- include_self(
  st_knn(st_geometry(stores), 6)) # i want it to search for the 6 nearest neighbours - stay with even number you will never have a balance of neighbours

# weight
wt <- st_kernel_weights(nb, # calculate weight metrics using adaptive and gaussian mtd
                        stores,
                        "gaussian",
                        adaptive = TRUE)

FamilyMart <- stores %>%
  filter(Name == "Family Mart")
A <- FamilyMart$Name # variable is a vector to be used in local_colocation()

SevenEleven <- stores %>%
  filter(Name == "7-Eleven")
B <- SevenEleven$Name
# A = target
# B = neighbour to find out if colocate or not
# once you take nsim it will take the p-value automatically
LCLQ <- local_colocation(A, B, nb, wt, 49) # this is a data table with 2 columns in order to map it we need to combine it back with the stores. NA means cannot find colocation or isolation (not significant)
LCLQ_stores <- cbind(stores, LCLQ) # only works if you don't sort the LCLQ it binds by the same table order from the orginal stores data

LCLQ_stores
Simple feature collection with 1409 features and 6 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 346837.2 ymin: 2767381 xmax: 356661.3 ymax: 2775447
Projected CRS: Hu Tzu Shan 1950 / UTM zone 51N
First 10 features:
          Name  CompNum      lat      lng X7.Eleven p_sim_7.Eleven
1  Family Mart 16080660 25.04065 121.5022  0.998818           0.04
2  Family Mart 16082885 25.04251 121.5768  0.998818           0.06
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
6  Family Mart 16098747 25.05705 121.5255        NA             NA
7  Family Mart 16434609 25.03642 121.5016        NA             NA
8  Family Mart 16435369 25.04579 121.5717        NA             NA
9  Family Mart 16435564 25.06487 121.5228        NA             NA
10 Family Mart 16438035 25.04535 121.5756        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)
6  POINT (350433.4 2772319)
7  POINT (347995.4 2770060)
8  POINT (355076.8 2771021)
9  POINT (350166.9 2773188)
10 POINT (355472.4 2770968)
# see which points are colocated and their corresponding p-value
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))