Skip to contents

Summarize the sample

srv_cont_stats() summarizes numeric variables. For this example, convert the ordered news-response categories into scores from 1 to 4, with higher scores representing greater interest or trust.

data <- example_srv_main |>
  mutate(
    news_interest_score = 5 - as.numeric(m1_news_interest),
    news_trust_score = 5 - as.numeric(m1_news_trust)
  )

attr(data$news_interest_score, "label") <- "Interest in following news"
attr(data$news_trust_score, "label") <- "Trust in news media"

Select the score columns with sbj_cols and supply a weight column for weighted results.

overall_stats <- srv_cont_stats(
  data,
  sbj_cols = c(news_interest_score, news_trust_score),
  weight_col = weight
)

overall_stats
#> # A tibble: 2 × 11
#>   sbj_col       sbj_label     n     N  mean median   q05   q95    sd   min   max
#>   <chr>         <chr>     <int> <dbl> <dbl>  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 news_interes… Interest…   400   400  2.62      3     1     4  1.02     1     4
#> 2 news_trust_s… Trust in…   400   400  2.70      3     1     4  1.00     1     4

Each variable produces one row with the weighted mean, median, standard deviation, 5th and 95th percentiles (q05 and q95), and observed min and max. Quantiles use the first observed value where cumulative weight reaches the requested proportion. n counts valid respondents, while N is the weighted base. Omit weight_col to give each respondent equal weight.

By default, missing values are removed separately for each variable. Set na.rm = FALSE to retain them.

Summarize a subgroup

A cohort identifies a group of respondents by their IDs and stores their analysis weights. Create one for female respondents, then use it to summarize their scores.

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

female_stats <- srv_cont_stats(
  data,
  sbj_cols = c(news_interest_score, news_trust_score),
  cohorts = cohort_female,
  id_col = response_id
)

female_stats
#> # A tibble: 2 × 11
#>   sbj_col       sbj_label     n     N  mean median   q05   q95    sd   min   max
#>   <chr>         <chr>     <int> <dbl> <dbl>  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 news_interes… Interest…   215  216.  2.85      3     1     4 0.949     1     4
#> 2 news_trust_s… Trust in…   215  216.  2.98      3     1     4 0.877     1     4

id_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_cont_stats() uses the first cohort as the reference, which is the whole sample in this example.

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

gender_stats <- srv_cont_stats(
  data,
  sbj_cols = c(news_interest_score, news_trust_score),
  cohorts = cohort_set_gender,
  id_col = response_id
)

gender_stats |>
  select(
    label, level, sbj_label, n, mean,
    ref_mean, diff, pval, .is_reference
  )
#> # A tibble: 6 × 9
#>   label       level sbj_label     n  mean ref_mean   diff     pval .is_reference
#>   <chr>       <chr> <chr>     <int> <dbl>    <dbl>  <dbl>    <dbl> <lgl>        
#> 1 Overall po… popu… Interest…   400  2.62     2.62  0     NA       TRUE         
#> 2 Overall po… popu… Trust in…   400  2.70     2.70  0     NA       TRUE         
#> 3 Gender      Male  Interest…   185  2.35     2.62 -0.272  3.55e-3 FALSE        
#> 4 Gender      Male  Trust in…   185  2.38     2.70 -0.319  7.46e-4 FALSE        
#> 5 Gender      Fema… Interest…   215  2.85     2.62  0.232  5.76e-3 FALSE        
#> 6 Gender      Fema… Trust in…   215  2.98     2.70  0.273  6.45e-4 FALSE

The result adds cohort labels and reference statistics. diff is the group mean minus ref_mean, in the score’s original units. For example, diff = 0.3 means the group averages 0.3 points above the reference.

pval is the two-sided p-value for a test of equal group and reference means. Smaller values provide stronger evidence of a difference. .is_reference identifies reference rows, which have diff = 0 and no p-value.

How comparisons work

Both the mean difference 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 test uses weighted variances and Kish effective sample sizes in a Welch comparison. 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_cont_stats_render() returns an openxlsx workbook with variables in rows and cohorts in columns. By default, it shows n, mean, median, q05, and q95. Only means receive p-value coloring.

wb <- srv_cont_stats_render(gender_stats)

Use stats to add other values or change their order.

wb_detailed <- srv_cont_stats_render(
  gender_stats,
  stats = c("n", "mean", "median", "q05", "q95", "diff", "pval")
)

The diff and pval columns show the mean difference and its test result. Mean-cell colors indicate whether the mean is higher or lower than the reference, with stronger colors at smaller p-values. Other statistics receive no p-value coloring. 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,
  "continuous-stats.xlsx",
  overwrite = TRUE
)