Objectives

In this Take-home Exercise, we are going to conduct a use-case to demonstrate the potential contribution of geospatial analytics in R to integrate, analyse and communicate teh analysis results by using open data provided by different government agencies. The specific objectives of the study are as follow:

Loading packages

packages = c('sp','rgdal', 'rgeos','sf', 'spdep', 'tmap', 'tidyverse','rvest','spatstat', 'raster', 'maptools')
for (p in packages){
  if(!require(p, character.only = T)){
    install.packages(p)
  }
  library(p,character.only = T)
}
## Loading required package: sp
## Loading required package: rgdal
## rgdal: version: 1.5-16, (SVN revision 1050)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.0.4, released 2020/01/28
## Path to GDAL shared files: C:/Users/John Ng/Documents/R/win-library/4.0/rgdal/gdal
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 6.3.1, February 10th, 2020, [PJ_VERSION: 631]
## Path to PROJ shared files: C:/Users/John Ng/Documents/R/win-library/4.0/rgdal/proj
## Linking to sp version:1.4-2
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading rgdal.
## Loading required package: rgeos
## rgeos version: 0.5-3, (SVN revision 634)
##  GEOS runtime version: 3.8.0-CAPI-1.13.1 
##  Linking to sp version: 1.4-2 
##  Polygon checking: TRUE
## Loading required package: sf
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
## Loading required package: spdep
## Loading required package: spData
## To access larger datasets in this package, install the spDataLarge
## package with: `install.packages('spDataLarge',
## repos='https://nowosad.github.io/drat/', type='source')`
## Loading required package: tmap
## Loading required package: tidyverse
## -- Attaching packages --------------------------------------------------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2     v purrr   0.3.4
## v tibble  3.0.3     v dplyr   1.0.2
## v tidyr   1.1.1     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.0
## -- Conflicts ------------------------------------------------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
## Loading required package: rvest
## Loading required package: xml2
## 
## Attaching package: 'rvest'
## The following object is masked from 'package:purrr':
## 
##     pluck
## The following object is masked from 'package:readr':
## 
##     guess_encoding
## Loading required package: spatstat
## Loading required package: spatstat.data
## Loading required package: nlme
## 
## Attaching package: 'nlme'
## The following object is masked from 'package:dplyr':
## 
##     collapse
## Loading required package: rpart
## Registered S3 method overwritten by 'spatstat':
##   method     from
##   print.boxx cli
## 
## spatstat 1.64-1       (nickname: 'Help you I can, yes!') 
## For an introduction to spatstat, type 'beginner'
## 
## Note: spatstat version 1.64-1 is out of date by more than 5 months; we recommend upgrading to the latest version.
## Loading required package: raster
## 
## Attaching package: 'raster'
## The following objects are masked from 'package:spatstat':
## 
##     area, rotate, shift
## The following object is masked from 'package:nlme':
## 
##     getData
## The following object is masked from 'package:dplyr':
## 
##     select
## The following object is masked from 'package:tidyr':
## 
##     extract
## Loading required package: maptools
## Checking rgeos availability: TRUE

The task

In this take-home exercise, you are tasked to analyse the grographic distribution of childcare and kindergarten services by using appropriate map analysis and spatial point patterns analysis techniques. The specific tasks are as follows:

Section A: The supply and demand of childcare and kindergarten services by planning subzone

1) Exploratory Spatial Data Analysis

Import shapefile

sf_mpsz <- st_read(dsn = "Data/shapefile", layer = "MP14_SUBZONE_NO_SEA_PL")
## Reading layer `MP14_SUBZONE_NO_SEA_PL' from data source `D:\Users\John_Ng\Year 3 Term 1\(Monday) IS415 Geospatial Analytics & Applns (SMU-X)\Take home exercise\IS415_Take_Home_Exercise_01_NG_XUN_JIE\Data\shapefile' using driver `ESRI Shapefile'
## Simple feature collection with 323 features and 15 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 2667.538 ymin: 15748.72 xmax: 56396.44 ymax: 50256.33
## projected CRS:  SVY21

Import Sg boundary data

sg <- readOGR(dsn = 'Data/shapefile' , layer = 'CostalOutline')
## OGR data source with driver: ESRI Shapefile 
## Source: "D:\Users\John_Ng\Year 3 Term 1\(Monday) IS415 Geospatial Analytics & Applns (SMU-X)\Take home exercise\IS415_Take_Home_Exercise_01_NG_XUN_JIE\Data\shapefile", layer: "CostalOutline"
## with 60 features
## It has 4 fields
summary(sg)
## Object of class SpatialPolygonsDataFrame
## Coordinates:
##         min      max
## x  2663.926 56047.79
## y 16357.981 50244.03
## Is projected: TRUE 
## proj4string :
## [+proj=tmerc +lat_0=1.36666666666667 +lon_0=103.833333333333 +k=1
## +x_0=28001.642 +y_0=38744.572 +datum=WGS84 +units=m +no_defs]
## Data attributes:
##     GDO_GID          MSLINK          MAPID    COSTAL_NAM       
##  Min.   : 1.00   Min.   : 1.00   Min.   :0   Length:60         
##  1st Qu.:15.75   1st Qu.:17.75   1st Qu.:0   Class :character  
##  Median :30.50   Median :33.50   Median :0   Mode  :character  
##  Mean   :30.50   Mean   :33.77   Mean   :0                     
##  3rd Qu.:45.25   3rd Qu.:49.25   3rd Qu.:0                     
##  Max.   :60.00   Max.   :67.00   Max.   :0

Import kml file

This code read the KML file as a Spatial object

sf_preschool = read_sf("data/kml/pre-schools-location-kml.kml")

###Inspecting the class of sf_preschool

class(sf_preschool)
## [1] "sf"         "tbl_df"     "tbl"        "data.frame"

Inspect data in tibble form

It can be seen here that the data that we would like to use like the centre name is hidden in the Description column, within a myriad of html tags.

sf_preschool %>%
  glimpse()
## Rows: 1,925
## Columns: 3
## $ Name        <chr> "kml_1", "kml_2", "kml_3", "kml_4", "kml_5", "kml_6", "...
## $ Description <chr> "<center><table><tr><th colspan='2' align='center'><em>...
## $ geometry    <POINT [°]> POINT Z (103.7009 1.338325 0), POINT Z (103.8987 ...

Map out sf_preschool

plot(sf_preschool)

Get the attributes for each observation

This code chunk require rvest package find out more about that package using a simple lapply

source = https://stackoverflow.com/questions/50775357/how-to-read-in-kml-file-properly-in-r-or-separate-out-lumped-variables-into-col

attributes <- lapply(X = 1:nrow(sf_preschool), 
                     FUN = function(x) {

                       sf_preschool %>% 
                         slice(x) %>%
                         pull(Description) %>%
                         read_html() %>%
                         html_node("table") %>%
                         html_table(header = TRUE, trim = TRUE, dec = ".", fill = TRUE) %>%
                         as_tibble(.name_repair = ~ make.names(c("Attribute", "Value"))) %>% 
                         pivot_wider(names_from = Attribute, values_from = Value)

                     })

Bind the attributes to each observation as new columns

sf_preschool_attr <- 
  sf_preschool %>%
  bind_cols(bind_rows(attributes)) %>%
  dplyr::select(-Description)

Using glimpse() function, we are able to see the different datas hidden in Description before.

sf_preschool_attr %>%
  glimpse()
## Rows: 1,925
## Columns: 8
## $ Name        <chr> "kml_1", "kml_2", "kml_3", "kml_4", "kml_5", "kml_6", "...
## $ CENTRE_NAME <chr> "BRILLIANT TOTS PTE. LTD.", "BUBBLESLAND PLAYHOUSE PTE ...
## $ CENTRE_CODE <chr> "PT9334", "PT7680", "PT9527", "PT3150", "PT9117", "PT90...
## $ ADDRESS     <chr> "610, JURONG WEST STREET 65, #01 - 534, S 640610", "238...
## $ POSTAL_CODE <chr> "640610", "540238", "737856", "730369", "542327", "8212...
## $ INC_CRC     <chr> "0523C7904478A63D", "18BED05A501AA168", "C88B9AC31EE71B...
## $ FMEL_UPD_D  <chr> "20200812235534", "20200812235534", "20200812235534", "...
## $ geometry    <POINT [°]> POINT Z (103.7009 1.338325 0), POINT Z (103.8987 ...

Finding out the contents of a SpatialDataframe

summary(sf_preschool_attr)
##      Name           CENTRE_NAME        CENTRE_CODE          ADDRESS         
##  Length:1925        Length:1925        Length:1925        Length:1925       
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##  POSTAL_CODE          INC_CRC           FMEL_UPD_D                 geometry   
##  Length:1925        Length:1925        Length:1925        POINT Z      :1925  
##  Class :character   Class :character   Class :character   epsg:4326    :   0  
##  Mode  :character   Mode  :character   Mode  :character   +proj=long...:   0
head(sf_preschool_attr, n=4)
## Simple feature collection with 4 features and 7 fields
## geometry type:  POINT
## dimension:      XYZ
## bbox:           xmin: 103.7009 ymin: 1.338325 xmax: 103.8987 ymax: 1.438017
## z_range:        zmin: 0 zmax: 0
## geographic CRS: WGS 84
##    Name                   CENTRE_NAME CENTRE_CODE
## 1 kml_1      BRILLIANT TOTS PTE. LTD.      PT9334
## 2 kml_2 BUBBLESLAND PLAYHOUSE PTE LTD      PT7680
## 3 kml_3        BUCKET HOUSE PRESCHOOL      PT9527
## 4 kml_4  BUMBLE BEE CHILD CARE CENTRE      PT3150
##                                                   ADDRESS POSTAL_CODE
## 1         610, JURONG WEST STREET 65, #01 - 534, S 640610      640610
## 2              238, COMPASSVALE WALK, #01 - 542, S 540238      540238
## 3 39, WOODLANDS CLOSE, #01 - 62, MEGA@WOODLANDS, S 737856      737856
## 4            369, WOODLANDS AVENUE 1, #01 - 853, S 730369      730369
##            INC_CRC     FMEL_UPD_D                      geometry
## 1 0523C7904478A63D 20200812235534 POINT Z (103.7009 1.338325 0)
## 2 18BED05A501AA168 20200812235534  POINT Z (103.8987 1.39044 0)
## 3 C88B9AC31EE71BF6 20200812235534 POINT Z (103.8068 1.438017 0)
## 4 64AB8FACA8F60129 20200812235534 POINT Z (103.7874 1.433436 0)
class(sf_preschool_attr)
## [1] "sf"         "data.frame"

Finding the differences of the childcare services + kindergartens dataset from the pre-schools location dataset

sf_kindergartens = read_sf("data/kml/kindergartens-kml.kml")
sf_childcare_services = read_sf("data/kml/child-care-services-kml.kml")


sf_kindergartens %>%
  glimpse()
## Rows: 448
## Columns: 3
## $ Name        <chr> "kml_1", "kml_2", "kml_3", "kml_4", "kml_5", "kml_6", "...
## $ Description <chr> "<center><table><tr><th colspan='2' align='center'><em>...
## $ geometry    <POINT [°]> POINT Z (103.8409 1.37915 0), POINT Z (103.7397 1...
sf_childcare_services %>%
  glimpse()
## Rows: 1,545
## Columns: 3
## $ Name        <chr> "kml_1", "kml_2", "kml_3", "kml_4", "kml_5", "kml_6", "...
## $ Description <chr> "<center><table><tr><th colspan='2' align='center'><em>...
## $ geometry    <POINT [°]> POINT Z (103.8331 1.42972 0), POINT Z (103.8138 1...

Since kml data is mainly stored in the Description column, and hidden between html tags, we will employ the same data extraction method from before.

Using a simple lapply

For sf_kindergartens

attributes_k <- lapply(X = 1:nrow(sf_kindergartens), 
                     FUN = function(x) {

                       sf_kindergartens %>% 
                         slice(x) %>%
                         pull(Description) %>%
                         read_html() %>%
                         html_node("table") %>%
                         html_table(header = TRUE, trim = TRUE, dec = ".", fill = TRUE) %>%
                         as_tibble(.name_repair = ~ make.names(c("Attribute", "Value"))) %>% 
                         pivot_wider(names_from = Attribute, values_from = Value)

                     })

For sf_childcare_services

attributes_c <- lapply(X = 1:nrow(sf_childcare_services), 
                     FUN = function(x) {

                       sf_childcare_services %>% 
                         slice(x) %>%
                         pull(Description) %>%
                         read_html() %>%
                         html_node("table") %>%
                         html_table(header = TRUE, trim = TRUE, dec = ".", fill = TRUE) %>%
                         as_tibble(.name_repair = ~ make.names(c("Attribute", "Value"))) %>% 
                         pivot_wider(names_from = Attribute, values_from = Value)

                     })

Bind the attributes to each observation as new columns

For sf_kindergartens

sf_kindergartens_attr <- 
  sf_kindergartens %>%
  bind_cols(bind_rows(attributes_k)) %>%
  dplyr::select(-Description)
sf_kindergartens_attr %>%
  glimpse()
## Rows: 448
## Columns: 17
## $ Name                    <chr> "kml_1", "kml_2", "kml_3", "kml_4", "kml_5"...
## $ ADDRESSBLOCKHOUSENUMBER <chr> "", "", "", "", "", "", "", "", "", "", "",...
## $ ADDRESSBUILDINGNAME     <chr> "", "", "", "", "", "", "", "", "", "", "",...
## $ ADDRESSFLOORNUMBER      <chr> "", "", "", "", "", "", "", "", "", "", "",...
## $ ADDRESSPOSTALCODE       <chr> "560644", "600251", "600317", "671455", "67...
## $ ADDRESSSTREETNAME       <chr> "644 Ang Mo Kio Ave 4  #01-850 S(560644)", ...
## $ ADDRESSTYPE             <chr> "", "", "", "", "", "", "", "", "", "", "",...
## $ DESCRIPTION             <chr> "Kindergartens", "Kindergartens", "Kinderga...
## $ HYPERLINK               <chr> "", "", "", "", "", "", "", "", "", "", "",...
## $ LANDXADDRESSPOINT       <chr> "0", "0", "0", "0", "0", "0", "0", "0", "0"...
## $ LANDYADDRESSPOINT       <chr> "0", "0", "0", "0", "0", "0", "0", "0", "0"...
## $ NAME                    <chr> "PCF Sparkletots Preschool @ Yio Chu Kang B...
## $ PHOTOURL                <chr> "", "", "", "", "", "", "", "", "", "", "",...
## $ INC_CRC                 <chr> "904D106E26156265", "F735342764BD6BCC", "56...
## $ FMEL_UPD_D              <chr> "20200813015028", "20200813015028", "202008...
## $ ADDRESSUNITNUMBER       <chr> "", "", "", "", "", "", "", "", "", "", "",...
## $ geometry                <POINT [°]> POINT Z (103.8409 1.37915 0), POINT Z...

For sf_childcare_services

sf_childcare_services_attr <- 
  sf_childcare_services %>%
  bind_cols(bind_rows(attributes_c)) %>%
  dplyr::select(-Description)
sf_childcare_services_attr %>%
  glimpse()
## Rows: 1,545
## Columns: 17
## $ Name                    <chr> "kml_1", "kml_2", "kml_3", "kml_4", "kml_5"...
## $ ADDRESSBLOCKHOUSENUMBER <chr> "", "", "", "", "", "", "", "", "", "", "",...
## $ ADDRESSBUILDINGNAME     <chr> "", "", "", "", "", "", "", "", "", "", "",...
## $ ADDRESSPOSTALCODE       <chr> "760742", "159053", "556912", "569139", "46...
## $ ADDRESSSTREETNAME       <chr> "742, YISHUN AVENUE 5, #01 - 470, SINGAPORE...
## $ ADDRESSTYPE             <chr> "", "", "", "", "", "", "", "", "", "", "",...
## $ DESCRIPTION             <chr> "Child Care Services", "Child Care Services...
## $ HYPERLINK               <chr> "", "", "", "", "", "", "", "", "", "", "",...
## $ LANDXADDRESSPOINT       <chr> "0", "0", "0", "0", "0", "0", "0", "0", "0"...
## $ LANDYADDRESSPOINT       <chr> "0", "0", "0", "0", "0", "0", "0", "0", "0"...
## $ NAME                    <chr> "AVERBEL CHILD DEVELOPMENT CENTRE PTE LTD",...
## $ PHOTOURL                <chr> "", "", "", "", "", "", "", "", "", "", "",...
## $ ADDRESSFLOORNUMBER      <chr> "", "", "", "", "", "", "", "", "", "", "",...
## $ INC_CRC                 <chr> "AEA27114446235CE", "86B24416FB1663C6", "F9...
## $ FMEL_UPD_D              <chr> "20200826094036", "20200826094036", "202008...
## $ ADDRESSUNITNUMBER       <chr> "", "", "", "", "", "", "", "", "", "", "",...
## $ geometry                <POINT [°]> POINT Z (103.8331 1.42972 0), POINT Z...

Import csv file

popdata <- read_csv("data/attributes/respopagesextod2011to2019.csv")
popdata
## # A tibble: 883,728 x 7
##    PA        SZ               AG    Sex    TOD                         Pop  Time
##    <chr>     <chr>            <chr> <chr>  <chr>                     <dbl> <dbl>
##  1 Ang Mo K~ Ang Mo Kio Town~ 0_to~ Males  HDB 1- and 2-Room Flats       0  2011
##  2 Ang Mo K~ Ang Mo Kio Town~ 0_to~ Males  HDB 3-Room Flats             10  2011
##  3 Ang Mo K~ Ang Mo Kio Town~ 0_to~ Males  HDB 4-Room Flats             30  2011
##  4 Ang Mo K~ Ang Mo Kio Town~ 0_to~ Males  HDB 5-Room and Executive~    50  2011
##  5 Ang Mo K~ Ang Mo Kio Town~ 0_to~ Males  HUDC Flats (excluding th~     0  2011
##  6 Ang Mo K~ Ang Mo Kio Town~ 0_to~ Males  Landed Properties             0  2011
##  7 Ang Mo K~ Ang Mo Kio Town~ 0_to~ Males  Condominiums and Other A~    40  2011
##  8 Ang Mo K~ Ang Mo Kio Town~ 0_to~ Males  Others                        0  2011
##  9 Ang Mo K~ Ang Mo Kio Town~ 0_to~ Femal~ HDB 1- and 2-Room Flats       0  2011
## 10 Ang Mo K~ Ang Mo Kio Town~ 0_to~ Femal~ HDB 3-Room Flats             10  2011
## # ... with 883,718 more rows
distinct(popdata, AG)
## # A tibble: 19 x 1
##    AG         
##    <chr>      
##  1 0_to_4     
##  2 5_to_9     
##  3 10_to_14   
##  4 15_to_19   
##  5 20_to_24   
##  6 25_to_29   
##  7 30_to_34   
##  8 35_to_39   
##  9 40_to_44   
## 10 45_to_49   
## 11 50_to_54   
## 12 55_to_59   
## 13 60_to_64   
## 14 65_to_69   
## 15 70_to_74   
## 16 75_to_79   
## 17 80_to_84   
## 18 85_to_89   
## 19 90_and_over

Data Preparation for population data

popdata2019 <- popdata %>%
  filter(Time == 2019) %>%
  group_by(PA,SZ,AG)%>%
  summarise(`POP` = sum(`Pop`)) %>%
  ungroup() %>%
  spread(AG, POP)%>% 
  dplyr::select(1:3, "5_to_9", everything()) %>% # rearrange the order of the spreaded AG column
  mutate(`5_to_6_YO` = `5_to_9`/5 * 2 ) %>%  # assuming that all age group follows uniform distribution, calculate the number of 5 and 6 years old 
  mutate(`Num_children_under_6` = rowSums(.[3]) + rowSums(.[22])) %>% # age is from 0 to 6 years old
  mutate(`4_YO` = `0_to_4`/5) %>% # this is column 24 
  mutate(`Num_children_4_to_6` = rowSums(.[24]) + rowSums(.[22])) %>% # age is from 4 to 6 years old 
  dplyr::select(`PA`, `SZ`, , `0_to_4`, `5_to_9`, `5_to_6_YO`,`Num_children_under_6`, `4_YO`, `Num_children_4_to_6`)
## `summarise()` regrouping output by 'PA', 'SZ' (override with `.groups` argument)

Create Georelation join

Using planning subzone name as unique identifier

popdata2019 <-popdata2019 %>%
  mutate_at(.vars = vars(PA,SZ),
            .funs = funs(toupper)) 
## Warning: `funs()` is deprecated as of dplyr 0.8.0.
## Please use a list of either functions or lambdas: 
## 
##   # Simple named list: 
##   list(mean = mean, median = median)
## 
##   # Auto named with `tibble::lst()`: 
##   tibble::lst(mean, median)
## 
##   # Using lambdas
##   list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
mpszpop2019 <- left_join(sf_mpsz,popdata2019, by = c("SUBZONE_N" = "SZ"))
tm_shape(mpszpop2019) + 
  tm_fill("Num_children_under_6", 
          n= 6, 
          style = "quantile", 
          palette = "Reds") + 
  tm_layout(main.title = "Distribution of Children under the age of 6",
            main.title.position = "center",
            main.title.size = 1.2,
            legend.height = 0.45,
            legend.width = 0.35,
            frame = TRUE) + 
  tm_borders(alpha = 0.5) +
  tm_compass(type = "8star", size = 2) + 
  tm_scale_bar(width = 0.15) + 
  tm_grid(lwd = 0.5, alpha = 0.5 )

top_n(mpszpop2019, 5, "Num_children_under_6")
## Simple feature collection with 323 features and 22 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 2667.538 ymin: 15748.72 xmax: 56396.44 ymax: 50256.33
## projected CRS:  SVY21
## First 10 features:
##    OBJECTID SUBZONE_NO           SUBZONE_N SUBZONE_C CA_IND    PLN_AREA_N
## 1         1          2       PEOPLE'S PARK    OTSZ02      Y        OUTRAM
## 2         2          2         BUKIT MERAH    BMSZ02      N   BUKIT MERAH
## 3         3          3           CHINATOWN    OTSZ03      Y        OUTRAM
## 4         4          4             PHILLIP    DTSZ04      Y DOWNTOWN CORE
## 5         5          5       RAFFLES PLACE    DTSZ05      Y DOWNTOWN CORE
## 6         6          4        CHINA SQUARE    OTSZ04      Y        OUTRAM
## 7         7         10         TIONG BAHRU    BMSZ10      N   BUKIT MERAH
## 8         8         12    BAYFRONT SUBZONE    DTSZ12      Y DOWNTOWN CORE
## 9         9          4 TIONG BAHRU STATION    BMSZ04      N   BUKIT MERAH
## 10       10          6       CLIFFORD PIER    DTSZ06      Y DOWNTOWN CORE
##    PLN_AREA_C       REGION_N REGION_C          INC_CRC FMEL_UPD_D   X_ADDR
## 1          OT CENTRAL REGION       CR B4120D23006C932A 2016-05-11 28831.78
## 2          BM CENTRAL REGION       CR 1C51019439A68700 2016-05-11 26360.80
## 3          OT CENTRAL REGION       CR 0FF1661344C84AED 2016-05-11 29153.97
## 4          DT CENTRAL REGION       CR 615D4EDDEF809F8E 2016-05-11 29706.72
## 5          DT CENTRAL REGION       CR 72107B11807074F4 2016-05-11 29968.62
## 6          OT CENTRAL REGION       CR B609DF5587626C8F 2016-05-11 29509.64
## 7          BM CENTRAL REGION       CR A0FB4B68155D164A 2016-05-11 27785.67
## 8          DT CENTRAL REGION       CR 197F5E664DA4D5E1 2016-05-11 30806.24
## 9          BM CENTRAL REGION       CR 91FFE927ABE3E4DB 2016-05-11 27277.47
## 10         DT CENTRAL REGION       CR 945CC212CA80626F 2016-05-11 30379.50
##      Y_ADDR SHAPE_Leng SHAPE_Area            PA 0_to_4 5_to_9 5_to_6_YO
## 1  29419.65  1822.1927   93140.44        OUTRAM      0      0         0
## 2  29384.14  3074.9632  411722.82   BUKIT MERAH     20     40        16
## 3  29158.04  4297.5999  587222.68        OUTRAM    480    630       252
## 4  29744.91   871.5549   39437.94 DOWNTOWN CORE      0      0         0
## 5  29572.76  1872.7522  188767.49 DOWNTOWN CORE      0      0         0
## 6  29646.45  1605.2797  133006.94        OUTRAM     20     30        12
## 7  29590.40  3303.2149  448127.58   BUKIT MERAH    760    670       268
## 8  29530.17  2897.1264  521200.52 DOWNTOWN CORE      0      0         0
## 9  29607.02  2506.6879  350787.56   BUKIT MERAH    600    770       308
## 10 29776.43  2405.9909  261843.90 DOWNTOWN CORE      0      0         0
##    Num_children_under_6 4_YO Num_children_4_to_6                       geometry
## 1                     0    0                   0 MULTIPOLYGON (((29099.02 29...
## 2                    36    4                  20 MULTIPOLYGON (((26750.09 29...
## 3                   732   96                 348 MULTIPOLYGON (((29161.2 297...
## 4                     0    0                   0 MULTIPOLYGON (((29814.11 29...
## 5                     0    0                   0 MULTIPOLYGON (((30137.77 29...
## 6                    32    4                  16 MULTIPOLYGON (((29699.44 29...
## 7                  1028  152                 420 MULTIPOLYGON (((27748.04 29...
## 8                     0    0                   0 MULTIPOLYGON (((30844.87 29...
## 9                   908  120                 428 MULTIPOLYGON (((27444.04 29...
## 10                    0    0                   0 MULTIPOLYGON (((30436.73 29...
ggplot(data=mpszpop2019,
          aes(x = "",
            y = Num_children_under_6)) +
        geom_boxplot()

### Work with projection Check the projection of sf_mpsz.

st_crs(sf_mpsz)
## Coordinate Reference System:
##   User input: SVY21 
##   wkt:
## PROJCRS["SVY21",
##     BASEGEOGCRS["SVY21[WGS84]",
##         DATUM["World Geodetic System 1984",
##             ELLIPSOID["WGS 84",6378137,298.257223563,
##                 LENGTHUNIT["metre",1]],
##             ID["EPSG",6326]],
##         PRIMEM["Greenwich",0,
##             ANGLEUNIT["Degree",0.0174532925199433]]],
##     CONVERSION["unnamed",
##         METHOD["Transverse Mercator",
##             ID["EPSG",9807]],
##         PARAMETER["Latitude of natural origin",1.36666666666667,
##             ANGLEUNIT["Degree",0.0174532925199433],
##             ID["EPSG",8801]],
##         PARAMETER["Longitude of natural origin",103.833333333333,
##             ANGLEUNIT["Degree",0.0174532925199433],
##             ID["EPSG",8802]],
##         PARAMETER["Scale factor at natural origin",1,
##             SCALEUNIT["unity",1],
##             ID["EPSG",8805]],
##         PARAMETER["False easting",28001.642,
##             LENGTHUNIT["metre",1],
##             ID["EPSG",8806]],
##         PARAMETER["False northing",38744.572,
##             LENGTHUNIT["metre",1],
##             ID["EPSG",8807]]],
##     CS[Cartesian,2],
##         AXIS["(E)",east,
##             ORDER[1],
##             LENGTHUNIT["metre",1,
##                 ID["EPSG",9001]]],
##         AXIS["(N)",north,
##             ORDER[2],
##             LENGTHUNIT["metre",1,
##                 ID["EPSG",9001]]]]
sf_mpsz3414 <- st_set_crs(sf_mpsz, 3414)
## Warning: st_crs<- : replacing crs does not reproject data; use st_transform for
## that
st_crs(sf_mpsz3414)
## Coordinate Reference System:
##   User input: EPSG:3414 
##   wkt:
## PROJCRS["SVY21 / Singapore TM",
##     BASEGEOGCRS["SVY21",
##         DATUM["SVY21",
##             ELLIPSOID["WGS 84",6378137,298.257223563,
##                 LENGTHUNIT["metre",1]]],
##         PRIMEM["Greenwich",0,
##             ANGLEUNIT["degree",0.0174532925199433]],
##         ID["EPSG",4757]],
##     CONVERSION["Singapore Transverse Mercator",
##         METHOD["Transverse Mercator",
##             ID["EPSG",9807]],
##         PARAMETER["Latitude of natural origin",1.36666666666667,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8801]],
##         PARAMETER["Longitude of natural origin",103.833333333333,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8802]],
##         PARAMETER["Scale factor at natural origin",1,
##             SCALEUNIT["unity",1],
##             ID["EPSG",8805]],
##         PARAMETER["False easting",28001.642,
##             LENGTHUNIT["metre",1],
##             ID["EPSG",8806]],
##         PARAMETER["False northing",38744.572,
##             LENGTHUNIT["metre",1],
##             ID["EPSG",8807]]],
##     CS[Cartesian,2],
##         AXIS["northing (N)",north,
##             ORDER[1],
##             LENGTHUNIT["metre",1]],
##         AXIS["easting (E)",east,
##             ORDER[2],
##             LENGTHUNIT["metre",1]],
##     USAGE[
##         SCOPE["unknown"],
##         AREA["Singapore"],
##         BBOX[1.13,103.59,1.47,104.07]],
##     ID["EPSG",3414]]

Transforming the projection from wgs81 to svy21

Check the projection of sf_preschool_attr

st_crs(sf_preschool_attr)
## Coordinate Reference System:
##   User input: WGS 84 
##   wkt:
## GEOGCRS["WGS 84",
##     DATUM["World Geodetic System 1984",
##         ELLIPSOID["WGS 84",6378137,298.257223563,
##             LENGTHUNIT["metre",1]]],
##     PRIMEM["Greenwich",0,
##         ANGLEUNIT["degree",0.0174532925199433]],
##     CS[ellipsoidal,2],
##         AXIS["geodetic latitude (Lat)",north,
##             ORDER[1],
##             ANGLEUNIT["degree",0.0174532925199433]],
##         AXIS["geodetic longitude (Lon)",east,
##             ORDER[2],
##             ANGLEUNIT["degree",0.0174532925199433]],
##     ID["EPSG",4326]]

Transform sf_preschool_attr simple feature dataframe onto svy21 projected coordinate system (i.e. EPSG 3414)

sf_preschool_attr3414 <- st_transform(sf_preschool_attr, 3414)
st_crs(sf_preschool_attr3414)
## Coordinate Reference System:
##   User input: EPSG:3414 
##   wkt:
## PROJCRS["SVY21 / Singapore TM",
##     BASEGEOGCRS["SVY21",
##         DATUM["SVY21",
##             ELLIPSOID["WGS 84",6378137,298.257223563,
##                 LENGTHUNIT["metre",1]]],
##         PRIMEM["Greenwich",0,
##             ANGLEUNIT["degree",0.0174532925199433]],
##         ID["EPSG",4757]],
##     CONVERSION["Singapore Transverse Mercator",
##         METHOD["Transverse Mercator",
##             ID["EPSG",9807]],
##         PARAMETER["Latitude of natural origin",1.36666666666667,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8801]],
##         PARAMETER["Longitude of natural origin",103.833333333333,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8802]],
##         PARAMETER["Scale factor at natural origin",1,
##             SCALEUNIT["unity",1],
##             ID["EPSG",8805]],
##         PARAMETER["False easting",28001.642,
##             LENGTHUNIT["metre",1],
##             ID["EPSG",8806]],
##         PARAMETER["False northing",38744.572,
##             LENGTHUNIT["metre",1],
##             ID["EPSG",8807]]],
##     CS[Cartesian,2],
##         AXIS["northing (N)",north,
##             ORDER[1],
##             LENGTHUNIT["metre",1]],
##         AXIS["easting (E)",east,
##             ORDER[2],
##             LENGTHUNIT["metre",1]],
##     USAGE[
##         SCOPE["unknown"],
##         AREA["Singapore"],
##         BBOX[1.13,103.59,1.47,104.07]],
##     ID["EPSG",3414]]

Point-in-polygon count

Finding out numbers of pre-school in each Planning Subzone using st_intersects(). Then, the length() is used to calculate numbers of pre-school fall inside each planning subzone.

sf_mpsz3414$`PreSch Count`<- lengths(st_intersects(sf_mpsz3414, sf_preschool_attr3414))

Summary statistics of the newly derived PreSch Count field by using summary() are as shown in the code chunk below.

summary(sf_mpsz3414$`PreSch Count`)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.00    0.00    3.00    5.96    9.00   58.00

To list the planning subzone with the most number of of pre-school, the top_n() of dplyr package is used as shown in the code chunk below.

top_n(sf_mpsz3414, 1, `PreSch Count`)
## Simple feature collection with 1 feature and 16 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 39655.33 ymin: 35966 xmax: 42940.57 ymax: 38622.37
## projected CRS:  SVY21 / Singapore TM
##   OBJECTID SUBZONE_NO     SUBZONE_N SUBZONE_C CA_IND PLN_AREA_N PLN_AREA_C
## 1      142          2 TAMPINES EAST    TMSZ02      N   TAMPINES         TM
##      REGION_N REGION_C          INC_CRC FMEL_UPD_D   X_ADDR   Y_ADDR SHAPE_Leng
## 1 EAST REGION       ER 21658EAAF84F4D8D 2016-05-11 41122.55 37392.39   10180.62
##   SHAPE_Area                       geometry PreSch Count
## 1    4339824 MULTIPOLYGON (((42196.76 38...           58
tmap_mode("plot")
## tmap mode set to plotting
tm_shape(sf_mpsz3414)+
tm_bubbles(col = "red",
           size = 1,
           border.col = "black",
           border.lwd = 1)

Create Georelation join

Using planning subzone name as unique identifier

popdata2019 <-popdata2019 %>%
  mutate_at(.vars = vars(PA,SZ),
            .funs = funs(toupper)) 
sf_mpsz3414pop2019 <- left_join(sf_mpsz3414,popdata2019, by = c("SUBZONE_N" = "SZ"))
tmap_mode("plot")
## tmap mode set to plotting
## tmap mode set to plotting

tm1 <- tm_shape(sf_mpsz3414pop2019) + 
  tm_fill("Num_children_under_6", 
          n= 6, 
          style = "equal", 
          palette = "Reds") + 
  tm_layout(main.title = "Distribution of Children under the age of 6",
            main.title.position = "center",
            main.title.size = 1.2,
            legend.height = 0.45,
            legend.width = 0.35,
            frame = TRUE) + 
  tm_borders(alpha = 0.5) +
  tm_compass(type = "8star", size = 2) + 
  tm_scale_bar(width = 0.15) + 
  tm_grid(lwd = 0.5, alpha = 0.5 )
tm2 <- tm_shape(sf_mpsz3414pop2019) + tm_bubbles(size = "PreSch Count")

tmap_arrange(tm1, tm2, ncol=2)

Description: According to the pre-school count that we have generated, it can be assumed that pre-schools supply in individual subzone seems to be proportional with the number of children aged below 7, where subzones with higher number of children under the age of 7 contain higher number of pre-school facilities.

2) Analytics mapping

Work with projection Transforming the projection from wgs81 to svy21

Check the projection of sf_preschool_attr

st_crs(sf_kindergartens_attr)
## Coordinate Reference System:
##   User input: WGS 84 
##   wkt:
## GEOGCRS["WGS 84",
##     DATUM["World Geodetic System 1984",
##         ELLIPSOID["WGS 84",6378137,298.257223563,
##             LENGTHUNIT["metre",1]]],
##     PRIMEM["Greenwich",0,
##         ANGLEUNIT["degree",0.0174532925199433]],
##     CS[ellipsoidal,2],
##         AXIS["geodetic latitude (Lat)",north,
##             ORDER[1],
##             ANGLEUNIT["degree",0.0174532925199433]],
##         AXIS["geodetic longitude (Lon)",east,
##             ORDER[2],
##             ANGLEUNIT["degree",0.0174532925199433]],
##     ID["EPSG",4326]]
st_crs(sf_childcare_services_attr)
## Coordinate Reference System:
##   User input: WGS 84 
##   wkt:
## GEOGCRS["WGS 84",
##     DATUM["World Geodetic System 1984",
##         ELLIPSOID["WGS 84",6378137,298.257223563,
##             LENGTHUNIT["metre",1]]],
##     PRIMEM["Greenwich",0,
##         ANGLEUNIT["degree",0.0174532925199433]],
##     CS[ellipsoidal,2],
##         AXIS["geodetic latitude (Lat)",north,
##             ORDER[1],
##             ANGLEUNIT["degree",0.0174532925199433]],
##         AXIS["geodetic longitude (Lon)",east,
##             ORDER[2],
##             ANGLEUNIT["degree",0.0174532925199433]],
##     ID["EPSG",4326]]

Transform sf_kindergartens_attr and sf_childcare_services_attr simple feature dataframes onto svy21 projected coordinate system (i.e. EPSG 3414)

sf_kindergartens_attr3414 <- st_transform(sf_kindergartens_attr, 3414)
st_crs(sf_kindergartens_attr3414)
## Coordinate Reference System:
##   User input: EPSG:3414 
##   wkt:
## PROJCRS["SVY21 / Singapore TM",
##     BASEGEOGCRS["SVY21",
##         DATUM["SVY21",
##             ELLIPSOID["WGS 84",6378137,298.257223563,
##                 LENGTHUNIT["metre",1]]],
##         PRIMEM["Greenwich",0,
##             ANGLEUNIT["degree",0.0174532925199433]],
##         ID["EPSG",4757]],
##     CONVERSION["Singapore Transverse Mercator",
##         METHOD["Transverse Mercator",
##             ID["EPSG",9807]],
##         PARAMETER["Latitude of natural origin",1.36666666666667,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8801]],
##         PARAMETER["Longitude of natural origin",103.833333333333,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8802]],
##         PARAMETER["Scale factor at natural origin",1,
##             SCALEUNIT["unity",1],
##             ID["EPSG",8805]],
##         PARAMETER["False easting",28001.642,
##             LENGTHUNIT["metre",1],
##             ID["EPSG",8806]],
##         PARAMETER["False northing",38744.572,
##             LENGTHUNIT["metre",1],
##             ID["EPSG",8807]]],
##     CS[Cartesian,2],
##         AXIS["northing (N)",north,
##             ORDER[1],
##             LENGTHUNIT["metre",1]],
##         AXIS["easting (E)",east,
##             ORDER[2],
##             LENGTHUNIT["metre",1]],
##     USAGE[
##         SCOPE["unknown"],
##         AREA["Singapore"],
##         BBOX[1.13,103.59,1.47,104.07]],
##     ID["EPSG",3414]]
sf_childcare_services_attr3414 <- st_transform(sf_childcare_services_attr, 3414)
st_crs(sf_childcare_services_attr3414)
## Coordinate Reference System:
##   User input: EPSG:3414 
##   wkt:
## PROJCRS["SVY21 / Singapore TM",
##     BASEGEOGCRS["SVY21",
##         DATUM["SVY21",
##             ELLIPSOID["WGS 84",6378137,298.257223563,
##                 LENGTHUNIT["metre",1]]],
##         PRIMEM["Greenwich",0,
##             ANGLEUNIT["degree",0.0174532925199433]],
##         ID["EPSG",4757]],
##     CONVERSION["Singapore Transverse Mercator",
##         METHOD["Transverse Mercator",
##             ID["EPSG",9807]],
##         PARAMETER["Latitude of natural origin",1.36666666666667,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8801]],
##         PARAMETER["Longitude of natural origin",103.833333333333,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8802]],
##         PARAMETER["Scale factor at natural origin",1,
##             SCALEUNIT["unity",1],
##             ID["EPSG",8805]],
##         PARAMETER["False easting",28001.642,
##             LENGTHUNIT["metre",1],
##             ID["EPSG",8806]],
##         PARAMETER["False northing",38744.572,
##             LENGTHUNIT["metre",1],
##             ID["EPSG",8807]]],
##     CS[Cartesian,2],
##         AXIS["northing (N)",north,
##             ORDER[1],
##             LENGTHUNIT["metre",1]],
##         AXIS["easting (E)",east,
##             ORDER[2],
##             LENGTHUNIT["metre",1]],
##     USAGE[
##         SCOPE["unknown"],
##         AREA["Singapore"],
##         BBOX[1.13,103.59,1.47,104.07]],
##     ID["EPSG",3414]]

Point-in-polygon count

Finding out numbers of childcare and kindergarten services in each Planning Subzone using st_intersects(). Then, the length() is used to calculate numbers of pre-school fall inside each planning subzone.

sf_mpsz3414$`Childcare Count`<- lengths(st_intersects(sf_mpsz3414, sf_childcare_services_attr3414))
sf_mpsz3414$`Kindergarten Count`<- lengths(st_intersects(sf_mpsz3414, sf_kindergartens_attr3414))

Summary statistics of the newly derived PreSch Count field by using summary() are as shown in the code chunk below.

summary(sf_mpsz3414$`Childcare Count`)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.000   0.000   3.000   4.783   7.500  42.000
summary(sf_mpsz3414$`Kindergarten Count`)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.000   0.000   1.000   1.387   2.000  17.000

To list the planning subzone with the most number of of pre-school, the top_n() of dplyr package is used as shown in the code chunk below.

top_n(sf_mpsz3414, 5, `Childcare Count`)
## Simple feature collection with 6 features and 18 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 23449.05 ymin: 33973.71 xmax: 42940.57 ymax: 47996.47
## projected CRS:  SVY21 / Singapore TM
##   OBJECTID SUBZONE_NO            SUBZONE_N SUBZONE_C CA_IND PLN_AREA_N
## 1      142          2        TAMPINES EAST    TMSZ02      N   TAMPINES
## 2      167          3        TAMPINES WEST    TMSZ03      N   TAMPINES
## 3      173          4          BEDOK NORTH    BDSZ04      N      BEDOK
## 4      285          3       WOODLANDS EAST    WDSZ03      N  WOODLANDS
## 5      292          2            RIVERVALE    SESZ02      N   SENGKANG
## 6      314          3 SENGKANG TOWN CENTRE    SESZ03      N   SENGKANG
##   PLN_AREA_C          REGION_N REGION_C          INC_CRC FMEL_UPD_D   X_ADDR
## 1         TM       EAST REGION       ER 21658EAAF84F4D8D 2016-05-11 41122.55
## 2         TM       EAST REGION       ER 2E3EA3D1BBF9A601 2016-05-11 39421.66
## 3         BD       EAST REGION       ER A2254301F85C1EDF 2016-05-11 39429.21
## 4         WD      NORTH REGION       NR C90769E43EE6B0F2 2016-05-11 24506.64
## 5         SE NORTH-EAST REGION      NER 986666487FF7CF78 2016-05-11 35977.61
## 6         SE NORTH-EAST REGION      NER 5A2D0E9E6B285069 2016-05-11 35163.81
##     Y_ADDR SHAPE_Leng SHAPE_Area                       geometry PreSch Count
## 1 37392.39  10180.624    4339824 MULTIPOLYGON (((42196.76 38...           58
## 2 36739.21   8058.336    3475210 MULTIPOLYGON (((39918.43 35...           30
## 3 34737.62   8414.962    3203663 MULTIPOLYGON (((40284.24 35...           31
## 4 46991.63   6603.608    2553464 MULTIPOLYGON (((24786.75 46...           47
## 5 41060.80   6315.954    1569035 MULTIPOLYGON (((37015.07 40...           26
## 6 41501.14   5216.401    1455508 MULTIPOLYGON (((35615.75 40...           30
##   Childcare Count Kindergarten Count
## 1              42                 17
## 2              23                  9
## 3              25                  7
## 4              42                  5
## 5              23                  4
## 6              27                  3
top_n(sf_mpsz3414, 5, `Kindergarten Count`)
## Simple feature collection with 5 features and 18 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 32605.74 ymin: 31587.44 xmax: 42940.57 ymax: 40973.79
## projected CRS:  SVY21 / Singapore TM
##   OBJECTID SUBZONE_NO      SUBZONE_N SUBZONE_C CA_IND    PLN_AREA_N PLN_AREA_C
## 1       93          2         KATONG    MPSZ02      N MARINE PARADE         MP
## 2      117          4       ALJUNIED    GLSZ04      N       GEYLANG         GL
## 3      121          5        FRANKEL    BDSZ05      N         BEDOK         BD
## 4      142          2  TAMPINES EAST    TMSZ02      N      TAMPINES         TM
## 5      233          2 PASIR RIS WEST    PRSZ02      N     PASIR RIS         PR
##         REGION_N REGION_C          INC_CRC FMEL_UPD_D   X_ADDR   Y_ADDR
## 1 CENTRAL REGION       CR 55705659E2A91D11 2016-05-11 35680.90 32176.35
## 2 CENTRAL REGION       CR 83AFAB768B6B2B66 2016-05-11 33592.58 32970.83
## 3    EAST REGION       ER B34F041CC4B050EC 2016-05-11 37694.55 33007.27
## 4    EAST REGION       ER 21658EAAF84F4D8D 2016-05-11 41122.55 37392.39
## 5    EAST REGION       ER 504681216FE2A24B 2016-05-11 39851.41 40140.22
##   SHAPE_Leng SHAPE_Area                       geometry PreSch Count
## 1   5062.815    1078992 MULTIPOLYGON (((36317.74 32...           15
## 2   7100.699    2959368 MULTIPOLYGON (((34449.13 33...           30
## 3   8750.386    4297141 MULTIPOLYGON (((36551.53 31...           27
## 4  10180.624    4339824 MULTIPOLYGON (((42196.76 38...           58
## 5   5128.495    1583440 MULTIPOLYGON (((40016.02 39...           18
##   Childcare Count Kindergarten Count
## 1               9                 11
## 2              22                 10
## 3              18                 10
## 4              42                 17
## 5              11                 10
tmap_mode("plot")
## tmap mode set to plotting
tm_child <- tm_shape(sf_mpsz3414)+
tm_bubbles(size = "Childcare Count",
          col = "red",
           #size = 1,
           border.col = "black",
           border.lwd = 1)
tm_kindergarten <- tm_shape(sf_mpsz3414)+
tm_bubbles( size = "Kindergarten Count",
          col = "blue",
           #size = 1,
           border.col = "black",
           border.lwd = 1)

tmap_arrange(tm_child, tm_kindergarten)

3) Geocommunication

Tampines East has the highest number of Chilcare and Kindergarten count as compared to any other subzones. However, preliminary analysis on the number of child below 6 years old shows that Tiong Bahru and Tiong Bahru Station subzones has the highest population of children below 6 years old. This could mean that either the parents of the child actually sends their children to other subzones to acquire childcare and kindergarten services or the land scarce Tiong Bahru subzone which is near the Central Business District do not have enough land space for such facilities to be built.

Section B: Spatial Point Pattern Analysis

4) Exploratory Spatial Data Analtysis

Changing projection of SG coastal outline data

sg <- spTransform(sg, CRS("+init=EPSG:3414"))
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO"): Discarded
## datum SVY21 in CRS definition

Converting point feature data.frame to SpatialPointsDataFrame

sp_preschool <- as_Spatial(sf_preschool_attr3414)
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO"): Discarded datum Unknown based on WGS84 ellipsoid in CRS definition,
##  but +towgs84= values preserved
sp_chilcare <-as_Spatial(sf_childcare_services_attr3414)
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO"): Discarded datum Unknown based on WGS84 ellipsoid in CRS definition,
##  but +towgs84= values preserved
sp_kindergarten <- as_Spatial(sf_kindergartens_attr3414)
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO"): Discarded datum Unknown based on WGS84 ellipsoid in CRS definition,
##  but +towgs84= values preserved
sp_mpsz <- as_Spatial(sf_mpsz3414)
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO"): Discarded datum Unknown based on WGS84 ellipsoid in CRS definition,
##  but +towgs84= values preserved
summary(sp_preschool)
## Object of class SpatialPointsDataFrame
## Coordinates:
##                min      max
## coords.x1 11203.01 45404.24
## coords.x2 25596.33 49300.88
## coords.x3     0.00     0.00
## Is projected: TRUE 
## proj4string :
## [+proj=tmerc +lat_0=1.36666666666667 +lon_0=103.833333333333 +k=1
## +x_0=28001.642 +y_0=38744.572 +ellps=WGS84 +towgs84=0,0,0,0,0,0,0
## +units=m +no_defs]
## Number of points: 1925
## Data attributes:
##      Name           CENTRE_NAME        CENTRE_CODE          ADDRESS         
##  Length:1925        Length:1925        Length:1925        Length:1925       
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##  POSTAL_CODE          INC_CRC           FMEL_UPD_D       
##  Length:1925        Length:1925        Length:1925       
##  Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character
summary(sp_chilcare)
## Object of class SpatialPointsDataFrame
## Coordinates:
##                min      max
## coords.x1 11203.01 45404.24
## coords.x2 25667.60 49300.88
## coords.x3     0.00     0.00
## Is projected: TRUE 
## proj4string :
## [+proj=tmerc +lat_0=1.36666666666667 +lon_0=103.833333333333 +k=1
## +x_0=28001.642 +y_0=38744.572 +ellps=WGS84 +towgs84=0,0,0,0,0,0,0
## +units=m +no_defs]
## Number of points: 1545
## Data attributes:
##      Name           ADDRESSBLOCKHOUSENUMBER ADDRESSBUILDINGNAME
##  Length:1545        Length:1545             Length:1545        
##  Class :character   Class :character        Class :character   
##  Mode  :character   Mode  :character        Mode  :character   
##  ADDRESSPOSTALCODE  ADDRESSSTREETNAME  ADDRESSTYPE        DESCRIPTION       
##  Length:1545        Length:1545        Length:1545        Length:1545       
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##   HYPERLINK         LANDXADDRESSPOINT  LANDYADDRESSPOINT      NAME          
##  Length:1545        Length:1545        Length:1545        Length:1545       
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##    PHOTOURL         ADDRESSFLOORNUMBER   INC_CRC           FMEL_UPD_D       
##  Length:1545        Length:1545        Length:1545        Length:1545       
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##  ADDRESSUNITNUMBER 
##  Length:1545       
##  Class :character  
##  Mode  :character
summary(sp_kindergarten)
## Object of class SpatialPointsDataFrame
## Coordinates:
##                min      max
## coords.x1 11909.70 43395.47
## coords.x2 25596.33 48562.06
## coords.x3     0.00     0.00
## Is projected: TRUE 
## proj4string :
## [+proj=tmerc +lat_0=1.36666666666667 +lon_0=103.833333333333 +k=1
## +x_0=28001.642 +y_0=38744.572 +ellps=WGS84 +towgs84=0,0,0,0,0,0,0
## +units=m +no_defs]
## Number of points: 448
## Data attributes:
##      Name           ADDRESSBLOCKHOUSENUMBER ADDRESSBUILDINGNAME
##  Length:448         Length:448              Length:448         
##  Class :character   Class :character        Class :character   
##  Mode  :character   Mode  :character        Mode  :character   
##  ADDRESSFLOORNUMBER ADDRESSPOSTALCODE  ADDRESSSTREETNAME  ADDRESSTYPE       
##  Length:448         Length:448         Length:448         Length:448        
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##  DESCRIPTION         HYPERLINK         LANDXADDRESSPOINT  LANDYADDRESSPOINT 
##  Length:448         Length:448         Length:448         Length:448        
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##      NAME             PHOTOURL           INC_CRC           FMEL_UPD_D       
##  Length:448         Length:448         Length:448         Length:448        
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##  ADDRESSUNITNUMBER 
##  Length:448        
##  Class :character  
##  Mode  :character
summary(sp_mpsz)
## Object of class SpatialPolygonsDataFrame
## Coordinates:
##         min      max
## x  2667.538 56396.44
## y 15748.721 50256.33
## Is projected: TRUE 
## proj4string :
## [+proj=tmerc +lat_0=1.36666666666667 +lon_0=103.833333333333 +k=1
## +x_0=28001.642 +y_0=38744.572 +ellps=WGS84 +towgs84=0,0,0,0,0,0,0
## +units=m +no_defs]
## Data attributes:
##     OBJECTID       SUBZONE_NO      SUBZONE_N          SUBZONE_C        
##  Min.   :  1.0   Min.   : 1.000   Length:323         Length:323        
##  1st Qu.: 81.5   1st Qu.: 2.000   Class :character   Class :character  
##  Median :162.0   Median : 4.000   Mode  :character   Mode  :character  
##  Mean   :162.0   Mean   : 4.625                                        
##  3rd Qu.:242.5   3rd Qu.: 6.500                                        
##  Max.   :323.0   Max.   :17.000                                        
##     CA_IND           PLN_AREA_N         PLN_AREA_C          REGION_N        
##  Length:323         Length:323         Length:323         Length:323        
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##    REGION_C           INC_CRC            FMEL_UPD_D             X_ADDR     
##  Length:323         Length:323         Min.   :2016-05-11   Min.   : 5093  
##  Class :character   Class :character   1st Qu.:2016-05-11   1st Qu.:21864  
##  Mode  :character   Mode  :character   Median :2016-05-11   Median :28465  
##                                        Mean   :2016-05-11   Mean   :27257  
##                                        3rd Qu.:2016-05-11   3rd Qu.:31674  
##                                        Max.   :2016-05-11   Max.   :50425  
##      Y_ADDR        SHAPE_Leng        SHAPE_Area        PreSch.Count  
##  Min.   :19579   Min.   :  871.5   Min.   :   39438   Min.   : 0.00  
##  1st Qu.:31776   1st Qu.: 3709.6   1st Qu.:  628261   1st Qu.: 0.00  
##  Median :35113   Median : 5211.9   Median : 1229894   Median : 3.00  
##  Mean   :36106   Mean   : 6524.4   Mean   : 2420882   Mean   : 5.96  
##  3rd Qu.:39869   3rd Qu.: 6942.6   3rd Qu.: 2106483   3rd Qu.: 9.00  
##  Max.   :49553   Max.   :68083.9   Max.   :69748299   Max.   :58.00  
##  Childcare.Count  Kindergarten.Count
##  Min.   : 0.000   Min.   : 0.000    
##  1st Qu.: 0.000   1st Qu.: 0.000    
##  Median : 3.000   Median : 1.000    
##  Mean   : 4.783   Mean   : 1.387    
##  3rd Qu.: 7.500   3rd Qu.: 2.000    
##  Max.   :42.000   Max.   :17.000
sp_chilcare
## class       : SpatialPointsDataFrame 
## features    : 1545 
## extent      : 11203.01, 45404.24, 25667.6, 49300.88  (xmin, xmax, ymin, ymax)
## crs         : +proj=tmerc +lat_0=1.36666666666667 +lon_0=103.833333333333 +k=1 +x_0=28001.642 +y_0=38744.572 +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs 
## variables   : 16
## names       :    Name, ADDRESSBLOCKHOUSENUMBER, ADDRESSBUILDINGNAME, ADDRESSPOSTALCODE,                                                                       ADDRESSSTREETNAME, ADDRESSTYPE,         DESCRIPTION, HYPERLINK, LANDXADDRESSPOINT, LANDYADDRESSPOINT,                    NAME, PHOTOURL, ADDRESSFLOORNUMBER,          INC_CRC,     FMEL_UPD_D, ... 
## min values  :   kml_1,                        ,                    ,            018989,                                                  1 & 3, Stratton Road, SINGAPORE 806787,            ,                    ,          ,                 0,                 0,    3-IN-1 FAMILY CENTRE,         ,                   , 00A958622500BF89, 20200812221033, ... 
## max values  : kml_999,                        ,                    ,            829646, UPPER BASEMENT LEVEL, WEST WING, TERMINAL 1, SINGAPORE CHANGI AIRPORT, SINGAPORE 819642,            , Child Care Services,          ,                 0,                 0, ZEE SCHOOLHOUSE PTE LTD,         ,                   , FFCFA88A8CE5665A, 20200826094036, ...

Convert Spatial Point DataFrame to Generic sp format

preschool_sp <- as(sp_preschool, "SpatialPoints")
childcare_sp <- as(sp_chilcare, "SpatialPoints")
kindergarten_sp <- as(sp_kindergarten, 'SpatialPoints')
sg_sp <- as(sg, "SpatialPolygons")

Converting the generic sp format into spatstat’s ppp format

preschool_ppp <- as(preschool_sp, "ppp")
childcare_ppp <- as(childcare_sp, "ppp")
kindergarten_ppp <- as(kindergarten_sp, "ppp")

Examining the summary of the ppp objects

summary(preschool_ppp)
## Planar point pattern:  1925 points
## Average intensity 2.374419e-06 points per square unit
## 
## *Pattern contains duplicated points*
## 
## Coordinates are given to 3 decimal places
## i.e. rounded to the nearest multiple of 0.001 units
## 
## Window: rectangle = [11203.01, 45404.24] x [25596.33, 49300.88] units
##                     (34200 x 23700 units)
## Window area = 810725000 square units
summary(childcare_ppp)
## Planar point pattern:  1545 points
## Average intensity 1.91145e-06 points per square unit
## 
## *Pattern contains duplicated points*
## 
## Coordinates are given to 3 decimal places
## i.e. rounded to the nearest multiple of 0.001 units
## 
## Window: rectangle = [11203.01, 45404.24] x [25667.6, 49300.88] units
##                     (34200 x 23630 units)
## Window area = 808287000 square units
summary(kindergarten_ppp)
## Planar point pattern:  448 points
## Average intensity 6.195602e-07 points per square unit
## 
## *Pattern contains duplicated points*
## 
## Coordinates are given to 3 decimal places
## i.e. rounded to the nearest multiple of 0.001 units
## 
## Window: rectangle = [11909.7, 43395.47] x [25596.33, 48562.06] units
##                     (31490 x 22970 units)
## Window area = 723094000 square units

Since all 3 ppp objects has error message of duplicated points, we will perform duplication point handling.

Handling ppp object duplicated points

any(duplicated(childcare_ppp))
## [1] TRUE

To count the number of coindicence point, we will use the multiplicity() function as shown in the code chunk below.

multiplicity(childcare_ppp)
##    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16 
##    1    1    1    3    1    1    1    1    2    1    1    1    1    1    1    1 
##   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32 
##    1    1    1    1    1    1    1    1    1    1    9    1    1    1    1    1 
##   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
##   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64 
##    1    1    1    1    1    1    2    1    1    3    1    1    1    1    1    1 
##   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80 
##    1    1    1    1    1    2    1    1    1    1    1    2    1    1    1    1 
##   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96 
##    1    1    1    3    1    1    1    1    1    1    1    1    1    1    1    1 
##   97   98   99  100  101  102  103  104  105  106  107  108  109  110  111  112 
##    1    1    1    1    1    1    1    1    2    1    1    1    1    1    1    1 
##  113  114  115  116  117  118  119  120  121  122  123  124  125  126  127  128 
##    1    1    1    1    1    1    2    1    1    1    3    1    1    1    2    1 
##  129  130  131  132  133  134  135  136  137  138  139  140  141  142  143  144 
##    1    1    1    1    1    2    1    1    1    1    1    1    1    1    3    2 
##  145  146  147  148  149  150  151  152  153  154  155  156  157  158  159  160 
##    1    2    1    1    1    2    2    3    1    5    1    5    1    1    1    2 
##  161  162  163  164  165  166  167  168  169  170  171  172  173  174  175  176 
##    1    1    1    1    2    1    1    1    1    1    1    2    1    1    1    1 
##  177  178  179  180  181  182  183  184  185  186  187  188  189  190  191  192 
##    1    4    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
##  193  194  195  196  197  198  199  200  201  202  203  204  205  206  207  208 
##    1    1    1    1    1    2    2    1    1    1    1    2    1    4    1    1 
##  209  210  211  212  213  214  215  216  217  218  219  220  221  222  223  224 
##    2    1    1    1    1    1    1    1    1    1    1    1    2    1    1    1 
##  225  226  227  228  229  230  231  232  233  234  235  236  237  238  239  240 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
##  241  242  243  244  245  246  247  248  249  250  251  252  253  254  255  256 
##    1    1    1    1    2    1    1    1    1    1    1    1    1    1    1    1 
##  257  258  259  260  261  262  263  264  265  266  267  268  269  270  271  272 
##    1    1    1    1    1    1    1    1    1    1    2    1    1    1    1    3 
##  273  274  275  276  277  278  279  280  281  282  283  284  285  286  287  288 
##    1    1    1    1    1    1    3    1    1    1    1    1    1    1    1    1 
##  289  290  291  292  293  294  295  296  297  298  299  300  301  302  303  304 
##    1    1    1    1    1    1    1    9    1    1    2    1    1    1    1    1 
##  305  306  307  308  309  310  311  312  313  314  315  316  317  318  319  320 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
##  321  322  323  324  325  326  327  328  329  330  331  332  333  334  335  336 
##    1    1    1    5    1    1    1    1    1    2    1    1    2    2    1    1 
##  337  338  339  340  341  342  343  344  345  346  347  348  349  350  351  352 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    2    2    1 
##  353  354  355  356  357  358  359  360  361  362  363  364  365  366  367  368 
##    1    1    1    1    9    1    1    1    1    1    1    1    1    1    1    1 
##  369  370  371  372  373  374  375  376  377  378  379  380  381  382  383  384 
##    1    3    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
##  385  386  387  388  389  390  391  392  393  394  395  396  397  398  399  400 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
##  401  402  403  404  405  406  407  408  409  410  411  412  413  414  415  416 
##    1    1    2    1    1    1    1    1    1    1    2    1    1    1    1    1 
##  417  418  419  420  421  422  423  424  425  426  427  428  429  430  431  432 
##    1    1    1    1    1    1    1    2    1    1    2    1    1    1    1    1 
##  433  434  435  436  437  438  439  440  441  442  443  444  445  446  447  448 
##    1    1    1    1    2    1    1    1    1    1    1    1    1    1    1    1 
##  449  450  451  452  453  454  455  456  457  458  459  460  461  462  463  464 
##    1    1    9    9    1    1    1    1    1    1    1    1    1    1    2    1 
##  465  466  467  468  469  470  471  472  473  474  475  476  477  478  479  480 
##    2    1    1    1    1    1    1    1    1    1    1    1    2    2    1    1 
##  481  482  483  484  485  486  487  488  489  490  491  492  493  494  495  496 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
##  497  498  499  500  501  502  503  504  505  506  507  508  509  510  511  512 
##    1    1    1    1    1    1    2    1    1    1    1    1    1    1    1    2 
##  513  514  515  516  517  518  519  520  521  522  523  524  525  526  527  528 
##    1    1    1    1    1    1    1    1    1    1    1    2    1    1    3    1 
##  529  530  531  532  533  534  535  536  537  538  539  540  541  542  543  544 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
##  545  546  547  548  549  550  551  552  553  554  555  556  557  558  559  560 
##    1    1    1    1    1    1    1    1    1    3    1    1    1    1    1    1 
##  561  562  563  564  565  566  567  568  569  570  571  572  573  574  575  576 
##    2    2    2    1    1    1    1    2    1    1    2    1    1    1    2    1 
##  577  578  579  580  581  582  583  584  585  586  587  588  589  590  591  592 
##    1    2    1    1    1    1    1    9    1    4    1    2    1    1    1    1 
##  593  594  595  596  597  598  599  600  601  602  603  604  605  606  607  608 
##    2    1    1    1    1    1    1    1    2    1    2    1    1    1    1    1 
##  609  610  611  612  613  614  615  616  617  618  619  620  621  622  623  624 
##    1    1    1    1    1    1    1    1    1    2    1    2    1    1    1    1 
##  625  626  627  628  629  630  631  632  633  634  635  636  637  638  639  640 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
##  641  642  643  644  645  646  647  648  649  650  651  652  653  654  655  656 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    4 
##  657  658  659  660  661  662  663  664  665  666  667  668  669  670  671  672 
##    1    1    1    1    1    1    1    3    1    1    1    1    1    1    1    1 
##  673  674  675  676  677  678  679  680  681  682  683  684  685  686  687  688 
##    1    1    1    1    1    4    1    1    1    1    1    4    1    1    1    1 
##  689  690  691  692  693  694  695  696  697  698  699  700  701  702  703  704 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
##  705  706  707  708  709  710  711  712  713  714  715  716  717  718  719  720 
##    1    1    2    1    1    1    1    1    1    1    1    1    1    1    1    1 
##  721  722  723  724  725  726  727  728  729  730  731  732  733  734  735  736 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
##  737  738  739  740  741  742  743  744  745  746  747  748  749  750  751  752 
##    1    2    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
##  753  754  755  756  757  758  759  760  761  762  763  764  765  766  767  768 
##    1    1    1    1    1    2    1    1    1    1    1    1    1    1    1    1 
##  769  770  771  772  773  774  775  776  777  778  779  780  781  782  783  784 
##    1    1    1    1    1    1    1    1    1    4    1    1    1    1    1    1 
##  785  786  787  788  789  790  791  792  793  794  795  796  797  798  799  800 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
##  801  802  803  804  805  806  807  808  809  810  811  812  813  814  815  816 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
##  817  818  819  820  821  822  823  824  825  826  827  828  829  830  831  832 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
##  833  834  835  836  837  838  839  840  841  842  843  844  845  846  847  848 
##    1    1    1    1    1    1    1    2    1    1    1    1    1    1    1    1 
##  849  850  851  852  853  854  855  856  857  858  859  860  861  862  863  864 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
##  865  866  867  868  869  870  871  872  873  874  875  876  877  878  879  880 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    2 
##  881  882  883  884  885  886  887  888  889  890  891  892  893  894  895  896 
##    3    1    1    1    2    1    1    1    3    1    1    3    1    1    1    1 
##  897  898  899  900  901  902  903  904  905  906  907  908  909  910  911  912 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
##  913  914  915  916  917  918  919  920  921  922  923  924  925  926  927  928 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
##  929  930  931  932  933  934  935  936  937  938  939  940  941  942  943  944 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
##  945  946  947  948  949  950  951  952  953  954  955  956  957  958  959  960 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    2 
##  961  962  963  964  965  966  967  968  969  970  971  972  973  974  975  976 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
##  977  978  979  980  981  982  983  984  985  986  987  988  989  990  991  992 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
##  993  994  995  996  997  998  999 1000 1001 1002 1003 1004 1005 1006 1007 1008 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 
##    1    1    1    1    1    1    1    1    1    2    2    1    1    1    1    1 
## 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 
##    1    1    1    1    1    2    1    1    1    1    1    1    1    1    1    1 
## 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 
##    1    1    1    1    1    1    1    1    2    2    1    1    1    5    1    1 
## 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 
##    1    1    1    1    1    1    1    1    1    2    1    1    1    1    1    1 
## 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 
##    1    1    1    1    1    1    1    1    1    1    2    1    1    1    1    1 
## 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 
##    1    9    1    2    2    1    1    1    2    1    1    1    1    1    1    1 
## 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 
##    1    1    1    1    2    1    1    1    3    1    1    1    1    1    1    1 
## 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 
##    9    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 
##    1    1    1    2    1    1    1    1    1    1    1    1    1    1    1    1 
## 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    2 
## 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 
##    1    1    1    2    1    2    1    1    1    2    2    2    1    1    1    1 
## 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 
##    1    1    2    1    1    1    1    1    1    1    1    1    2    1    1    1 
## 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 
##    1    1    1    1    3    1    1    1    1    1    1    1    1    1    1    1 
## 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 
##    1    1    1    1    1    1    1    1    4    1    1    1    1    1    2    1 
## 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 
##    1    1    1    1    1    1    1    1    1    9    1    1    1    1    1    1 
## 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    2    1 
## 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 
##    1    2    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 
##    1    1    1    1    1    1    1    1    1    1    2    1    1    1    1    1 
## 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 
##    1    1    1    1    1    1    2    1    1    1    1    1    1    1    1    1 
## 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 
##    1    1    1    1    1    1    1    1    1    1    5    1    1    1    1    1 
## 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 
##    1    1    1    1    1    2    1    1    1    1    2    1    1    1    1    3 
## 1537 1538 1539 1540 1541 1542 1543 1544 1545 
##    1    1    1    1    1    1    2    1    1
sum(multiplicity(preschool_ppp) > 1)
## [1] 302
sum(multiplicity(childcare_ppp) > 1)
## [1] 128
sum(multiplicity(kindergarten_ppp) > 1)
## [1] 100

Handle duplicated points using jittering approach. Jittering, which will add a small perturbation to the duplicate points so that they do not occupy the exact same space.

childcare_ppp_jit <- rjitter(childcare_ppp, retry=TRUE, nsim=1, drop=TRUE)

plot(childcare_ppp_jit)

preschool_ppp_jit <- rjitter(preschool_ppp, retry=TRUE, nsim=1, drop=TRUE)

plot(preschool_ppp_jit)

kindergarten_ppp_jit <- rjitter(kindergarten_ppp, retry=TRUE, nsim=1, drop=TRUE)

plot(kindergarten_ppp_jit)

### Creating owin objects Confining analysis within Singapore boundaries using sg_sp data

sg_owin <- as(sg_sp, 'owin')
plot(sg_owin)

Combining chilcare and kindergarten services with the study area

preschoolSG_ppp = preschool_ppp_jit[sg_owin]
childcareSG_ppp = childcare_ppp_jit[sg_owin]
kindergartenSG_ppp = kindergarten_ppp_jit[sg_owin]

Spatial Point Pattern Analysis - 1st Order Analysis

Quadrat Analysis

Perform a test of Complete Spatial Randomness for a given point pattern, based on quadrat counts by using quadrat.test() of statspat.

The test hypotheses are:

Ho = The distribution of childcare/kindergarten services are randomly distributed in space.

H1= The distribution of childcare/kindergarten services are not randomly distributed in space.

The 95% confident interval will be used.

Distribution of Childcare centre

childcare_qt <- quadrat.test(childcareSG_ppp, nx=20, ny=15, method='M', nsim=999)
childcare_qt
## 
##  Conditional Monte Carlo test of CSR using quadrat counts
##  Test statistic: Pearson X2 statistic
## 
## data:  childcareSG_ppp
## X2 = 2409.2, p-value = 0.004
## alternative hypothesis: two.sided
## 
## Quadrats: 185 tiles (irregular windows)

Since the p-value is lesser than the pre-defined alpha value of 0.05 (confidence interval 95%), we will reject the null hypothesis. Thus we can conclude with 95% confidence that childcare services are not randomly distributed.

Distribution of Kindergarten

kindergarten_qt <- quadrat.test(kindergartenSG_ppp, nx=20, ny=15, method='M', nsim=999)
kindergarten_qt
## 
##  Conditional Monte Carlo test of CSR using quadrat counts
##  Test statistic: Pearson X2 statistic
## 
## data:  kindergartenSG_ppp
## X2 = 770.75, p-value = 0.002
## alternative hypothesis: two.sided
## 
## Quadrats: 185 tiles (irregular windows)

Since the p-value is lesser than the pre-defined alpha value of 0.05 (confidence interval 95%), we will reject the null hypothesis. Thus we can conclude with 95% confidence that kindergarten services are not randomly distributed.

Compare distribution of childcare and kindergarten services

par(mfrow=c(1, 2))
plot(childcareSG_ppp, main = 'Distribution of Childcare Services')
plot(kindergartenSG_ppp, main = 'Distribution of Kindergartens')

5) Statistical interpretations on Section B (4)

2nd order spatial point patterns analysis

Focus on the following four planning areas instead of the entire country. They are: Sengkang, Bedok, Bukit Batok and Hougang.

Import subzone data

mpsz <- readOGR(dsn = "Data/shapefile", layer="MP14_SUBZONE_NO_SEA_PL")
## OGR data source with driver: ESRI Shapefile 
## Source: "D:\Users\John_Ng\Year 3 Term 1\(Monday) IS415 Geospatial Analytics & Applns (SMU-X)\Take home exercise\IS415_Take_Home_Exercise_01_NG_XUN_JIE\Data\shapefile", layer: "MP14_SUBZONE_NO_SEA_PL"
## with 323 features
## It has 15 fields

Extract subzone of interest

sk = mpsz[mpsz@data$PLN_AREA_N == "SENGKANG",]
bk = mpsz[mpsz@data$PLN_AREA_N == "BEDOK",]
bb = mpsz[mpsz@data$PLN_AREA_N == "BUKIT BATOK",]
hg = mpsz[mpsz@data$PLN_AREA_N == "HOUGANG",]

Convert SpatialPolygonDataFrame into Spatial Polygon

sk_sp = as(sk, "SpatialPolygons")
bk_sp = as(bk, "SpatialPolygons")
bb_sp = as(bb, "SpatialPolygons")
hg_sp = as(hg, "SpatialPolygons")

Create owin object

sk_owin = as(sk_sp, "owin")
bk_owin = as(bk_sp, "owin")
bb_owin = as(bb_sp, "owin")
hg_owin = as(hg_sp, "owin")

Combining childcare points and the study area Extract childcare that is within the specific region to do our analysis later on.

childcare_sk_ppp = childcare_ppp_jit[sk_owin]
childcare_bk_ppp = childcare_ppp_jit[bk_owin]
childcare_bb_ppp = childcare_ppp_jit[bb_owin]
childcare_hg_ppp = childcare_ppp_jit[hg_owin]
plot(childcare_sk_ppp)

plot(childcare_bk_ppp)

plot(childcare_bb_ppp)

plot(childcare_hg_ppp)

Extract kindergarten that is within the specific region to do our analysis later on.

kindergarten_sk_ppp = kindergarten_ppp_jit[sk_owin]
kindergarten_bk_ppp = kindergarten_ppp_jit[bk_owin]
kindergarten_bb_ppp = kindergarten_ppp_jit[bb_owin]
kindergarten_hg_ppp = kindergarten_ppp_jit[hg_owin]
plot(kindergarten_sk_ppp)

plot(kindergarten_bk_ppp)

plot(kindergarten_bb_ppp)

plot(kindergarten_hg_ppp)

#####################################################################################################

Analysing Spatial Point Process Using G-Function ### Sengkang planning area Compute G-function estimation Chilcare Distribution

G_SK_CC = Gest(childcare_sk_ppp, correction = "border")
plot(G_SK_CC)

Compute G-function estimation Kindergarten Distribution

G_SK_KG = Gest(kindergarten_sk_ppp, correction = "border")
plot(G_SK_KG)

Performing Complete Spatial Randomness Test To confirm the observed spatial patterns above, a hypothesis test will be conducted. The hypothesis and test are as follows:

Ho = The distribution of childcare/kindergarten services at SengKang are randomly distributed.

H1= The distribution of childcare/kindergarten services at SengKang are not randomly distributed.

The null hypothesis will be rejected if p-value is smaller than alpha value of 0.001.

Monte Carlo test with G-FUNCTION

For childcare

G_SK_CC.csr <- envelope(childcare_sk_ppp, Gest, nsim = 999)
## Generating 999 simulations of CSR  ...
## 1, 2, 3, ......10.........20.........30.........40.........50.........60........
## .70.........80.........90.........100.........110.........120.........130......
## ...140.........150.........160.........170.........180.........190.........200....
## .....210.........220.........230.........240.........250.........260.........270..
## .......280.........290.........300.........310.........320.........330.........340
## .........350.........360.........370.........380.........390.........400........
## .410.........420.........430.........440.........450.........460.........470......
## ...480.........490.........500.........510.........520.........530.........540....
## .....550.........560.........570.........580.........590.........600.........610..
## .......620.........630.........640.........650.........660.........670.........680
## .........690.........700.........710.........720.........730.........740........
## .750.........760.........770.........780.........790.........800.........810......
## ...820.........830.........840.........850.........860.........870.........880....
## .....890.........900.........910.........920.........930.........940.........950..
## .......960.........970.........980.........990........ 999.
## 
## Done.
plot(G_SK_CC.csr)

For Kindergarten

G_SK_KG.csr <- envelope(kindergarten_sk_ppp, Gest, nsim = 999)
## Generating 999 simulations of CSR  ...
## 1, 2, 3, ......10.........20.........30.........40.........50.........60........
## .70.........80.........90.........100.........110.........120.........130......
## ...140.........150.........160.........170.........180.........190.........200....
## .....210.........220.........230.........240.........250.........260.........270..
## .......280.........290.........300.........310.........320.........330.........340
## .........350.........360.........370.........380.........390.........400........
## .410.........420.........430.........440.........450.........460.........470......
## ...480.........490.........500.........510.........520.........530.........540....
## .....550.........560.........570.........580.........590.........600.........610..
## .......620.........630.........640.........650.........660.........670.........680
## .........690.........700.........710.........720.........730.........740........
## .750.........760.........770.........780.........790.........800.........810......
## ...820.........830.........840.........850.........860.........870.........880....
## .....890.........900.........910.........920.........930.........940.........950..
## .......960.........970.........980.........990........ 999.
## 
## Done.
plot(G_SK_KG.csr)

############## ### Bedok planning area Compute G-function estimation Chilcare Distribution

G_BK_CC = Gest(childcare_bk_ppp, correction = "border")
plot(G_BK_CC)

Compute G-function estimation Kindergarten Distribution

G_BK_KG = Gest(kindergarten_bk_ppp, correction = "border")
plot(G_BK_KG)

Performing Complete Spatial Randomness Test To confirm the observed spatial patterns above, a hypothesis test will be conducted. The hypothesis and test are as follows:

Ho = The distribution of childcare/kindergarten services at Bedok are randomly distributed.

H1= The distribution of childcare/kindergarten services at Bedok are not randomly distributed.

The null hypothesis will be rejected if p-value is smaller than alpha value of 0.001.

Monte Carlo test with G-FUNCTION

For childcare

G_BK_CC.csr <- envelope(childcare_bk_ppp, Gest, nsim = 999)
## Generating 999 simulations of CSR  ...
## 1, 2, 3, ......10.........20.........30.........40.........50.........60........
## .70.........80.........90.........100.........110.........120.........130......
## ...140.........150.........160.........170.........180.........190.........200....
## .....210.........220.........230.........240.........250.........260.........270..
## .......280.........290.........300.........310.........320.........330.........340
## .........350.........360.........370.........380.........390.........400........
## .410.........420.........430.........440.........450.........460.........470......
## ...480.........490.........500.........510.........520.........530.........540....
## .....550.........560.........570.........580.........590.........600.........610..
## .......620.........630.........640.........650.........660.........670.........680
## .........690.........700.........710.........720.........730.........740........
## .750.........760.........770.........780.........790.........800.........810......
## ...820.........830.........840.........850.........860.........870.........880....
## .....890.........900.........910.........920.........930.........940.........950..
## .......960.........970.........980.........990........ 999.
## 
## Done.
plot(G_BK_CC.csr)

For Kindergarten

G_BK_KG.csr <- envelope(kindergarten_bk_ppp, Gest, nsim = 999)
## Generating 999 simulations of CSR  ...
## 1, 2, 3, ......10.........20.........30.........40.........50.........60........
## .70.........80.........90.........100.........110.........120.........130......
## ...140.........150.........160.........170.........180.........190.........200....
## .....210.........220.........230.........240.........250.........260.........270..
## .......280.........290.........300.........310.........320.........330.........340
## .........350.........360.........370.........380.........390.........400........
## .410.........420.........430.........440.........450.........460.........470......
## ...480.........490.........500.........510.........520.........530.........540....
## .....550.........560.........570.........580.........590.........600.........610..
## .......620.........630.........640.........650.........660.........670.........680
## .........690.........700.........710.........720.........730.........740........
## .750.........760.........770.........780.........790.........800.........810......
## ...820.........830.........840.........850.........860.........870.........880....
## .....890.........900.........910.........920.........930.........940.........950..
## .......960.........970.........980.........990........ 999.
## 
## Done.
plot(G_BK_KG.csr)

#################################################################

BUKIT BATOK planning area

Compute G-function estimation Chilcare Distribution

G_BB_CC = Gest(childcare_bb_ppp, correction = "border")
plot(G_BB_CC)

Compute G-function estimation Kindergarten Distribution

G_BB_KG = Gest(kindergarten_bb_ppp, correction = "border")
plot(G_BB_KG)

Performing Complete Spatial Randomness Test To confirm the observed spatial patterns above, a hypothesis test will be conducted. The hypothesis and test are as follows:

Ho = The distribution of childcare/kindergarten services at BUKIT BATOK are randomly distributed.

H1= The distribution of childcare/kindergarten services at BUKIT BATOK are not randomly distributed.

The null hypothesis will be rejected if p-value is smaller than alpha value of 0.001.

Monte Carlo test with G-FUNCTION

For childcare

G_BB_CC.csr <- envelope(childcare_bb_ppp, Gest, nsim = 999)
## Generating 999 simulations of CSR  ...
## 1, 2, 3, ......10.........20.........30.........40.........50.........60........
## .70.........80.........90.........100.........110.........120.........130......
## ...140.........150.........160.........170.........180.........190.........200....
## .....210.........220.........230.........240.........250.........260.........270..
## .......280.........290.........300.........310.........320.........330.........340
## .........350.........360.........370.........380.........390.........400........
## .410.........420.........430.........440.........450.........460.........470......
## ...480.........490.........500.........510.........520.........530.........540....
## .....550.........560.........570.........580.........590.........600.........610..
## .......620.........630.........640.........650.........660.........670.........680
## .........690.........700.........710.........720.........730.........740........
## .750.........760.........770.........780.........790.........800.........810......
## ...820.........830.........840.........850.........860.........870.........880....
## .....890.........900.........910.........920.........930.........940.........950..
## .......960.........970.........980.........990........ 999.
## 
## Done.
plot(G_BB_CC.csr)

For Kindergarten

G_BB_KG.csr <- envelope(kindergarten_bb_ppp, Gest, nsim = 999)
## Generating 999 simulations of CSR  ...
## 1, 2, 3, ......10.........20.........30.........40.........50.........60........
## .70.........80.........90.........100.........110.........120.........130......
## ...140.........150.........160.........170.........180.........190.........200....
## .....210.........220.........230.........240.........250.........260.........270..
## .......280.........290.........300.........310.........320.........330.........340
## .........350.........360.........370.........380.........390.........400........
## .410.........420.........430.........440.........450.........460.........470......
## ...480.........490.........500.........510.........520.........530.........540....
## .....550.........560.........570.........580.........590.........600.........610..
## .......620.........630.........640.........650.........660.........670.........680
## .........690.........700.........710.........720.........730.........740........
## .750.........760.........770.........780.........790.........800.........810......
## ...820.........830.........840.........850.........860.........870.........880....
## .....890.........900.........910.........920.........930.........940.........950..
## .......960.........970.........980.........990........ 999.
## 
## Done.
plot(G_BB_KG.csr)

HOUGANG planning area

Compute G-function estimation Chilcare Distribution

G_HG_CC = Gest(childcare_hg_ppp, correction = "border")
plot(G_HG_CC)

Compute G-function estimation Kindergarten Distribution

G_HG_KG = Gest(kindergarten_hg_ppp, correction = "border")
plot(G_HG_KG)

Performing Complete Spatial Randomness Test To confirm the observed spatial patterns above, a hypothesis test will be conducted. The hypothesis and test are as follows:

Ho = The distribution of childcare/kindergarten services at HOUGANG are randomly distributed.

H1= The distribution of childcare/kindergarten services at HOUGANG are not randomly distributed.

The null hypothesis will be rejected if p-value is smaller than alpha value of 0.001.

Monte Carlo test with G-FUNCTION

For childcare

G_HG_CC.csr <- envelope(childcare_hg_ppp, Gest, nsim = 999)
## Generating 999 simulations of CSR  ...
## 1, 2, 3, ......10.........20.........30.........40.........50.........60........
## .70.........80.........90.........100.........110.........120.........130......
## ...140.........150.........160.........170.........180.........190.........200....
## .....210.........220.........230.........240.........250.........260.........270..
## .......280.........290.........300.........310.........320.........330.........340
## .........350.........360.........370.........380.........390.........400........
## .410.........420.........430.........440.........450.........460.........470......
## ...480.........490.........500.........510.........520.........530.........540....
## .....550.........560.........570.........580.........590.........600.........610..
## .......620.........630.........640.........650.........660.........670.........680
## .........690.........700.........710.........720.........730.........740........
## .750.........760.........770.........780.........790.........800.........810......
## ...820.........830.........840.........850.........860.........870.........880....
## .....890.........900.........910.........920.........930.........940.........950..
## .......960.........970.........980.........990........ 999.
## 
## Done.
plot(G_HG_CC.csr)

For Kindergarten

G_HG_KG.csr <- envelope(kindergarten_hg_ppp, Gest, nsim = 999)
## Generating 999 simulations of CSR  ...
## 1, 2, 3, ......10.........20.........30.........40.........50.........60........
## .70.........80.........90.........100.........110.........120.........130......
## ...140.........150.........160.........170.........180.........190.........200....
## .....210.........220.........230.........240.........250.........260.........270..
## .......280.........290.........300.........310.........320.........330.........340
## .........350.........360.........370.........380.........390.........400........
## .410.........420.........430.........440.........450.........460.........470......
## ...480.........490.........500.........510.........520.........530.........540....
## .....550.........560.........570.........580.........590.........600.........610..
## .......620.........630.........640.........650.........660.........670.........680
## .........690.........700.........710.........720.........730.........740........
## .750.........760.........770.........780.........790.........800.........810......
## ...820.........830.........840.........850.........860.........870.........880....
## .....890.........900.........910.........920.........930.........940.........950..
## .......960.........970.........980.........990........ 999.
## 
## Done.
plot(G_HG_KG.csr)

6) Result

Kernel Density