leaflet() %>%Ĭalculate isochrone and Census block group overlap Map the isochrones to make sure they make sense. Select(hospital_id, drive_time, everything()) %>%Īrrange(state, city, hospital_id, drive_time) Select(-range, -departure, -arrival, -id, -rank) %>% # Using a timer to avoid rate limit errors (Status 429) # Loop over points to make isochrones file # Choose a return value in case of warning Message(paste("Hospital ID caused a warning:", point_id)) Message(paste("Hospital ID failed: ", point_id)) If you’re using a larger dataset I highly recommend having a setup like this so that you’re not making a bunch of API hits only to hit an error and abort your whole script. Then, if any points did fail, I loop through the list that failed to try them again. If you’re new to error handling, Hadley Wickham’s primer is helpful. I like to wrap the API function in a tr圜atch() function so that if any one point fails the rest still go forward. Once in a while you might have a server issue or some other error. We’ll use hereR’s isoline() function to get the isochrones. This small project fits well within the freemium tier, as of May 2021. This package wraps the HERE Technologies API. Now that we have our set of stroke centers, we’ll get the 45-minute drive time radius for each one using the hereR package. Retrieve isochrones with the hereR package # Project back first - can't put projected coordinates on a leaflet map # 12 2 4 17 13 10 # Now let's plot those on a leaflet map to see where they are # Do the states make sense for a buffer around MS? centers_ms % filter(st_intersects(geometry, ms_buffer, sparse = FALSE)) Then filter the stroke centers to include just those within our 100 mile state buffer. Geom_sf(data = ms_buffer, fill = NA, color = "red") + Geom_sf(data = ms_boundary, color = "blue") + Google tells me that 100 miles = 160,934 meters. Now make a buffer using the st_buffer() function. Important note: for computations in sf we should use a planar projection, not a lat/long projection like we’d use for making Leaflet maps. We’ll get high-quality state boundaries from the US Census Bureau using the tigris package, select just Mississippi, and then expand the boundary by 100 miles in every direction. (100 miles is a lot for 45 minutes even if driving in a perfectly straight line, but let’s be safe.) To choose those relevant locations, we’ll create an 100 mile buffer from the state border. We’ll want to filter the dataset down to just stroke centers in and near Mississippi - anywhere you could possibly reach in a 45-minute drive from any point in the state. We don’t need all of these locations for out Mississippi anaysis. Stroke_centers$city, ", ", stroke_centers$state))įilter stroke centers with st_buffer and st_intersects For this tutorial we’ll just focus on those in and near Mississippi, but map the full dataset to see what it includes. The stroke centers dataset includes locations in the Mississippi Delta and Appalachia and surrounding states. You can read how we collected the locations in that repo. We’ll get stroke center locations from the published KHN analysis. If you try to replicate this and run the isochrone code here you will likely get slightly different results. Road closures and other changes will affect the drive time results. IMPORTANT NOTE: the isochrones used in this analysis were calculated several weeks ago for the story published in early May. Retrieve a drive-time isochrone for each pointĬalculate how much each Census block group in our area of interest overlaps with the isochronesĬalculate how many people live within the isochrones and how many people live outside, using our block group data It looks at how many Mississippi state residents live within 45-minutes of a stroke center and how many live further away. This is a simplified analysis based on that larger project. The story was a collaboration between KHN and InvestigateTV and published in May, 2021. The tutorial is adapted from code used in the analysis for In Appalachia and the Mississippi Delta, Millions Face Long Drives to Stroke Care. Drive time analyses can answer what areas lie within a specified driving time from a certain point, and what are the demographics of the people inside and outside that area. This tutorial demonstrates how to do a drive time analysis in R. Drive Time Analysis in R Hannah Recht May 17, 2021
0 Comments
Leave a Reply. |