AbstractThis vignette shows how to use and interpret the
time_windowparameter in r5r.
To calculate the travel time from A to B, or to calculate the
accessibility level at a given location, one has to select a departure
time. However, travel time and accessibility estimates can differ
significantly at different departure times because of how public
transport service levels vary across the day (Stepniak et al. 2019). Even a small difference,
say leaving at
importantly change travel time and accessibility estimates depending on
when a person departs relative to when a public transport vehicle
arrives, and how well transfers are coordinated given a service
timetable. This is a very common issue related to the modifiable
temporal unit problem (MTUP) (Pereira 2019;
Levinson and et al. 2020).
This problem gets even more complicated when public transport GTFS
feeds have a
frequencies.txt table. In these cases, we
cannot know the exact departure time of vehicles, what creates greater
uncertainty for our travel time and accessibility estimates (Conway, Byrd, and van Eggermond 2018; Stewart and Byrd
A common strategy to overcome this problem is to calculate travel times and accessibilities at multiple departure times sampled over a time window, and then take the average or median value. Now you may ask, but how many departure times should I use? You might also be thinking that doing multiple repeated routing analysis can be cumbersome and take a lot of time. Right?
Here is where
r5r comes in. Both the
functions have a parameter called
time_window. When this
parameter is set, R5 will automatically compute multiple
travel times / accessibility estimates considering multiple departures
per minute within the
time_window selected by the user.
This vignette shows a reproducible example to explain how one can use
time_window and interpret the results.
time_windowworks and how to interpret the results.
As mentioned above, when
time_window is set,
R5 computes multiple travel times / accessibility estimates
starting at the specified
departure_datetime and within the
time_window selected by the user. By default,
r5r will generate one estimate per minute. Nonetheless,
users can set a number to the
that will change the number of Monte Carlo draws to perform per time
window minute. The default value of
draws_per_minute is 5,
which mean 300 draws in a 60 minutes time window, for example. For a
detailed discussion on the effect of number of draws on result
stability, see Stewart et al (2022).
In this case, there isn’t a single estimate of travel time /
accessibility, but a distribution of several estimates that reflect the
travel time / accessibility uncertainties in the specified time window.
To get our heads around so many estimates, we can use the
percentiles parameter to specify the percentiles of the
distribution we are interested in. For example, if we select the 25th
travel time percentile and the results show that the travel time
estimate between A and B is 15 minutes, this means that 25% of all trips
taken between these points within the specified time window are shorter
than 15 minutes.
Let’s see a couple concrete examples now.
First, let’s build the network and create the routing inputs. In this
example we’ll be using the a sample data set for the city of São Paulo
(Brazil) included in
# increase Java memory options(java.parameters = "-Xmx2G") # load libraries library(r5r) library(sf) library(data.table) library(ggplot2) library(dplyr) # build a routable transport network with r5r <- system.file("extdata/spo", package = "r5r") data_path <- setup_r5(data_path) r5r_core # routing inputs <- c('walk', 'transit') mode <- 30 # minutes max_walk_time <- 90 # minutes max_trip_duration # load origin/destination points <- fread(file.path(data_path, "spo_hexgrid.csv")) points # departure datetime = as.POSIXct("13-05-2019 14:00:00", departure_datetime format = "%d-%m-%Y %H:%M:%S")
ps. Please keep in mind that the
affects the results when the GTFS feeds contain a
In this example we calculate the number of schools accessible from
each location within a 60-minute time window departing between 2pm and
3pm. In this example we’ll be using a cumulative accessibility metric
decay_function = "step" with a max time threshold of 45
cutoffs = 45.
# estimate accessibility <- r5r::accessibility(r5r_core = r5r_core, acc origins = points, destinations = points, opportunities_colnames = 'schools', mode = mode, max_walk_time = max_walk_time, decay_function = "step", cutoffs = 45, departure_datetime = departure_datetime, progress = FALSE, time_window = 30, percentiles = c(10, 20, 50, 70, 80) ) head(acc, n = 10)
This output is in long format, so the first 5 rows show the result for the same origin. In this case, we see that in only 10% of the trips departing from that origin between 2pm and 3pm a person would be able to access up to 111 schools. Meanwhile, 50% of the times she would only access 79 schools. By contrast, the accessibility from the other origin shown in the output above is 0, meaning there are no schools accessible from that location given the max travel time of 45 minutes.
We can use a plot like the one below to visualize this uncertainty in how accessibility levels might vary between 2pm and 3pm depending on the departure time within that 60-minute time window.
# summarize <- acc[, .(min_acc = min(accessibility), df median = accessibility[which(percentile == 50)], max_acc = max(accessibility)), by = id] # plot ggplot(data=df) + geom_linerange(color='gray', alpha=.5, aes(x = reorder(id, median) , y=median, ymin=min_acc, ymax=max_acc)) + geom_point(color='#0570b0', size=.5, aes(x = reorder(id, median), y=median)) + labs(y='N. of schools accessible\nby public transport', x='Origins sorted by accessibility', title="Accessibility uncertainty between 2pm and 3pm", subtitle = 'Upper limit 10% and lower limit 80% of the times') + theme_classic() + theme(axis.text.x=element_blank(), axis.ticks.x=element_blank())
Now let’s calculate all-to-all travel time estimates within a 60-minute time window departing between 2pm and 3pm and see how the output looks like.
# estimate travel time matrix <- travel_time_matrix(r5r_core = r5r_core, ttm origins = points, destinations = points, mode = mode, max_walk_time = max_walk_time, max_trip_duration = max_trip_duration, departure_datetime = departure_datetime, progress = TRUE, time_window = 20, percentiles = c(10, 20, 50, 70, 80) ) head(ttm, n = 10)
Now let’s look at the 2nd row of the output above. This output tell us that only 10% of the trips between 2pm and 3pm for that origin-destination pair took 39 minutes or less. Meanwhile, 50% of those trips took up tp 45 minutes and 80% of them were 48-minute long or shorter.
The last row in the result above has a few
tell us that at least 50% of all simulated trips between 2pm and 3pm for
that origin-destination pair could not be completed because they took
longer than the
max_trip_duration we have set (90
Finally, we can also use the
time_window in the
expanded_travel_time_matrix() function. In this case,
though, when the user sets a
time_window value, the
expanded_travel_time_matrix() will return the fastest route
alternative departing each minute within the specified time window.
Please note this function can be very memory intensive for large data
sets and time windows.
<- r5r::expanded_travel_time_matrix(r5r_core = r5r_core, ettm origins = points[1:30,], destinations = points[31:61,], mode = mode, max_walk_time = max_walk_time, max_trip_duration = max_trip_duration, departure_datetime = departure_datetime, progress = FALSE, time_window = 20) head(ettm, n = 10)
r5r objects are still allocated to any amount of memory
previously set after they are done with their calculations. In order to
remove an existing
r5r object and reallocate the memory it
had been using, we use the
stop_r5 function followed by a
call to Java’s garbage collector, as follows:
::stop_r5(r5r_core) r5r::.jgc(R.gc = TRUE)rJava
If you have any suggestions or want to report an error, please visit the package GitHub page.