Skip to contents

About TravelSurveyTools

The travelSurveyTools package provides tools for R users to aid use of data from household travel surveys. Some possible uses include creating custom cross tabs, labeling data, and calculating trip rates.

Data Assumptions

travelSurveyTools assumes the the data have the structure shown below.

hts_data

hts_data is a list of five core tables:

hh

Household dataset

  • hh_id: household ID
  • survey variables asked on a household level
  • hh_weight: household weight

person

Person dataset

  • hh_id: household ID
  • person_id: person ID
  • survey variables asked on a person level
  • person_weight: person weight

day

Day dataset

  • hh_id: household ID
  • person_id: person ID
  • day_id: day ID
  • survey variable asked on a day level
  • day_weight: day weight

trip

Trip dataset

  • hh_id: household ID
  • person_id: person ID
  • day_id: day ID
  • trip_id: trip ID
  • survey variables asked on a trip level
  • trip_weight: trip weight

vehicle

Vehicle dataset

  • hh_id: household ID
  • vehicle_id: vehicle ID
  • survey responses asked on a vehicle level
  • hh_weight: household weight

Codebook

In addition to data from the household travel survey. The codebook is also required. The codebook is assumed to be in two parts:

variable_list

A dataset containing information about all variables existing in the hh, person, day, trip, and vehicle tables. The variables are as follows:

  • variable: Name of the variable
  • is_checkbox: The variable is a multiple response categorical variable question (multiple or mrcv variable)
  • hh: The variable exists in the hh table
  • person: The variable exists in the person table
  • day: The variable exists in the day table
  • trip: The variable exists in the trip table
  • vehicle: The variable exists in the vehicle table
  • location: The variable exists in the location table
  • data_type: Data type of the variable
  • description: A description of the variable
  • logic: Conditions where the variable should have a value
  • shared_name: the shared name of a multiple response categorical variable variable or the variable name for non-multiple response categorical variable variables

value_labels

A dataset containing the values for all variables found in variable_list The variables are as follows:

  • variable: Name of the variable
  • value: The numeric value of the variable
  • label: What the numeric value of the variable represents
  • label_value: value concatenated with the label (e.g., 11 85 or older)
  • val_order: order for each variable label to appear in

Using travelSurveyTools

Prepping the Data

In order to create summaries of our data we first need to prepare our data. We can do this by using hts_prep_variable. This will return a categorical (cat) and numeric (num) (if applicable) prepped data table that can be used to create summaries.

library(travelSurveyTools)
library(data.table)

# Load data
data("test_data")
data("variable_list")
data("value_labels")

speed_list = hts_prep_variable(
  summarize_var = "speed_mph",
  variables_dt = variable_list,
  data = test_data
)
## Warning in hts_remove_outliers(var_dt, numvar = summarize_var, threshold =
## threshold): 378 outliers were removed based on the threshold of 0.975.

Numeric variables will be automatically binned in hts_prep_variable to create categorical summaries. Here we can make a categorical summary of a numeric variable using hts_summary.

speed_cat_summary = hts_summary(
  prepped_dt = speed_list$cat,
  summarize_var = "speed_mph",
  summarize_by = NULL,
  summarize_vartype = "categorical",
  weighted = FALSE
)

speed_cat_summary$summary
## $unwtd
##     speed_mph count       prop
## 1:  1 or less   635 0.04313273
## 2:       1-10  7289 0.49510936
## 3:      10-19  3618 0.24575465
## 4:      19-28  1819 0.12355658
## 5:      28-37   726 0.04931395
## 6:      37-44   285 0.01935878
## 7: 44 or more   350 0.02377394

Additionally, for numeric variables we can create numeric summaries.

speed_num_summary = hts_summary(
  prepped_dt = speed_list$num,
  summarize_var = "speed_mph",
  summarize_by = NULL,
  summarize_vartype = "numeric",
  weighted = FALSE
)

speed_num_summary$summary
## $unwtd
##    count min      max     mean   median
## 1: 14722   0 112.5371 12.27017 8.914392

Using Weighted Data

Additionally, we can use weighted data by setting weighted = TRUE and specifying the name of the weight to be used (wtname).

speed_cat_summary = hts_summary(
  prepped_dt = speed_list$cat,
  summarize_var = "speed_mph",
  summarize_by = NULL,
  summarize_vartype = "categorical",
  weighted = TRUE,
  wtname = "trip_weight"
)

speed_cat_summary$summary
## $unwtd
##     speed_mph count       prop
## 1:  1 or less   635 0.04313273
## 2:       1-10  7289 0.49510936
## 3:      10-19  3618 0.24575465
## 4:      19-28  1819 0.12355658
## 5:      28-37   726 0.04931395
## 6:      37-44   285 0.01935878
## 7: 44 or more   350 0.02377394
## 
## $wtd
##     speed_mph count       prop     est
## 1:  1 or less   635 0.04225006  314952
## 2:       1-10  7289 0.49882701 3718493
## 3:      10-19  3618 0.24538136 1829189
## 4:      19-28  1819 0.12194475  909034
## 5:      28-37   726 0.04891599  364643
## 6:      37-44   285 0.02049065  152747
## 7: 44 or more   350 0.02219016  165416
## 
## $weight_name
## [1] "trip_weight"

Calculating Standard Errors

Additionally, by specifying se = TRUE we can calculate standard errors.

speed_cat_summary = hts_summary(
  prepped_dt = speed_list$cat,
  summarize_var = "speed_mph",
  summarize_by = NULL,
  summarize_vartype = "categorical",
  weighted = TRUE,
  wtname = "trip_weight",
  se = TRUE
)

speed_cat_summary$summary
## $unwtd
##     speed_mph count       prop
## 1:  1 or less   635 0.04313273
## 2:       1-10  7289 0.49510936
## 3:      10-19  3618 0.24575465
## 4:      19-28  1819 0.12355658
## 5:      28-37   726 0.04931395
## 6:      37-44   285 0.01935878
## 7: 44 or more   350 0.02377394
## 
## $wtd
##     speed_mph count       prop     prop_se     est   est_se
## 1:  1 or less   635 0.04225006 0.001886624  314952 14128.27
## 2:       1-10  7289 0.49882701 0.004730868 3718493 39396.65
## 3:      10-19  3618 0.24538136 0.004073123 1829189 31541.65
## 4:      19-28  1819 0.12194475 0.003087738  909034 23382.12
## 5:      28-37   726 0.04891599 0.002026164  364643 15186.92
## 6:      37-44   285 0.02049065 0.001361407  152747 10179.97
## 7: 44 or more   350 0.02219016 0.001373240  165416 10258.47
## 
## $weight_name
## [1] "trip_weight"

Summarizing Two Variables

If we want to summarize a variable by another variable (e.g., mode type by a person’s race, mode_type by a person’s ethnicity, age by study year) we can use the summarize_by argument.

mode_type_list = hts_prep_variable(
  summarize_var = "mode_type",
  summarize_by = "race",
  variables_dt = variable_list,
  data = test_data
)

mode_by_race_summary = hts_summary(
  prepped_dt = mode_type_list$cat,
  summarize_var = "mode_type",
  summarize_by = "race",
  summarize_vartype = "categorical",
  weighted = TRUE,
  wtname = "trip_weight",
  se = TRUE
)


mode_by_race_summary$summary
## $unwtd
##                                          race mode_type count         prop
##  1:                 African American or Black         1    22 0.2222222222
##  2:                 African American or Black         2     3 0.0303030303
##  3:                 African American or Black         8    68 0.6868686869
##  4:                 African American or Black        13     6 0.0606060606
##  5:          American Indian or Alaska Native         1    23 0.2555555556
##  6:          American Indian or Alaska Native         2     3 0.0333333333
##  7:          American Indian or Alaska Native         5     1 0.0111111111
##  8:          American Indian or Alaska Native         6     1 0.0111111111
##  9:          American Indian or Alaska Native         8    57 0.6333333333
## 10:          American Indian or Alaska Native        10     2 0.0222222222
## 11:          American Indian or Alaska Native        11     1 0.0111111111
## 12:          American Indian or Alaska Native        13     2 0.0222222222
## 13:                                     Asian         1    46 0.2705882353
## 14:                                     Asian         2     4 0.0235294118
## 15:                                     Asian         6     1 0.0058823529
## 16:                                     Asian         7     1 0.0058823529
## 17:                                     Asian         8   104 0.6117647059
## 18:                                     Asian        10     3 0.0176470588
## 19:                                     Asian        11     1 0.0058823529
## 20:                                     Asian        13    10 0.0588235294
## 21: Native Hawaiian or other Pacific Islander         1    43 0.2544378698
## 22: Native Hawaiian or other Pacific Islander         2     7 0.0414201183
## 23: Native Hawaiian or other Pacific Islander         6     2 0.0118343195
## 24: Native Hawaiian or other Pacific Islander         7     1 0.0059171598
## 25: Native Hawaiian or other Pacific Islander         8   106 0.6272189349
## 26: Native Hawaiian or other Pacific Islander        11     1 0.0059171598
## 27: Native Hawaiian or other Pacific Islander        13     9 0.0532544379
## 28:                                     White         1    45 0.2941176471
## 29:                                     White         2     4 0.0261437908
## 30:                                     White         4     1 0.0065359477
## 31:                                     White         6     2 0.0130718954
## 32:                                     White         8    90 0.5882352941
## 33:                                     White        13    11 0.0718954248
## 34:                               Two or more         1  2058 0.2920391656
## 35:                               Two or more         2   148 0.0210018448
## 36:                               Two or more         3     3 0.0004257131
## 37:                               Two or more         4    20 0.0028380871
## 38:                               Two or more         5     4 0.0005676174
## 39:                               Two or more         6    36 0.0051085568
## 40:                               Two or more         7    40 0.0056761743
## 41:                               Two or more         8  4299 0.6100468284
## 42:                               Two or more        10    36 0.0051085568
## 43:                               Two or more        11    28 0.0039733220
## 44:                               Two or more        12     6 0.0008514261
## 45:                               Two or more        13   355 0.0503760465
## 46:                               Two or more        14    14 0.0019866610
## 47:                                Other race         1    28 0.3146067416
## 48:                                Other race         6     1 0.0112359551
## 49:                                Other race         8    53 0.5955056180
## 50:                                Other race        10     2 0.0224719101
## 51:                                Other race        13     5 0.0561797753
## 52:                      Prefer not to answer         1  2176 0.2918846412
## 53:                      Prefer not to answer         2   183 0.0245472837
## 54:                      Prefer not to answer         3     9 0.0012072435
## 55:                      Prefer not to answer         4    22 0.0029510396
## 56:                      Prefer not to answer         5     3 0.0004024145
## 57:                      Prefer not to answer         6    43 0.0057679410
## 58:                      Prefer not to answer         7    43 0.0057679410
## 59:                      Prefer not to answer         8  4456 0.5977196512
## 60:                      Prefer not to answer        10    51 0.0068410463
## 61:                      Prefer not to answer        11    22 0.0029510396
## 62:                      Prefer not to answer        12     6 0.0008048290
## 63:                      Prefer not to answer        13   425 0.0570087190
## 64:                      Prefer not to answer        14    16 0.0021462106
##                                          race mode_type count         prop
## 
## $wtd
##                                          race mode_type count         prop
##  1:                 African American or Black         1    22 0.2425302553
##  2:                 African American or Black         2     3 0.0322180413
##  3:                 African American or Black         8    68 0.6657378216
##  4:                 African American or Black        13     6 0.0595138818
##  5:          American Indian or Alaska Native         1    23 0.2377237162
##  6:          American Indian or Alaska Native         2     3 0.0464969871
##  7:          American Indian or Alaska Native         5     1 0.0064529184
##  8:          American Indian or Alaska Native         6     1 0.0122088317
##  9:          American Indian or Alaska Native         8    57 0.6401879665
## 10:          American Indian or Alaska Native        10     2 0.0240129508
## 11:          American Indian or Alaska Native        11     1 0.0215621908
## 12:          American Indian or Alaska Native        13     2 0.0113544383
## 13:                                     Asian         1    46 0.2677774299
## 14:                                     Asian         2     4 0.0178672881
## 15:                                     Asian         6     1 0.0057161409
## 16:                                     Asian         7     1 0.0007536494
## 17:                                     Asian         8   104 0.6057138219
## 18:                                     Asian        10     3 0.0213688592
## 19:                                     Asian        11     1 0.0104699294
## 20:                                     Asian        13    10 0.0703328811
## 21: Native Hawaiian or other Pacific Islander         1    43 0.2519546682
## 22: Native Hawaiian or other Pacific Islander         2     7 0.0575492058
## 23: Native Hawaiian or other Pacific Islander         6     2 0.0097055320
## 24: Native Hawaiian or other Pacific Islander         7     1 0.0047996981
## 25: Native Hawaiian or other Pacific Islander         8   106 0.6093847660
## 26: Native Hawaiian or other Pacific Islander        11     1 0.0097998750
## 27: Native Hawaiian or other Pacific Islander        13     9 0.0568062549
## 28:                                     White         1    45 0.2736028717
## 29:                                     White         2     4 0.0297990108
## 30:                                     White         4     1 0.0128238502
## 31:                                     White         6     2 0.0193727822
## 32:                                     White         8    90 0.5994327912
## 33:                                     White        13    11 0.0649686939
## 34:                               Two or more         1  2058 0.2915903665
## 35:                               Two or more         2   148 0.0193177986
## 36:                               Two or more         3     3 0.0002755179
## 37:                               Two or more         4    20 0.0034360184
## 38:                               Two or more         5     4 0.0009396083
## 39:                               Two or more         6    36 0.0053118852
## 40:                               Two or more         7    40 0.0052803416
## 41:                               Two or more         8  4299 0.6102537891
## 42:                               Two or more        10    36 0.0057560078
## 43:                               Two or more        11    28 0.0039066600
## 44:                               Two or more        12     6 0.0008086884
## 45:                               Two or more        13   355 0.0509043799
## 46:                               Two or more        14    14 0.0022189382
## 47:                                Other race         1    28 0.3300569182
## 48:                                Other race         6     1 0.0121151165
## 49:                                Other race         8    53 0.5754451734
## 50:                                Other race        10     2 0.0096463758
## 51:                                Other race        13     5 0.0727364162
## 52:                      Prefer not to answer         1  2176 0.2901116906
## 53:                      Prefer not to answer         2   183 0.0250127937
## 54:                      Prefer not to answer         3     9 0.0008927077
## 55:                      Prefer not to answer         4    22 0.0023674661
## 56:                      Prefer not to answer         5     3 0.0004499353
## 57:                      Prefer not to answer         6    43 0.0059550102
## 58:                      Prefer not to answer         7    43 0.0054772192
## 59:                      Prefer not to answer         8  4456 0.5990344431
## 60:                      Prefer not to answer        10    51 0.0066169138
## 61:                      Prefer not to answer        11    22 0.0030776528
## 62:                      Prefer not to answer        12     6 0.0008958912
## 63:                      Prefer not to answer        13   425 0.0587054120
## 64:                      Prefer not to answer        14    16 0.0014028643
##                                          race mode_type count         prop
##          prop_se     est     est_se
##  1: 0.0488793701   11924  2760.2835
##  2: 0.0192747891    1584   961.7070
##  3: 0.0538431192   32731  4557.6808
##  4: 0.0278595569    2926  1415.2034
##  5: 0.0489623011   10573  2431.3038
##  6: 0.0265355469    2068  1210.9162
##  7: 0.0064587476     287   287.0000
##  8: 0.0121494434     543   543.0000
##  9: 0.0568590617   28473  4415.2742
## 10: 0.0171042374    1068   768.2942
## 11: 0.0212536599     959   959.0000
## 12: 0.0089016245     505   395.6596
## 13: 0.0390288610   23095  3939.9840
## 14: 0.0108784324    1541   945.4068
## 15: 0.0057056370     493   493.0000
## 16: 0.0007560023      65    65.0000
## 17: 0.0430582623   52241  5824.9319
## 18: 0.0128200078    1843  1117.8535
## 19: 0.0104007037     903   903.0000
## 20: 0.0233047946    6066  2090.2978
## 21: 0.0388570716   21365  3803.6897
## 22: 0.0228735703    4880  2008.4349
## 23: 0.0071925215     823   611.3568
## 24: 0.0047960813     407   407.0000
## 25: 0.0440034185   51674  5845.4095
## 26: 0.0097433256     831   831.0000
## 27: 0.0217247910    4817  1901.8419
## 28: 0.0409308580   19970  3464.8052
## 29: 0.0159829589    2175  1184.4750
## 30: 0.0127152614     936   936.0000
## 31: 0.0136901767    1414  1010.1692
## 32: 0.0457651407   43752  5263.5573
## 33: 0.0239756794    4742  1814.6243
## 34: 0.0062024440 1044577 25013.0886
## 35: 0.0018149697   69203  6536.9674
## 36: 0.0001992965     987   713.9738
## 37: 0.0008360485   12309  2998.9750
## 38: 0.0004717023    3366  1690.5458
## 39: 0.0009889333   19029  3548.7518
## 40: 0.0009940850   18916  3567.4116
## 41: 0.0066548054 2186139 33829.8317
## 42: 0.0010634803   20620  3817.7647
## 43: 0.0008631699   13995  3096.3278
## 44: 0.0003923923    2897  1406.0707
## 45: 0.0030039411  182357 10947.9882
## 46: 0.0006557321    7949  2350.8972
## 47: 0.0573116049   14439  3049.7214
## 48: 0.0120592582     530   530.0000
## 49: 0.0604560934   25174  4056.9946
## 50: 0.0088957777     422   389.4846
## 51: 0.0338891184    3182  1547.2956
## 52: 0.0060416540 1093556 25527.6793
## 53: 0.0020680881   94284  7856.6878
## 54: 0.0003637299    3365  1371.2763
## 55: 0.0005670664    8924  2138.0610
## 56: 0.0003045156    1696  1148.0627
## 57: 0.0010522269   22447  3974.6248
## 58: 0.0009766971   20646  3687.7976
## 59: 0.0065250441 2258019 34349.4918
## 60: 0.0010559060   24942  3987.6756
## 61: 0.0007423448   11601  2801.0127
## 62: 0.0004094685    3377  1543.9496
## 63: 0.0031475192  221286 12101.0995
## 64: 0.0004001287    5288  1508.1227
##          prop_se     est     est_se
## 
## $weight_name
## [1] "trip_weight"
if (FALSE) {
  age_study_year_list = hts_prep_variable(
    summarize_var = "age",
    summarize_by = "study_year",
    variables_dt = variable_list,
    data = test_data
  )

  mode_by_race_summary = hts_summary(
    prepped_dt = age_study_year_list$cat,
    summarize_var = "age",
    summarize_by = "study_year",
    summarize_vartype = "categorical",
    weighted = TRUE,
    wtname = "trip_weight",
    se = TRUE
  )
}

If we want to summarize a multiple response categorical variable (mrcv or multiple), we can set summarize_vartype to checkbox.

race_list = hts_prep_variable(
  summarize_var = "race",
  summarize_by = "mode_type",
  variables_dt = variable_list,
  data = test_data
)

mode_by_race_summary = hts_summary(
  prepped_dt = race_list$cat,
  summarize_var = "race",
  summarize_by = "mode_type",
  summarize_vartype = "checkbox",
  weighted = TRUE,
  wtname = "trip_weight",
  se = TRUE
)


mode_by_race_summary$summary
## $unwtd
##     mode_type                                      race count       prop
##  1:         1                 African American or Black  1173 0.26072461
##  2:         1          American Indian or Alaska Native  1149 0.25539009
##  3:         1                                     Asian  1095 0.24338742
##  4:         1 Native Hawaiian or other Pacific Islander  1145 0.25450100
##  5:         1                                     White  1188 0.26405868
##  6:         1                                Other race  1150 0.25561236
##  7:         1                      Prefer not to answer  2176 0.48366304
##  8:         2                 African American or Black    79 0.22005571
##  9:         2          American Indian or Alaska Native    78 0.21727019
## 10:         2                                     Asian    68 0.18941504
## 11:         2 Native Hawaiian or other Pacific Islander    94 0.26183844
## 12:         2                                     White    87 0.24233983
## 13:         2                                Other race    85 0.23676880
## 14:         2                      Prefer not to answer   183 0.50974930
## 15:         3          American Indian or Alaska Native     2 0.16666667
## 16:         3                                     Asian     1 0.08333333
## 17:         3 Native Hawaiian or other Pacific Islander     1 0.08333333
## 18:         3                                     White     2 0.16666667
## 19:         3                                Other race     1 0.08333333
## 20:         3                      Prefer not to answer     9 0.75000000
## 21:         4                 African American or Black     9 0.20454545
## 22:         4          American Indian or Alaska Native    11 0.25000000
## 23:         4                                     Asian    11 0.25000000
## 24:         4 Native Hawaiian or other Pacific Islander     9 0.20454545
## 25:         4                                     White    10 0.22727273
## 26:         4                                Other race    10 0.22727273
## 27:         4                      Prefer not to answer    22 0.50000000
## 28:         5                 African American or Black     2 0.25000000
## 29:         5          American Indian or Alaska Native     2 0.25000000
## 30:         5                                     Asian     2 0.25000000
## 31:         5 Native Hawaiian or other Pacific Islander     3 0.37500000
## 32:         5                                     White     1 0.12500000
## 33:         5                                Other race     2 0.25000000
## 34:         5                      Prefer not to answer     3 0.37500000
## 35:         6                 African American or Black    24 0.27586207
## 36:         6          American Indian or Alaska Native    21 0.24137931
## 37:         6                                     Asian    17 0.19540230
## 38:         6 Native Hawaiian or other Pacific Islander    18 0.20689655
## 39:         6                                     White    16 0.18390805
## 40:         6                                Other race    27 0.31034483
## 41:         6                      Prefer not to answer    43 0.49425287
## 42:         7                 African American or Black    21 0.24418605
## 43:         7          American Indian or Alaska Native    21 0.24418605
## 44:         7                                     Asian    15 0.17441860
## 45:         7 Native Hawaiian or other Pacific Islander    24 0.27906977
## 46:         7                                     White    20 0.23255814
## 47:         7                                Other race    21 0.24418605
## 48:         7                      Prefer not to answer    43 0.50000000
## 49:         8                 African American or Black  2422 0.25956489
## 50:         8          American Indian or Alaska Native  2341 0.25088415
## 51:         8                                     Asian  2333 0.25002679
## 52:         8 Native Hawaiian or other Pacific Islander  2457 0.26331583
## 53:         8                                     White  2503 0.26824563
## 54:         8                                Other race  2375 0.25452792
## 55:         8                      Prefer not to answer  4456 0.47754796
## 56:        10                 African American or Black    19 0.19587629
## 57:        10          American Indian or Alaska Native    26 0.26804124
## 58:        10                                     Asian    24 0.24742268
## 59:        10 Native Hawaiian or other Pacific Islander    20 0.20618557
## 60:        10                                     White    17 0.17525773
## 61:        10                                Other race    21 0.21649485
## 62:        10                      Prefer not to answer    51 0.52577320
## 63:        11                 African American or Black    18 0.33962264
## 64:        11          American Indian or Alaska Native    14 0.26415094
## 65:        11                                     Asian    18 0.33962264
## 66:        11 Native Hawaiian or other Pacific Islander    16 0.30188679
## 67:        11                                     White    15 0.28301887
## 68:        11                                Other race    16 0.30188679
## 69:        11                      Prefer not to answer    22 0.41509434
## 70:        12                 African American or Black     4 0.33333333
## 71:        12          American Indian or Alaska Native     3 0.25000000
## 72:        12                                     Asian     1 0.08333333
## 73:        12 Native Hawaiian or other Pacific Islander     4 0.33333333
## 74:        12                                     White     5 0.41666667
## 75:        12                                Other race     2 0.16666667
## 76:        12                      Prefer not to answer     6 0.50000000
## 77:        13                 African American or Black   206 0.24700240
## 78:        13          American Indian or Alaska Native   189 0.22661871
## 79:        13                                     Asian   184 0.22062350
## 80:        13 Native Hawaiian or other Pacific Islander   210 0.25179856
## 81:        13                                     White   220 0.26378897
## 82:        13                                Other race   196 0.23501199
## 83:        13                      Prefer not to answer   425 0.50959233
## 84:        14                 African American or Black     5 0.16129032
## 85:        14          American Indian or Alaska Native     8 0.25806452
## 86:        14                                     Asian     8 0.25806452
## 87:        14 Native Hawaiian or other Pacific Islander     7 0.22580645
## 88:        14                                     White     5 0.16129032
## 89:        14                                Other race     6 0.19354839
## 90:        14                      Prefer not to answer    16 0.51612903
##     mode_type                                      race count       prop
## 
## $wtd
##     mode_type                                      race count       prop
##  1:         1                 African American or Black  1173 0.26371389
##  2:         1          American Indian or Alaska Native  1149 0.26062794
##  3:         1                                     Asian  1095 0.25433099
##  4:         1 Native Hawaiian or other Pacific Islander  1145 0.25516511
##  5:         1                                     White  1188 0.26543213
##  6:         1                                Other race  1150 0.25876904
##  7:         1                      Prefer not to answer  2176 0.48830386
##  8:         2                 African American or Black    79 0.19397957
##  9:         2          American Indian or Alaska Native    78 0.21172220
## 10:         2                                     Asian    68 0.17892850
## 11:         2 Native Hawaiian or other Pacific Islander    94 0.25340427
## 12:         2                                     White    87 0.22521979
## 13:         2                                Other race    85 0.21375366
## 14:         2                      Prefer not to answer   183 0.53651236
## 15:         3          American Indian or Alaska Native     2 0.07054228
## 16:         3                                     Asian     1 0.15625000
## 17:         3 Native Hawaiian or other Pacific Islander     1 0.03791360
## 18:         3                                     White     2 0.18887868
## 19:         3                                Other race     1 0.03791360
## 20:         3                      Prefer not to answer     9 0.77320772
## 21:         4                 African American or Black     9 0.28639091
## 22:         4          American Indian or Alaska Native    11 0.26956561
## 23:         4                                     Asian    11 0.29396906
## 24:         4 Native Hawaiian or other Pacific Islander     9 0.24547792
## 25:         4                                     White    10 0.32080834
## 26:         4                                Other race    10 0.27299382
## 27:         4                      Prefer not to answer    22 0.40254409
## 28:         5                 African American or Black     2 0.33090297
## 29:         5          American Indian or Alaska Native     2 0.18508132
## 30:         5                                     Asian     2 0.32978127
## 31:         5 Native Hawaiian or other Pacific Islander     3 0.46120770
## 32:         5                                     White     1 0.16806880
## 33:         5                                Other race     2 0.33501589
## 34:         5                      Prefer not to answer     3 0.31706861
## 35:         6                 African American or Black    24 0.29466198
## 36:         6          American Indian or Alaska Native    21 0.22747852
## 37:         6                                     Asian    17 0.19717750
## 38:         6 Native Hawaiian or other Pacific Islander    18 0.19435058
## 39:         6                                     White    16 0.19828176
## 40:         6                                Other race    27 0.33450385
## 41:         6                      Prefer not to answer    43 0.49574858
## 42:         7                 African American or Black    21 0.26624869
## 43:         7          American Indian or Alaska Native    21 0.23157816
## 44:         7                                     Asian    15 0.20025478
## 45:         7 Native Hawaiian or other Pacific Islander    24 0.26315132
## 46:         7                                     White    20 0.26202728
## 47:         7                                Other race    21 0.28235999
## 48:         7                      Prefer not to answer    43 0.51571165
## 49:         8                 African American or Black  2422 0.26321795
## 50:         8          American Indian or Alaska Native  2341 0.25013558
## 51:         8                                     Asian  2333 0.25196235
## 52:         8 Native Hawaiian or other Pacific Islander  2457 0.26776841
## 53:         8                                     White  2503 0.27305121
## 54:         8                                Other race  2375 0.25516272
## 55:         8                      Prefer not to answer  4456 0.48266802
## 56:        10                 African American or Black    19 0.23781573
## 57:        10          American Indian or Alaska Native    26 0.28499847
## 58:        10                                     Asian    24 0.31129972
## 59:        10 Native Hawaiian or other Pacific Islander    20 0.23196646
## 60:        10                                     White    17 0.18989672
## 61:        10                                Other race    21 0.26256263
## 62:        10                      Prefer not to answer    51 0.51011351
## 63:        11                 African American or Black    18 0.31075683
## 64:        11          American Indian or Alaska Native    14 0.25281912
## 65:        11                                     Asian    18 0.37686026
## 66:        11 Native Hawaiian or other Pacific Islander    16 0.29131465
## 67:        11                                     White    15 0.22994804
## 68:        11                                Other race    16 0.29364771
## 69:        11                      Prefer not to answer    22 0.41008873
## 70:        12                 African American or Black     4 0.30108384
## 71:        12          American Indian or Alaska Native     3 0.29693975
## 72:        12                                     Asian     1 0.06391457
## 73:        12 Native Hawaiian or other Pacific Islander     4 0.33854001
## 74:        12                                     White     5 0.30554670
## 75:        12                                Other race     2 0.12320689
## 76:        12                      Prefer not to answer     6 0.53825311
## 77:        13                 African American or Black   206 0.26177265
## 78:        13          American Indian or Alaska Native   189 0.22290264
## 79:        13                                     Asian   184 0.22623925
## 80:        13 Native Hawaiian or other Pacific Islander   210 0.25100439
## 81:        13                                     White   220 0.25915925
## 82:        13                                Other race   196 0.24087949
## 83:        13                      Prefer not to answer   425 0.51959585
## 84:        14                 African American or Black     5 0.21787414
## 85:        14          American Indian or Alaska Native     8 0.32953086
## 86:        14                                     Asian     8 0.34955050
## 87:        14 Native Hawaiian or other Pacific Islander     7 0.25783788
## 88:        14                                     White     5 0.25043439
## 89:        14                                Other race     6 0.25542041
## 90:        14                      Prefer not to answer    16 0.39948629
##     mode_type                                      race count       prop
##         prop_se     est     est_se wtd_group_n
##  1: 0.007521060  590587 19751.7986     2239499
##  2: 0.007495255  583676 19648.7332     2239499
##  3: 0.007466454  569574 19535.6370     2239499
##  4: 0.007414976  571442 19331.1747     2239499
##  5: 0.007526441  594435 19772.6450     2239499
##  6: 0.007461661  579513 19514.5648     2239499
##  7: 0.008563971 1093556 26811.8530     2239499
##  8: 0.022599934   34089  4414.1432      175735
##  9: 0.024164798   37207  4823.3924      175735
## 10: 0.022551372   31444  4410.9500      175735
## 11: 0.026088794   44532  5374.6399      175735
## 12: 0.024342203   39579  4866.3344      175735
## 13: 0.023920166   37564  4755.1810      175735
## 14: 0.030021051   94284  7888.2174      175735
## 15: 0.052699591     307   217.6892        4352
## 16: 0.140949165     680   680.0000        4352
## 17: 0.038857534     165   165.0000        4352
## 18: 0.142679479     822   694.6670        4352
## 19: 0.038857534     165   165.0000        4352
## 20: 0.145603070    3365  1371.4699        4352
## 21: 0.080437905    6349  2240.6010       22169
## 22: 0.076052484    5976  2049.5226       22169
## 23: 0.077637475    6517  2123.5988       22169
## 24: 0.075599347    5442  2022.4376       22169
## 25: 0.082213793    7112  2351.4577       22169
## 26: 0.077765448    6052  2120.6468       22169
## 27: 0.079192007    8924  2139.0479       22169
## 28: 0.186600411    1770  1251.7298        5349
## 29: 0.133384954     990   759.3249        5349
## 30: 0.186290950    1764  1247.4276        5349
## 31: 0.192933642    2467  1431.8735        5349
## 32: 0.151516165     899   899.0000        5349
## 33: 0.187681926    1792  1267.1366        5349
## 34: 0.178382865    1696  1148.1009        5349
## 35: 0.055114087   13342  2984.7356       45279
## 36: 0.049043617   10300  2506.0621       45279
## 37: 0.048517999    8928  2474.9189       45279
## 38: 0.046308723    8800  2326.6173       45279
## 39: 0.049260333    8978  2524.8655       45279
## 40: 0.057426192   15146  3218.0346       45279
## 41: 0.060813165   22447  3978.0761       45279
## 42: 0.056861230   10659  2706.3282       40034
## 43: 0.053446701    9271  2467.7153       40034
## 44: 0.051527293    8017  2345.8605       40034
## 45: 0.056127613   10535  2652.5818       40034
## 46: 0.055797849   10490  2628.6720       40034
## 47: 0.058773581   11304  2854.9687       40034
## 48: 0.063825811   20646  3690.9426       40034
## 49: 0.005203138 1231387 28332.1942     4678203
## 50: 0.005116724 1170185 27637.5572     4678203
## 51: 0.005134471 1178731 27779.5760     4678203
## 52: 0.005248280 1252675 28712.6809     4678203
## 53: 0.005278611 1277389 28969.5310     4678203
## 54: 0.005139851 1193703 27816.7752     4678203
## 55: 0.005936967 2258019 38295.4940     4678203
## 56: 0.051116600   11628  2965.5116       48895
## 57: 0.051754394   13935  3038.6851       48895
## 58: 0.054519915   15221  3324.7134       48895
## 59: 0.049258980   11342  2814.1460       48895
## 60: 0.045780759    9285  2543.1649       48895
## 61: 0.052387775   12838  3088.3310       48895
## 62: 0.057635458   24942  3991.9477       48895
## 63: 0.072982647    8791  2487.8448       28289
## 64: 0.068914514    7152  2262.1340       28289
## 65: 0.076902249   10661  2784.2457       28289
## 66: 0.070817274    8241  2360.7733       28289
## 67: 0.063881177    6505  2024.8278       28289
## 68: 0.071296301    8307  2387.9966       28289
## 69: 0.077086742   11601  2802.2847       28289
## 70: 0.142535861    1889  1008.0910        6274
## 71: 0.154063036    1863  1163.4025        6274
## 72: 0.063368644     401   401.0000        6274
## 73: 0.156085712    2124  1192.3160        6274
## 74: 0.142806765    1917  1008.4793        6274
## 75: 0.111008499     773   745.5257        6274
## 76: 0.165739202    3377  1544.1139        6274
## 77: 0.017327203  111484  8690.1437      425881
## 78: 0.016211618   94930  7891.0872      425881
## 79: 0.016475113   96351  8071.0462      425881
## 80: 0.017035182  106898  8469.7402      425881
## 81: 0.017218169  110371  8606.6809      425881
## 82: 0.016808579  102586  8305.0090      425881
## 83: 0.019747128  221286 12214.0113      425881
## 84: 0.089745276    2884  1418.4716       13237
## 85: 0.099541462    4362  1704.0613       13237
## 86: 0.102184473    4627  1800.9467       13237
## 87: 0.093495068    3413  1514.9622       13237
## 88: 0.095464165    3315  1556.3302       13237
## 89: 0.095448565    3381  1558.7443       13237
## 90: 0.095344414    5288  1508.6015       13237
##         prop_se     est     est_se wtd_group_n
## 
## $weight_name
## [1] "trip_weight"

summarize_by can be used with an unlimited amount of variables. To use more than one summarize_by variable pass a vector to the argument.

mode_type_race_ethnicity_list = hts_prep_variable(
  summarize_var = "mode_type",
  summarize_by = c("race", "ethnicity"),
  variables_dt = variable_list,
  data = list(
    "hh" = hh,
    "person" = person,
    "day" = day,
    "trip" = trip,
    "vehicle" = vehicle
  )
)

mode_by_race_ethnicity_summary = hts_summary(
  prepped_dt = mode_type_race_ethnicity_list$cat,
  summarize_var = "mode_type",
  summarize_by = c("race", "ethnicity"),
  wtname = "trip_weight"
)


head(mode_by_race_ethnicity_summary$summary$wtd, 10)
##                                 race                                  ethnicity
##  1:        African American or Black                                      Cuban
##  2:        African American or Black                                      Cuban
##  3:        African American or Black                                Two or more
##  4:        African American or Black                                Two or more
##  5:        African American or Black                                Two or more
##  6:        African American or Black                       Prefer not to answer
##  7:        African American or Black                       Prefer not to answer
##  8:        African American or Black                       Prefer not to answer
##  9:        African American or Black                       Prefer not to answer
## 10: American Indian or Alaska Native Not of Hispanic, Latino, or Spanish origin
##     mode_type count       prop   est
##  1:         8    20 0.85193115  9948
##  2:         1     4 0.14806885  1729
##  3:         8    19 0.61909727  8312
##  4:         1     6 0.28318189  3802
##  5:        13     2 0.09772084  1312
##  6:         8    29 0.60140470 14471
##  7:         1    12 0.26568864  6393
##  8:        13     4 0.06707672  1614
##  9:         2     3 0.06582994  1584
## 10:         1     4 0.18561680  1768

Calculating trip rates

hts_summary can also be used to calculate trip rates.

employment_triprate_list = hts_prep_triprate(
  summarize_by = "employment",
  variables_dt = variable_list,
  trip_name = "trip",
  day_name = "day",
  hts_data = list(
    "hh" = hh,
    "person" = person,
    "day" = day,
    "trip" = trip,
    "vehicle" = vehicle
  )
)

trip_rate_by_employment_summary = hts_summary(
  prepped_dt = employment_triprate_list$num,
  summarize_var = "num_trips_wtd",
  summarize_by = "employment",
  summarize_vartype = "numeric",
  weighted = TRUE,
  wtname = "day_weight",
  se = TRUE
)

head(trip_rate_by_employment_summary$summary$wtd, 10)
##    employment count       min       max     mean    mean_se   median
## 1:          1  1798 0.0000000 391.81818 3.810070 0.07239468 2.745520
## 2:          2   298 0.0000000 130.57143 4.064631 0.18903924 3.023569
## 3:          3   246 0.0000000 186.50000 3.685040 0.18526271 2.721311
## 4:          5   982 0.0000000 246.71429 3.855805 0.10134559 2.697699
## 5:          6   182 0.0000000 163.23529 4.033846 0.23908385 2.775264
## 6:          7    28 0.2647059 165.04545 3.851056 0.66241644 2.773455
## 7:          8    30 0.7719101  26.95098 4.065571 0.58654865 3.592087
## 8:        995   561 0.0000000 299.10000 4.031359 0.13624559 2.922734

Labeling Values

To label values we can use factorize_column.

trip_rate_by_employment_summary$summary$wtd$employment = factorize_column(
  trip_rate_by_employment_summary$summary$wtd$employment,
  "employment",
  value_labels,
  variable_colname = "variable",
  value_colname = "value",
  value_label_colname = "label",
  value_order_colname = "val_order"
)


trip_rate_by_employment_summary$summary$wtd
##                                                                             employment
## 1:                                           Employed full-time (35+ hours/week, paid)
## 2:                                 Employed part-time (fewer than 35 hours/week, paid)
## 3:                                                                       Self-employed
## 4: Not employed and not looking for work (e.g., retired, stay-at-home parent, student)
## 5:                                                     Unemployed and looking for work
## 6:                                                          Unpaid volunteer or intern
## 7:               Employed, but not currently working (e.g., on leave, furloughed 100%)
## 8:                                                                    Missing Response
##    count       min       max     mean    mean_se   median
## 1:  1798 0.0000000 391.81818 3.810070 0.07239468 2.745520
## 2:   298 0.0000000 130.57143 4.064631 0.18903924 3.023569
## 3:   246 0.0000000 186.50000 3.685040 0.18526271 2.721311
## 4:   982 0.0000000 246.71429 3.855805 0.10134559 2.697699
## 5:   182 0.0000000 163.23529 4.033846 0.23908385 2.775264
## 6:    28 0.2647059 165.04545 3.851056 0.66241644 2.773455
## 7:    30 0.7719101  26.95098 4.065571 0.58654865 3.592087
## 8:   561 0.0000000 299.10000 4.031359 0.13624559 2.922734

Creating Visuals using hts_summary output

hts_summary creates outputs that can easily be used to create visuals.

## Warning: package 'ggplot2' was built under R version 4.3.2
p = ggplot(
  trip_rate_by_employment_summary$summary$wtd,
  aes(x = mean, y = employment, label = count)
) +
  geom_bar(stat = "identity") +
  geom_errorbar(
    aes(
      xmin = (mean - mean_se),
      xmax = (mean + mean_se),
      width = .2
    )
  ) +
  labs(
    x = "Mean Trip Rate",
    y = "Employment"
  ) +
  scale_y_discrete(
    labels = function(x) stringr::str_wrap(x, width = 50),
    limits = rev
  )

print(p)

Summarizing a new variable

To summarize a new variable with hts_summary it must first be added to the variable_list and value_labels. In this example we are creating a new variable called hh_size that we want to summarize.

test_data$hh[, hh_size := ifelse(num_people < 4, 0, 1)]

variable_list = rbind(
  variable_list,
  data.table(
    variable = "hh_size",
    is_checkbox = 0,
    hh = 1,
    person = 0,
    day = 0,
    trip = 0,
    vehicle = 0,
    description = "Household size",
    data_type = "integer/categorical",
    shared_name = "hh_size"
  )
)

value_labels = rbind(
  value_labels,
  data.table(
    variable = rep("hh_size", 2),
    value = c(0, 1),
    label = c("Small household", "Large household"),
    val_order = c(214:215)
  )
)

hh_size_list = hts_prep_variable(
  summarize_var = "hh_size",
  variables_dt = variable_list,
  data = test_data
)

hh_size_summary = hts_summary(
  prepped_dt = hh_size_list$cat,
  summarize_var = "hh_size",
  summarize_vartype = "categorical",
  weighted = TRUE,
  wtname = "hh_weight"
)

factorize_df(df = hh_size_summary$summary$wtd, value_labels, value_label_colname = "label")
##            hh_size count      prop    est
## 1: Small household   842 0.8400047 436054
## 2: Large household   158 0.1599953  83055