knitr::opts_chunk$set(warning=FALSE,message=FALSE,error=FALSE, quiet=TRUE)
library(tidytransit)
library(dplyr)
library(tidycensus)
library(tidyr)
library(sf)
library(units)
library(gtfsrouter)
library(viridis)
library(purrr)
library(tidyverse)
library(sp)
library(leaflet)
library(units)
library(nngeo)
SEPTA is the Southeastern Pennsylvania Transportation Authority that provides bus, trolley, light rail, and commuter rail for Philadelphia, PA and surrounding areas, including parts of the New Jersey and Delaware regions.
For this analysis, I want to compare headways for a particular route/stop pair with the walking times to a station (with census tracts as the unit of analysis). This analysis focuses on stop frequencies between 6:00 AM and 10:00 AM (morning peak). First I can get the average headway for all SEPTA bus stops, removing stop/route pairs that have headways greater than 60 minutes.
#process SEPTA bus data
septa_bus <- read_gtfs("/Users/shriyakaram/Desktop/gtfs_public/google_bus.zip")
am_stop_freq <- get_stop_frequency(septa_bus, start_time = "06:00:00", end_time = "10:00:00", by_route = TRUE)
septa_stops <- septa_bus[["stops"]]
am_stop_freq_with_stops <- merge(am_stop_freq, septa_stops, by = 'stop_id') %>% filter(direction_id == 1)
#map lat/lon to points
makePoints <- function(stop_lon, stop_lat) {
return(st_point(c(stop_lon, stop_lat)))
}
geom_bus <- am_stop_freq_with_stops %>%
select(stop_lon, stop_lat) %>%
pmap(makePoints) %>%
st_as_sfc(crs = 4326) %>%
{cbind(am_stop_freq_with_stops, geom = .)} %>%
st_sf()
#calculate average headways and filter for those <= 60 min.
geom_bus <- geom_bus %>%
arrange(mean_headway) %>%
mutate(mean_headway=round(mean_headway/60,2)) %>%
filter(mean_headway <= 60)
#remove rows with duplicated stop/routes
dup_rows <- duplicated(geom_bus$stop_id) & duplicated(geom_bus$route_id)
geom_bus <- geom_bus[!dup_rows, ]
The colors differentiate between the routes, and the size of the bubble is the average headwa for a stop.
pal <- colorFactor(
palette = c("darkcyan", "orchid", "orange", "green", "midnightblue", "darkslategrey"),
domain = geom_bus$route_id
)
# make interactive map
leaflet(geom_bus) %>%
addTiles() %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addCircles(lng = ~stop_lon, lat = ~stop_lat, radius = ~mean_headway, weight=3,
color = ~pal(route_id), popup = ~paste("Route", route_id, "mean headway (min):", mean_headway))
I look at census tracts in Philadelphia county as the unit of analysis, and for each census tract centroid, I want to be able to find the walking time from the centroid to the nearest transit stop.
#process census data from Philadelphia county
phl_tracts <- get_acs(geography = "tract", variables = "B19013_001",
state = "PA", county = "Philadelphia", geometry = TRUE, year = 2020)
phl_tracts <- phl_tracts %>% st_transform(st_crs(geom_bus))
centroids <- st_centroid(phl_tracts$geometry)
phl_tracts$centroids <- centroids
phl_tracts <- phl_tracts %>% st_drop_geometry()
phl_tracts <- st_set_geometry(phl_tracts, phl_tracts$centroids)
#unique bus stops
unique_df <- geom_bus %>%
distinct(stop_id, .keep_all = TRUE) %>% arrange(stop_id)
unique_df$stop_num <- 1:nrow(unique_df)
#find the stops that are closest to each census tract centroid
dfjoin <- st_nn(phl_tracts, unique_df, k = 1, returnDist = T)
stops_closest_tracts <- unlist(dfjoin[[1]])
distances <- unlist(dfjoin[[2]])
phl_tracts$stop_num <- stops_closest_tracts
phl_tracts$distances <- distances
#compute walking times (minutes) from distances (assuming a constant walking speed) and ratios
stations_nosf <- unique_df %>% st_drop_geometry()
joined <- left_join(phl_tracts, stations_nosf, by='stop_num') %>% filter(distances > 0)
joined$walking_time_closest_station <- round((joined$distances/1.3)/60, 2)
joined$headway_walking_ratio <- round(joined$mean_headway / joined$walking_time_closest_station, 2)
joined <- joined %>% select(c(route_id, walking_time_closest_station, stop_id, stop_name, headway_walking_ratio, centroids))
#get latitude and longitude
sf_data <- st_as_sf(joined, wkt = "centroids")
joined$latitude <- st_coordinates(sf_data)[, 2]
joined$longitude <- st_coordinates(sf_data)[, 1]
joined <- joined %>% filter(headway_walking_ratio < 100)
This shows the walking times to the nearest transit stop for each census tract centroid. Intuitively, this plot shows that areas in the outskirts of the county (particularly in the south where the airport is and the western suburbs) with fewer transit stops have higher walking times to their nearest stops.
leaflet(joined) %>%
addTiles() %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addCircles(lng = ~longitude, lat = ~latitude,
weight = ~walking_time_closest_station,
popup = ~paste("Walking time to ", stop_name, " station is ", walking_time_closest_station, "(min.)"))
This plot shows the ratio of the headway associated with the nearest stop and the walking times to that stop. Yes, some stations have multiple routes associated with it but it only affects about ~20 stations, so I just removed a bit of data to ensure that each stop corresponded to one headway.
It seems as though areas with a low ratio seem to match the areas with a high walking time. This rough analysis also goes to show that even for the suburban areas with higher walking times than areas in the inner city (with presumably a higher density of transit tops), the ratio of headways to walking times seem to scale accordingly. That is; for census tracts with a higher walking time, we’d expect longer headways, and for tracts with a lower walking time, we’d expect shorter headways.
leaflet(joined) %>%
addTiles() %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addCircles(lng = ~longitude, lat = ~latitude,
weight = ~headway_walking_ratio,
popup = ~paste("Ratio of headway to walking time to ", stop_name, " station is ", headway_walking_ratio))