In-class Exercise 5

Author

Wen Yang

Published

February 6, 2023

Modified

April 3, 2023

Packages

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

Study Area

studyArea <- st_read(dsn="data", layer="study_area") %>% st_transform(crs=3829)
Reading layer `study_area' from data source 
  `D:\Documents\IS415-GAA-WY\inclass-ex\inclass-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 
  `D:\Documents\IS415-GAA-WY\inclass-ex\inclass-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
#plot the polygon first
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))

Local colocation quotient

Tip

#k-nearest neigbour (adaptive)

#choose 6 is not good, cos there is a chance of equal proportion

#but with include self, it will become odd (6+1), no chance equal proportion

nb -> list of length is the distance between one point against every other point

nb <- include_self(
  st_knn(st_geometry(stores), 6))
wt <- st_kernel_weights(nb,
                        stores,
                        "gaussian",
                        adaptive=TRUE)
FamilyMart <- stores %>%
  filter(Name == "Family Mart")
A <- FamilyMart$Name
SevenEleven <- stores %>%
  filter(Name == "7-Eleven")
B <- SevenEleven$Name
#A is my target
#B is my neighbour to see if we colocate
#Run 50 simulations, will convert to p-value
#NA is common
#There are different p-value some over some below 0.05
LCLQ <- local_colocation(A, B, nb,wt, 49)
#No unique identifier, so cannot left/right join
#So no sorting, as the sequence is preserved already
#Stores must be the first to keep the geometry column
LCLQ_stores <- cbind(stores, LCLQ)

Plot the map

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))
#The map below means dot is the target is family mart that is colocated with 7-eleven