Skip to content

Commit

Permalink
various fixes to appease CRAN
Browse files Browse the repository at this point in the history
  • Loading branch information
meggehsc committed May 25, 2024
1 parent 367ab70 commit 4b194e8
Show file tree
Hide file tree
Showing 21 changed files with 148 additions and 51 deletions.
41 changes: 41 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,43 @@
^pkgdown$
^.*\.Rproj$
^\.Rproj\.user$
^\.travis\.yml$
^.Rprofile$
^inst/db$
^inst/bench$
^man-roxygen$
^demo/pandas$
^\.httr-oauth$
^cran-comments\.md$
^README\.Rmd$
^revdep$
^README-.*\.png$
^codecov\.yml$
^appveyor\.yml$
^Doxyfile$
^clion-test\.R$
^API$
^ISSUE_TEMPLATE\.md$
^data-raw$
^LICENSE\.md$
^BROWSE$
^GPATH$
^GRTAGS$
^GTAGS$
^TAGS$
^\.dir-locals\.el$
^vignettes/rsconnect$
^docs$
^_pkgdown\.yml$
^issues$
^CONDUCT\.md$
^archive$
^\.drake$
^CRAN-RELEASE$
^\.github$
^\.github/workflows/R-CMD-check\.yaml$
^\.github/workflows/pr-commands\.yaml$
^\.github/workflows/pkgdown\.yaml$
^doc$
^Meta$
^CRAN-SUBMISSION$
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.Rhistory
.RData
.Ruserdata
tpm.Rcheck/
test_data/*
10 changes: 5 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
Package: tpm
Title: FHWA TPM Score Calculation Functions
Version: 1.2.0
Version: 2.0.1
Authors@R:
person(given = "Mark",
family = "Egge",
role = c("aut", "cre"),
email = "egge@highstreetconsulting.com",
comment = c(ORCID = "YOUR-ORCID-ID"))
Description: Contains functions for calculating TPM PM3 Level of Travel Time Reliability and Truck Travel Time Reliability metric scores from NPMRDS travel time data and for calculating statewide reliability performance measures.
email = "mark@eateggs.com",
comment = c(ORCID = "0009-0007-9128-2099"))
Description: Contains functions for calculating the Federal Highway Administration (FHWA) Transportation Performance Management (TPM) performance measures. Currently, the package provides methods for the System Reliablity and Freight (PM3) performance measures calculated from travel time data provided by The National Performance Management Research Data Set (NPMRDS), including Level of Travel Time Reliability (LOTTR), Truck Travel Time Reliability (TTTR), and Peak Hour Excessive Delay (PHED) metric scores for calculating statewide reliability performance measures.
License: Mozilla Public License Version 2.0
Encoding: UTF-8
URL: https://github.com/markegge/fhwa_pm3/
BugReports: https://github.com/markegge/fhwa_pm3/issues
LazyData: false
LazyData: true
Depends:
R (>= 3.5.0)
Imports:
Expand Down
13 changes: 13 additions & 0 deletions R/data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#' FIPS Codes
#'
#' This dataset provides a crosswalk between state names, postal codes, and
#' FIPS codes. Used by the hpms() function. The variables are as follows:
#'
#' @format A data frame with 56rows and 4 variables:
#' \describe{
#' \item{State_Name}{state name (e.g. 'Alabama')}
#' \item{Postal_Code}{two character state postal code (e.g. 'AL')}
#' \item{FIPS_Code}{Census Bureau FIPS Code (e.g. 1)}
#' \item{STATE_NAME}{capitalized state name (e.g. 'ALAMABA')}
#' }
"fips_lookup"
35 changes: 34 additions & 1 deletion R/functions.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
utils::globalVariables(".")
utils::globalVariables(c("errorMessage"))
#' Calculate LOTTR Metric Score
#'
#' Calculate LOTTR given a RITIS NPMRDS export of travel time data.
Expand All @@ -19,6 +21,10 @@
#'
#' @export
lottr <- function(travel_time_readings = NULL, monthly = FALSE, verbose = FALSE) {
# bind variables to an object to suppress R CMD check warnings
max_lottr <- score_weekday_am <- score_weekday_mid <- score_weekday_pm <- score_weekend <- NULL
weekday_am <- weekday_mid <- weekday_pm <- weekend <- reliable <- NULL

scores <- score(travel_time_readings, metric = "LOTTR", monthly, verbose)

if(verbose == TRUE) {
Expand Down Expand Up @@ -53,6 +59,10 @@ lottr <- function(travel_time_readings = NULL, monthly = FALSE, verbose = FALSE)
#'
#' @export
tttr <- function(travel_time_readings = NULL, monthly = FALSE, verbose = FALSE) {
# bind variables to an object to suppress R CMD check warnings
max_tttr <- score_weekday_am <- score_weekday_mid <- score_weekday_pm <- score_overnight <- score_weekend <- NULL
weekday_am <- weekday_mid <- weekday_pm <- weekend <- overnight <- NULL

scores <- score(travel_time_readings, metric = "TTTR", monthly, verbose)

if(verbose == TRUE) {
Expand Down Expand Up @@ -92,6 +102,10 @@ tttr <- function(travel_time_readings = NULL, monthly = FALSE, verbose = FALSE)
#' }

score <- function(input_file = NULL, metric, monthly = FALSE, verbose = FALSE) {
# bind variables to an object to suppress R CMD check warnings
measurement_tstamp <- period <- dow <- nhpp_period <- hod <- travel_time_seconds <- NULL
numerator <- denominator <- NULL

if (!is.null(input_file)) {
DT <- fread(input_file)
} else {
Expand Down Expand Up @@ -269,6 +283,11 @@ phed <- function(travel_time_readings, tmc_identification,
dow_factor = dow_factor_default,
hod_profile = hod_profile_default,
population = NA) {
# bind variables to an object to suppress R CMD check warnings
day <- faciltype <- nhs <- road_class <- f_system <- aadt_cars <- aadt <- NULL
aadt_singl <- aadt_combi <- aadp <- nhs_pct <- hod_factor <- speed_limit <- NULL
threshold_speed <- threshold_travel_time <- miles <- measurement_tstamp <- NULL
tmc <- delay_seconds <- travel_time_seconds <- delay_person_hours <- delay <- NULL

if(as.integer(pm_peak) == 3) {
hours <- c(6, 7, 8, 9, 15, 16, 17, 18) # 3 - 7 pm
Expand Down Expand Up @@ -380,6 +399,11 @@ phed <- function(travel_time_readings, tmc_identification,
}

setnames(travel_time, "tmc_code", "tmc")

missing_tmcs <- tmcs$tmc[!tmcs$tmc %in% travel_time$tmc]
if(length(missing_tmcs) > 0) {
warning(paste("Warning: travel time data missing for ", missing_tmcs, collapse = ","))
}

# join in threshold travel times with TMCs
travel_time <- merge(travel_time, tmcs[, .(tmc, road_class, aadp, threshold_travel_time)], by = "tmc")
Expand Down Expand Up @@ -437,6 +461,15 @@ phed <- function(travel_time_readings, tmc_identification,
#'
#' @export
hpms <- function(tmc_identification, lottr_scores, tttr_scores, phed_scores = NULL, occ_fac = 1.7) {
# bind variables to an object to suppress R CMD check warnings
Postal_Code <- isprimary <- nhs <- tmc <- f_system <- urban_code <- NULL
faciltype <- miles <- nhs_pct <- direction <- aadt <- delay <- PHED <- NULL
tmc_code <- score_weekday_am <- denominator_weekday_am <- numerator_weekday_am <- NULL
score_weekday_mid <- denominator_weekday_mid <- numerator_weekday_mid <- NULL
score_weekday_pm <- denominator_weekday_pm <- numerator_weekday_pm <- NULL
score_weekend <- denominator_weekend <- numerator_weekend <- NULL
score_overnight <- denominator_overnight <- numerator_overnight <- NULL

DT <- fread(tmc_identification)

yr <- first(year(DT$active_start_date))
Expand All @@ -448,7 +481,7 @@ hpms <- function(tmc_identification, lottr_scores, tttr_scores, phed_scores = NU

state <- unique(toupper(DT$state))
stopifnot(length(state) == 1)
state_fips <- tpm:::fips_lookup[Postal_Code == state]$FIPS_Code
state_fips <- tpm::fips_lookup[Postal_Code == state]$FIPS_Code

# Set NHS Value appropriately - no zeros allowed by FHWA!
DT[isprimary == "0", nhs := -1]
Expand Down
Binary file removed R/sysdata.rda
Binary file not shown.
1 change: 0 additions & 1 deletion R/tpm.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#' \code{\link{hpms}} generates an HPMS submission file in pipe delimited format
#'
#'
#' @docType package
#' @name tpm
#'
#' @import data.table
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ To calculate LOTTR or TTTR Metric scores:
3. Choose the appropriate "TMC segments from" value (e.g. "NPMRDS INRIX 2019")
4. Choose your region (e.g. Wyoming) click Add
5. Specify appropriate date range, e.g 01/01/2019 – 12/31/2019
![Massive Data Downloader Region and Dates](man/mdd_1.png)
![Massive Data Downloader Region and Dates](man/figures/mdd_1.png)
6. Select data sources and measures:
* "NPMRDS form INRIX (Trucks and Passenger Vehicles): Travel Time" for LOTTR Measure (the other fields are optional)
* "NPMRDS from Inrix (Trucks): Travel Time" for TTTR Measure
9. Set averaging to 15 minutes (per PM3 Final Rule) and Submit
![Massive Data Downloader Data Sources and Units](man/mdd_2.png)
![Massive Data Downloader Data Sources and Units](man/figures/mdd_2.png)
10. Download and extract the resulting dataset
11. Calculate scores using `lottr` and `tttr`. Monthly scores may be calculated using `monthly = TRUE`

Expand Down
Binary file added data/fips_lookup.rda
Binary file not shown.
File renamed without changes
File renamed without changes
File renamed without changes
23 changes: 23 additions & 0 deletions man/fips_lookup.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 0 additions & 14 deletions man/tpm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions tests/testthat/test-hpms.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
test_that("hpms runs correctly", {
expect_equal({
lottr_scores <- lottr("Readings.csv", verbose = TRUE)
tttr_scores <- tttr("Readings.csv", verbose = TRUE)
phed_scores <- phed(travel_time_readings = "Readings.csv",
tmc_identification = "TMC_Identification.csv",
speed_limits = fread("speed_limits.csv"),
lottr_scores <- lottr(test_path("testdata", "Readings.csv"), verbose = TRUE)
tttr_scores <- tttr(test_path("testdata", "Readings.csv"), verbose = TRUE)
phed_scores <- phed(travel_time_readings = test_path("testdata", "Readings.csv"),
tmc_identification = test_path("testdata", "TMC_Identification.csv"),
speed_limits = fread(test_path("testdata", "speed_limits.csv")),
urban_code = 56139,
population = 50000)
hpms("TMC_Identification.csv", lottr_scores, tttr_scores, phed_scores)
hpms(test_path("testdata", "TMC_Identification.csv"), lottr_scores, tttr_scores, phed_scores)
},
TRUE
)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-lottr.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
test_that("null file_path throws error", {
expect_warning(score(), )

scores_monthly <- lottr("Readings.csv", monthly = TRUE)
scores_monthly <- lottr(test_path("testdata", "Readings.csv"), monthly = TRUE)

})
33 changes: 17 additions & 16 deletions tests/testthat/test-phed.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ test_that("phed runs correctly", {
9,0.99,0.99
12,0.97,0.97")

expect_warning(phed(travel_time_readings = "Readings.csv",
tmc_identification = "TMC_Identification.csv",
speed_limits = fread("speed_limits.csv"),
expect_warning(phed(travel_time_readings = test_path("testdata", "Readings.csv"),
tmc_identification = test_path("testdata", "TMC_Identification.csv"),
speed_limits = fread(test_path("testdata", "speed_limits.csv")),
urban_code = 56139,
population = 52898,
moy_factor = moy_factor_test))
Expand All @@ -30,9 +30,9 @@ test_that("phed runs correctly", {
4,1.05,1.05
5,1.1,1.1")

expect_warning(phed(travel_time_readings = "Readings.csv",
tmc_identification = "TMC_Identification.csv",
speed_limits = fread("speed_limits.csv"),
expect_warning(phed(travel_time_readings = test_path("testdata", "Readings.csv"),
tmc_identification = test_path("testdata", "TMC_Identification.csv"),
speed_limits = fread(test_path("testdata", "speed_limits.csv")),
urban_code = 56139,
population = 52898,
dow_factor = dow_factor_test))
Expand All @@ -46,9 +46,9 @@ test_that("phed runs correctly", {
5,0.05,0.05
6,0.1,0.1")

expect_warning(phed(travel_time_readings = "Readings.csv",
tmc_identification = "TMC_Identification.csv",
speed_limits = fread("speed_limits.csv"),
expect_warning(phed(travel_time_readings = test_path("testdata", "Readings.csv"),
tmc_identification = test_path("testdata", "TMC_Identification.csv"),
speed_limits = fread(test_path("testdata", "speed_limits.csv")),
urban_code = 56139,
population = 52898,
dow_factor = dow_factor_test))
Expand All @@ -66,20 +66,21 @@ test_that("phed runs correctly", {
18,0.0555,0.0575
19,0.042,0.047")

expect_warning(phed(travel_time_readings = "Readings.csv",
tmc_identification = "TMC_Identification.csv",
speed_limits = fread("speed_limits.csv"),
expect_warning(phed(travel_time_readings = test_path("testdata", "Readings.csv"),
tmc_identification = test_path("testdata", "TMC_Identification.csv"),
speed_limits = fread(test_path("testdata", "speed_limits.csv")),
urban_code = 56139,
population = 52898,
hod_profile = hod_profile_test))

expect_equal({
phed_scores <- phed(urban_code = 56139,
population = 52898,
travel_time_readings = "Readings.csv",
tmc_identification = "TMC_Identification.csv",
speed_limits = fread("speed_limits.csv"))
round(phed_scores[, sum(delay) / 52898], 2)
travel_time_readings = test_path("testdata", "Readings.csv"),
tmc_identification = test_path("testdata", "TMC_Identification.csv"),
speed_limits = fread(test_path("testdata", "speed_limits.csv"))
)
round(phed_scores[, sum(delay, na.rm = TRUE) / 52898], 2)
},
0.18,
tolerance = 1
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-tttr.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ test_that("null file_path throws error", {

test_that("tttr runs correctly", {
expect_equal({
tttr_scores <- tttr("Readings.csv")
tttr_scores <- tttr(test_path("testdata", "Readings.csv"))
mean(tttr_scores$max_tttr)
},
1.638
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ tmc,road,direction,intersection,state,county,zip,start_latitude,start_longitude,
000+10003,US-3,WESTBOUND,,WY,LARAMIE,82009,,,,,0.54,,America/Denver,,USA,,2,N,3,56139,2,,4,3,3,1,3,28730,495,1920,1,100,0,0,1,1,2020-01-01T05:00:00Z,2021-01-01T05:00:00Z
000P10004,US-4,EASTBOUND,,WY,LARAMIE,82009,,,,,0.08,,America/Denver,,USA,,2,N,3,99999,2,,2,4,4,1,4,2125,45,200,1,100,0,0,1,1,2020-01-01T05:00:00Z,2021-01-01T05:00:00Z
000-10005,US-5,WESTBOUND,,WY,LARAMIE,82009,,,,,3.45,,America/Denver,,USA,,1,N,1,99999,2,,4,5,5,1,5,28380,425,8050,1,100,0,0,1,1,2020-01-01T05:00:00Z,2021-01-01T05:00:00Z
000P10006,US-6,WESTBOUND,,WY,LARAMIE,82009,,,,,0.56,,America/Denver,,USA,,2,N,2,56139,2,,6,6,6,1,6,72120,705,1650,1,100,0,0,1,1,2020-01-01T05:00:00Z,2021-01-01T05:00:00Z
000P10007,US-7,EASTBOUND,,WY,LARAMIE,82009,,,,,1,,America/Denver,,USA,,4,N,3,56139,2,,4,7,7,1,7,18800,325,210,1,100,0,0,0,1,2020-01-01T05:00:00Z,2021-01-01T05:00:00Z
000P10006,US-6,WESTBOUND,,WY,LARAMIE,82009,,,,,0.56,,America/Denver,,USA,,2,N,2,56139,2,,6,6,6,1,6,12120,405,1250,1,100,0,0,1,1,2020-01-01T05:00:00Z,2021-01-01T05:00:00Z
000+10007,US-6,WESTBOUND,,WY,LARAMIE,82009,,,,,0.56,,America/Denver,,USA,,2,N,2,56139,2,,6,6,6,1,6,72120,705,1650,1,100,0,0,1,1,2020-01-01T05:00:00Z,2021-01-01T05:00:00Z
000+10008,US-8,EASTBOUND,,WY,LARAMIE,82009,,,,,1.96,,America/Denver,,USA,,3,N,3,56139,2,,2,8,8,1,8,1670,135,130,1,100,0,0,1,1,2020-01-01T05:00:00Z,2021-01-01T05:00:00Z
000+10009,US-9,NORTHBOUND,,WY,LARAMIE,82009,,,,,1.83,,America/Denver,,USA,,2,N,3,56139,2,,2,9,9,1,9,1645,35,90,1,100,0,0,1,1,2020-01-01T05:00:00Z,2021-01-01T05:00:00Z
000P10009,US-10,NORTHBOUND,,WY,LARAMIE,82009,,,,,0.09,,America/Denver,,USA,,2,N,2,99999,2,,4,10,10,1,10,20605,385,4290,1,100,0,0,1,1,2020-01-01T05:00:00Z,2021-01-01T05:00:00Z
000P10010,US-10,NORTHBOUND,,WY,LARAMIE,82009,,,,,0.09,,America/Denver,,USA,,2,N,2,99999,2,,4,10,10,1,10,30605,585,3290,1,100,0,0,1,1,2020-01-01T05:00:00Z,2021-01-01T05:00:00Z
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ tmc,speed_limit
000P10004,65
000-10005,55
000P10006,55
000P10007,55
000+10007,55
000+10008,55
000+10009,45
000P10010,65

0 comments on commit 4b194e8

Please sign in to comment.