Calculates full contingent statistics in the manner of srvyr::cascade()
.
Details
When calculating a two-way contingency table (a.k.a. crosstab),
srvyr::cascade()
calculates proportions of the column variable for each level
of the row variable, but calculates the proportion of the row variable for
the overall sample. This makes two-way contingency tables difficult with
srvyr::cascade()
.
bhhi_cascade()
instead calculates the proportion of the column variable for
the overall sample, which produces results that are compatible and comparable
to the results for the levels of the row variable
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 |>
srvyr::group_by(gender, race) |>
bhhi_cascade(srvyr::survey_prop(proportion = TRUE))
#> # A tibble: 12 × 4
#> # Groups: gender [3]
#> gender race coef `_se`
#> <fct> <fct> <dbl> <dbl>
#> 1 Male Hispanic 0.158 0.00507
#> 2 Male White 0.662 0.00818
#> 3 Male Black 0.111 0.00432
#> 4 Male Other 0.0682 0.00520
#> 5 Female Hispanic 0.143 0.00443
#> 6 Female White 0.653 0.00805
#> 7 Female Black 0.127 0.00471
#> 8 Female Other 0.0769 0.00536
#> 9 Overall Hispanic 0.151 0.00336
#> 10 Overall White 0.657 0.00574
#> 11 Overall Black 0.119 0.00321
#> 12 Overall Other 0.0726 0.00374
survey_object |>
srvyr::group_by(gender, race) |>
srvyr::cascade(srvyr::survey_prop(proportion = TRUE), .fill = "Overall")
#> Warning: There was 1 warning in `dplyr::summarise()`.
#> ℹ In argument: `srvyr::survey_prop(proportion = TRUE)`.
#> Caused by warning:
#> ! glm.fit: algorithm did not converge
#> # A tibble: 11 × 4
#> # Groups: gender [3]
#> gender race coef `_se`
#> <fct> <fct> <dbl> <dbl>
#> 1 Male Hispanic 0.158 5.07e- 3
#> 2 Male White 0.662 8.18e- 3
#> 3 Male Black 0.111 4.32e- 3
#> 4 Male Other 0.0682 5.20e- 3
#> 5 Male Overall 0.488 6.82e- 3
#> 6 Female Hispanic 0.143 4.43e- 3
#> 7 Female White 0.653 8.05e- 3
#> 8 Female Black 0.127 4.71e- 3
#> 9 Female Other 0.0769 5.36e- 3
#> 10 Female Overall 0.512 6.82e- 3
#> 11 Overall Overall 1.00 9.26e-19