This article will demonstrate a real world example with a 191-participant dataset.

Reading data

All data should be loaded at the beginning of the script. This includes the PALMS dataset, and any other shapefiles and csv files. Any preprocessing, such as buffering can also be done here.

library(palmsplusr)
library(readr)

# Load PALMS dataset
palms <- read_palms("D:/data/csv/palms_output.csv")

# Load other csvs
participant_basis <- read_csv("D:/data/csv/participant_basis.csv")
class_timetable <- read_csv("D:/data/csv/class_timetable.csv")

# Load shapefiles (home points & schoolyard polygons)
home <- read_sf("D:/data/shapefiles/home.shp")
school <- read_sf("D:/data/shapefiles/school.shp")

# Buffer home points
home.buffer <- palms_buffer(point = home, distance = 100, crs = 2193)

The participant_basis file is used to link participant identifier with the correct school and classroom:

head(participant_basis, n = 5)
#> # A tibble: 5 x 3
#>   identifier school_id class_id
#>   <chr>          <int>    <int>
#> 1 RC0400             2        2
#> 2 RC0402             2        2
#> 3 RC0404             2        2
#> 4 RC0406             2        2
#> 5 RC0414             2        2

The class_timetable file details start and end times for school and recess preiods, for each school/classroom combination:

head(class_timetable, n = 5)
#> # A tibble: 5 x 9
#>   school_id class_id date       school_start        school_end         
#>       <int>    <int> <date>     <dttm>              <dttm>             
#> 1         2        2 2013-06-10 2013-06-10 08:45:00 2013-06-10 15:15:00
#> 2         2        2 2013-06-11 2013-06-11 08:45:00 2013-06-11 15:15:00
#> 3         2        2 2013-06-12 2013-06-12 08:45:00 2013-06-12 15:15:00
#> 4         2        2 2013-06-13 2013-06-13 08:45:00 2013-06-13 15:15:00
#> 5         2        2 2013-06-14 2013-06-14 08:45:00 2013-06-14 15:15:00
#> # ... with 4 more variables: recess1_start <dttm>, recess1_end <dttm>,
#> #   recess2_start <dttm>, recess2_end <dttm>

Creating the five field tables

All formulas for building the palmsplus, days, trajectories, and multimodal datasets are created. This is done by creating the five field tables:

  • palmsplus_fields
  • trajectory_fields
  • multimodal_fields
  • palmsplus_domains
  • trajectory_locations

The first three of these tables can be populated using the palms_load_defaults(). I use this function as a base, but then add additional fields that are specific to my dataset and research questions. To see a list of fields that are included by default, please see the building formulas article.

# Remove all field tables that exist
palms_remove_tables()

# Load defaults
palms_load_defaults(palms_epoch(palms))

# palmsplus_fields (from other files)
palms_add_field("in_school_time", "palms_in_time(., i, class_timetable, participant_basis, school_start, school_end)")
palms_add_field("in_recess1", "palms_in_time(., i, class_timetable, participant_basis, recess1_start, recess1_end)")
palms_add_field("in_recess2", "palms_in_time(., i, class_timetable, participant_basis, recess2_start, recess2_end)")
palms_add_field("at_home", "palms_in_polygon(., filter(home.buffer, identifier == i), identifier)")
palms_add_field("at_school", "palms_in_polygon(., filter(school, school_id == participant_basis %>% filter(identifier == i) %>% pull(school_id)))")

# palmsplus_domains
palms_add_domain("home", "at_home")
palms_add_domain("school", "(!at_home & at_school & in_school_time)")
palms_add_domain("transport", "!at_home & !(at_school & in_school_time) & (pedestrian | bicycle | vehicle)")
palms_add_domain("leisure", "!at_home & !(at_school & in_school_time) & !pedestrian & !bicycle & !vehicle")

# trajectory_locations
palms_add_trajectory_location("home_school", "at_home", "at_school")
palms_add_trajectory_location("school_home", "at_school", "at_home")
palms_add_trajectory_location("home_home", "at_home", "at_home")
palms_add_trajectory_location("school_school", "at_school", "at_school")

I didn’t specify any additional trajectory_fields or multimodal_fields as the default fields will suffice.

Build palmsplus, days, trajectories, and multimodal

Once all of the formula tables are created, the datasets can be built. I’ve included a timer here to see how long it takes to process:

# Start the timer
t <- proc.time()

# Build palmsplus
palmsplus <- palms_build_palmsplus(palms)
#> [1/191] Computed palmsplus for: RC0430
#> [2/191] Computed palmsplus for: RC0495
#> [3/191] Computed palmsplus for: RC0404
#> [4/191] Computed palmsplus for: RC0475
#> [5/191] Computed palmsplus for: RC0488
#> [6/191] Computed palmsplus for: RC0507
#> [7/191] Computed palmsplus for: RC0456
#> [8/191] Computed palmsplus for: RC0515
#> [9/191] Computed palmsplus for: RC0509
#> [10/191] Computed palmsplus for: RC0494
#> [11/191] Computed palmsplus for: RC0400
#> [12/191] Computed palmsplus for: RC0422
#> [13/191] Computed palmsplus for: RC0423
#> [14/191] Computed palmsplus for: RC0506
#> [15/191] Computed palmsplus for: RC0439
#> [16/191] Computed palmsplus for: RC0445
#> [17/191] Computed palmsplus for: RC0465
#> [18/191] Computed palmsplus for: RC0476
#> [19/191] Computed palmsplus for: RC0470
#> [20/191] Computed palmsplus for: RC0478
#> [21/191] Computed palmsplus for: RC0420
#> [22/191] Computed palmsplus for: RC0437
#> [23/191] Computed palmsplus for: RC0415
#> [24/191] Computed palmsplus for: RC0510
#> [25/191] Computed palmsplus for: RC0520
#> [26/191] Computed palmsplus for: RC0466
#> [27/191] Computed palmsplus for: RC0414
#> [28/191] Computed palmsplus for: RC0406
#> [29/191] Computed palmsplus for: BC0608
#> [30/191] Computed palmsplus for: BC0626
#> [31/191] Computed palmsplus for: BC0627
#> [32/191] Computed palmsplus for: BC0629
#> [33/191] Computed palmsplus for: BC0632
#> [34/191] Computed palmsplus for: BC0668
#> [35/191] Computed palmsplus for: BC0670
#> [36/191] Computed palmsplus for: BC0676
#> [37/191] Computed palmsplus for: BC0684
#> [38/191] Computed palmsplus for: BC0710
#> [39/191] Computed palmsplus for: BC0733
#> [40/191] Computed palmsplus for: BC0750
#> [41/191] Computed palmsplus for: BC0771
#> [42/191] Computed palmsplus for: BC0828
#> [43/191] Computed palmsplus for: BC0838
#> [44/191] Computed palmsplus for: BC0912
#> [45/191] Computed palmsplus for: BC0938
#> [46/191] Computed palmsplus for: BC0948
#> [47/191] Computed palmsplus for: BC1005
#> [48/191] Computed palmsplus for: BC1012
#> [49/191] Computed palmsplus for: BC1177
#> [50/191] Computed palmsplus for: BC1212
#> [51/191] Computed palmsplus for: BC1215
#> [52/191] Computed palmsplus for: BC1227
#> [53/191] Computed palmsplus for: BC1252
#> [54/191] Computed palmsplus for: BC1255
#> [55/191] Computed palmsplus for: BC1268
#> [56/191] Computed palmsplus for: BC1305
#> [57/191] Computed palmsplus for: HC1904
#> [58/191] Computed palmsplus for: HC1906
#> [59/191] Computed palmsplus for: HC1907
#> [60/191] Computed palmsplus for: HC1909
#> [61/191] Computed palmsplus for: HC1911
#> [62/191] Computed palmsplus for: HC1916
#> [63/191] Computed palmsplus for: HC1917
#> [64/191] Computed palmsplus for: HC1918
#> [65/191] Computed palmsplus for: HC1924
#> [66/191] Computed palmsplus for: HC1925
#> [67/191] Computed palmsplus for: HC1927
#> [68/191] Computed palmsplus for: HC1936
#> [69/191] Computed palmsplus for: HC1939
#> [70/191] Computed palmsplus for: HC1944
#> [71/191] Computed palmsplus for: HC1948
#> [72/191] Computed palmsplus for: HC1952
#> [73/191] Computed palmsplus for: HC1954
#> [74/191] Computed palmsplus for: HC1961
#> [75/191] Computed palmsplus for: HC1962
#> [76/191] Computed palmsplus for: HC1963
#> [77/191] Computed palmsplus for: HC1968
#> [78/191] Computed palmsplus for: HC1970
#> [79/191] Computed palmsplus for: HC1971
#> [80/191] Computed palmsplus for: HC1976
#> [81/191] Computed palmsplus for: HC1979
#> [82/191] Computed palmsplus for: HC1980
#> [83/191] Computed palmsplus for: OC1402
#> [84/191] Computed palmsplus for: OC1403
#> [85/191] Computed palmsplus for: OC1404
#> [86/191] Computed palmsplus for: OC1413
#> [87/191] Computed palmsplus for: OC1414
#> [88/191] Computed palmsplus for: OC1421
#> [89/191] Computed palmsplus for: OC1429
#> [90/191] Computed palmsplus for: OC1439
#> [91/191] Computed palmsplus for: OC1442
#> [92/191] Computed palmsplus for: OC1443
#> [93/191] Computed palmsplus for: OC1444
#> [94/191] Computed palmsplus for: OC1450
#> [95/191] Computed palmsplus for: OC1451
#> [96/191] Computed palmsplus for: OC1454
#> [97/191] Computed palmsplus for: OC1461
#> [98/191] Computed palmsplus for: OC1466
#> [99/191] Computed palmsplus for: OC1467
#> [100/191] Computed palmsplus for: OC1468
#> [101/191] Computed palmsplus for: OC1473
#> [102/191] Computed palmsplus for: OC1476
#> [103/191] Computed palmsplus for: OC1479
#> [104/191] Computed palmsplus for: OC1480
#> [105/191] Computed palmsplus for: OC1484
#> [106/191] Computed palmsplus for: OC1485
#> [107/191] Computed palmsplus for: OC1489
#> [108/191] Computed palmsplus for: OC1501
#> [109/191] Computed palmsplus for: OC1503
#> [110/191] Computed palmsplus for: OC1507
#> [111/191] Computed palmsplus for: PH1800
#> [112/191] Computed palmsplus for: PH1801
#> [113/191] Computed palmsplus for: PH1803
#> [114/191] Computed palmsplus for: PH1805
#> [115/191] Computed palmsplus for: PH1806
#> [116/191] Computed palmsplus for: PH1807
#> [117/191] Computed palmsplus for: PH1808
#> [118/191] Computed palmsplus for: PH1809
#> [119/191] Computed palmsplus for: PH1810
#> [120/191] Computed palmsplus for: PH1813
#> [121/191] Computed palmsplus for: PH1814
#> [122/191] Computed palmsplus for: PH1817
#> [123/191] Computed palmsplus for: PH1818
#> [124/191] Computed palmsplus for: PH1820
#> [125/191] Computed palmsplus for: PH1823
#> [126/191] Computed palmsplus for: PH1824
#> [127/191] Computed palmsplus for: PH1825
#> [128/191] Computed palmsplus for: PH1826
#> [129/191] Computed palmsplus for: PH1827
#> [130/191] Computed palmsplus for: PH1828
#> [131/191] Computed palmsplus for: PH1829
#> [132/191] Computed palmsplus for: PH1830
#> [133/191] Computed palmsplus for: RT1607
#> [134/191] Computed palmsplus for: PH1832
#> [135/191] Computed palmsplus for: PH1835
#> [136/191] Computed palmsplus for: RT1608
#> [137/191] Computed palmsplus for: RT1611
#> [138/191] Computed palmsplus for: RT1617
#> [139/191] Computed palmsplus for: RT1620
#> [140/191] Computed palmsplus for: RT1622
#> [141/191] Computed palmsplus for: RT1625
#> [142/191] Computed palmsplus for: RT1632
#> [143/191] Computed palmsplus for: RT1634
#> [144/191] Computed palmsplus for: RT1636
#> [145/191] Computed palmsplus for: RT1637
#> [146/191] Computed palmsplus for: RT1645
#> [147/191] Computed palmsplus for: RT1647
#> [148/191] Computed palmsplus for: RT1649
#> [149/191] Computed palmsplus for: RT1657
#> [150/191] Computed palmsplus for: RT1658
#> [151/191] Computed palmsplus for: RT1663
#> [152/191] Computed palmsplus for: RT1665
#> [153/191] Computed palmsplus for: RT1666
#> [154/191] Computed palmsplus for: RT1668
#> [155/191] Computed palmsplus for: RT1669
#> [156/191] Computed palmsplus for: RT1671
#> [157/191] Computed palmsplus for: RT1672
#> [158/191] Computed palmsplus for: RT1673
#> [159/191] Computed palmsplus for: RT1674
#> [160/191] Computed palmsplus for: RT1677
#> [161/191] Computed palmsplus for: RT1678
#> [162/191] Computed palmsplus for: RT1680
#> [163/191] Computed palmsplus for: WH1700
#> [164/191] Computed palmsplus for: WH1701
#> [165/191] Computed palmsplus for: WH1702
#> [166/191] Computed palmsplus for: WH1703
#> [167/191] Computed palmsplus for: WH1713
#> [168/191] Computed palmsplus for: WH1714
#> [169/191] Computed palmsplus for: WH1715
#> [170/191] Computed palmsplus for: WH1718
#> [171/191] Computed palmsplus for: WH1720
#> [172/191] Computed palmsplus for: WH1719
#> [173/191] Computed palmsplus for: WH1721
#> [174/191] Computed palmsplus for: WH1722
#> [175/191] Computed palmsplus for: WH1731
#> [176/191] Computed palmsplus for: WH1732
#> [177/191] Computed palmsplus for: WH1734
#> [178/191] Computed palmsplus for: WH1733
#> [179/191] Computed palmsplus for: WH1744
#> [180/191] Computed palmsplus for: WH1745
#> [181/191] Computed palmsplus for: WH1746
#> [182/191] Computed palmsplus for: WH1747
#> [183/191] Computed palmsplus for: WH1757
#> [184/191] Computed palmsplus for: WH1758
#> [185/191] Computed palmsplus for: WH1759
#> [186/191] Computed palmsplus for: WH1762
#> [187/191] Computed palmsplus for: WH1768
#> [188/191] Computed palmsplus for: WH1769
#> [189/191] Computed palmsplus for: WH1770
#> [190/191] Computed palmsplus for: WH1771
#> [191/191] Computed palmsplus for: WH1784

# Build days
days <- palms_build_days(palmsplus)

# Build trajectories
trajectories <- palms_build_trajectories(palmsplus)

# Build multimodal trips
multimodal <- palms_build_multimodal(trajectories, 200, 10)
#> Calculating multimodal eligibility...done
#> Assigning trip numbers...done
#> Calculating fields...done

cat("Run time:", (proc.time() - t)[[1]]/60, "minutes")
#> Run time: 17.45183 minutes

Around 20 minutes for 191 participants isn’t bad!

Saving results

write_csv(palmsplus, "palmsplus.csv")
write_csv(days, "days.csv")
write_csv(trajectories, "trajectories.csv")
write_csv(trajectories, "multimodal.csv")

st_write(palmsplus, "palmsplus.shp")
st_write(trajectories, "trajecories.shp")
st_write(trajectories, "multimodal.shp")