Skip to contents

g6tr.stats provides weighted statistical summaries and cohort comparisons for survey data. It separates statistical calculation from workbook rendering and uses g6tr.cohorts to define the populations being summarized.

Installation

Install from Gradient’s internal R repository using your usual repository access:

install.packages("g6tr.stats")

Proportions by cohort

Select categorical questions and define a cohort set for the demographic breaks you want. The first cohort is the reference population. The result contains weighted counts, percentages, reference values, indices, and p-values comparing each cohort directly with that reference.

library(g6tr.stats)

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

prop_stats <- srv_prop_stats(
  example_srv_main,
  sbj_cols = m1_news_interest,
  cohorts = cohort_set_gender,
  id_col = response_id
)

prop_stats |>
  dplyr::select(level, sbj_level, percentage, ref_percentage, index, pval)
#> # A tibble: 12 × 6
#>    level      sbj_level             percentage ref_percentage index    pval
#>    <chr>      <chr>                      <dbl>          <dbl> <dbl>   <dbl>
#>  1 population Very interested            0.221          0.221 100   NA
#>  2 population Somewhat interested        0.354          0.354 100   NA
#>  3 population Not very interested        0.249          0.249 100   NA
#>  4 population Not at all interested      0.176          0.176 100   NA
#>  5 Male       Very interested            0.153          0.221  69.4  0.0614
#>  6 Male       Somewhat interested        0.299          0.354  84.4  0.197
#>  7 Male       Not very interested        0.290          0.249 117.   0.299
#>  8 Male       Not at all interested      0.258          0.176 146.   0.0250
#>  9 Female     Very interested            0.279          0.221 126.   0.116
#> 10 Female     Somewhat interested        0.401          0.354 113.   0.257
#> 11 Female     Not very interested        0.214          0.249  85.8  0.334
#> 12 Female     Not at all interested      0.107          0.176  60.5  0.0242

wb <- srv_prop_stats_render(prop_stats)

For a whole-sample summary, omit cohorts and supply weight_col directly. A single cohort produces a subgroup summary without reference comparisons.

Continuous statistics

srv_cont_stats() summarizes numeric columns with weighted means, medians, 5th and 95th percentiles, standard deviations, and ranges. Cohort sets also add diff (cohort mean minus reference mean) and weighted Welch p-values.

data_scores <- dplyr::mutate(
  example_srv_main,
  interest_score = 5 - as.numeric(m1_news_interest)
)

cont_stats <- srv_cont_stats(
  data_scores,
  sbj_cols = interest_score,
  cohorts = cohort_set_gender,
  id_col = response_id
)

cont_stats |>
  dplyr::select(level, mean, median, q05, q95, diff, pval)
#> # A tibble: 3 × 7
#>   level       mean median   q05   q95   diff     pval
#>   <chr>      <dbl>  <dbl> <dbl> <dbl>  <dbl>    <dbl>
#> 1 population  2.62      3     1     4  0     NA
#> 2 Male        2.35      2     1     4 -0.272  0.00355
#> 3 Female      2.85      3     1     4  0.232  0.00576

wb <- srv_cont_stats_render(cont_stats)

The continuous renderer shows n, mean, median, q05, and q95 by default. Both renderers accept stats to select workbook columns and return an openxlsx workbook that you can save with openxlsx::saveWorkbook().

Multiple-response questions (experimental)

collect_mr() combines binary answer columns into one multiple_response question. You can summarize it alongside ordinary single-response columns:

data_mr <- collect_mr(
  example_srv_main,
  news_sources = starts_with("m1_news_source_")
)

mixed_stats <- srv_prop_stats(
  data_mr,
  sbj_cols = c(m1_news_interest, news_sources),
  cohorts = cohort_set_gender,
  id_col = response_id
)

wb <- srv_prop_stats_render(mixed_stats)

Each MR level counts selections against a respondent base, so percentages can sum above 100%. This class is experimental and may move to another package.

For complete examples, see vignette("srv_prop_stats"), vignette("srv_cont_stats"), and vignette("multiple_response").