In-class_Ex04

Overview

In this in-class exercise, we will perform the following tasks:

  • perform geocoding using data from data.gov.sg
  • calibrate geographically weighted poisson regression

Loading packages and data

Loading packages

pacman::p_load(tidyverse,sf,httr,tmap, performance)

Geocosing using SLA API

Address geocoding is the process of taking an aspatial decription of a location such as an address and returning a coordinate.

SLA hosts a geocoding service called OneMap API. It uses an address data and returns X-Y coordinates and latitude-longitude coordinates information.

url <- "https://www.onemap.gov.sg/api/common/elastic/search"
csv <- read_csv("data/aspatial/Generalinformationofschools.csv")
Rows: 346 Columns: 31
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (31): school_name, url_address, address, postal_code, telephone_no, tele...

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
postcodes <- csv$`postal_code`

found <- data.frame()
not_found <-data.frame()
for (postcode in postcodes){
query<-list('searchVal'=postcode,'returnGeom' = 'Y', 'getAddrDetails' = 'Y', 'pageNum' = '1')
res<- GET(url,query=query)
if((content(res)$found)!=0){
found<-rbind(found,data.frame(content(res))[4:13])

} else{
  not_found =data.frame(postcode)}

}

Next we will search for any postal codes without a school.

merged = merge(csv, found, by.x = 'postal_code', by.y = 'results.POSTAL', all = TRUE)
write.csv(merged, file = "data/aspatial/schools.csv")
write.csv(not_found, file = "data/aspatial/not_found.csv")

Here we find out that Zhenghua Secondary has missing longitude and latitude information, We will insert it manually into the schools.csv.

Converting an aspatial data into a simple feature tibble data.frame

importing and tidying schools data

schools <- read_csv("data/aspatial/schools.csv") %>%
  rename(latitude=results.LATITUDE , longtitude = results.LONGITUDE ) %>%
  select(2:3,40:41)
New names:
Rows: 350 Columns: 41
── Column specification
──────────────────────────────────────────────────────── Delimiter: "," chr
(35): school_name, url_address, address, telephone_no, telephone_no_2, f... dbl
(6): ...1, postal_code, results.X, results.Y, results.LATITUDE, results...
ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
Specify the column types or set `show_col_types = FALSE` to quiet this message.
• `` -> `...1`

converting an aspatial data into sf tibble data.frame

Next we will convert the aspatial data into a simple feature tibble data.frame called schools_sf using st_as_sf(). The following code converts 2 points into a single point and convert from decimal degree to projected coordinate system which is in meters.

It is important to standardise the type of geocoordinate system used.

schools_sf<-st_as_sf(schools,
                     coords = c("longtitude", "latitude"),
                     crs = 4326) %>%
  st_transform(crs = 3414)

Plotting a point simple feature later

To ensure that schools sf tibble data.frame has been projected and converted correctly, we can plot the schools point data for visual inspection.

tmap_mode("view")
tmap mode set to interactive viewing
tm_shape(schools_sf)+
tm_dots()+
  tm_view(set.zoom.limits = c(11,14))
tmap_mode("plot")
tmap mode set to plotting

Preparing geospatial data

We will first import the planning subzones of Singapore.

mpsz <- st_read(dsn = "data/geospatial",
                   layer = "MP14_SUBZONE_WEB_PL") %>%
  st_transform(crs = 3414)
Reading layer `MP14_SUBZONE_WEB_PL' from data source 
  `C:\Users\Lian Khye\Desktop\MITB\Geospatial\geartooth\ISSS624\In-class_Ex\Ex4\data\geospatial' 
  using driver `ESRI Shapefile'
Simple feature collection with 323 features and 15 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 2667.538 ymin: 15748.72 xmax: 56396.44 ymax: 50256.33
Projected CRS: SVY21

Next we will count the number of schools that can be found within each subzone.

mpsz$`SCHOOL_COUNT` <- lengths(
  st_intersects(
    mpsz, schools_sf
  )
)
summary(mpsz$SCHOOL_COUNT)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.000   0.000   0.000   1.084   2.000  12.000 

Import business data

Here we will import the business geospatial data as our factor when considering the attractiveness factor.

biz <- st_read(dsn = "data/geospatial",
                   layer = "Business")
Reading layer `Business' from data source 
  `C:\Users\Lian Khye\Desktop\MITB\Geospatial\geartooth\ISSS624\In-class_Ex\Ex4\data\geospatial' 
  using driver `ESRI Shapefile'
Simple feature collection with 6550 features and 3 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 3669.148 ymin: 25408.41 xmax: 47034.83 ymax: 50148.54
Projected CRS: SVY21 / Singapore TM

We can then map out the locations of various businesses in Singapore. We will include the tmap_options(check.and.fix = TRUE) to the code to help close any unclosed polygons.

tmap_options(check.and.fix = TRUE)  #polygon may not close, so need to auto close
tm_shape(mpsz)+  #sg boundary
  tm_polygons()+  #sg boundary
  tm_shape(biz)+  #biz layer
  tm_dots()
Warning: The shape mpsz is invalid. See sf::st_is_valid

We can then take a look at the summary of the various businesses being spread out across Singapore.

mpsz$BIZ_COUNT <- lengths(st_intersects(mpsz, biz))
summary(biz)
   POI_NAME          POI_ST_NUM         POI_ST_NAM                 geometry   
 Length:6550        Length:6550        Length:6550        POINT        :6550  
 Class :character   Class :character   Class :character   epsg:3414    :   0  
 Mode  :character   Mode  :character   Mode  :character   +proj=tmer...:   0  

Data intergration and wrangling

Here we will read the flow data from the rds file that we have created from the hands on ex.

flow_data <- read_rds("data/rds/flow_data_tidy.rds")
glimpse(flow_data)
Rows: 14,734
Columns: 13
$ ORIGIN_SZ       <chr> "AMSZ01", "AMSZ01", "AMSZ01", "AMSZ01", "AMSZ01", "AMS…
$ DESTIN_SZ       <chr> "AMSZ01", "AMSZ02", "AMSZ03", "AMSZ04", "AMSZ05", "AMS…
$ MORNING_PEAK    <dbl> 1998, 8289, 8971, 2252, 6136, 2148, 1620, 1925, 1773, …
$ dist            <dbl> 50.0000, 810.4491, 1360.9294, 840.4432, 1076.7916, 805…
$ ORIGIN_AGE7_12  <dbl> 310, 310, 310, 310, 310, 310, 310, 310, 310, 310, 310,…
$ ORIGIN_AGE13_24 <dbl> 710, 710, 710, 710, 710, 710, 710, 710, 710, 710, 710,…
$ ORIGIN_AGE25_64 <dbl> 2780, 2780, 2780, 2780, 2780, 2780, 2780, 2780, 2780, …
$ DESTIN_AGE7_12  <dbl> 310.00, 1140.00, 1010.00, 980.00, 810.00, 1050.00, 420…
$ DESTIN_AGE13_24 <dbl> 710.00, 2770.00, 2650.00, 2000.00, 1920.00, 2390.00, 1…
$ DESTIN_AGE25_64 <dbl> 2780.00, 15700.00, 14240.00, 11320.00, 9650.00, 12460.…
$ SCHOOL_COUNT    <dbl> 0.99, 2.00, 2.00, 1.00, 3.00, 2.00, 0.99, 0.99, 3.00, …
$ RETAIL_COUNT    <dbl> 1.00, 0.99, 6.00, 0.99, 0.99, 0.99, 1.00, 117.00, 0.99…
$ geometry        <LINESTRING [m]> LINESTRING (29501.77 39419...., LINESTRING …
flow_data$FlowNoIntra <- ifelse(
  flow_data$ORIGIN_SZ == flow_data$DESTIN_SZ,
  0, flow_data$MORNING_PEAK)
flow_data$offset<-ifelse(
  flow_data$ORIGIN_SZ == flow_data$DESTIN_SZ,
  0.000001,1)

inter_zonal_flow<-flow_data%>%
  filter(FlowNoIntra >0)
inter_zonal_flow <-inter_zonal_flow %>%
  rename(TRIPS = MORNING_PEAK,
         DIST = dist)

Origing(Production) constrained SIM

Firstly we will increae the amount of printed rows for R.

getOption("max.print")
[1] 99999
options(max.print=1000000)

Here we will fit an origin constrained SIM by using the code chunk below.

orcSIM_Poisson <- glm(formula = TRIPS ~
                        ORIGIN_SZ + 
                        log(SCHOOL_COUNT) +  #use attractiveness factors
                        log(RETAIL_COUNT)+
                        log(DIST)+
                        log(DIST) - 1,  #have - 1 to remove intersept, already constrain to origin
                      family = poisson(link = "log"),
                      data = inter_zonal_flow,
                      na.action = na.exclude)
summary(orcSIM_Poisson)

Call:
glm(formula = TRIPS ~ ORIGIN_SZ + log(SCHOOL_COUNT) + log(RETAIL_COUNT) + 
    log(DIST) + log(DIST) - 1, family = poisson(link = "log"), 
    data = inter_zonal_flow, na.action = na.exclude)

Coefficients:
                    Estimate Std. Error  z value Pr(>|z|)    
ORIGIN_SZAMSZ01   19.8739840  0.0047627  4172.84   <2e-16 ***
ORIGIN_SZAMSZ02   20.5902203  0.0042786  4812.33   <2e-16 ***
ORIGIN_SZAMSZ03   20.2327026  0.0045531  4443.70   <2e-16 ***
ORIGIN_SZAMSZ04   19.7744438  0.0049837  3967.79   <2e-16 ***
ORIGIN_SZAMSZ05   19.6574529  0.0056396  3485.61   <2e-16 ***
ORIGIN_SZAMSZ06   19.9659115  0.0048946  4079.16   <2e-16 ***
ORIGIN_SZAMSZ07   18.6746164  0.0096316  1938.90   <2e-16 ***
ORIGIN_SZAMSZ08   19.2701601  0.0090776  2122.82   <2e-16 ***
ORIGIN_SZAMSZ09   19.9889467  0.0052858  3781.64   <2e-16 ***
ORIGIN_SZAMSZ10   20.3422035  0.0045778  4443.62   <2e-16 ***
ORIGIN_SZAMSZ11   18.3944113  0.0129212  1423.58   <2e-16 ***
ORIGIN_SZAMSZ12   18.3484209  0.0109652  1673.33   <2e-16 ***
ORIGIN_SZBDSZ01   20.9668587  0.0043388  4832.36   <2e-16 ***
ORIGIN_SZBDSZ02   20.4059518  0.0050601  4032.75   <2e-16 ***
ORIGIN_SZBDSZ03   20.6725514  0.0045276  4565.93   <2e-16 ***
ORIGIN_SZBDSZ04   21.6703853  0.0038930  5566.44   <2e-16 ***
ORIGIN_SZBDSZ05   20.7497445  0.0046085  4502.46   <2e-16 ***
ORIGIN_SZBDSZ06   20.9119361  0.0046432  4503.77   <2e-16 ***
ORIGIN_SZBDSZ07   18.9749815  0.0097896  1938.28   <2e-16 ***
ORIGIN_SZBDSZ08   19.1933901  0.0091312  2101.95   <2e-16 ***
ORIGIN_SZBKSZ01   19.5422606  0.0064732  3018.96   <2e-16 ***
ORIGIN_SZBKSZ02   20.1748913  0.0050076  4028.89   <2e-16 ***
ORIGIN_SZBKSZ03   20.3984624  0.0047226  4319.35   <2e-16 ***
ORIGIN_SZBKSZ04   19.6182212  0.0059652  3288.76   <2e-16 ***
ORIGIN_SZBKSZ05   19.6033818  0.0063181  3102.74   <2e-16 ***
ORIGIN_SZBKSZ06   19.7145224  0.0056372  3497.20   <2e-16 ***
ORIGIN_SZBKSZ07   20.4237448  0.0041912  4873.03   <2e-16 ***
ORIGIN_SZBKSZ08   19.7992538  0.0050405  3928.02   <2e-16 ***
ORIGIN_SZBKSZ09   19.7821586  0.0055558  3560.66   <2e-16 ***
ORIGIN_SZBLSZ01   17.7977276  0.0149058  1194.01   <2e-16 ***
ORIGIN_SZBLSZ02   17.4287491  0.0192364   906.03   <2e-16 ***
ORIGIN_SZBLSZ03   16.5884288  0.0459848   360.74   <2e-16 ***
ORIGIN_SZBLSZ04   17.7851626  0.0232823   763.89   <2e-16 ***
ORIGIN_SZBMSZ01   20.0751840  0.0052887  3795.89   <2e-16 ***
ORIGIN_SZBMSZ02   18.6956140  0.0066656  2804.80   <2e-16 ***
ORIGIN_SZBMSZ03   19.3204425  0.0054755  3528.56   <2e-16 ***
ORIGIN_SZBMSZ04   19.4724220  0.0049390  3942.59   <2e-16 ***
ORIGIN_SZBMSZ05   16.9581801  0.0168804  1004.61   <2e-16 ***
ORIGIN_SZBMSZ06   16.9898638  0.0181852   934.27   <2e-16 ***
ORIGIN_SZBMSZ07   19.2868403  0.0056231  3429.91   <2e-16 ***
ORIGIN_SZBMSZ08   19.1477543  0.0055918  3424.28   <2e-16 ***
ORIGIN_SZBMSZ09   18.7564539  0.0086298  2173.46   <2e-16 ***
ORIGIN_SZBMSZ10   18.3617854  0.0089250  2057.35   <2e-16 ***
ORIGIN_SZBMSZ11   18.9167941  0.0063340  2986.54   <2e-16 ***
ORIGIN_SZBMSZ12   18.7874661  0.0093024  2019.63   <2e-16 ***
ORIGIN_SZBMSZ13   19.5654046  0.0057517  3401.70   <2e-16 ***
ORIGIN_SZBMSZ14   19.0685619  0.0063346  3010.24   <2e-16 ***
ORIGIN_SZBMSZ15   19.4403124  0.0058147  3343.30   <2e-16 ***
ORIGIN_SZBMSZ16   18.4469203  0.0092638  1991.28   <2e-16 ***
ORIGIN_SZBMSZ17   18.3430175  0.0157692  1163.22   <2e-16 ***
ORIGIN_SZBPSZ01   20.1806714  0.0053660  3760.81   <2e-16 ***
ORIGIN_SZBPSZ02   19.8116707  0.0061485  3222.19   <2e-16 ***
ORIGIN_SZBPSZ03   19.8467602  0.0059769  3320.57   <2e-16 ***
ORIGIN_SZBPSZ04   20.4613200  0.0048398  4227.72   <2e-16 ***
ORIGIN_SZBPSZ05   20.5379711  0.0043769  4692.39   <2e-16 ***
ORIGIN_SZBPSZ06   18.8948034  0.0093668  2017.21   <2e-16 ***
ORIGIN_SZBPSZ07   19.4104568  0.0087961  2206.70   <2e-16 ***
ORIGIN_SZBSSZ01   20.0139503  0.0056561  3538.45   <2e-16 ***
ORIGIN_SZBSSZ02   20.2543885  0.0047198  4291.38   <2e-16 ***
ORIGIN_SZBSSZ03   19.5428803  0.0052713  3707.41   <2e-16 ***
ORIGIN_SZBTSZ01   20.0198045  0.0058541  3419.77   <2e-16 ***
ORIGIN_SZBTSZ02   19.3618525  0.0081472  2376.51   <2e-16 ***
ORIGIN_SZBTSZ03   19.5883853  0.0068935  2841.59   <2e-16 ***
ORIGIN_SZBTSZ04   18.7720238  0.0103909  1806.58   <2e-16 ***
ORIGIN_SZBTSZ05   18.8069026  0.0120628  1559.08   <2e-16 ***
ORIGIN_SZBTSZ06   18.7068633  0.0094575  1978.00   <2e-16 ***
ORIGIN_SZBTSZ07   17.6292257  0.0141551  1245.43   <2e-16 ***
ORIGIN_SZBTSZ08   18.6989374  0.0109610  1705.94   <2e-16 ***
ORIGIN_SZCBSZ01   18.2189868  0.0548317   332.27   <2e-16 ***
ORIGIN_SZCCSZ01   18.9734563  0.0139450  1360.59   <2e-16 ***
ORIGIN_SZCHSZ01   19.5955119  0.0121035  1619.00   <2e-16 ***
ORIGIN_SZCHSZ02   19.3320960  0.0081620  2368.55   <2e-16 ***
ORIGIN_SZCHSZ03   21.2164518  0.0063552  3338.43   <2e-16 ***
ORIGIN_SZCKSZ01   20.1046845  0.0049333  4075.29   <2e-16 ***
ORIGIN_SZCKSZ02   20.5371946  0.0050256  4086.53   <2e-16 ***
ORIGIN_SZCKSZ03   20.7210560  0.0042184  4912.07   <2e-16 ***
ORIGIN_SZCKSZ04   21.4013886  0.0042524  5032.80   <2e-16 ***
ORIGIN_SZCKSZ05   20.9413146  0.0049434  4236.18   <2e-16 ***
ORIGIN_SZCKSZ06   20.2557727  0.0071832  2819.88   <2e-16 ***
ORIGIN_SZCLSZ01   19.3383703  0.0076634  2523.46   <2e-16 ***
ORIGIN_SZCLSZ02   18.5226956  0.0135522  1366.77   <2e-16 ***
ORIGIN_SZCLSZ03   19.0225512  0.0080145  2373.51   <2e-16 ***
ORIGIN_SZCLSZ04   20.7981505  0.0042400  4905.22   <2e-16 ***
ORIGIN_SZCLSZ05   18.3015625  0.0146815  1246.58   <2e-16 ***
ORIGIN_SZCLSZ06   20.8207386  0.0039567  5262.09   <2e-16 ***
ORIGIN_SZCLSZ07   19.6728958  0.0054199  3629.76   <2e-16 ***
ORIGIN_SZCLSZ08   20.0851929  0.0056956  3526.43   <2e-16 ***
ORIGIN_SZCLSZ09   18.5749589  0.0165415  1122.93   <2e-16 ***
ORIGIN_SZDTSZ02   15.8276209  0.0833992   189.78   <2e-16 ***
ORIGIN_SZDTSZ03   16.2512838  0.0737972   220.22   <2e-16 ***
ORIGIN_SZDTSZ13   16.7744385  0.0312450   536.87   <2e-16 ***
ORIGIN_SZGLSZ01   18.2368248  0.0096104  1897.62   <2e-16 ***
ORIGIN_SZGLSZ02   19.8705255  0.0049014  4054.06   <2e-16 ***
ORIGIN_SZGLSZ03   19.8249435  0.0053109  3732.85   <2e-16 ***
ORIGIN_SZGLSZ04   20.7800335  0.0041261  5036.20   <2e-16 ***
ORIGIN_SZGLSZ05   20.6040494  0.0043049  4786.23   <2e-16 ***
ORIGIN_SZHGSZ01   20.0273475  0.0044824  4468.04   <2e-16 ***
ORIGIN_SZHGSZ02   20.2480656  0.0044575  4542.47   <2e-16 ***
ORIGIN_SZHGSZ03   20.0756442  0.0049003  4096.81   <2e-16 ***
ORIGIN_SZHGSZ04   20.7577748  0.0040465  5129.84   <2e-16 ***
ORIGIN_SZHGSZ05   20.9779992  0.0040123  5228.42   <2e-16 ***
ORIGIN_SZHGSZ06   19.7403058  0.0054229  3640.20   <2e-16 ***
ORIGIN_SZHGSZ07   20.1896268  0.0046051  4384.22   <2e-16 ***
ORIGIN_SZHGSZ08   19.8646492  0.0052403  3790.72   <2e-16 ***
ORIGIN_SZHGSZ09   18.3647736  0.0069196  2654.04   <2e-16 ***
ORIGIN_SZHGSZ10   16.8720475  0.0421046   400.72   <2e-16 ***
ORIGIN_SZJESZ01   20.2673794  0.0046723  4337.79   <2e-16 ***
ORIGIN_SZJESZ02   20.0595982  0.0046503  4313.61   <2e-16 ***
ORIGIN_SZJESZ03   19.9128778  0.0049848  3994.75   <2e-16 ***
ORIGIN_SZJESZ04   18.5053667  0.0099227  1864.94   <2e-16 ***
ORIGIN_SZJESZ05   17.8172930  0.0138840  1283.29   <2e-16 ***
ORIGIN_SZJESZ06   20.0124157  0.0045009  4446.36   <2e-16 ***
ORIGIN_SZJESZ07   18.1821423  0.0117267  1550.49   <2e-16 ***
ORIGIN_SZJESZ08   18.8713046  0.0116456  1620.46   <2e-16 ***
ORIGIN_SZJESZ09   20.5535527  0.0048456  4241.72   <2e-16 ***
ORIGIN_SZJESZ10   18.4922322  0.0191243   966.95   <2e-16 ***
ORIGIN_SZJESZ11   18.2891211  0.0197114   927.85   <2e-16 ***
ORIGIN_SZJWSZ01   20.4912737  0.0063102  3247.35   <2e-16 ***
ORIGIN_SZJWSZ02   20.8236694  0.0042249  4928.82   <2e-16 ***
ORIGIN_SZJWSZ03   21.2587613  0.0039733  5350.40   <2e-16 ***
ORIGIN_SZJWSZ04   20.3816464  0.0046199  4411.67   <2e-16 ***
ORIGIN_SZJWSZ05   18.0607448  0.0128857  1401.61   <2e-16 ***
ORIGIN_SZJWSZ06   18.7015202  0.0107614  1737.83   <2e-16 ***
ORIGIN_SZJWSZ07   17.3991822  0.0277096   627.91   <2e-16 ***
ORIGIN_SZJWSZ08   21.8044465  0.0037356  5836.95   <2e-16 ***
ORIGIN_SZJWSZ09   21.5414930  0.0036033  5978.19   <2e-16 ***
ORIGIN_SZKLSZ01   20.0307712  0.0047868  4184.59   <2e-16 ***
ORIGIN_SZKLSZ02   19.0634769  0.0062318  3059.05   <2e-16 ***
ORIGIN_SZKLSZ03   19.2685700  0.0057172  3370.25   <2e-16 ***
ORIGIN_SZKLSZ04   17.7085067  0.0119809  1478.06   <2e-16 ***
ORIGIN_SZKLSZ05   18.6384471  0.0107596  1732.26   <2e-16 ***
ORIGIN_SZKLSZ06   13.7280296  0.1857160    73.92   <2e-16 ***
ORIGIN_SZKLSZ07   18.6425146  0.0084952  2194.47   <2e-16 ***
ORIGIN_SZKLSZ08   18.0928506  0.0101567  1781.37   <2e-16 ***
ORIGIN_SZLKSZ01   17.8907138  0.0397083   450.55   <2e-16 ***
ORIGIN_SZMDSZ01   18.7605188  0.0285455   657.22   <2e-16 ***
ORIGIN_SZMDSZ02   19.1533927  0.0102815  1862.90   <2e-16 ***
ORIGIN_SZMDSZ03   17.8404982  0.0169690  1051.36   <2e-16 ***
ORIGIN_SZMPSZ01   19.0765941  0.0083937  2272.74   <2e-16 ***
ORIGIN_SZMPSZ02   19.2162527  0.0068331  2812.24   <2e-16 ***
ORIGIN_SZMPSZ03   19.9965344  0.0054569  3664.44   <2e-16 ***
ORIGIN_SZMUSZ02   15.9130765  0.1037472   153.38   <2e-16 ***
ORIGIN_SZNTSZ01   17.0840999  0.0352513   484.64   <2e-16 ***
ORIGIN_SZNTSZ02   16.5792122  0.0233186   710.99   <2e-16 ***
ORIGIN_SZNTSZ03   18.9506415  0.0075957  2494.93   <2e-16 ***
ORIGIN_SZNTSZ05   15.8770261  0.0495825   320.21   <2e-16 ***
ORIGIN_SZNTSZ06   15.3997415  0.0557029   276.46   <2e-16 ***
ORIGIN_SZNVSZ01   20.2241694  0.0043487  4650.65   <2e-16 ***
ORIGIN_SZNVSZ02   19.1897826  0.0065383  2934.97   <2e-16 ***
ORIGIN_SZNVSZ03   18.8854268  0.0080459  2347.22   <2e-16 ***
ORIGIN_SZNVSZ04   18.8940191  0.0090985  2076.61   <2e-16 ***
ORIGIN_SZNVSZ05   17.6278585  0.0168107  1048.61   <2e-16 ***
ORIGIN_SZPGSZ01   19.4825220  0.0122960  1584.46   <2e-16 ***
ORIGIN_SZPGSZ02   19.4726761  0.0073116  2663.25   <2e-16 ***
ORIGIN_SZPGSZ03   20.5515713  0.0045631  4503.86   <2e-16 ***
ORIGIN_SZPGSZ04   21.0527131  0.0041500  5072.89   <2e-16 ***
ORIGIN_SZPGSZ05   20.1436604  0.0057267  3517.48   <2e-16 ***
ORIGIN_SZPLSZ01   19.1832002  0.0120006  1598.53   <2e-16 ***
ORIGIN_SZPLSZ02   18.8752206  0.0149740  1260.53   <2e-16 ***
ORIGIN_SZPLSZ03   18.1000818  0.0371769   486.86   <2e-16 ***
ORIGIN_SZPLSZ04   17.1730559  0.0370280   463.79   <2e-16 ***
ORIGIN_SZPLSZ05   17.9084439  0.0225031   795.82   <2e-16 ***
ORIGIN_SZPNSZ01   21.0804425  0.0044829  4702.41   <2e-16 ***
ORIGIN_SZPNSZ02   19.8822123  0.0111507  1783.05   <2e-16 ***
ORIGIN_SZPNSZ03   17.9293289  0.0193571   926.24   <2e-16 ***
ORIGIN_SZPNSZ04   17.1039594  0.0334954   510.64   <2e-16 ***
ORIGIN_SZPNSZ05   18.2543864  0.0275554   662.46   <2e-16 ***
ORIGIN_SZPRSZ01   19.8777935  0.0117586  1690.49   <2e-16 ***
ORIGIN_SZPRSZ02   21.0751780  0.0044832  4700.88   <2e-16 ***
ORIGIN_SZPRSZ03   20.6717019  0.0045577  4535.55   <2e-16 ***
ORIGIN_SZPRSZ04   19.6365125  0.0074923  2620.90   <2e-16 ***
ORIGIN_SZPRSZ05   21.3132151  0.0042119  5060.24   <2e-16 ***
ORIGIN_SZPRSZ06   18.9314574  0.0117278  1614.24   <2e-16 ***
ORIGIN_SZPRSZ07   17.2822918  0.0162430  1063.98   <2e-16 ***
ORIGIN_SZPRSZ08   19.9267642  0.0062298  3198.62   <2e-16 ***
ORIGIN_SZQTSZ01   19.7357175  0.0066359  2974.08   <2e-16 ***
ORIGIN_SZQTSZ02   19.2082141  0.0061402  3128.26   <2e-16 ***
ORIGIN_SZQTSZ03   19.7771883  0.0056220  3517.83   <2e-16 ***
ORIGIN_SZQTSZ04   18.7114421  0.0072842  2568.76   <2e-16 ***
ORIGIN_SZQTSZ05   19.3049324  0.0062401  3093.69   <2e-16 ***
ORIGIN_SZQTSZ06   19.2643228  0.0065590  2937.09   <2e-16 ***
ORIGIN_SZQTSZ07   18.5697347  0.0095373  1947.06   <2e-16 ***
ORIGIN_SZQTSZ08   19.6147001  0.0061330  3198.21   <2e-16 ***
ORIGIN_SZQTSZ09   19.2550793  0.0069947  2752.82   <2e-16 ***
ORIGIN_SZQTSZ10   19.5801866  0.0064513  3035.07   <2e-16 ***
ORIGIN_SZQTSZ11   17.7398366  0.0143648  1234.95   <2e-16 ***
ORIGIN_SZQTSZ12   17.2420354  0.0186736   923.34   <2e-16 ***
ORIGIN_SZQTSZ13   19.3857418  0.0078878  2457.69   <2e-16 ***
ORIGIN_SZQTSZ14   18.1300753  0.0122096  1484.90   <2e-16 ***
ORIGIN_SZQTSZ15   19.4222283  0.0120871  1606.86   <2e-16 ***
ORIGIN_SZRCSZ01   18.1549045  0.0125108  1451.13   <2e-16 ***
ORIGIN_SZRCSZ06   18.8836400  0.0082161  2298.38   <2e-16 ***
ORIGIN_SZRVSZ01   16.7864438  0.0323796   518.43   <2e-16 ***
ORIGIN_SZRVSZ02   16.4203244  0.0276836   593.14   <2e-16 ***
ORIGIN_SZRVSZ03   16.6453738  0.0244992   679.42   <2e-16 ***
ORIGIN_SZRVSZ04   15.9559213  0.0556344   286.80   <2e-16 ***
ORIGIN_SZRVSZ05   17.0476331  0.0164122  1038.71   <2e-16 ***
ORIGIN_SZSBSZ01   20.0417968  0.0062488  3207.29   <2e-16 ***
ORIGIN_SZSBSZ02   19.1869565  0.0081051  2367.26   <2e-16 ***
ORIGIN_SZSBSZ03   20.5769861  0.0045108  4561.70   <2e-16 ***
ORIGIN_SZSBSZ04   20.5154199  0.0050548  4058.57   <2e-16 ***
ORIGIN_SZSBSZ05   19.6250669  0.0065562  2993.35   <2e-16 ***
ORIGIN_SZSBSZ06   18.8419757  0.0171135  1101.00   <2e-16 ***
ORIGIN_SZSBSZ07   19.4897259  0.0124528  1565.09   <2e-16 ***
ORIGIN_SZSBSZ08   18.7027917  0.0140545  1330.73   <2e-16 ***
ORIGIN_SZSBSZ09   18.8893480  0.0088571  2132.67   <2e-16 ***
ORIGIN_SZSESZ02   20.8962192  0.0041665  5015.34   <2e-16 ***
ORIGIN_SZSESZ03   20.9452771  0.0039737  5270.94   <2e-16 ***
ORIGIN_SZSESZ04   20.6576142  0.0046364  4455.55   <2e-16 ***
ORIGIN_SZSESZ05   19.5170732  0.0058912  3312.92   <2e-16 ***
ORIGIN_SZSESZ06   20.7595824  0.0045747  4537.89   <2e-16 ***
ORIGIN_SZSESZ07   17.6888256  0.0195787   903.47   <2e-16 ***
ORIGIN_SZSGSZ01   19.1359250  0.0085781  2230.79   <2e-16 ***
ORIGIN_SZSGSZ02   18.5614369  0.0102037  1819.10   <2e-16 ***
ORIGIN_SZSGSZ03   19.9933176  0.0050434  3964.23   <2e-16 ***
ORIGIN_SZSGSZ04   20.2426871  0.0047211  4287.71   <2e-16 ***
ORIGIN_SZSGSZ05   18.0114965  0.0107743  1671.70   <2e-16 ***
ORIGIN_SZSGSZ06   20.2593194  0.0044538  4548.76   <2e-16 ***
ORIGIN_SZSGSZ07   19.0763664  0.0062968  3029.54   <2e-16 ***
ORIGIN_SZSKSZ01   19.9222451  0.0085136  2340.04   <2e-16 ***
ORIGIN_SZSKSZ02   20.8633383  0.0055248  3776.33   <2e-16 ***
ORIGIN_SZSKSZ03   19.6528148  0.0080534  2440.33   <2e-16 ***
ORIGIN_SZSKSZ04   18.0754470  0.0275771   655.45   <2e-16 ***
ORIGIN_SZSKSZ05   19.1192521  0.0155579  1228.91   <2e-16 ***
ORIGIN_SZSLSZ01   17.1501034  0.0329384   520.67   <2e-16 ***
ORIGIN_SZSLSZ04   19.5949774  0.0076753  2552.98   <2e-16 ***
ORIGIN_SZSRSZ01   16.9761403  0.0162020  1047.78   <2e-16 ***
ORIGIN_SZTHSZ01   17.9695687  0.0488559   367.81   <2e-16 ***
ORIGIN_SZTHSZ03   18.5427522  0.0223617   829.22   <2e-16 ***
ORIGIN_SZTHSZ04   17.4760374  0.0286247   610.52   <2e-16 ***
ORIGIN_SZTHSZ06   17.8401186  0.0183322   973.16   <2e-16 ***
ORIGIN_SZTMSZ01   20.3406361  0.0056607  3593.33   <2e-16 ***
ORIGIN_SZTMSZ02   22.0307026  0.0037386  5892.85   <2e-16 ***
ORIGIN_SZTMSZ03   21.3451920  0.0040606  5256.65   <2e-16 ***
ORIGIN_SZTMSZ04   20.6611593  0.0049896  4140.87   <2e-16 ***
ORIGIN_SZTMSZ05   19.3323133  0.0112868  1712.82   <2e-16 ***
ORIGIN_SZTNSZ01   17.9513571  0.0128266  1399.54   <2e-16 ***
ORIGIN_SZTNSZ02   18.0267387  0.0098372  1832.51   <2e-16 ***
ORIGIN_SZTNSZ03   17.7253700  0.0134668  1316.23   <2e-16 ***
ORIGIN_SZTNSZ04   19.4474075  0.0073760  2636.59   <2e-16 ***
ORIGIN_SZTPSZ01   19.1078631  0.0065635  2911.25   <2e-16 ***
ORIGIN_SZTPSZ02   20.2837634  0.0041411  4898.18   <2e-16 ***
ORIGIN_SZTPSZ03   19.1838238  0.0059552  3221.37   <2e-16 ***
ORIGIN_SZTPSZ04   19.1805388  0.0054778  3501.53   <2e-16 ***
ORIGIN_SZTPSZ05   19.3718076  0.0058610  3305.18   <2e-16 ***
ORIGIN_SZTPSZ06   19.6605723  0.0054968  3576.70   <2e-16 ***
ORIGIN_SZTPSZ07   19.4499807  0.0060491  3215.36   <2e-16 ***
ORIGIN_SZTPSZ08   18.7996538  0.0095757  1963.28   <2e-16 ***
ORIGIN_SZTPSZ09   19.0025110  0.0067068  2833.31   <2e-16 ***
ORIGIN_SZTPSZ10   18.8899657  0.0076094  2482.46   <2e-16 ***
ORIGIN_SZTPSZ11   19.6277780  0.0053983  3635.93   <2e-16 ***
ORIGIN_SZTPSZ12   19.1471104  0.0065742  2912.45   <2e-16 ***
ORIGIN_SZTSSZ01   17.4901113  0.0478954   365.17   <2e-16 ***
ORIGIN_SZTSSZ02   20.4997466  0.0081850  2504.55   <2e-16 ***
ORIGIN_SZTSSZ03   20.1076553  0.0084728  2373.19   <2e-16 ***
ORIGIN_SZTSSZ04   20.0646610  0.0089008  2254.26   <2e-16 ***
ORIGIN_SZTSSZ05   19.3962067  0.0151392  1281.19   <2e-16 ***
ORIGIN_SZTSSZ06   20.9235857  0.0178278  1173.65   <2e-16 ***
ORIGIN_SZWCSZ01   20.8411600  0.0086519  2408.86   <2e-16 ***
ORIGIN_SZWCSZ02   17.7355404  0.0328889   539.26   <2e-16 ***
ORIGIN_SZWCSZ03   14.9380886  0.1240699   120.40   <2e-16 ***
ORIGIN_SZWDSZ01   21.1969012  0.0037830  5603.23   <2e-16 ***
ORIGIN_SZWDSZ02   20.5930001  0.0044572  4620.13   <2e-16 ***
ORIGIN_SZWDSZ03   21.2521867  0.0041672  5099.85   <2e-16 ***
ORIGIN_SZWDSZ04   21.0702687  0.0048648  4331.13   <2e-16 ***
ORIGIN_SZWDSZ05   20.4008998  0.0051801  3938.35   <2e-16 ***
ORIGIN_SZWDSZ06   20.6669176  0.0049280  4193.78   <2e-16 ***
ORIGIN_SZWDSZ07   19.0500370  0.0082729  2302.71   <2e-16 ***
ORIGIN_SZWDSZ08   19.0816252  0.0080667  2365.49   <2e-16 ***
ORIGIN_SZWDSZ09   21.4182096  0.0040391  5302.73   <2e-16 ***
ORIGIN_SZYSSZ01   19.5355157  0.0057540  3395.14   <2e-16 ***
ORIGIN_SZYSSZ02   20.8737972  0.0048278  4323.64   <2e-16 ***
ORIGIN_SZYSSZ03   21.6614437  0.0040011  5413.81   <2e-16 ***
ORIGIN_SZYSSZ04   20.9305289  0.0043595  4801.10   <2e-16 ***
ORIGIN_SZYSSZ05   20.1727678  0.0058466  3450.34   <2e-16 ***
ORIGIN_SZYSSZ06   19.1481507  0.0116724  1640.47   <2e-16 ***
ORIGIN_SZYSSZ07   18.7919074  0.0141636  1326.78   <2e-16 ***
ORIGIN_SZYSSZ08   19.9733515  0.0061229  3262.07   <2e-16 ***
ORIGIN_SZYSSZ09   20.9366181  0.0040347  5189.15   <2e-16 ***
log(SCHOOL_COUNT)  0.4755516  0.0004701  1011.55   <2e-16 ***
log(RETAIL_COUNT)  0.1796905  0.0001856   968.12   <2e-16 ***
log(DIST)         -1.6929522  0.0004093 -4136.01   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for poisson family taken to be 1)

    Null deviance: 189463537  on 14471  degrees of freedom
Residual deviance:  15526121  on 14189  degrees of freedom
AIC: 15615824

Number of Fisher Scoring iterations: 6

What we want is the Estimate log(SCHOOL_COUNT) and log(RETAIL_COUNT) which are attractiveness factors and will tend to be positive. log(DIST) must be negative.

p-value for all logs have to be less than 0.05 to be significant for attractiveness.

Goodness-of-fit

Here we will create our own function for calculating R2

CalcRSquared <- function(observed, estimated){
  r<-cor(observed, estimated)
  R2<-r^2
  R2
  }

Next we will apply the R2 function to our data.

CalcRSquared(orcSIM_Poisson$data$TRIPS, orcSIM_Poisson$fitted.values)
[1] 0.4362208

The R2 measures how well it explains factor affects flow

performance_rmse(orcSIM_Poisson, normalized = FALSE)  #normalized will set mean to 0 like a z distrib
[1] 2613.236

Root mean square error(RMSE) measures how well the factors estimates flow, bigger = less nice fitting. An average 2613 trips false estimates

Doubly constrained

dbcSIM_Poisson <- glm(formula = TRIPS~
                        ORIGIN_SZ + 
                        DESTIN_SZ + 
                        log(DIST), # no -1 cus no attractiveness btw origin and des
                      family = poisson(link = "log"),
                      data = inter_zonal_flow,
                      na.action = na.exclude)

Goodness-of-fit

Here we can apply the R2 function again.

CalcRSquared(dbcSIM_Poisson$data$TRIPS, dbcSIM_Poisson$fitted.values)
[1] 0.7001882

Model Comparison

We will first convert the models that we have created into a list.

model_list <- list(originConstrained=orcSIM_Poisson,
                   doublyConstrained=dbcSIM_Poisson)

We can then finally use the compare_performance() function of the performance package to check for the better model.

compare_performance(model_list, metrics = "RMSE")
# Comparison of Model Performance Indices

Name              | Model |     RMSE
------------------------------------
originConstrained |   glm | 2613.236
doublyConstrained |   glm | 1906.694

A smaller RMSE represents a better model