Simplifies running a weighted crosstab with bhhi_cascade()
and
srvyr::survey_prop()
. Returns a tibble that can be directly customized or
formatted with bhhi_reshape_crosstab()
and bhhi_format_crosstab()
.
Usage
bhhi_crosstab(
.data,
row_var,
col_var,
pct_direction = "col",
add_n = FALSE,
vartype = c("se", "ci", "var", "cv"),
level = 0.95,
proportion = TRUE,
convert_labelled = TRUE,
na.rm = FALSE
)
Arguments
- .data
tbl A
tbl_svy
object- row_var
Variable to show in the rows in output.
- col_var
Variable to show in the columns in output.
- pct_direction
'col'
to calculate column percentages or'row'
to calculate row percentages. Defaults to'col'
.- add_n
Add cell N to output. Defaults to FALSE.
- vartype
Report variability as zero or more of: standard error ("se", default), confidence interval ("ci"), variance ("var") or coefficient of variation ("cv"). Defaults to hiding variability.
- level
(For vartype = "ci" only) A single number or vector of numbers indicating the confidence level
- proportion
Use methods to calculate the proportion that may have more accurate confidence intervals near 0 and 1. Based on
svyciprop
.- convert_labelled
If either
row_var
orcol_var
ishaven::labelled
, automatically convert to factor. Defaults to TRUE.- na.rm
Drop missing values. Defaults to FALSE.
Examples
data("nhanes", package = "survey")
survey_object <- nhanes |>
dplyr::rename(gender = RIAGENDR) |>
dplyr::mutate(
gender = factor(gender, 1:2, c("Male", "Female")),
race = factor(race, 1:4, c("Hispanic", "White", "Black", "Other"))
) |>
srvyr::as_survey(weights = WTMEC2YR)
survey_object |>
bhhi_crosstab(race, gender)
#> # A tibble: 12 × 3
#> # Groups: gender [3]
#> gender race coef
#> <fct> <fct> <dbl>
#> 1 Male Hispanic 0.158
#> 2 Male White 0.662
#> 3 Male Black 0.111
#> 4 Male Other 0.0682
#> 5 Female Hispanic 0.143
#> 6 Female White 0.653
#> 7 Female Black 0.127
#> 8 Female Other 0.0769
#> 9 Overall Hispanic 0.151
#> 10 Overall White 0.657
#> 11 Overall Black 0.119
#> 12 Overall Other 0.0726
survey_object |>
bhhi_crosstab(race, gender, pct_direction = "row", vartype = "ci")
#> # A tibble: 10 × 5
#> # Groups: race [5]
#> race gender coef `_low` `_upp`
#> <fct> <fct> <dbl> <dbl> <dbl>
#> 1 Hispanic Male 0.514 0.493 0.534
#> 2 Hispanic Female 0.486 0.466 0.507
#> 3 White Male 0.491 0.473 0.510
#> 4 White Female 0.509 0.490 0.527
#> 5 Black Male 0.456 0.430 0.482
#> 6 Black Female 0.544 0.518 0.570
#> 7 Other Male 0.458 0.406 0.511
#> 8 Other Female 0.542 0.489 0.594
#> 9 Overall Male 0.488 0.475 0.501
#> 10 Overall Female 0.512 0.499 0.525
survey_object_labelled <- survey_object |>
srvyr::mutate(
srvyr::across(c(gender, race), labelled::to_labelled)
)
survey_object_labelled |>
bhhi_gt_crosstab(race, gender)
Hispanic
White
Black
Other