Skip to contents

Calculate proportion statistics for levels of one categorical variable (sbj_var) within groups provided by another categorical variable (grp_var). Statistics include:

  • proportion index with respect to overall proportion

  • proportion pvalue showing how significantly proportions in group differs from proportion in overall population

Usage

srv_prop_stats(data, grp_var, sbj_var, wt = NULL, na.rm = FALSE)

Arguments

data

a dataframe

grp_var

quoted or unquoted grouping variable

sbj_var

quoted or unquoted variable to calculate proportion statistics for

wt

optional weight variable

na.rm

drop subject variable NA categories before calculating percentages? By default is FALSE

Value

A data.frame containing three columns: grp_var,sbj_var and index. grp_var, sbj_var are always converted to factor if they were not initially.

Details

Category indices inside a group show how level percentages inside this group differ from such in overall population. Namely, if sbj_var has {n} categories and percentages of these categories inside group number \(k\) of grp_var are \(p_{k, 1}, p_{k, 2}, ... p_{k, n}\) then indices are calculated as: \(ind_{k, i} = 100*p_{k, i}/P_i\) where \(P_i\) is percentage of i-th category of sbj_var in overall population.

Examples

srv_prop_stats(mtcars, cyl, carb)
#> Warning: There were 18 warnings in `mutate()`.
#> The first warning was:
#> ℹ In argument: `pval = safe_prop_test_pval(x = c(n, overall_n), n = c(sum_n,
#>   overall_sum_n))`.
#> ℹ In row 1.
#> Caused by warning in `.f()`:
#> ! Chi-squared approximation may be incorrect
#> ℹ Run `dplyr::last_dplyr_warnings()` to see the 17 remaining warnings.
#> # A tibble: 18 × 10
#>    cyl   sum_n carb      n percentage overall_n overall_sum_n overall_percentage
#>    <fct> <int> <fct> <int>      <dbl>     <int>         <int>              <dbl>
#>  1 4        11 1         5     0.455          7            32             0.219 
#>  2 4        11 2         6     0.545         10            32             0.312 
#>  3 4        11 3         0     0              3            32             0.0938
#>  4 4        11 4         0     0             10            32             0.312 
#>  5 4        11 6         0     0              1            32             0.0312
#>  6 4        11 8         0     0              1            32             0.0312
#>  7 6         7 1         2     0.286          7            32             0.219 
#>  8 6         7 2         0     0             10            32             0.312 
#>  9 6         7 3         0     0              3            32             0.0938
#> 10 6         7 4         4     0.571         10            32             0.312 
#> 11 6         7 6         1     0.143          1            32             0.0312
#> 12 6         7 8         0     0              1            32             0.0312
#> 13 8        14 1         0     0              7            32             0.219 
#> 14 8        14 2         4     0.286         10            32             0.312 
#> 15 8        14 3         3     0.214          3            32             0.0938
#> 16 8        14 4         6     0.429         10            32             0.312 
#> 17 8        14 6         0     0              1            32             0.0312
#> 18 8        14 8         1     0.0714         1            32             0.0312
#> # ℹ 2 more variables: index <dbl>, pval <dbl>

# pass one or two variables quoted
srv_prop_stats(mtcars, "cyl", carb)
#> Warning: There were 18 warnings in `mutate()`.
#> The first warning was:
#> ℹ In argument: `pval = safe_prop_test_pval(x = c(n, overall_n), n = c(sum_n,
#>   overall_sum_n))`.
#> ℹ In row 1.
#> Caused by warning in `.f()`:
#> ! Chi-squared approximation may be incorrect
#> ℹ Run `dplyr::last_dplyr_warnings()` to see the 17 remaining warnings.
#> # A tibble: 18 × 10
#>    cyl   sum_n carb      n percentage overall_n overall_sum_n overall_percentage
#>    <fct> <int> <fct> <int>      <dbl>     <int>         <int>              <dbl>
#>  1 4        11 1         5     0.455          7            32             0.219 
#>  2 4        11 2         6     0.545         10            32             0.312 
#>  3 4        11 3         0     0              3            32             0.0938
#>  4 4        11 4         0     0             10            32             0.312 
#>  5 4        11 6         0     0              1            32             0.0312
#>  6 4        11 8         0     0              1            32             0.0312
#>  7 6         7 1         2     0.286          7            32             0.219 
#>  8 6         7 2         0     0             10            32             0.312 
#>  9 6         7 3         0     0              3            32             0.0938
#> 10 6         7 4         4     0.571         10            32             0.312 
#> 11 6         7 6         1     0.143          1            32             0.0312
#> 12 6         7 8         0     0              1            32             0.0312
#> 13 8        14 1         0     0              7            32             0.219 
#> 14 8        14 2         4     0.286         10            32             0.312 
#> 15 8        14 3         3     0.214          3            32             0.0938
#> 16 8        14 4         6     0.429         10            32             0.312 
#> 17 8        14 6         0     0              1            32             0.0312
#> 18 8        14 8         1     0.0714         1            32             0.0312
#> # ℹ 2 more variables: index <dbl>, pval <dbl>
srv_prop_stats(mtcars, "cyl", "carb")
#> Warning: There were 18 warnings in `mutate()`.
#> The first warning was:
#> ℹ In argument: `pval = safe_prop_test_pval(x = c(n, overall_n), n = c(sum_n,
#>   overall_sum_n))`.
#> ℹ In row 1.
#> Caused by warning in `.f()`:
#> ! Chi-squared approximation may be incorrect
#> ℹ Run `dplyr::last_dplyr_warnings()` to see the 17 remaining warnings.
#> # A tibble: 18 × 10
#>    cyl   sum_n carb      n percentage overall_n overall_sum_n overall_percentage
#>    <fct> <int> <fct> <int>      <dbl>     <int>         <int>              <dbl>
#>  1 4        11 1         5     0.455          7            32             0.219 
#>  2 4        11 2         6     0.545         10            32             0.312 
#>  3 4        11 3         0     0              3            32             0.0938
#>  4 4        11 4         0     0             10            32             0.312 
#>  5 4        11 6         0     0              1            32             0.0312
#>  6 4        11 8         0     0              1            32             0.0312
#>  7 6         7 1         2     0.286          7            32             0.219 
#>  8 6         7 2         0     0             10            32             0.312 
#>  9 6         7 3         0     0              3            32             0.0938
#> 10 6         7 4         4     0.571         10            32             0.312 
#> 11 6         7 6         1     0.143          1            32             0.0312
#> 12 6         7 8         0     0              1            32             0.0312
#> 13 8        14 1         0     0              7            32             0.219 
#> 14 8        14 2         4     0.286         10            32             0.312 
#> 15 8        14 3         3     0.214          3            32             0.0938
#> 16 8        14 4         6     0.429         10            32             0.312 
#> 17 8        14 6         0     0              1            32             0.0312
#> 18 8        14 8         1     0.0714         1            32             0.0312
#> # ℹ 2 more variables: index <dbl>, pval <dbl>

# NA levels in sbj_var
library(dplyr)
df <- mutate(mtcars, carb = ifelse(carb == 3, NA, carb))
# don't do anything specific
srv_prop_stats(df, cyl, carb)
#> Warning: There were 16 warnings in `mutate()`.
#> The first warning was:
#> ℹ In argument: `pval = safe_prop_test_pval(x = c(n, overall_n), n = c(sum_n,
#>   overall_sum_n))`.
#> ℹ In row 1.
#> Caused by warning in `.f()`:
#> ! Chi-squared approximation may be incorrect
#> ℹ Run `dplyr::last_dplyr_warnings()` to see the 15 remaining warnings.
#> # A tibble: 16 × 10
#>    cyl   sum_n carb      n percentage overall_n overall_sum_n overall_percentage
#>    <fct> <int> <fct> <int>      <dbl>     <int>         <int>              <dbl>
#>  1 4        11 1         5     0.455          7            32             0.219 
#>  2 4        11 2         6     0.545         10            32             0.312 
#>  3 4        11 4         0     0             10            32             0.312 
#>  4 4        11 6         0     0              1            32             0.0312
#>  5 4        11 8         0     0              1            32             0.0312
#>  6 6         7 1         2     0.286          7            32             0.219 
#>  7 6         7 2         0     0             10            32             0.312 
#>  8 6         7 4         4     0.571         10            32             0.312 
#>  9 6         7 6         1     0.143          1            32             0.0312
#> 10 6         7 8         0     0              1            32             0.0312
#> 11 8        14 1         0     0              7            32             0.219 
#> 12 8        14 2         4     0.286         10            32             0.312 
#> 13 8        14 4         6     0.429         10            32             0.312 
#> 14 8        14 6         0     0              1            32             0.0312
#> 15 8        14 8         1     0.0714         1            32             0.0312
#> 16 8        14 NA        3     0.214          3            32             0.0938
#> # ℹ 2 more variables: index <dbl>, pval <dbl>
# drop NA before calculations
srv_prop_stats(df, cyl, carb, na.rm = TRUE)
#> Warning: There were 15 warnings in `mutate()`.
#> The first warning was:
#> ℹ In argument: `pval = safe_prop_test_pval(x = c(n, overall_n), n = c(sum_n,
#>   overall_sum_n))`.
#> ℹ In row 1.
#> Caused by warning in `.f()`:
#> ! Chi-squared approximation may be incorrect
#> ℹ Run `dplyr::last_dplyr_warnings()` to see the 14 remaining warnings.
#> # A tibble: 15 × 10
#>    cyl   sum_n carb      n percentage overall_n overall_sum_n overall_percentage
#>    <fct> <int> <fct> <int>      <dbl>     <int>         <int>              <dbl>
#>  1 4        11 1         5     0.455          7            29             0.241 
#>  2 4        11 2         6     0.545         10            29             0.345 
#>  3 4        11 4         0     0             10            29             0.345 
#>  4 4        11 6         0     0              1            29             0.0345
#>  5 4        11 8         0     0              1            29             0.0345
#>  6 6         7 1         2     0.286          7            29             0.241 
#>  7 6         7 2         0     0             10            29             0.345 
#>  8 6         7 4         4     0.571         10            29             0.345 
#>  9 6         7 6         1     0.143          1            29             0.0345
#> 10 6         7 8         0     0              1            29             0.0345
#> 11 8        11 1         0     0              7            29             0.241 
#> 12 8        11 2         4     0.364         10            29             0.345 
#> 13 8        11 4         6     0.545         10            29             0.345 
#> 14 8        11 6         0     0              1            29             0.0345
#> 15 8        11 8         1     0.0909         1            29             0.0345
#> # ℹ 2 more variables: index <dbl>, pval <dbl>