Skip to contents

Calculate weighted counts and percentages for one or many subject columns within cohorts.

Usage

srv_prop_stats(
  data,
  sbj_cols,
  cohorts = NULL,
  id_col = NULL,
  weight_col = NULL,
  na.rm = FALSE
)

# S3 method for class '`NULL`'
srv_prop_stats(
  data,
  sbj_cols,
  cohorts = NULL,
  id_col = NULL,
  weight_col = NULL,
  na.rm = FALSE
)

# S3 method for class 'cohort'
srv_prop_stats(
  data,
  sbj_cols,
  cohorts,
  id_col = NULL,
  weight_col = NULL,
  na.rm = FALSE
)

# S3 method for class 'cohort_set'
srv_prop_stats(
  data,
  sbj_cols,
  cohorts,
  id_col = NULL,
  weight_col = NULL,
  na.rm = FALSE
)

Arguments

data

A data frame.

sbj_cols

Tidy-select expression for subject columns to summarize.

cohorts

NULL, a single cohort, or a cohort_set.

id_col

Tidy-select expression for the respondent ID column. Required when cohorts is not NULL. Optional otherwise.

weight_col

Tidy-select expression for the weight column, used only when cohorts = NULL.

na.rm

Logical. Drop missing subject levels before calculating percentages? Default is FALSE.

Value

A tibble with one row per cohort x subject-column level. For cohorts = NULL and a single cohort, the output contains only subject summary columns. For a cohort_set, the cohort-set metadata columns are carried into the output as-is, followed by subject summaries and reference comparison columns.

Details

When cohorts is a cohort_set, the first cohort in the set is used as the reference cohort and additional comparison columns are appended with ref_ prefixes, plus index and pval. Comparisons with a zero base or zero standard error return NA p-values.

Columns of class multiple_response produce one row per response option, counting only selected answers. The base includes respondents who did not select that option, so percentages can sum above 100%. With na.rm = TRUE, each option excludes its missing answers. These summaries also include question_N, the weighted question base used for workbook total rows. It excludes entirely missing MR responses when na.rm = TRUE.

Examples

srv_prop_stats(
  example_srv_main,
  sbj_cols = c(m1_news_interest, m1_news_trust),
  weight_col = weight
)
#> # A tibble: 8 × 6
#>   sbj_col          sbj_label                    sbj_level     n     N percentage
#>   <chr>            <chr>                        <chr>     <dbl> <dbl>      <dbl>
#> 1 m1_news_interest Module 1: Interest in follo… Very int…  88.3   400      0.221
#> 2 m1_news_interest Module 1: Interest in follo… Somewhat… 142.    400      0.354
#> 3 m1_news_interest Module 1: Interest in follo… Not very…  99.6   400      0.249
#> 4 m1_news_interest Module 1: Interest in follo… Not at a…  70.5   400      0.176
#> 5 m1_news_trust    Module 1: Trust in news med… A lot      99.1   400      0.248
#> 6 m1_news_trust    Module 1: Trust in news med… Some      144.    400      0.360
#> 7 m1_news_trust    Module 1: Trust in news med… Not much   96.3   400      0.241
#> 8 m1_news_trust    Module 1: Trust in news med… Not at a…  60.5   400      0.151

female_cohort <- example_srv_main |>
  dplyr::filter(d_gender == "Female") |>
  g6tr.cohorts::create_cohort(id_col = response_id, weight_col = weight)

srv_prop_stats(
  example_srv_main,
  sbj_cols = m1_news_interest,
  cohorts = female_cohort,
  id_col = response_id
)
#> # A tibble: 4 × 6
#>   sbj_col          sbj_label                    sbj_level     n     N percentage
#>   <chr>            <chr>                        <chr>     <dbl> <dbl>      <dbl>
#> 1 m1_news_interest Module 1: Interest in follo… Very int…  60.1  216.      0.279
#> 2 m1_news_interest Module 1: Interest in follo… Somewhat…  86.6  216.      0.401
#> 3 m1_news_interest Module 1: Interest in follo… Not very…  46.1  216.      0.214
#> 4 m1_news_interest Module 1: Interest in follo… Not at a…  23.0  216.      0.107

gender_cohorts <- example_srv_main |>
  g6tr.cohorts::create_cohort_set(
    id_col = response_id,
    grp_cols = d_gender,
    weight_col = weight
  )

srv_prop_stats(
  example_srv_main,
  sbj_cols = m1_news_interest,
  cohorts = gender_cohorts,
  id_col = response_id
)
#> # A tibble: 12 × 15
#>    column     label  level .is_reference sbj_col sbj_label sbj_level     n     N
#>    <chr>      <chr>  <chr> <lgl>         <chr>   <chr>     <chr>     <dbl> <dbl>
#>  1 population Overa… popu… TRUE          m1_new… Module 1… Very int…  88.3  400 
#>  2 population Overa… popu… TRUE          m1_new… Module 1… Somewhat… 142.   400 
#>  3 population Overa… popu… TRUE          m1_new… Module 1… Not very…  99.6  400 
#>  4 population Overa… popu… TRUE          m1_new… Module 1… Not at a…  70.5  400 
#>  5 d_gender   Gender Male  FALSE         m1_new… Module 1… Very int…  28.2  184.
#>  6 d_gender   Gender Male  FALSE         m1_new… Module 1… Somewhat…  55.1  184.
#>  7 d_gender   Gender Male  FALSE         m1_new… Module 1… Not very…  53.5  184.
#>  8 d_gender   Gender Male  FALSE         m1_new… Module 1… Not at a…  47.5  184.
#>  9 d_gender   Gender Fema… FALSE         m1_new… Module 1… Very int…  60.1  216.
#> 10 d_gender   Gender Fema… FALSE         m1_new… Module 1… Somewhat…  86.6  216.
#> 11 d_gender   Gender Fema… FALSE         m1_new… Module 1… Not very…  46.1  216.
#> 12 d_gender   Gender Fema… FALSE         m1_new… Module 1… Not at a…  23.0  216.
#> # ℹ 6 more variables: percentage <dbl>, ref_n <dbl>, ref_N <dbl>,
#> #   ref_percentage <dbl>, index <dbl>, pval <dbl>