In-class_Ex4A: Calibrating Spatial Interaction Models with R

Author

Widya Tantiya Yutika

Published

December 9, 2023

Modified

January 17, 2024

1 Overview

This in-class exercise is a continuation of Hands-on Exercise 3, In-class Exercise 3 and In-class Exercise 4: Preparing Spatial Interaction Modelling Variables. We will continue our journey of calibrating Spatial Interaction Models by using propulsiveness and attractiveness variables prepared in earlier in-class exercise.

2 Getting Started

For the purpose of this exercise, five r packages will be used. They are:

  • sf for importing, integrating, processing and transforming geospatial data.

  • tidyverse for importing, integrating, wrangling and visualising data.

  • tmap for plotting cartographicquality thematic maps.

  • performance for computing model comparison matrices such as rmse.

  • ggpubr for creating publication quality statistical graphics.

  • knitr integrates computing and reporting

pacman::p_load(tmap, sf, performance, ggpubr, tidyverse, knitr)

3 The Data

This exercise is a continuation of Hands-on Exercise 3 and In-class Exercise 4: Preparing Spatial Interaction Modelling Variables. The following data will be used:

  • flow_data_tidy.rds, weekday morning peak passenger flows at planning subzone level.

  • mpsz.rds, URA Master Plan 2019 Planning Subzone boundary in simple feature tibble data frame format.

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 …

Notice that this sf tibble data.frame includes two additional fields namely: SCHOOL_COUNT and BUSINESS_COUNT. Both of them will be used as attractiveness variables when calibrating origin constrained SIM.

The code chunk below is used to display the first five columns and rows of flow_data.

kable(head(flow_data[, 1:5], n = 5))
ORIGIN_SZ DESTIN_SZ MORNING_PEAK dist ORIGIN_AGE7_12 geometry
AMSZ01 AMSZ01 1998 50.0000 310 LINESTRING (29501.77 39419….
AMSZ01 AMSZ02 8289 810.4491 310 LINESTRING (29501.77 39419….
AMSZ01 AMSZ03 8971 1360.9294 310 LINESTRING (29501.77 39419….
AMSZ01 AMSZ04 2252 840.4432 310 LINESTRING (29501.77 39419….
AMSZ01 AMSZ05 6136 1076.7916 310 LINESTRING (29501.77 39419….
Important

Notice that this data.frame include intra-zonal flow.

3.1 Preparing inter-zonal flow data

In general, we will calibrate separate Spatial Interaction Models for inter- and intra-zonal flows. In this hands-on exercise, we will focus our attention on inter-zonal flow. Hence, we need to exclude the intra-zonal flow from flow_data.

First, two new columns called FlowNoIntra and offset will be created by using the code chunk below.

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
)

According to the syntax used to derive values in FlowNoIntra field, all intra-zonal flow will be given a value of 0 or else the original flow values will be inserted.

Next, inter-zonal flow will be selected from flow_data and save into a new output data.frame called inter_zonal_flow by using the code chunk below.

inter_zonal_flow <- flow_data %>%
  filter(FlowNoIntra >0)
Note
  • business small scale and middle scale industry; retail mainly for shopping purposes.

  • change morning_peak to TRIPS and dist to DIST (because there is a dist function in R)

inter_zonal_flow <- inter_zonal_flow %>%
  rename(TRIPS= MORNING_PEAK,
         DIST=dist)

You are ready to calibrate the Spatial Interaction Models now.

4 Calibrating Spatial Interaction Models

In this section, we will focus on calibrating an origin constrained SIM and a doubly constrained by using flow_data prepared. They complement what you have learned in Hands-on Exercise 3.

4.1 Origin(Production) constrained SIM

Figure below shows the general formula of the origin-constrained model.

Code chunk below shows the calibration of the model by using glm() of R and flow_data.

orcSIM_Poisson <- glm(formula = TRIPS ~ 
                        ORIGIN_SZ +
                        log(SCHOOL_COUNT)+
                        log(RETAIL_COUNT)+
                        log(DIST) -1,
                      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) - 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
Thing to learn from the code chunk above
  • For origin-constrained model, only explanatory variables representing the attractiveness at the destinations will be used.

  • All the explanatory variables including distance will be log transformed.

  • ORIGIN_SZ is used to model 𝜇𝑖 . It must be in categorical data type.

  • It is important to note that -1 is added in the equation after the distance variable. The -1 serves the purpose of removing the intercept that by default, glm will insert into the model.

Note

Need to look at log(SCHOOL_COUNT) , log(RETAIL_COUNT), log(DIST), p-value

  • log(SCHOOL_COUNT) , log(RETAIL_COUNT) -> attractiveness, need to be positive but if crime rate need to be negative (people dont want to stay near area with high crime rate)

  • log(DIST) -> need to be negative(inverse distance), meaning that the closer the distance, people are more willing to go

  • p-value < 0.0.5 -> if not <0.05 need to recalibrate the model, remove the variables

4.2 Goodness-of-Fit

In statistical modelling, the next question we would like to answer is how well the proportion of variance in the dependent variable (i.e. TRIPS) that can be explained by the explanatory variables.

In order to provide answer to this question, R-squared statistics will be used. However, R-squared is not an output of glm(). Hence we will write a function called CalcRSquared by using the code chunk below.

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

Now, we can examine how the constraints hold for destinations this time.

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

With reference to the R-Squared above, we can conclude that the model accounts for about 44% of the variation of flows in the systems. Not bad, but not brilliant either.

We can use the performance to get the Root Mean Square Error with the below chunk code.

performance_rmse(orcSIM_Poisson, normalized= FALSE)
[1] 2613.236

4.3 Doubly Constrained model

In this section, we will fit a doubly constrained SIM by using the general formula shown below:

and the code chunk used is shown below.

dbcSIM_Poisson <- glm(formula = TRIPS ~
                        ORIGIN_SZ+
                        DESTIN_SZ+
                        log(DIST),
                      family = poisson(link='log'),
                      data= inter_zonal_flow,
                      na.action= na.exclude
                      )
dbcSIM_Poisson

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

Coefficients:
    (Intercept)  ORIGIN_SZAMSZ02  ORIGIN_SZAMSZ03  ORIGIN_SZAMSZ04  
     21.8312374        0.5263502        0.3139982       -0.2146257  
ORIGIN_SZAMSZ05  ORIGIN_SZAMSZ06  ORIGIN_SZAMSZ07  ORIGIN_SZAMSZ08  
     -0.1890446        0.1539201       -0.9826565       -0.4488417  
ORIGIN_SZAMSZ09  ORIGIN_SZAMSZ10  ORIGIN_SZAMSZ11  ORIGIN_SZAMSZ12  
      0.0713474        0.4313742       -1.4712226       -1.7250733  
ORIGIN_SZBDSZ01  ORIGIN_SZBDSZ02  ORIGIN_SZBDSZ03  ORIGIN_SZBDSZ04  
      0.8810576        0.1100240        0.3606166        1.4624347  
ORIGIN_SZBDSZ05  ORIGIN_SZBDSZ06  ORIGIN_SZBDSZ07  ORIGIN_SZBDSZ08  
      0.6207557        0.6712973       -1.2338669       -1.0444562  
ORIGIN_SZBKSZ01  ORIGIN_SZBKSZ02  ORIGIN_SZBKSZ03  ORIGIN_SZBKSZ04  
     -0.2838426        0.5550522        0.7396640       -0.2242451  
ORIGIN_SZBKSZ05  ORIGIN_SZBKSZ06  ORIGIN_SZBKSZ07  ORIGIN_SZBKSZ08  
     -0.2371614       -0.1413812        0.7089989       -0.0907065  
ORIGIN_SZBKSZ09  ORIGIN_SZBLSZ01  ORIGIN_SZBLSZ02  ORIGIN_SZBLSZ03  
     -0.1775146       -2.3684539       -2.8078475       -3.3122763  
ORIGIN_SZBLSZ04  ORIGIN_SZBMSZ01  ORIGIN_SZBMSZ02  ORIGIN_SZBMSZ03  
     -2.6770542        0.0618035       -1.3535767       -0.7569095  
ORIGIN_SZBMSZ04  ORIGIN_SZBMSZ05  ORIGIN_SZBMSZ06  ORIGIN_SZBMSZ07  
     -0.2949304       -2.6131992       -3.0315024       -0.6962524  
ORIGIN_SZBMSZ08  ORIGIN_SZBMSZ09  ORIGIN_SZBMSZ10  ORIGIN_SZBMSZ11  
     -0.9310730       -1.2911253       -1.6687004       -1.1152794  
ORIGIN_SZBMSZ12  ORIGIN_SZBMSZ13  ORIGIN_SZBMSZ14  ORIGIN_SZBMSZ15  
     -1.5323954       -0.6267376       -1.0475467       -0.5049444  
ORIGIN_SZBMSZ16  ORIGIN_SZBMSZ17  ORIGIN_SZBPSZ01  ORIGIN_SZBPSZ02  
     -1.5282897       -1.5722349        0.5814175        0.0875442  
ORIGIN_SZBPSZ03  ORIGIN_SZBPSZ04  ORIGIN_SZBPSZ05  ORIGIN_SZBPSZ06  
      0.3358227        0.6507586        0.9502124       -1.0480314  
ORIGIN_SZBPSZ07  ORIGIN_SZBSSZ01  ORIGIN_SZBSSZ02  ORIGIN_SZBSSZ03  
     -0.5467931        0.2998334        0.2841036       -0.2331505  
ORIGIN_SZBTSZ01  ORIGIN_SZBTSZ02  ORIGIN_SZBTSZ03  ORIGIN_SZBTSZ04  
      0.0987284       -0.6261229       -0.4326963       -1.4998668  
ORIGIN_SZBTSZ05  ORIGIN_SZBTSZ06  ORIGIN_SZBTSZ07  ORIGIN_SZBTSZ08  
     -0.9564768       -1.2853131       -2.3870991       -1.3715855  
ORIGIN_SZCBSZ01  ORIGIN_SZCCSZ01  ORIGIN_SZCHSZ01  ORIGIN_SZCHSZ02  
     -3.5940232       -0.7008220       -0.9109524       -0.8566547  
ORIGIN_SZCHSZ03  ORIGIN_SZCKSZ01  ORIGIN_SZCKSZ02  ORIGIN_SZCKSZ03  
      1.1153731        0.3001815        0.7185711        1.1389824  
ORIGIN_SZCKSZ04  ORIGIN_SZCKSZ05  ORIGIN_SZCKSZ06  ORIGIN_SZCLSZ01  
      1.6281772        0.8338470        0.6528993       -0.7174758  
ORIGIN_SZCLSZ02  ORIGIN_SZCLSZ03  ORIGIN_SZCLSZ04  ORIGIN_SZCLSZ05  
     -1.7513100       -1.0362873        0.6160017       -2.1005122  
ORIGIN_SZCLSZ06  ORIGIN_SZCLSZ07  ORIGIN_SZCLSZ08  ORIGIN_SZCLSZ09  
      0.7252108       -0.5343482       -0.2153408       -1.8019961  
ORIGIN_SZDTSZ02  ORIGIN_SZDTSZ03  ORIGIN_SZDTSZ13  ORIGIN_SZGLSZ01  
     -3.9057711       -3.4152419       -3.0183438       -1.7812384  
ORIGIN_SZGLSZ02  ORIGIN_SZGLSZ03  ORIGIN_SZGLSZ04  ORIGIN_SZGLSZ05  
     -0.1074991       -0.2461106        0.8657186        0.5871393  
ORIGIN_SZHGSZ01  ORIGIN_SZHGSZ02  ORIGIN_SZHGSZ03  ORIGIN_SZHGSZ04  
      0.3543819        0.4218178        0.2411309        0.8180622  
ORIGIN_SZHGSZ05  ORIGIN_SZHGSZ06  ORIGIN_SZHGSZ07  ORIGIN_SZHGSZ08  
      1.2173687       -0.1826300        0.3172839       -0.1151369  
ORIGIN_SZHGSZ09  ORIGIN_SZHGSZ10  ORIGIN_SZJESZ01  ORIGIN_SZJESZ02  
     -1.2873441       -3.3783178        0.4859234        0.1766088  
ORIGIN_SZJESZ03  ORIGIN_SZJESZ04  ORIGIN_SZJESZ05  ORIGIN_SZJESZ06  
     -0.2177441       -1.5532182       -2.3332926        0.3007382  
ORIGIN_SZJESZ07  ORIGIN_SZJESZ08  ORIGIN_SZJESZ09  ORIGIN_SZJESZ10  
     -1.9687994       -1.3032070        0.5762635       -1.4423113  
ORIGIN_SZJESZ11  ORIGIN_SZJWSZ01  ORIGIN_SZJWSZ02  ORIGIN_SZJWSZ03  
     -1.9720897        0.3808627        0.7963999        1.5429636  
ORIGIN_SZJWSZ04  ORIGIN_SZJWSZ05  ORIGIN_SZJWSZ06  ORIGIN_SZJWSZ07  
      0.6410760       -2.1571049       -1.5174532       -2.7089963  
ORIGIN_SZJWSZ08  ORIGIN_SZJWSZ09  ORIGIN_SZKLSZ01  ORIGIN_SZKLSZ02  
      1.5343415        1.8837410        0.1081286       -0.8844695  
ORIGIN_SZKLSZ03  ORIGIN_SZKLSZ04  ORIGIN_SZKLSZ05  ORIGIN_SZKLSZ06  
     -0.6872640       -2.2090319       -1.1728726       -6.1162315  
ORIGIN_SZKLSZ07  ORIGIN_SZKLSZ08  ORIGIN_SZLKSZ01  ORIGIN_SZMDSZ01  
     -1.4082749       -1.7781551       -2.0531568       -0.8825639  
ORIGIN_SZMDSZ02  ORIGIN_SZMDSZ03  ORIGIN_SZMPSZ01  ORIGIN_SZMPSZ02  
     -0.6219993       -2.0840156       -0.9659093       -1.0411153  
ORIGIN_SZMPSZ03  ORIGIN_SZMUSZ02  ORIGIN_SZNTSZ01  ORIGIN_SZNTSZ02  
      0.0001659       -3.7599031       -3.0388366       -3.4230640  
ORIGIN_SZNTSZ03  ORIGIN_SZNTSZ05  ORIGIN_SZNTSZ06  ORIGIN_SZNVSZ01  
     -0.9094796       -4.0861681       -3.9497128        0.3235636  
ORIGIN_SZNVSZ02  ORIGIN_SZNVSZ03  ORIGIN_SZNVSZ04  ORIGIN_SZNVSZ05  
     -0.6946748       -1.0540196       -0.9897977       -2.2578432  
ORIGIN_SZPGSZ01  ORIGIN_SZPGSZ02  ORIGIN_SZPGSZ03  ORIGIN_SZPGSZ04  
      0.2399827       -0.3352342        0.9515148        1.3998952  
ORIGIN_SZPGSZ05  ORIGIN_SZPLSZ01  ORIGIN_SZPLSZ02  ORIGIN_SZPLSZ03  
      0.4451629       -0.9705918       -1.0670151       -2.1229124  
ORIGIN_SZPLSZ04  ORIGIN_SZPLSZ05  ORIGIN_SZPNSZ01  ORIGIN_SZPNSZ02  
     -3.0911932       -2.1705708        0.9052637       -0.1720425  
ORIGIN_SZPNSZ03  ORIGIN_SZPNSZ04  ORIGIN_SZPNSZ05  ORIGIN_SZPRSZ01  
     -2.3973459       -3.4483689       -2.0588530       -0.6399015  
ORIGIN_SZPRSZ02  ORIGIN_SZPRSZ03  ORIGIN_SZPRSZ04  ORIGIN_SZPRSZ05  
      0.8122270        0.3990960       -0.8485348        0.8008791  
ORIGIN_SZPRSZ06  ORIGIN_SZPRSZ07  ORIGIN_SZPRSZ08  ORIGIN_SZQTSZ01  
     -1.4498806       -3.2025045       -0.5862269       -0.1859270  
ORIGIN_SZQTSZ02  ORIGIN_SZQTSZ03  ORIGIN_SZQTSZ04  ORIGIN_SZQTSZ05  
     -0.8715122       -0.1259816       -1.4620032       -0.6675643  
ORIGIN_SZQTSZ06  ORIGIN_SZQTSZ07  ORIGIN_SZQTSZ08  ORIGIN_SZQTSZ09  
     -0.8190026       -1.5189403       -0.4976238       -0.9006162  
ORIGIN_SZQTSZ10  ORIGIN_SZQTSZ11  ORIGIN_SZQTSZ12  ORIGIN_SZQTSZ13  
     -0.6690184       -2.5203437       -3.0461675       -0.7501068  
ORIGIN_SZQTSZ14  ORIGIN_SZQTSZ15  ORIGIN_SZRCSZ01  ORIGIN_SZRCSZ06  
     -1.9321849       -0.9576828       -1.8167951       -0.5560563  
ORIGIN_SZRVSZ01  ORIGIN_SZRVSZ02  ORIGIN_SZRVSZ03  ORIGIN_SZRVSZ04  
     -2.8862570       -3.1555662       -2.9836089       -3.5520422  
ORIGIN_SZRVSZ05  ORIGIN_SZSBSZ01  ORIGIN_SZSBSZ02  ORIGIN_SZSBSZ03  
     -2.5866584        0.2867444       -0.9012334        0.8311038  
ORIGIN_SZSBSZ04  ORIGIN_SZSBSZ05  ORIGIN_SZSBSZ06  ORIGIN_SZSBSZ07  
      0.4044170       -0.2661845       -0.9023075        0.0505870  
ORIGIN_SZSBSZ08  ORIGIN_SZSBSZ09  ORIGIN_SZSESZ02  ORIGIN_SZSESZ03  
     -1.1158011       -0.9682835        1.1452735        1.2815277  
ORIGIN_SZSESZ04  ORIGIN_SZSESZ05  ORIGIN_SZSESZ06  ORIGIN_SZSESZ07  
      0.8085857       -0.2329413        1.0576879       -2.3165908  
ORIGIN_SZSGSZ01  ORIGIN_SZSGSZ02  ORIGIN_SZSGSZ03  ORIGIN_SZSGSZ04  
     -0.6606350       -1.3638984        0.1152591        0.2954067  
ORIGIN_SZSGSZ05  ORIGIN_SZSGSZ06  ORIGIN_SZSGSZ07  ORIGIN_SZSKSZ01  
     -2.0792678        0.4563227       -0.8955254       -0.3184402  
ORIGIN_SZSKSZ02  ORIGIN_SZSKSZ03  ORIGIN_SZSKSZ04  ORIGIN_SZSKSZ05  
      1.1160484       -0.2566692       -1.5781827       -0.2724361  
ORIGIN_SZSLSZ01  ORIGIN_SZSLSZ04  ORIGIN_SZSRSZ01  ORIGIN_SZTHSZ01  
     -2.4458625       -0.0987076       -2.2584977       -2.5878524  
ORIGIN_SZTHSZ03  ORIGIN_SZTHSZ04  ORIGIN_SZTHSZ06  ORIGIN_SZTMSZ01  
     -0.8101746       -2.4186655       -1.7080541       -0.2193476  
ORIGIN_SZTMSZ02  ORIGIN_SZTMSZ03  ORIGIN_SZTMSZ04  ORIGIN_SZTMSZ05  
      1.7772464        1.0051343        0.1642370       -1.2878706  
ORIGIN_SZTNSZ01  ORIGIN_SZTNSZ02  ORIGIN_SZTNSZ03  ORIGIN_SZTNSZ04  
     -1.7163504       -1.6508988       -2.1545577       -0.3949120  
ORIGIN_SZTPSZ01  ORIGIN_SZTPSZ02  ORIGIN_SZTPSZ03  ORIGIN_SZTPSZ04  
     -0.8058100        0.5369060       -0.7779333       -0.8153581  
ORIGIN_SZTPSZ05  ORIGIN_SZTPSZ06  ORIGIN_SZTPSZ07  ORIGIN_SZTPSZ08  
     -0.5073676        0.0847301       -0.5839519       -1.0577941  
ORIGIN_SZTPSZ09  ORIGIN_SZTPSZ10  ORIGIN_SZTPSZ11  ORIGIN_SZTPSZ12  
     -0.9067707       -1.1362091       -0.2374621       -0.8028874  
ORIGIN_SZTSSZ01  ORIGIN_SZTSSZ02  ORIGIN_SZTSSZ03  ORIGIN_SZTSSZ04  
     -2.7809271        0.0425804        0.1142369       -0.6186261  
ORIGIN_SZTSSZ05  ORIGIN_SZTSSZ06  ORIGIN_SZWCSZ01  ORIGIN_SZWCSZ02  
     -1.0846732        0.3980173        1.3545143       -2.9863278  
ORIGIN_SZWCSZ03  ORIGIN_SZWDSZ01  ORIGIN_SZWDSZ02  ORIGIN_SZWDSZ03  
     -5.0504916        1.5238429        0.2832576        1.3702524  
ORIGIN_SZWDSZ04  ORIGIN_SZWDSZ05  ORIGIN_SZWDSZ06  ORIGIN_SZWDSZ07  
      1.0248225        0.2356778        0.3146925       -1.4971897  
ORIGIN_SZWDSZ08  ORIGIN_SZWDSZ09  ORIGIN_SZYSSZ01  ORIGIN_SZYSSZ02  
     -0.8894079        1.4437633       -0.2519398        0.8726785  
ORIGIN_SZYSSZ03  ORIGIN_SZYSSZ04  ORIGIN_SZYSSZ05  ORIGIN_SZYSSZ06  
      1.7868139        0.8418040        0.4292096       -0.7459961  
ORIGIN_SZYSSZ07  ORIGIN_SZYSSZ08  ORIGIN_SZYSSZ09  DESTIN_SZAMSZ02  
     -0.8422281        0.1829428        1.1159712        0.0694567  
DESTIN_SZAMSZ03  DESTIN_SZAMSZ04  DESTIN_SZAMSZ05  DESTIN_SZAMSZ06  
      0.0760100       -1.1306391       -1.0751133       -0.9624298  
DESTIN_SZAMSZ07  DESTIN_SZAMSZ08  DESTIN_SZAMSZ09  DESTIN_SZAMSZ10  
     -1.5060319       -0.4813202       -1.0220675        0.1235142  
DESTIN_SZAMSZ11  DESTIN_SZAMSZ12  DESTIN_SZBDSZ01  DESTIN_SZBDSZ02  
     -0.8917993        0.0195208        0.9736349       -0.1969470  
DESTIN_SZBDSZ03  DESTIN_SZBDSZ04  DESTIN_SZBDSZ05  DESTIN_SZBDSZ06  
      0.1266471        1.1608485        0.9293840        0.4090567  
DESTIN_SZBDSZ07  DESTIN_SZBDSZ08  DESTIN_SZBKSZ01  DESTIN_SZBKSZ02  
     -0.8171478       -1.5895287       -1.3793311       -0.5253670  
DESTIN_SZBKSZ03  DESTIN_SZBKSZ04  DESTIN_SZBKSZ05  DESTIN_SZBKSZ06  
     -1.0095362       -0.5662858       -0.9406607       -1.3129276  
DESTIN_SZBKSZ07  DESTIN_SZBKSZ08  DESTIN_SZBKSZ09  DESTIN_SZBLSZ01  
      0.0120605       -1.3658471       -0.1771310       -0.8175223  
DESTIN_SZBLSZ02  DESTIN_SZBLSZ03  DESTIN_SZBLSZ04  DESTIN_SZBMSZ01  
      0.1631280        1.2598494       -0.5642975        0.6921844  
DESTIN_SZBMSZ02  DESTIN_SZBMSZ03  DESTIN_SZBMSZ04  DESTIN_SZBMSZ05  
     -0.1209392       -0.2373881       -0.0407117       -0.2363309  
DESTIN_SZBMSZ06  DESTIN_SZBMSZ07  DESTIN_SZBMSZ08  DESTIN_SZBMSZ09  
     -1.1930710        0.4625103       -0.8604731       -2.1290239  
DESTIN_SZBMSZ10  DESTIN_SZBMSZ11  DESTIN_SZBMSZ12  DESTIN_SZBMSZ13  
     -1.4617153       -1.3234050       -0.8399230        0.1366529  
DESTIN_SZBMSZ14  DESTIN_SZBMSZ15  DESTIN_SZBMSZ16  DESTIN_SZBMSZ17  
     -1.0491968       -0.6726684       -1.4011734       -1.5682752  
DESTIN_SZBPSZ01  DESTIN_SZBPSZ02  DESTIN_SZBPSZ03  DESTIN_SZBPSZ04  
     -1.1120017       -2.0833466       -1.6937265       -0.7964999  
DESTIN_SZBPSZ05  DESTIN_SZBPSZ06  DESTIN_SZBPSZ07  DESTIN_SZBSSZ01  
      0.2109118       -1.1808365       -0.2077428        0.3164175  
DESTIN_SZBSSZ02  DESTIN_SZBSSZ03  DESTIN_SZBTSZ01  DESTIN_SZBTSZ02  
     -0.4852688        0.4130432        0.6215095       -0.0145076  
DESTIN_SZBTSZ03  DESTIN_SZBTSZ04  DESTIN_SZBTSZ05  DESTIN_SZBTSZ06  
      0.4919981       -0.6957555        0.3329814       -0.1333295  
DESTIN_SZBTSZ07  DESTIN_SZBTSZ08  DESTIN_SZCBSZ01  DESTIN_SZCCSZ01  
     -1.4449581       -0.7079056       -5.7344725       -0.0009541  
DESTIN_SZCHSZ01  DESTIN_SZCHSZ02  DESTIN_SZCHSZ03  DESTIN_SZCKSZ01  
     -0.2083016        0.5369606        2.5530638       -0.5725975  
DESTIN_SZCKSZ02  DESTIN_SZCKSZ03  DESTIN_SZCKSZ04  DESTIN_SZCKSZ05  
     -1.1181852        0.1156680       -0.8647725       -1.1641791  
DESTIN_SZCKSZ06  DESTIN_SZCLSZ01  DESTIN_SZCLSZ02  DESTIN_SZCLSZ03  
     -0.4397612        0.1930552       -2.0436501       -0.9338571  
DESTIN_SZCLSZ04  DESTIN_SZCLSZ05  DESTIN_SZCLSZ06  DESTIN_SZCLSZ07  
      0.0532041       -1.0782781        0.4068171       -0.3579507  
DESTIN_SZCLSZ08  DESTIN_SZCLSZ09  DESTIN_SZDTSZ02  DESTIN_SZDTSZ03  
     -0.2487993        0.1611080       -1.7308348       -0.5994253  
DESTIN_SZDTSZ13  DESTIN_SZGLSZ01  DESTIN_SZGLSZ02  DESTIN_SZGLSZ03  
     -1.3685031       -0.0910001       -0.0692224        0.6493421  
DESTIN_SZGLSZ04  DESTIN_SZGLSZ05  DESTIN_SZHGSZ01  DESTIN_SZHGSZ02  
      0.9327947        0.8161728        0.0658625       -0.8134329  
DESTIN_SZHGSZ03  DESTIN_SZHGSZ04  DESTIN_SZHGSZ05  DESTIN_SZHGSZ06  
     -1.3546132       -0.4500588       -0.5026431       -0.8673686  
DESTIN_SZHGSZ07  DESTIN_SZHGSZ08  DESTIN_SZHGSZ09  DESTIN_SZHGSZ10  
      0.0560490       -0.0443189       -0.0126355       -3.5821793  
DESTIN_SZJESZ01  DESTIN_SZJESZ02  DESTIN_SZJESZ03  DESTIN_SZJESZ04  
     -0.3704281       -0.7369159       -0.8985484       -1.0511995  
DESTIN_SZJESZ05  DESTIN_SZJESZ06  DESTIN_SZJESZ07  DESTIN_SZJESZ08  
     -1.5324974        0.3105267       -1.3234483       -0.6559742  
DESTIN_SZJESZ09  DESTIN_SZJESZ10  DESTIN_SZJESZ11  DESTIN_SZJWSZ01  
      0.2663752        0.8529026        0.5559641       -0.9790971  
DESTIN_SZJWSZ02  DESTIN_SZJWSZ03  DESTIN_SZJWSZ04  DESTIN_SZJWSZ05  
     -0.8746590        0.5689062        0.4520963       -1.0249671  
DESTIN_SZJWSZ06  DESTIN_SZJWSZ07  DESTIN_SZJWSZ08  DESTIN_SZJWSZ09  
     -0.7451483       -2.8453099       -0.3372309        1.0505330  
DESTIN_SZKLSZ01  DESTIN_SZKLSZ02  DESTIN_SZKLSZ03  DESTIN_SZKLSZ04  
     -0.2334836       -0.5416148       -0.8026495       -1.2918594  
DESTIN_SZKLSZ05  DESTIN_SZKLSZ06  DESTIN_SZKLSZ07  DESTIN_SZKLSZ08  
     -0.4069101       -2.5333101       -0.6623343       -0.1408205  
DESTIN_SZLKSZ01  DESTIN_SZMDSZ01  DESTIN_SZMDSZ02  DESTIN_SZMDSZ03  
     -1.2639235       -1.5655800       -0.9767682       -3.3328109  
DESTIN_SZMPSZ01  DESTIN_SZMPSZ02  DESTIN_SZMPSZ03  DESTIN_SZMUSZ02  
     -0.4552859       -0.5386560        0.4952000       -1.4434175  
DESTIN_SZNTSZ01  DESTIN_SZNTSZ02  DESTIN_SZNTSZ03  DESTIN_SZNTSZ05  
     -2.9194067       -1.3780179       -0.5044699       -2.0017134  
DESTIN_SZNTSZ06  DESTIN_SZNVSZ01  DESTIN_SZNVSZ02  DESTIN_SZNVSZ03  
     -3.8120537       -0.1071506       -0.0274710        0.1076352  
DESTIN_SZNVSZ04  DESTIN_SZNVSZ05  DESTIN_SZPGSZ01  DESTIN_SZPGSZ02  
     -1.2087250       -1.0058290       -1.2029931       -1.2878671  
DESTIN_SZPGSZ03  DESTIN_SZPGSZ04  DESTIN_SZPGSZ05  DESTIN_SZPLSZ01  
     -0.1520894       -0.1985959       -1.5290983       -0.3567934  
DESTIN_SZPLSZ02  DESTIN_SZPLSZ03  DESTIN_SZPLSZ04  DESTIN_SZPLSZ05  
     -1.7114351       -0.3241427       -1.7117196       -0.5086379  
DESTIN_SZPNSZ01  DESTIN_SZPNSZ02  DESTIN_SZPNSZ03  DESTIN_SZPNSZ04  
      0.2026781        0.8313754       -0.4041254        1.5814539  
DESTIN_SZPNSZ05  DESTIN_SZPRSZ01  DESTIN_SZPRSZ02  DESTIN_SZPRSZ03  
      1.1823430       -1.1057553        0.0895099        0.6921925  
DESTIN_SZPRSZ04  DESTIN_SZPRSZ05  DESTIN_SZPRSZ06  DESTIN_SZPRSZ07  
     -0.2848336        0.1744480        0.4279206       -1.5123108  
DESTIN_SZPRSZ08  DESTIN_SZQTSZ01  DESTIN_SZQTSZ02  DESTIN_SZQTSZ03  
     -0.5650226       -0.5952360       -0.7728170       -0.5066812  
DESTIN_SZQTSZ04  DESTIN_SZQTSZ05  DESTIN_SZQTSZ06  DESTIN_SZQTSZ07  
     -0.6398414       -0.4354527       -0.6597391       -0.9392696  
DESTIN_SZQTSZ08  DESTIN_SZQTSZ09  DESTIN_SZQTSZ10  DESTIN_SZQTSZ11  
      0.4617774       -0.3174497        0.1993449        0.2551535  
DESTIN_SZQTSZ12  DESTIN_SZQTSZ13  DESTIN_SZQTSZ14  DESTIN_SZQTSZ15  
     -0.1662603        0.5500978        0.5364435        1.3611043  
DESTIN_SZRCSZ01  DESTIN_SZRCSZ06  DESTIN_SZRVSZ01  DESTIN_SZRVSZ02  
     -0.1034049       -1.0633902       -1.5486221       -2.4092611  
DESTIN_SZRVSZ03  DESTIN_SZRVSZ04  DESTIN_SZRVSZ05  DESTIN_SZSBSZ01  
     -1.5172079       -1.1663615       -2.2404292       -1.3783780  
DESTIN_SZSBSZ02  DESTIN_SZSBSZ03  DESTIN_SZSBSZ04  DESTIN_SZSBSZ05  
     -1.4445213        0.5149906        0.2389086       -1.2737442  
DESTIN_SZSBSZ06  DESTIN_SZSBSZ07  DESTIN_SZSBSZ08  DESTIN_SZSBSZ09  
     -1.8683520       -0.5993154        0.8156302        0.0900611  
DESTIN_SZSESZ02  DESTIN_SZSESZ03  DESTIN_SZSESZ04  DESTIN_SZSESZ05  
     -0.6397704        0.1714103       -1.0596175       -0.8071891  
DESTIN_SZSESZ06  DESTIN_SZSESZ07  DESTIN_SZSGSZ01  DESTIN_SZSGSZ02  
     -0.5580934       -3.1448863       -0.1795225       -0.2986570  
DESTIN_SZSGSZ03  DESTIN_SZSGSZ04  DESTIN_SZSGSZ05  DESTIN_SZSGSZ06  
     -0.4074671       -0.1505164       -1.9908372        0.6715268  
DESTIN_SZSGSZ07  DESTIN_SZSISZ01  DESTIN_SZSKSZ01  DESTIN_SZSKSZ02  
     -0.4494757       -0.5517983       -0.4749154        0.9400302  
DESTIN_SZSKSZ03  DESTIN_SZSKSZ04  DESTIN_SZSKSZ05  DESTIN_SZSLSZ01  
     -0.2800377       -1.2570212       -0.2600474       -0.7775604  
DESTIN_SZSLSZ04  DESTIN_SZSRSZ01  DESTIN_SZTHSZ01  DESTIN_SZTHSZ03  
     -0.8586515       -1.1370887       -4.3259988       -2.6632914  
DESTIN_SZTHSZ04  DESTIN_SZTHSZ06  DESTIN_SZTMSZ01  DESTIN_SZTMSZ02  
     -3.1000906       -2.5952642       -0.2092828        1.8238139  
DESTIN_SZTMSZ03  DESTIN_SZTMSZ04  DESTIN_SZTMSZ05  DESTIN_SZTNSZ01  
      0.8518259        1.0222812        0.6323777       -0.3336078  
DESTIN_SZTNSZ02  DESTIN_SZTNSZ03  DESTIN_SZTNSZ04  DESTIN_SZTPSZ01  
     -1.0820469       -1.4186505       -0.3058199       -0.4872299  
DESTIN_SZTPSZ02  DESTIN_SZTPSZ03  DESTIN_SZTPSZ04  DESTIN_SZTPSZ05  
      0.7158441       -0.4314229       -1.5898245       -1.0445550  
DESTIN_SZTPSZ06  DESTIN_SZTPSZ07  DESTIN_SZTPSZ08  DESTIN_SZTPSZ09  
     -0.4319582       -2.1602303       -1.1920493       -0.2022481  
DESTIN_SZTPSZ10  DESTIN_SZTPSZ11  DESTIN_SZTPSZ12  DESTIN_SZTSSZ01  
     -1.2464793       -0.0808445       -0.6784376       -1.5845062  
DESTIN_SZTSSZ02  DESTIN_SZTSSZ03  DESTIN_SZTSSZ04  DESTIN_SZTSSZ05  
     -0.1886010        0.6525526        0.5285464        1.4670106  
DESTIN_SZTSSZ06  DESTIN_SZWCSZ01  DESTIN_SZWCSZ02  DESTIN_SZWCSZ03  
      2.5043588        1.9787931       -2.2593108       -3.1897655  
DESTIN_SZWDSZ01  DESTIN_SZWDSZ02  DESTIN_SZWDSZ03  DESTIN_SZWDSZ04  
      1.0476108       -1.3176990        0.3432057       -0.7895927  
DESTIN_SZWDSZ05  DESTIN_SZWDSZ06  DESTIN_SZWDSZ07  DESTIN_SZWDSZ08  
     -0.8751665       -0.2106221       -1.6050834       -0.5124717  
DESTIN_SZWDSZ09  DESTIN_SZYSSZ01  DESTIN_SZYSSZ02  DESTIN_SZYSSZ03  
      0.3813542        0.0853753       -0.3227172       -0.4151283  
DESTIN_SZYSSZ04  DESTIN_SZYSSZ05  DESTIN_SZYSSZ06  DESTIN_SZYSSZ07  
     -0.4637327       -1.5888242       -1.4606209       -0.7839065  
DESTIN_SZYSSZ08  DESTIN_SZYSSZ09        log(DIST)  
      0.6265412        0.1520067       -1.8468315  

Degrees of Freedom: 14470 Total (i.e. Null);  13912 Residual
Null Deviance:      47090000 
Residual Deviance: 10420000     AIC: 10510000
Important

It is important to note that there is a slight change of the code chunk. I have removed the -1 which means that an intercept will appear in the model again. This is not because I want an intercept as it makes the origin and destination coefficients harder to interpret, rather the -1 cheat for removing the intercept only works with one factor level but in double-constrained model we have two factor levels, namely: origins and destinations.

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

Notice that there is a relatively greater improvement in the R-Squared value.

5 Model comparison

5.1 Statistical measures

Another useful model performance measure for continuous dependent variable is Root Mean Squared Error. In this sub-section, you will learn how to use compare_performance() of performance package

First of all, let us create a list called model_list by using the code chunk below.

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

Next, we will compute the RMSE of all the models in model_list file by using the code chunk below.

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

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

The print above reveals that doubly constrained SIM is the best model among the two SIMs because it has the smallest RMSE value of 1906.694.

Note
  • smaller RMSE -> better model

  • if there is an outlier, try to remove and compare with current model.

5.2 Visualising fitted values

In this section, you will learn how to visualise the observed values and the fitted values.

Firstly we will extract the fitted values from Origin-constrained Model by using the code chunk below.

df <- as.data.frame(orcSIM_Poisson$fitted.values) %>%
  round(digits = 0)

Next, we will append the fitted values into inter_zonal_flow data frame by using the code chunk below.

inter_zonal_flow <- inter_zonal_flow %>%
  cbind(df) %>%
  rename(orcTRIPS = "orcSIM_Poisson.fitted.values")
Tip

Notice that rename() is used to rename the field name and the $ in the original field name has been replaced with an .. This is because R replaced $ with . during the cbind().

We will also do the same step for doubly constrained model

df <- as.data.frame(dbcSIM_Poisson$fitted.values) %>%
  round(digits = 0)

Next, we will append the fitted values into inter_zonal_flow data frame by using the code chunk below.

inter_zonal_flow <- inter_zonal_flow %>%
  cbind(df) %>%
  rename(dbcTRIPS = "dbcSIM_Poisson.fitted.values")

Next, two scatterplots will be created by using geom_point() and other appropriate functions of ggplot2 package.

orc_p <- ggplot(data = inter_zonal_flow,
                aes(x = orcTRIPS,
                    y = TRIPS)) +
  geom_point() +
  geom_smooth(method = lm) +
  coord_cartesian(xlim=c(0,150000),
                  ylim=c(0,150000))

dbc_p <- ggplot(data = inter_zonal_flow,
                aes(x = dbcTRIPS,
                    y = TRIPS)) +
  geom_point() +
  geom_smooth(method = lm) +
  coord_cartesian(xlim=c(0,150000),
                  ylim=c(0,150000))

Now, we will put all the graphs into a single visual for better comparison by using the code chunk below.

ggarrange(orc_p, dbc_p,
          ncol = 2,
          nrow = 1)