Summarize the sample
srv_prop_stats() summarizes categorical variables.
Select one or more columns with sbj_cols and supply a
weight column for weighted results.
overall_stats <- srv_prop_stats(
example_srv_main,
sbj_cols = c(m1_news_interest, m1_news_trust),
weight_col = weight
)
overall_stats
#> # 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.151The result contains one row for each response level. n
is the weighted count, N is the weighted base, and
percentage is n / N. Omit
weight_col to give each respondent equal weight.
By default, missing responses form a separate level. Set
na.rm = TRUE to remove missing responses from the base of
each subject column.
Summarize a subgroup
A cohort identifies a group of respondents by their IDs and stores
their analysis weights. Create one for female respondents, then pass it
to srv_prop_stats() to summarize that group.
cohort_female <- example_srv_main |>
filter(d_gender == "Female") |>
g6tr.cohorts::create_cohort(
id_col = response_id,
weight_col = weight
)
female_stats <- srv_prop_stats(
example_srv_main,
sbj_cols = m1_news_interest,
cohorts = cohort_female,
id_col = response_id
)
female_stats
#> # 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.107id_col connects the cohort to the survey data. Weights
now come from cohort_female, so omit
weight_col. The result has the same summary columns as the
whole-sample result.
Compare groups
To summarize several groups together, create a cohort set. Here,
create_cohort_set() includes the whole sample and each
gender group. srv_prop_stats() uses the first cohort as the
reference, which is the whole sample in this example.
cohort_set_gender <- g6tr.cohorts::create_cohort_set(
example_srv_main,
id_col = response_id,
grp_cols = d_gender,
weight_col = weight
)
gender_stats <- srv_prop_stats(
example_srv_main,
sbj_cols = m1_news_interest,
cohorts = cohort_set_gender,
id_col = response_id
)
gender_stats |>
select(
label, level, sbj_level, n, percentage,
ref_percentage, index, pval, .is_reference
)
#> # A tibble: 12 × 9
#> label level sbj_level n percentage ref_percentage index pval
#> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 Overall popula… popu… Very int… 88.3 0.221 0.221 100 NA
#> 2 Overall popula… popu… Somewhat… 142. 0.354 0.354 100 NA
#> 3 Overall popula… popu… Not very… 99.6 0.249 0.249 100 NA
#> 4 Overall popula… popu… Not at a… 70.5 0.176 0.176 100 NA
#> 5 Gender Male Very int… 28.2 0.153 0.221 69.4 0.0614
#> 6 Gender Male Somewhat… 55.1 0.299 0.354 84.4 0.197
#> 7 Gender Male Not very… 53.5 0.290 0.249 117. 0.299
#> 8 Gender Male Not at a… 47.5 0.258 0.176 146. 0.0250
#> 9 Gender Fema… Very int… 60.1 0.279 0.221 126. 0.116
#> 10 Gender Fema… Somewhat… 86.6 0.401 0.354 113. 0.257
#> 11 Gender Fema… Not very… 46.1 0.214 0.249 85.8 0.334
#> 12 Gender Fema… Not at a… 23.0 0.107 0.176 60.5 0.0242
#> # ℹ 1 more variable: .is_reference <lgl>The result adds cohort labels and reference values:
ref_n, ref_N, and ref_percentage.
An index of 120 means the group’s percentage is 20% higher
than the reference percentage. .is_reference identifies
reference rows, which have an index of 100 and no p-value.
How comparisons work
Both the index and the p-value compare a group directly with the full reference population. In this example, each gender group is compared with the overall sample.
The proportion test uses a normal approximation and Kish effective sample sizes:
effective N = sum(weight)^2 / sum(weight^2)
Unequal weights reduce the effective sample size. Multiplying all weights by a constant leaves the p-values unchanged. The test assumes independent respondents and does not account for survey strata or clusters.
Export a workbook
srv_prop_stats_render() returns an openxlsx
workbook with response levels in rows and cohorts in columns. By
default, it shows counts and percentages.
wb <- srv_prop_stats_render(gender_stats)Use stats to select the statistics and their order.
wb_detailed <- srv_prop_stats_render(
gender_stats,
stats = c("n", "percentage", "index", "pval")
)The workbook highlights reference values and differences from the
reference. Headers use the cohort label and
level columns. Use hierarchy_cols to choose
other header columns when working with custom cohort metadata.
Save the workbook with openxlsx::saveWorkbook().
openxlsx::saveWorkbook(
wb_detailed,
"prop-stats.xlsx",
overwrite = TRUE
)