Retrieve and summarize data
retrieve-and-summarize-data.Rmd
Works with travelSurveyTools
The psrc.travelsurvey package is meant to complement
and enhance the travelSurveyTools package for PSRC
users. It delivers data in the hts_data
format appropriate
as input to subsequent travelSurveyTools functions. It
also provides wrapper functions that provide PSRC defaults to
travelSurveyTools functions, resulting in simpler
workflows with fewer required arguments.
psrc.travelsurvey is constrained to an extent by
fitting into the travelSurveyTools system, but if you
have suggestions for other features or functions you’d find helpful,
please contact the authors and we’ll see if we can implement them.
Data retrieval
Since psrc.travelsurvey uses Elmer, the agency’s central database, to
retrieve PSRC household travel survey data you’ll need to be connected
(in office or via VPN). Then use get_psrc_hts()
, which
takes two arguments:
-
survey_years - a vector of one or more years; the
default is all available i.e.,
c(2017,2019,2021,2023)
as of this writing. - survey_vars - a vector of variable names (regardless of the table in which they occur)
The return object is a list with separate data.table elements for
each potential unit of analysis, i.e. household (hh
),
person
, day
, trip
, and
vehicle
. The travelSurveyTools package
workflow uses this central object for related or independent variable
summaries. It can include the full set of variables for all your
summaries, so it’s unnecessary to request separate data objects to feed
each separate summary data table. It also supports multiyear summaries
(trends/comparisons) via the survey_year
field.
For efficiency, get_psrc_hts()
only requests the
variables you specify, so you must know their precise names. A lookup
function, psrc_hts_varsearch()
can assist; it takes a
single string argument as the codebook search term and matches either
the variable name or description fields. Regular expressions are
supported for more complex searches; for example, the word boundary
operator \\b
can be used to omit vehicle model from results
when searching for “mode”.
View(psrc_hts_varsearch("\\bmode\\b"))
To browse the entire variable list, open the internal data object
init_variable_list
:
View(psrc.travelsurvey:::init_variable_list)
Note: It is unnecessary to include the survey year, table ids,
weights, or survey stratification variable (sample_segment
)
in your survey_vars
argument; since they are necessary for
later steps, get_psrc_hts()
includes them implicitly.
Factor variables and levels are applied from the codebook, and missing
or skip-value codes are recoded as NA
.
Summarization
To summarize data, use psrc_hts_stat()
. This has four
primary arguments, and one option. The arguments are:
-
hts_data - the
hts_data
object - analysis_unit - string specifying the scale at which statistics are calculated, either “hh”, “person”, “day”,“trip”, or “vehicle”.
- group_vars - any grouping variables, in nesting order (share summaries are calculated per categories of the variable listed last).
- stat_var (optional) - Numeric variable if min/max/median/mean is desired; omit for count/share summaries.
As an example, we’ll retrieve the following survey variables, then do
one count/share summary, and one numeric summary. Notice,
travelSurveyTools handles the relational element of the data, so tables
lower in the organizational heirarchy (like trips
) inherit
attributes from those above them (such as hh
); this is a
handy feature and avoids unnecessary data duplication among tables.
library(psrc.travelsurvey)
library(data.table)
# Specify which variables to retrieve
vars <- c("hhsize", "employment", "commute_mode",
"mode_characterization", "distance_miles")
# Retrieve the data
hts_data <- get_psrc_hts(survey_vars = vars) # default includes all survey_years
# Calculate a numeric summary, i.e. min/max/median/mean
rs1 <- psrc_hts_stat(hts_data,
analysis_unit="trip",
group_vars="hhsize",
stat_var="distance_miles")
head(dplyr::select(rs1[survey_year==2023], -c(survey_year, min, max)))
#> hhsize count mean mean_moe median
#> <ord> <int> <num> <num> <num>
#> 1: 1 person 10075 6.380222 0.8284701 2.169828
#> 2: 2 people 11871 13.174915 3.0272559 3.311287
#> 3: 3 people 3517 8.357221 0.9016603 4.094215
#> 4: 4 people 3595 10.254066 5.6922574 3.233616
#> 5: 5 people 1018 32.329314 19.3722674 2.761995
#> 6: 6 people 371 5.709866 0.8525478 3.046583
# Calculate a categorical summary, i.e. count/share
rs2 <- psrc_hts_stat(hts_data, "trip", c("hhsize", "mode_characterization"))
head(dplyr::select(rs2[survey_year==2023 & as.character(mode_characterization)=="Transit"],
-c(survey_year, mode_characterization)))
#> hhsize count prop prop_moe est est_moe
#> <ord> <int> <num> <num> <num> <num>
#> 1: 1 person 908 0.08090005 0.012606740 173635.72 28466.102
#> 2: 2 people 563 0.02056404 0.004167832 88135.46 17783.128
#> 3: 3 people 165 0.01946050 0.005394446 32299.75 8864.421
#> 4: 4 people 205 0.04214847 0.009085101 162495.97 35391.190
#> 5: 5 people 77 0.06091950 0.015838454 131608.22 34802.763
#> 6: 6 people 49 0.10200039 0.026777949 140783.94 38384.352
Custom variables and filtering
Since each hts_data
element is a data.table, they can be
manipulated with any combination of base r, tidyverse
(e.g. mutate()
), or data.table (e.g :=
)
syntax. Additionally, standard category groupings not present in the
base data can be added via the functions described in the accompanying vignette.
Keep in mind that filtering any table removes records available for
subsequent analyses. Rather than manage multiple filtered copies of the
data object, you may want to create a new variable on the relevant table
with NA
values for the records you intend to exclude from
the summary. You can then use the incl_na=FALSE
option to
exclude that line from summary results (of particular value when you
want shares calculated from only reported categories). Here we will add
a dichotomous worker
variable, then calculate shares with
NA
values excluded (they would otherwise comprise about 20
percent of the total).
# Add the worker variable
hts_data <- hts_bin_worker(hts_data)
# Calculate a categorical summary, i.e. count/share
rs3 <- psrc_hts_stat(hts_data, "person", c("survey_year", "worker"), incl_na=FALSE)
head(rs3)
#> survey_year worker count prop prop_moe est est_moe
#> <int> <fctr> <int> <num> <num> <num> <num>
#> 1: 2017 Worker 3863 0.6681291 0.03075054 1991691.5 139270.04
#> 2: 2017 Not Worker 1517 0.3318709 0.03075054 989306.5 104621.62
#> 3: 2019 Worker 3585 0.6718722 0.03146935 2024432.5 142762.94
#> 4: 2019 Not Worker 1325 0.3281278 0.03146935 988688.8 109105.54
#> 5: 2021 Worker 2125 0.6848086 0.01923162 2268045.2 88724.59
#> 6: 2021 Not Worker 1313 0.3151914 0.01923162 1043894.8 66403.29
Note: travelSurveyTools summary functions require additional preparatory steps that psrc.travelsurvey handles for you, for simplicity. To use native travelSurveyTools summary functions, you’ll need to handle these extra steps yourself, since travelSurveyTools does not have a dependency on the psrc.travelsurvey package.