Code and data for project calculating the distance and drive times between maternal delivery hospitals and maternal residence ZIP codes.
centroids_distances.r
: Computes and analyzes the distances between weighted and unweighted ZCTA centroids.distance_matrix.r
: Computes the long-form (Haversine) distance matrix between ZCTA centroids and hospital addresses. The output dataset is fed to the script for querying the googlemaps API.distance_matrix_hospital.r
: Computes the long-form Haversine distance matrix between hospital addresses within the same state.function.r
: Contains all of the R functions used within the other R scripts.googlemaps_request.py
: Makes the API calls for driving/transit distance and time between ZCTA centroids and hospital addresses.merge_zip_codes.r
: Merges ZIP code information to the distance matrices with google maps data.plot_map.r
: Maps ZCTAs and their centroid.pop_weight_centroid.r
: Computes the population-weighted centroids for 2020 ZCTAs.state_query_count.r
: Counts the number of queries would be needed at varying levels of geographic complexity.zcta_block_group_cw.r
: Checks if census block groups map uniquely to a ZCTA.zcta_cbsa_cw.r
: Creates a crosswalk between ZCTAs and core-based statistical areas (metropolitan and micropolitan statistical areas).zip_code_prefix_scrape.py
: Scrapes the Wikipedia page with a table that crosswalks three-digit ZIP code prefixes to states.
To generate the distance matrix output from scratch, run the scripts in the following order:
pop_weight_centroid.r
distance_matrix.r
googlemaps_request.py
merge_zip_codes.r
To generate the distance matrices between hospitals within the same state, run the script distance_matrix_hospital.r
.
The following raw data files are not included in this repository.
- AHA birth hospital addresses
- National ZCTA - census block crosswalk from the Census Bureau.
- State-specific shapefiles for census block from the Census Bureau.
- Core-Based Statistical Area shapefile from the Census Bureau.
- ZIP Code - ZCTA crosswalk from UDS Mapper.
There are two versions of the distance matrix file for each state - one without the Google maps columns, and one with the Google maps columns. The same hospital - ZCTA pair may be represented multiple times in a file since multiple ZIP codes mapped to the same ZCTA in the crosswalk. If the value for any of the Google maps columns is -1, this means that Google maps could not find a valid route between the two requested locations.
state
: (chr) 2-letter state abbreviationfips_state
: (chr) 2-digit state FIPS codeyear
: (int) Year of the hospital addresshospital_id
: (chr) AHA ID of the hospitalhospital_latitude
: (num) Hospital latitudehospital_longitude
: (num) Hospital longitudezcta_geoid
: (chr) 5-digit ZCTA GEOIDzip_code
: (chr) 5-digit ZIP code matched to the ZCTAzcta_latitude
: (num) ZCTA centroid latitudezcta_longitude
: (num) ZCTA centroid longitudehaversine_dist_m
: (num) Crow-flies (Haversine) distance between the ZCTA centroid and the hospital address, in meters
distance_driving_m
: (num) Google maps driving distance in metersduration_driving_sec
: (num) Google maps driving time in secondsdistance_transit_m
: (num) Google maps transit distance in metersduration_transit_sec
: (num) Google maps transit time in seconds
The distance between two hospitals is symmetrical, and the labeling of source and destination hospitals is out of completeness and convenience for merging/filtering.
src_hospital_id
: (chr) AHA ID of the source hospitaldest_hospital_id
: (chr) AHA ID of the destination hospitalhaversine_dist_m
: (num) Crow-flies (Haversine) distance between the two hospital addresses, in meters