Skip to contents

proportion tests

  • against overall proportions or against a reference category (srv_prop_test_ref)

  • within categories (srv_prop_test_within)

Usage

srv_prop_test_ref(
  data,
  grp_var,
  sbj_var,
  wt = NULL,
  overall = TRUE,
  overall_name = "overall",
  reference = NULL,
  p_adjust = "fdr"
)

srv_prop_test_within(data, grp_var, sbj_var, wt = NULL, p_adjust = "fdr")

Arguments

data

A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr).

grp_var

quoted or unquoted grouping variable

sbj_var

quoted or unquoted variable to calculate proportion statistics for

wt

Unquoted weight variable

overall

Logical. Should the proportions be tested against the overall proportion of the group?

overall_name

Character. Name of the overall category to append to your data (e.g. "Full Sample").

reference

Character, optional. Name of the the y category to be used as a reference to compare proportions to if overall is not TRUE. If reference is not TRUE and the number of y categories equals 2, it is possible to leave this parameter NULL to perform between-categories proportion test.

p_adjust

Character. Should the p.value be adjusted? Use methods provided by p.adjust

Details

The data is split by categories of grp_var variable and within each of the grp_var categories, proportions of sbj_var variable categories are calculated and proportion confidence intervals are assessed. The two functions differ in the way proportion pvalues are calculated.

srv_prop_test_ref

By default overall parameter equals TRUE. This means that x category proportions will also be calculated for overall data. In case if reference category is not provided, these proportions will be used as a reference to compare other group proportions with.

If you specify the name of the reference grp_var category, then proportions inside other categories will be compared to proportions inside this category.

If overall is FALSE, reference is not provided, and grp_var has only 2 categories, these categories will be compared between each other. If the number of categories is not exactly 2, the function will throw an error.

In all cases stats::prop.test is used for proportion comparison.

srv_prop_test_within

Proportions of sbj_var categories are compared within each of the grp_var categories using stats::chisq.test.

Examples

library(g6tr.survey)
library(dplyr)
library(titanic)
df <- titanic::titanic_test %>%
  as_tibble() %>%
  mutate(across(Embarked, as.factor))

# compare to overall
srv_prop_test_ref(df, Embarked, Sex, overall_name = "All")
#> # A tibble: 8 × 9
#>   Embarked sum_n Sex        n percentage prop_pval prop_pval_adj std_error
#>   <fct>    <int> <fct>  <int>      <dbl>     <dbl>         <dbl>     <dbl>
#> 1 C          102 female    40      0.392    0.674          0.899    0.0483
#> 2 C          102 male      62      0.608    0.674          0.899    0.0483
#> 3 Q           46 female    24      0.522    0.0527         0.211    0.0737
#> 4 Q           46 male      22      0.478    0.0527         0.211    0.0737
#> 5 S          270 female    88      0.326    0.352          0.703    0.0285
#> 6 S          270 male     182      0.674    0.352          0.703    0.0285
#> 7 All        418 female   152      0.364    1              1        0.0235
#> 8 All        418 male     266      0.636    1              1        0.0235
#> # ℹ 1 more variable: conf_int <dbl>

# compare to a reference category
srv_prop_test_ref(df, Embarked, Sex, overall = FALSE, reference = "S")
#> # A tibble: 6 × 9
#>   Embarked sum_n Sex        n percentage prop_pval prop_pval_adj std_error
#>   <fct>    <int> <fct>  <int>      <dbl>     <dbl>         <dbl>     <dbl>
#> 1 C          102 female    40      0.392    0.281         0.422     0.0483
#> 2 C          102 male      62      0.608    0.281         0.422     0.0483
#> 3 Q           46 female    24      0.522    0.0164        0.0492    0.0737
#> 4 Q           46 male      22      0.478    0.0164        0.0492    0.0737
#> 5 S          270 female    88      0.326    1             1         0.0285
#> 6 S          270 male     182      0.674    1             1         0.0285
#> # ℹ 1 more variable: conf_int <dbl>

# tries to perform test between categories
# throws an error because there are more than 2 Embarked categories
if (FALSE) { # \dontrun{
srv_prop_test_ref(df, Embarked, Sex, overall = FALSE)
} # }

# perform test between two categories
srv_prop_test_ref(df, Sex, Embarked, overall = FALSE)
#> ! Note: overall != TRUE and a reference category wasn't supplied.
#> ℹ Performing proportion test between two categories.
#> # A tibble: 6 × 9
#>   Sex    sum_n Embarked     n percentage prop_pval prop_pval_adj std_error
#>   <fct>  <int> <fct>    <int>      <dbl>     <dbl>         <dbl>     <dbl>
#> 1 female   152 C           40     0.263     0.568         0.568     0.0357
#> 2 female   152 Q           24     0.158     0.0278        0.0593    0.0296
#> 3 female   152 S           88     0.579     0.0395        0.0593    0.0400
#> 4 male     266 C           62     0.233     0.568         0.568     0.0259
#> 5 male     266 Q           22     0.0827    0.0278        0.0593    0.0169
#> 6 male     266 S          182     0.684     0.0395        0.0593    0.0285
#> # ℹ 1 more variable: conf_int <dbl>

# test within categories
srv_prop_test_within(df, Sex, Embarked)
#> # A tibble: 6 × 9
#>   Sex    sum_n Embarked     n percentage prop_pval prop_pval_adj std_error
#>   <fct>  <int> <fct>    <int>      <dbl>     <dbl>         <dbl>     <dbl>
#> 1 female   152 C           40     0.263   3.10e-10      3.10e-10    0.0215
#> 2 female   152 Q           24     0.158   3.10e-10      3.10e-10    0.0178
#> 3 female   152 S           88     0.579   3.10e-10      3.10e-10    0.0241
#> 4 male     266 C           62     0.233   1.10e-34      2.19e-34    0.0207
#> 5 male     266 Q           22     0.0827  1.10e-34      2.19e-34    0.0135
#> 6 male     266 S          182     0.684   1.10e-34      2.19e-34    0.0227
#> # ℹ 1 more variable: conf_int <dbl>
srv_prop_test_within(df, Embarked, Sex)
#> # A tibble: 6 × 9
#>   Embarked sum_n Sex        n percentage    prop_pval prop_pval_adj std_error
#>   <fct>    <int> <fct>  <int>      <dbl>        <dbl>         <dbl>     <dbl>
#> 1 C          102 female    40      0.392 0.0294        0.0441          0.0239
#> 2 C          102 male      62      0.608 0.0294        0.0441          0.0239
#> 3 Q           46 female    24      0.522 0.768         0.768           0.0244
#> 4 Q           46 male      22      0.478 0.768         0.768           0.0244
#> 5 S          270 female    88      0.326 0.0000000106  0.0000000318    0.0229
#> 6 S          270 male     182      0.674 0.0000000106  0.0000000318    0.0229
#> # ℹ 1 more variable: conf_int <dbl>

# Easy to use in a chart to plot significant differences
# with confidence intervals:
if (FALSE) { # \dontrun{
library(g6tr.theme)
prop_test_df <-
  df %>%
  srv_prop_test_ref(Embarked, Sex) %>%
  # dirty hack
  mutate(prop_pval = prop_pval / 2)

prop_test_df %>%
  g6tr_stacked_bar_plot(
    Embarked, percentage, Sex,
    sig = prop_pval, flip = TRUE
  )

prop_test_df %>%
  g6tr_dodged_bar_plot(
    Embarked, percentage, Sex,
    sig = prop_pval, conf_int = conf_int
  )
} # }