Skip to contents

Summarize a categorical variable

Usage

hts_summary_cat(
  prepped_dt,
  summarize_var = NULL,
  summarize_by = NULL,
  weighted = TRUE,
  se = FALSE,
  wtname = NULL,
  strataname = NULL,
  checkbox_valname = "value",
  checkbox_yesval = 1,
  summarize_vartype = "categorical",
  id_cols = c("hh_id", "person_id", "day_id", "trip_id", "vehicle_id")
)

Arguments

prepped_dt

Dataset containing the summary variables and key columns in data.table format.

summarize_var

Name of the categorical variable to summarize. Default is NULL.

summarize_by

Name of the variable to summarize the summarize_var by. Default is NULL.

weighted

Whether the data is weighted. Default is TRUE.

se

Whether to calculate standard error. Default is FALSE.

wtname

Name of the weight column to use. Default is NULL.

strataname

Name of strata name to bring in. Default is NULL.

checkbox_valname

Name of the column with the checkbox value. Default is 'value'. Must be provided if summarize_var is a checkbox variable.

checkbox_yesval

Value of checkbox_valname that indicates it was selected. Default is 1. Must be provided if summarize_var is a checkbox variable.

summarize_vartype

String; one of either 'categorical' (when the variable being summarized is categorical) or 'checkbox' (when the variable being summarized is derived from a multiple response, aka select-all-that-apply question).

id_cols

names of possible ids in prepped_dt to return unique counts of

Value

List of unweighted and weighted categorical summaries including counts and proportions.

Examples


require(data.table)
require(stringr)
require(dplyr)
require(srvyr)
DT = hts_prep_variable(
  summarize_var = "age",
  variables_dt = variable_list,
  data = list(
    "hh" = hh,
    "person" = person,
    "day" = day,
    "trip" = trip,
    "vehicle" = vehicle
  )
)$cat
hts_summary_cat(
  prepped_dt = DT,
  summarize_var = "age",
  wtname = "person_weight"
)
#> $unwtd
#>     age count       prop
#>  1:   1   183 0.08939912
#>  2:   2   158 0.07718613
#>  3:   3   172 0.08402540
#>  4:   4   172 0.08402540
#>  5:   5   176 0.08597948
#>  6:   6   162 0.07914021
#>  7:   7   178 0.08695652
#>  8:   8   180 0.08793356
#>  9:   9   169 0.08255984
#> 10:  10   149 0.07278945
#> 11:  11   177 0.08646800
#> 12:  12   171 0.08353688
#> 
#> $wtd
#>     age count       prop   est
#>  1:   1   183 0.09068116 93964
#>  2:   2   158 0.07077481 73337
#>  3:   3   172 0.08828491 91481
#>  4:   4   172 0.08196664 84934
#>  5:   5   176 0.09268849 96044
#>  6:   6   162 0.07615986 78917
#>  7:   7   178 0.08525751 88344
#>  8:   8   180 0.07853874 81382
#>  9:   9   169 0.08182574 84788
#> 10:  10   149 0.07724652 80043
#> 11:  11   177 0.09292107 96285
#> 12:  12   171 0.08365454 86683
#> 
#> $weight_name
#> [1] "person_weight"
#> 

DT = hts_prep_variable(
  summarize_var = "race",
  summarize_by = "employment",
  variables_dt = variable_list,
  data = list(
    "hh" = hh,
    "person" = person,
    "day" = day,
    "trip" = trip,
    "vehicle" = vehicle
  )
)$cat
hts_summary_cat(
  prepped_dt = DT,
  summarize_var = "race",
  summarize_by = "employment",
  wtname = "person_weight",
  summarize_vartype = "checkbox"
)
#> $unwtd
#>     employment                                      race count       prop
#>  1:          1                 African American or Black   232 0.25863991
#>  2:          1          American Indian or Alaska Native   240 0.26755853
#>  3:          1                                     Asian   214 0.23857302
#>  4:          1 Native Hawaiian or other Pacific Islander   242 0.26978818
#>  5:          1                                     White   245 0.27313266
#>  6:          1                                Other race   220 0.24526198
#>  7:          1                      Prefer not to answer   442 0.49275362
#>  8:          2                 African American or Black    38 0.25165563
#>  9:          2          American Indian or Alaska Native    29 0.19205298
#> 10:          2                                     Asian    37 0.24503311
#> 11:          2 Native Hawaiian or other Pacific Islander    41 0.27152318
#> 12:          2                                     White    27 0.17880795
#> 13:          2                                Other race    46 0.30463576
#> 14:          2                      Prefer not to answer    75 0.49668874
#> 15:          3                 African American or Black    24 0.21818182
#> 16:          3          American Indian or Alaska Native    29 0.26363636
#> 17:          3                                     Asian    34 0.30909091
#> 18:          3 Native Hawaiian or other Pacific Islander    28 0.25454545
#> 19:          3                                     White    24 0.21818182
#> 20:          3                                Other race    28 0.25454545
#> 21:          3                      Prefer not to answer    57 0.51818182
#> 22:          5                 African American or Black   122 0.24351297
#> 23:          5          American Indian or Alaska Native   130 0.25948104
#> 24:          5                                     Asian   122 0.24351297
#> 25:          5 Native Hawaiian or other Pacific Islander   118 0.23552894
#> 26:          5                                     White   127 0.25349301
#> 27:          5                                Other race   118 0.23552894
#> 28:          5                      Prefer not to answer   256 0.51097804
#> 29:          6                 African American or Black    22 0.27160494
#> 30:          6          American Indian or Alaska Native    19 0.23456790
#> 31:          6                                     Asian    17 0.20987654
#> 32:          6 Native Hawaiian or other Pacific Islander    25 0.30864198
#> 33:          6                                     White    24 0.29629630
#> 34:          6                                Other race    15 0.18518519
#> 35:          6                      Prefer not to answer    36 0.44444444
#> 36:          7                 African American or Black     1 0.07142857
#> 37:          7 Native Hawaiian or other Pacific Islander     2 0.14285714
#> 38:          7                                     White     3 0.21428571
#> 39:          7                                Other race     2 0.14285714
#> 40:          7                      Prefer not to answer    11 0.78571429
#> 41:          8                 African American or Black     2 0.12500000
#> 42:          8          American Indian or Alaska Native     1 0.06250000
#> 43:          8                                     Asian     2 0.12500000
#> 44:          8 Native Hawaiian or other Pacific Islander     2 0.12500000
#> 45:          8                                     White     3 0.18750000
#> 46:          8                                Other race     4 0.25000000
#> 47:          8                      Prefer not to answer    11 0.68750000
#>     employment                                      race count       prop
#> 
#> $wtd
#>     employment                                      race count       prop
#>  1:          1                 African American or Black   232 0.27073716
#>  2:          1          American Indian or Alaska Native   240 0.28655208
#>  3:          1                                     Asian   214 0.24514797
#>  4:          1 Native Hawaiian or other Pacific Islander   242 0.27775413
#>  5:          1                                     White   245 0.28411353
#>  6:          1                                Other race   220 0.25628640
#>  7:          1                      Prefer not to answer   442 0.48754533
#>  8:          2                 African American or Black    38 0.26319327
#>  9:          2          American Indian or Alaska Native    29 0.17631616
#> 10:          2                                     Asian    37 0.22820145
#> 11:          2 Native Hawaiian or other Pacific Islander    41 0.26018745
#> 12:          2                                     White    27 0.16369678
#> 13:          2                                Other race    46 0.31722196
#> 14:          2                      Prefer not to answer    75 0.50837698
#> 15:          3                 African American or Black    24 0.24917350
#> 16:          3          American Indian or Alaska Native    29 0.29843295
#> 17:          3                                     Asian    34 0.33304681
#> 18:          3 Native Hawaiian or other Pacific Islander    28 0.28963898
#> 19:          3                                     White    24 0.22072534
#> 20:          3                                Other race    28 0.24761968
#> 21:          3                      Prefer not to answer    57 0.47690756
#> 22:          5                 African American or Black   122 0.25345189
#> 23:          5          American Indian or Alaska Native   130 0.26013795
#> 24:          5                                     Asian   122 0.24647690
#> 25:          5 Native Hawaiian or other Pacific Islander   118 0.24306672
#> 26:          5                                     White   127 0.24809246
#> 27:          5                                Other race   118 0.22771685
#> 28:          5                      Prefer not to answer   256 0.52309602
#> 29:          6                 African American or Black    22 0.29014733
#> 30:          6          American Indian or Alaska Native    19 0.22133978
#> 31:          6                                     Asian    17 0.19500460
#> 32:          6 Native Hawaiian or other Pacific Islander    25 0.30255525
#> 33:          6                                     White    24 0.27992634
#> 34:          6                                Other race    15 0.12228361
#> 35:          6                      Prefer not to answer    36 0.49417587
#> 36:          7                 African American or Black     1 0.10779358
#> 37:          7 Native Hawaiian or other Pacific Islander     2 0.21159045
#> 38:          7                                     White     3 0.22087692
#> 39:          7                                Other race     2 0.21159045
#> 40:          7                      Prefer not to answer    11 0.77912308
#> 41:          8                 African American or Black     2 0.15386297
#> 42:          8          American Indian or Alaska Native     1 0.04491312
#> 43:          8                                     Asian     2 0.09682002
#> 44:          8 Native Hawaiian or other Pacific Islander     2 0.17265873
#> 45:          8                                     White     3 0.12621571
#> 46:          8                                Other race     4 0.23516556
#> 47:          8                      Prefer not to answer    11 0.70112556
#>     employment                                      race count       prop
#>        est wtd_group_n
#>  1: 121460      448627
#>  2: 128555      448627
#>  3: 109980      448627
#>  4: 124608      448627
#>  5: 127461      448627
#>  6: 114977      448627
#>  7: 218726      448627
#>  8:  20752       78847
#>  9:  13902       78847
#> 10:  17993       78847
#> 11:  20515       78847
#> 12:  12907       78847
#> 13:  25012       78847
#> 14:  40084       78847
#> 15:  15074       60496
#> 16:  18054       60496
#> 17:  20148       60496
#> 18:  17522       60496
#> 19:  13353       60496
#> 20:  14980       60496
#> 21:  28851       60496
#> 22:  62282      245735
#> 23:  63925      245735
#> 24:  60568      245735
#> 25:  59730      245735
#> 26:  60965      245735
#> 27:  55958      245735
#> 28: 128543      245735
#> 29:  12604       43440
#> 30:   9615       43440
#> 31:   8471       43440
#> 32:  13143       43440
#> 33:  12160       43440
#> 34:   5312       43440
#> 35:  21467       43440
#> 36:    917        8507
#> 37:   1800        8507
#> 38:   1879        8507
#> 39:   1800        8507
#> 40:   6628        8507
#> 41:   1408        9151
#> 42:    411        9151
#> 43:    886        9151
#> 44:   1580        9151
#> 45:   1155        9151
#> 46:   2152        9151
#> 47:   6416        9151
#>        est wtd_group_n
#> 
#> $weight_name
#> [1] "person_weight"
#> 

DT = hts_prep_variable(
  summarize_var = "employment",
  summarize_by = c("race", "income_detailed", "gender"),
  variables_dt = variable_list,
  data = list(
    "hh" = hh,
    "person" = person,
    "day" = day,
    "trip" = trip,
    "vehicle" = vehicle
  )
)$cat

hts_summary_cat(
  prepped_dt = DT,
  summarize_var = "employment",
  summarize_by = c("race", "income_detailed", "gender"),
  wtname = "person_weight"
)$unwtd
#>                           race income_detailed gender employment count
#>   1: African American or Black               1      1          6     1
#>   2: African American or Black               4      2          5     1
#>   3: African American or Black               5      4          5     1
#>   4: African American or Black               6      4          6     1
#>   5: African American or Black               6    999          1     2
#>  ---                                                                  
#> 394:      Prefer not to answer             999    999          2     1
#> 395:      Prefer not to answer             999    999          3     2
#> 396:      Prefer not to answer             999    999          5     7
#> 397:      Prefer not to answer             999    999          7     1
#> 398:      Prefer not to answer             999    999          8     2
#>            prop
#>   1: 1.00000000
#>   2: 1.00000000
#>   3: 1.00000000
#>   4: 1.00000000
#>   5: 0.66666667
#>  ---           
#> 394: 0.04347826
#> 395: 0.08695652
#> 396: 0.30434783
#> 397: 0.04347826
#> 398: 0.08695652