Skip to contents

Calculate proportion from a survey design

Usage

create_analysis_prop_select_multiple(
  design,
  group_var = NA,
  analysis_var,
  level = 0.95,
  sm_separator = "."
)

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. It has to be the parent select multiple

level

the confidence level. 0.95 is default

sm_separator

Select multiple separator. Default is "."

Value

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

Examples

somedata <- data.frame(
  groups = sample(c("group_a", "group_b"), size = 100, replace = TRUE),
  smvar = rep(NA_character_, 100),
  smvar.option1 = sample(c(TRUE, FALSE), size = 100, replace = TRUE, prob = c(.7, .3)),
  smvar.option2 = sample(c(TRUE, FALSE), size = 100, replace = TRUE, prob = c(.6, .4)),
  smvar.option3 = sample(c(TRUE, FALSE), size = 100, replace = TRUE, prob = c(.1, .9)),
  smvar.option4 = sample(c(TRUE, FALSE), size = 100, replace = TRUE, prob = c(.8, .2)),
  uuid = 1:100 %>% as.character()
) %>%
  cleaningtools::recreate_parent_column(uuid = "uuid", sm_separator = ".")
#> Joining with `by = join_by(uuid)`
#> groups
#> smvar
#> smvar.option1
#> smvar.option2
#> smvar.option3
#> smvar.option4
#> groups
#> smvar.option1
#> smvar.option2
#> smvar.option3
#> smvar.option4
#> groups
#> smvar
#> smvar.option1
#> smvar.option2
#> smvar.option3
#> smvar.option4

somedata <- somedata$data_with_fix_concat

create_analysis_prop_select_multiple(srvyr::as_survey(somedata),
  group_var = NA,
  analysis_var = "smvar",
  level = 0.95
)
#> # A tibble: 5 × 13
#>   analysis_type analysis_var analysis_var_value group_var group_var_value   stat
#>   <chr>         <chr>        <chr>              <chr>     <chr>            <dbl>
#> 1 prop_select_… smvar        option1            NA        NA               0.625
#> 2 prop_select_… smvar        option2            NA        NA               0.562
#> 3 prop_select_… smvar        option3            NA        NA               0.115
#> 4 prop_select_… smvar        option4            NA        NA               0.854
#> 5 prop_select_… smvar        NA                 NA        NA              NA    
#> # ℹ 7 more variables: stat_low <dbl>, stat_upp <dbl>, n <dbl>, n_total <dbl>,
#> #   n_w <dbl>, n_w_total <dbl>, analysis_key <chr>

create_analysis_prop_select_multiple(srvyr::as_survey(somedata),
  group_var = "groups",
  analysis_var = "smvar",
  level = 0.95
)
#> # A tibble: 10 × 13
#>    analysis_type       analysis_var analysis_var_value group_var group_var_value
#>    <chr>               <chr>        <chr>              <chr>     <chr>          
#>  1 prop_select_multip… smvar        option1            groups    group_a        
#>  2 prop_select_multip… smvar        option2            groups    group_a        
#>  3 prop_select_multip… smvar        option3            groups    group_a        
#>  4 prop_select_multip… smvar        option4            groups    group_a        
#>  5 prop_select_multip… smvar        NA                 groups    group_a        
#>  6 prop_select_multip… smvar        option1            groups    group_b        
#>  7 prop_select_multip… smvar        option2            groups    group_b        
#>  8 prop_select_multip… smvar        option3            groups    group_b        
#>  9 prop_select_multip… smvar        option4            groups    group_b        
#> 10 prop_select_multip… smvar        NA                 groups    group_b        
#> # ℹ 8 more variables: stat <dbl>, stat_low <dbl>, stat_upp <dbl>, n <dbl>,
#> #   n_total <dbl>, n_w <dbl>, n_w_total <dbl>, analysis_key <chr>