pacman::p_load(sf, tmap, tidyverse, sfdep)In-class Exercise 5: Local Colocation Quotient Analysis
1 Getting Started
Using Taiwanese convenience stores as a use case.
2 Installing and loading R packages
sfdep package allows us to perform colocation methods
studyArea <- st_read(dsn="data",
layer="study_area") %>% st_transform(crs=3829)Reading layer `study_area' from data source
`C:\annatrw\IS415\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:\annatrw\IS415\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
[reference this for take home 1] [always plot polygon then points so points will not be blocked by polygon] [plotting functional and non-functional water point]
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))$tm_layout
$tm_layout$set.zoom.limits
[1] 12 16
$tm_layout$style
[1] NA
attr(,"class")
[1] "tm"
Searching for the 6 nearest neighbours (using adaptive kernel density method) - choosing 6 nearest since the target point is included in function as well (total 7 and will not get an even split)
nb <- include_self(st_knn(st_geometry(stores), 6))wt <- st_kernel_weights(nb, stores, "gaussian", adaptive=TRUE)Filtering out stores that are family marts and convert into a dataframe, before saving into a variable A
FamilyMart <- stores %>% filter(Name=="Family Mart")
A <-FamilyMart$NameSevenEleven <- stores %>% filter(Name=="7-Eleven")
B <-SevenEleven$NameA is target, B is neighbour category at 50 simulations outputs the p value directly
LCLQ <- local_colocation(A,B,nb,wt,49)LCLQ_stores <- cbind(stores,LCLQ)- cannot do relational join on LCLQ and LCLQ_stores since there is no unique identifier of LCLQ
- using cbind function only works if you do not sort the original input data
- cbind inherits the properties of the first parameter
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))$tm_layout
$tm_layout$set.zoom.limits
[1] 12 16
$tm_layout$style
[1] NA
attr(,"class")
[1] "tm"