| Title: | Highly Simplified Four-Step Travel Demand Models for Teaching |
|---|---|
| Description: | This package provides a highly simplified four-step model. Running the model requires only R, and is designed to be computationally efficient and run even on consumer-grade equipment. |
| Authors: | Matt Bhagat-Conway [aut, cre] (ORCID: <https://orcid.org/0000-0002-1210-2982>) |
| Maintainer: | Matt Bhagat-Conway <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.0.0 |
| Built: | 2026-05-30 09:19:48 UTC |
| Source: | https://github.com/mattwigway/MyFirstFourStepModel |
Add households to particular Census tracts in the region
add_households(scenario, tract, hhs)add_households(scenario, tract, hhs)
scenario |
An existing land-use scenario |
tract |
a Census tract ID (or vector of tract IDs)-should be strings, not numbers |
hhs |
Households to add to the tract |
a copy of the scenario with the requested households added
The hhs parameter should be a table/tibble/data.frame of households with the following columns:
income: Annual household income, dollars
hhsize: Household size
vehicles: Number of vehicles owned by the household
workers: Number of workers in the household
n: Number of households like this to add
If there are multiple tracts specified, the households will be evenly split among them
This calibrates the trip distance beta, using the method described in Merlin (2020) A new method using medians to calibrate single-parameter spatial interaction models, JTLU. The basic idea is that we adjust the beta for the decay function until half the distance-weighted accessibility occurs in the median travel distance. For NHB, productions and attractions will be the same
calibrate_trip_distance_beta(productions, attractions, median_dist_km, dmat)calibrate_trip_distance_beta(productions, attractions, median_dist_km, dmat)
Calibrate betas for all trip types median_distances should be a list with element HBW, HBO, and NHB with median crow-flies trip distances in each.
calibrate_trip_distance_betas(balanced, marginals, median_distances_m)calibrate_trip_distance_betas(balanced, marginals, median_distances_m)
Load the NHTS data with load_nhts(), and if desired filter the households table to just the households you want to use in estimation. Julia must already be installed. If you get an error that Julia is not found, you need to set the JULIA_HOME environment variable to the directory containing the Julia executable.
estimate( nhts, osm, state, county, year, highway_types = c("motorway", "motorway_link", "trunk", "trunk_link", "primary", "primary_link") )estimate( nhts, osm, state, county, year, highway_types = c("motorway", "motorway_link", "trunk", "trunk_link", "primary", "primary_link") )
nhts |
Path to 2017 NHTS CSV files |
osm |
Path to OSM .pbf file |
state |
State to estimate model for |
county |
County (or vector of counties) to estimate model for |
year |
Year of ACS and LODES data to use (if you get 404 errors, you are probably trying to use a year that LODES is not available for) |
highway_types |
OSM highway= tags to include in network, default "motorway", "motorway_link", "trunk", "trunk_link", "primary", "primary_link" |
Note that this is total VMT for the period the flows are for; you would have to do this for each period to get total daily VMT.
estimate_vmt(model, network, link_flows, period)estimate_vmt(model, network, link_flows, period)
model |
The estimated model object |
network |
The network to use (should be the same one used in |
link_flows |
Estimated link flows from the network assignment function |
period |
Time period (AM Peak, Midday, PM Peak, or Overnight) |
Load a land use scenario in Excel format
load_landuse_scenario(filename)load_landuse_scenario(filename)
filename |
file to read from |
Load a model
load_model(filename)load_model(filename)
filename |
File name or URL to load model from |
Load a model in model format 0 (used with pre-2026 releases)
load_model_v0(filename)load_model_v0(filename)
filename |
Filename or URL for model |
Load 2017 NHTS data, handling types appropriately
load_nhts(base_path)load_nhts(base_path)
base_path |
path to folder containing 2017 NHTS CSV files |
Produce a congestion map
map_congestion(model, network, flows)map_congestion(model, network, flows)
model |
the model in use |
network |
the network scenario in use (e.g. model$networks$baseline) |
flows |
output of |
Produces a map showing where trips produced in origin_tract are attracted.
map_trip_distribution(model, flows, timeperiod, triptype, origin_tract)map_trip_distribution(model, flows, timeperiod, triptype, origin_tract)
model |
the model to use |
flows |
|
timeperiod |
Time period to map (AM Peak, Midday, PM Peak, Overnight) |
triptype |
Trip type to map (HBO, HBW, NHB) |
origin_tract |
Tract to map productions of |
Produces a map showing where trips are produced or attracted.
map_trip_generation(model, trip_counts, end, timeperiod, triptype)map_trip_generation(model, trip_counts, end, timeperiod, triptype)
model |
the model to use |
trip_counts |
|
end |
"productions" or "attractions" |
timeperiod |
Time period to map (AM Peak, Midday, PM Peak, Overnight) |
triptype |
Trip type to map (HBO, HBW, NHB) |
This runs the mode choice step of the model, and returns flows differentiated by mode for each trip type and time of day.
mode_choice(model, scenario, flows)mode_choice(model, scenario, flows)
model |
The estimated model object |
scenario |
The scenario to use |
flows |
The output of trip_distribution |
Modify network attributes of existing OSM ways. Note that this currently only updates them for modeling; visualizations and GIS exports are not changed.
modify_ways( network, ways, lanes_per_direction = NULL, highway_type = NULL, maxspeed_kph = NULL )modify_ways( network, ways, lanes_per_direction = NULL, highway_type = NULL, maxspeed_kph = NULL )
network |
Network to modify |
ways |
OSM way ID or character vector of way IDs. Since OSM IDs are 64 bit integers and R does not support 64 bit integers, these should be strings |
lanes_per_direction |
Number of lanes per direction to set on the way (optional) |
highway_type |
Highway type of the way. Currently motorway has higher capacity and all other values are treated as the same. |
maxspeed_kph |
Maximum speed on the way, in kilometers per hour |
Network assignment is based on a Frank-Wolfe static traffic assignment algorithm. This returns a list of link-level flows. It applies peaking factors and average occupancy before doing assignment to convert period person-trips to hourly vehicle-trips.
network_assignment(model, scenario, network, mode_flows, period)network_assignment(model, scenario, network, mode_flows, period)
model |
The estimated model |
scenario |
The land-use scenario to use |
network |
The network scenario to use |
mode_flows |
Flows by mode and time of day, output of mode_choice function |
period |
Time period to assign, can be "AM Peak", "Midday", "PM Peak", or "Overnight". |
Convenience function to export a network scenario to a GIS file
network_to_gis(network, file)network_to_gis(network, file)
network |
the network to export, e.g. model$networks$baseline |
file |
where to save the file (file type determined by extension). |
Save a land use scenario in Excel format
save_landuse_scenario(scenario, filename)save_landuse_scenario(scenario, filename)
scenario |
scenario to save (often |
filename |
file to save in |
Save a model
save_model(model, filename)save_model(model, filename)
model |
model to save |
filename |
file to save to (canonical extension .mf4sm) |
Trip distribution is based on the relative locations of the tracts (in marginals),
the betas estimated during the estimation phase of the model, and the total trip productions
and attractions.
trip_distribution(model, scenario, balanced)trip_distribution(model, scenario, balanced)
model |
the model object, loaded via |
scenario |
the scenario to use, often |
balanced |
the balanced productions and attractions, returned by |
Trip production is based on the marginal distributions for vehicles, workers, household size, and income. Trip attraction is based on job counts. Attractions are balanced to match productions before being returned.
trip_generation(model, scenario)trip_generation(model, scenario)
model |
the model object, loaded via |
scenario |
the scenario to use, often |
Write the minimal information to be able to reconstruct enough of an lm to be able to do prediction.
write_lm(model, archive, name)write_lm(model, archive, name)
Write just enough of a multinomial logit model that we can deserialize and apply it
write_mnl(model, archive, name)write_mnl(model, archive, name)