Skip to contents

Calculate weighted continuous statistics for one or many numeric columns.

Usage

srv_cont_stats(
  data,
  sbj_cols,
  cohorts = NULL,
  id_col = NULL,
  weight_col = NULL,
  na.rm = TRUE
)

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

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

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

Arguments

data

A data frame.

sbj_cols

Tidy-select expression for numeric subject columns.

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.

weight_col

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

na.rm

Logical. Remove missing values before calculation? The default is TRUE.

Value

A tibble with one row per cohort and subject column. All results contain n, weighted N, mean, median, q05, q95, sd, min, and max. Quantiles use the smallest observed value whose cumulative weight reaches the requested proportion. Cohort-set results also contain ref_n, ref_N, ref_mean, and ref_sd for the first cohort, plus:

  • diff: the cohort mean minus ref_mean, in the original units. Positive values indicate a higher mean than the reference.

  • pval: a two-sided weighted Welch test of equal cohort and reference means, using Kish effective sample sizes. The comparison uses the full reference cohort, including any shared respondents. Comparisons without sufficient observations or with zero variance in both sample return NA.

  • .is_reference: identifies reference rows, where diff is zero and pval is NA.

Details

When cohorts is a cohort_set, the first cohort is the reference population. Each other cohort is compared directly with that population.

Examples

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

srv_cont_stats(
  data,
  sbj_cols = c(news_interest_score, news_trust_score),
  weight_col = weight
)
#> # 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… NA          400   400  2.62      3     1     4  1.02     1     4
#> 2 news_trust_s… NA          400   400  2.70      3     1     4  1.00     1     4

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

srv_cont_stats(
  data,
  sbj_cols = news_interest_score,
  cohorts = gender_cohorts,
  id_col = response_id
)
#> # A tibble: 3 × 21
#>   column    label level .is_reference sbj_col sbj_label     n     N  mean median
#>   <chr>     <chr> <chr> <lgl>         <chr>   <chr>     <int> <dbl> <dbl>  <dbl>
#> 1 populati… Over… popu… TRUE          news_i… NA          400  400   2.62      3
#> 2 d_gender  Gend… Male  FALSE         news_i… NA          185  184.  2.35      2
#> 3 d_gender  Gend… Fema… FALSE         news_i… NA          215  216.  2.85      3
#> # ℹ 11 more variables: q05 <dbl>, q95 <dbl>, sd <dbl>, min <dbl>, max <dbl>,
#> #   ref_n <int>, ref_N <dbl>, ref_mean <dbl>, ref_sd <dbl>, diff <dbl>,
#> #   pval <dbl>