Skip to contents

se_total_map() applies se_total() to a data frame for each of several grouping variables, returning a combined tibble of results.

Usage

se_total_map(data, ..., strata, weight, alpha = 0.05)

Arguments

data

A data frame or tibble.

...

One or more grouping variables. Can be passed unquoted (e.g., gender, birth_country) or programmatically using !!!syms(c("gender", "birth_country")).

strata

Unquoted or quoted name of the strata column. Defaults to zone if omitted.

weight

Unquoted or quoted name of the sampling weights column. For programmatic use with a string variable (e.g., wt <- "weights"), use !!sym(wt) in the function call.

alpha

Numeric significance level for confidence intervals. Default is 0.05 (95% CI).

Value

A tibble with results for each grouping variable, including:

variable

The name of the grouping variable.

value

The value of the grouping variable.

occ

Sample size for the group.

total

Estimated total for the group.

vhat, stand_dev

Estimated variance of the total (vhat) and its standard deviation (stand_dev, square root of the variance).

ci, ci_per, ci_l, ci_u

Confidence interval: half-width (ci), percentage of the total (ci_per), lower (ci_l) and upper (ci_u) bounds.

Details

This wrapper function allows to efficiently compute totals and confidence intervals for each grouping variable in the structural survey data in parallel.

This function iterates over each grouping variable supplied via ..., applies se_total() to the data grouped by that variable, and combines the results into a single tibble. The grouping variable is renamed to value and its name is stored in the variable column for clarity.

Examples

# Unquoted variables
se_total_map(
  nhanes,
  weight = weights,
  strata = strata,
  gender, marital_status, birth_country
)
#> # A tibble: 14 × 10
#>    variable    value   occ  total    vhat stand_dev     ci ci_per    ci_l   ci_u
#>    <chr>       <chr> <int>  <dbl>   <dbl>     <dbl>  <dbl>  <dbl>   <dbl>  <dbl>
#>  1 gender      Fema…  5079 1.62e8 7.82e12  2795884. 5.48e6   3.38  1.56e8 1.67e8
#>  2 gender      Male   4892 1.55e8 7.90e12  2810039. 5.51e6   3.56  1.49e8 1.60e8
#>  3 marital_st… Divo…   614 2.30e7 1.63e12  1278310. 2.51e6  10.9   2.05e7 2.55e7
#>  4 marital_st… Don'…     1 5.94e4 3.53e 9    59381. 1.16e5 196.   -5.70e4 1.76e5
#>  5 marital_st… Livi…   555 2.29e7 1.53e12  1237480. 2.43e6  10.6   2.04e7 2.53e7
#>  6 marital_st… Marr…  2886 1.27e8 9.39e12  3064624. 6.01e6   4.73  1.21e8 1.33e8
#>  7 marital_st… Miss…  4252 8.20e7 1.79e12  1337003. 2.62e6   3.20  7.94e7 8.46e7
#>  8 marital_st… Neve…  1048 4.20e7 2.64e12  1624603. 3.18e6   7.59  3.88e7 4.51e7
#>  9 marital_st… Refu…     2 3.25e4 5.86e 8    24211. 4.75e4 146.   -1.50e4 7.99e4
#> 10 marital_st… Sepa…   192 5.78e6 3.31e11   575051. 1.13e6  19.5   4.65e6 6.91e6
#> 11 marital_st… Wido…   421 1.39e7 8.24e11   907650. 1.78e6  12.8   1.22e7 1.57e7
#> 12 birth_coun… Miss…     2 2.97e4 4.61e 8    21460. 4.21e4 142.   -1.24e4 7.18e4
#> 13 birth_coun… Other  2236 4.78e7 1.30e12  1140910. 2.24e6   4.68  4.56e7 5.00e7
#> 14 birth_coun… US     7733 2.69e8 1.16e13  3402537. 6.67e6   2.48  2.62e8 2.75e8
# Programmatic use and quoted variables
v <- c("gender", "marital_status", "birth_country")
se_total_map(
  nhanes,
  weight = "weights",
  strata = "strata",
  !!!rlang::syms(v)
)
#> # A tibble: 14 × 10
#>    variable    value   occ  total    vhat stand_dev     ci ci_per    ci_l   ci_u
#>    <chr>       <chr> <int>  <dbl>   <dbl>     <dbl>  <dbl>  <dbl>   <dbl>  <dbl>
#>  1 gender      Fema…  5079 1.62e8 7.82e12  2795884. 5.48e6   3.38  1.56e8 1.67e8
#>  2 gender      Male   4892 1.55e8 7.90e12  2810039. 5.51e6   3.56  1.49e8 1.60e8
#>  3 marital_st… Divo…   614 2.30e7 1.63e12  1278310. 2.51e6  10.9   2.05e7 2.55e7
#>  4 marital_st… Don'…     1 5.94e4 3.53e 9    59381. 1.16e5 196.   -5.70e4 1.76e5
#>  5 marital_st… Livi…   555 2.29e7 1.53e12  1237480. 2.43e6  10.6   2.04e7 2.53e7
#>  6 marital_st… Marr…  2886 1.27e8 9.39e12  3064624. 6.01e6   4.73  1.21e8 1.33e8
#>  7 marital_st… Miss…  4252 8.20e7 1.79e12  1337003. 2.62e6   3.20  7.94e7 8.46e7
#>  8 marital_st… Neve…  1048 4.20e7 2.64e12  1624603. 3.18e6   7.59  3.88e7 4.51e7
#>  9 marital_st… Refu…     2 3.25e4 5.86e 8    24211. 4.75e4 146.   -1.50e4 7.99e4
#> 10 marital_st… Sepa…   192 5.78e6 3.31e11   575051. 1.13e6  19.5   4.65e6 6.91e6
#> 11 marital_st… Wido…   421 1.39e7 8.24e11   907650. 1.78e6  12.8   1.22e7 1.57e7
#> 12 birth_coun… Miss…     2 2.97e4 4.61e 8    21460. 4.21e4 142.   -1.24e4 7.18e4
#> 13 birth_coun… Other  2236 4.78e7 1.30e12  1140910. 2.24e6   4.68  4.56e7 5.00e7
#> 14 birth_coun… US     7733 2.69e8 1.16e13  3402537. 6.67e6   2.48  2.62e8 2.75e8