Skip to contents

Calculate proportion from a survey

Usage

create_analysis_prop_select_one(
  design,
  group_var = NA,
  analysis_var,
  level = 0.95
)

Arguments

design

design survey

group_var

dependent variable(s), variable to group by. If no dependent variable, it should be NA or empty string. If more than one variable, it should be one string with each variable separated by comma, e.g. "groupa, groupb" to group for groupa and groupb. NA is default for no grouping.

analysis_var

the independent variable, variable to summarise

level

the confidence level. 0.95 is default

Value

a data frame with the proportion for each group and th analysis index.

Examples

somedata <- data.frame(
  groups = sample(c("group_a", "group_b"),
    size = 100,
    replace = TRUE
  ),
  value = sample(c("a", "b", "c"),
    size = 100, replace = TRUE,
    prob = c(.6, .4, .1)
  )
)
create_analysis_prop_select_one(srvyr::as_survey(somedata, strata = groups),
  group_var = NA,
  analysis_var = "value",
  level = .95
)
#> Joining with `by = join_by(value)`
#> # A tibble: 3 × 13
#>   analysis_type  analysis_var analysis_var_value group_var group_var_value  stat
#>   <chr>          <chr>        <chr>              <chr>     <chr>           <dbl>
#> 1 prop_select_o… value        a                  NA        NA               0.47
#> 2 prop_select_o… value        b                  NA        NA               0.44
#> 3 prop_select_o… value        c                  NA        NA               0.09
#> # ℹ 7 more variables: stat_low <dbl>, stat_upp <dbl>, n <int>, n_total <dbl>,
#> #   n_w <dbl>, n_w_total <dbl>, analysis_key <chr>
create_analysis_prop_select_one(srvyr::as_survey(somedata, strata = groups),
  group_var = "groups",
  analysis_var = "value",
  level = .95
)
#> Joining with `by = join_by(groups, value)`
#> # A tibble: 6 × 13
#>   analysis_type analysis_var analysis_var_value group_var group_var_value   stat
#>   <chr>         <chr>        <chr>              <chr>     <chr>            <dbl>
#> 1 prop_select_… value        a                  groups    group_a         0.412 
#> 2 prop_select_… value        b                  groups    group_a         0.471 
#> 3 prop_select_… value        c                  groups    group_a         0.118 
#> 4 prop_select_… value        a                  groups    group_b         0.531 
#> 5 prop_select_… value        b                  groups    group_b         0.408 
#> 6 prop_select_… value        c                  groups    group_b         0.0612
#> # ℹ 7 more variables: stat_low <dbl>, stat_upp <dbl>, n <int>, n_total <dbl>,
#> #   n_w <dbl>, n_w_total <dbl>, analysis_key <chr>