Skip to contents

Motivation

In order to compare if frequencies of levels between two or more categorical groups are different we run statistical test. There are many tests that can perform this task (e.g. Chi-squared test, G test, Fisher’s exact test). g6tr.survey package uses:

  • proportion test for between groups proportions
  • Chi square goodness of fit test for withing group proportions (srv_prop_test_within()).

Tests compare two groups of observed frequencies and returns p value which allows us to decide whether two values are similar (p value greater than 0.05) or the difference is statistically significant (p value smaller than 0.05). For instance, let’s say that we want to know if teenagers are more into reading books than adults. Our proportion table might look like:

example_df <- data.frame(
  read_book = c("Yes", "No"),
  teenager = c(35, 66),
  adult = c(48, 51)
  ) 

example_df %>%
  knitr::kable()
read_book teenager adult
Yes 35 48
No 66 51

To calculate the p value we would simply run:

example_df %>%
  select(teenager, adult) %>%
  as.matrix() %>%
  prop.test

    2-sample test for equality of proportions with continuity correction

data:  .
X-squared = 3.3905, df = 1, p-value = 0.06557
alternative hypothesis: two.sided
95 percent confidence interval:
 -0.291854136  0.007022501
sample estimates:
   prop 1    prop 2 
0.4216867 0.5641026 

Since the survey data is more complex than the basic example above, the g6tr.survey package contains 5 functions that will help you with conducting and reporting proportion tests:

the following parts of the vignette will describe each of this function in detailed manner.

Data

We’ll use demographic data from the 2020 March Newsletter in order to explain all of the functions above. This dataset is distribubted within the g6tr.survey package.

data('ds_newsletter', package = 'g6tr.survey')

head(ds_newsletter)

[38;5;246m# A tibble: 6 × 6
[39m
  ResponseId        s_age s_gender s_hhincome                   s_race Region 
  
[3m
[38;5;246m<chr>
[39m
[23m             
[3m
[38;5;246m<fct>
[39m
[23m 
[3m
[38;5;246m<fct>
[39m
[23m    
[3m
[38;5;246m<fct>
[39m
[23m                        
[3m
[38;5;246m<fct>
[39m
[23m  
[3m
[38;5;246m<chr>
[39m
[23m  

[38;5;250m1
[39m R_12SlGpArLfScMm9 31-45 Female   $100,000 or more             White  WEST   

[38;5;250m2
[39m R_1jivVwiBohyJoV6 65+   Male     $50,000 - less than $100,000 White  MIDWEST

[38;5;250m3
[39m R_1IRaJfUso9uG3QU 65+   Female   $25,000 - less than $50,000  White  WEST   

[38;5;250m4
[39m R_2dTperXrEiq2T7U 65+   Male     $25,000 - less than $50,000  White  MIDWEST

[38;5;250m5
[39m R_W9BEuz81GirUzC1 65+   Male     $50,000 - less than $100,000 White  SOUTH  

[38;5;250m6
[39m R_3h6IIKGZJR2NKk4 65+   Female   $50,000 - less than $100,000 White  SOUTH  

Common function arguments

All the mentioned functions, except srv_prop_stats_xlsx() have 4 common arguments:

  • data – where you pass the name of your dataset (this can be omitted when using %>% operator),
  • grp_var – where you pass the name of grouping variable. The name can be quoted or unquoted (grp_var = s_age and grp_var = 's_age' will work). In the case of srv_prop_stats_crossing() this argument has name: grp_vars,
  • sbj_var – where you pass the name of the variable for which you want to calculate proportion test. The name can be quoted or unquoted (sbj_var = s_race and sbj_var = 's_race' will work). In the case of srv_prop_stats_crossing() this argument has name: sbj_vars,
  • wt – where you pass the name of weighting variable, This should be unquoted (e.g. wt = weight).

With that in mind, let’s move on to description of the particular functions.

How to work with functions

srv_prop_test_ref() – Calculate proportions by groups

This function calculates proportion test between group level proportion and a reference group (by default overall) proportion.

It will be easier to follow along if we look at the results of a function call:

ds_newsletter %>%
  srv_prop_test_ref(grp_var = s_age, sbj_var = s_gender)

[38;5;246m# A tibble: 10 × 9
[39m
   s_age   sum_n s_gender     n percentage prop_pval prop_pval_adj std_error conf_int
   
[3m
[38;5;246m<fct>
[39m
[23m   
[3m
[38;5;246m<int>
[39m
[23m 
[3m
[38;5;246m<fct>
[39m
[23m    
[3m
[38;5;246m<int>
[39m
[23m      
[3m
[38;5;246m<dbl>
[39m
[23m     
[3m
[38;5;246m<dbl>
[39m
[23m         
[3m
[38;5;246m<dbl>
[39m
[23m     
[3m
[38;5;246m<dbl>
[39m
[23m    
[3m
[38;5;246m<dbl>
[39m
[23m

[38;5;250m 1
[39m 18-30     191 Male        73      0.382     0.245         0.984    0.035
[4m2
[24m   0.068
[4m9
[24m

[38;5;250m 2
[39m 18-30     191 Female     118      0.618     0.245         0.984    0.035
[4m2
[24m   0.068
[4m9
[24m

[38;5;250m 3
[39m 31-45     233 Male        98      0.421     0.835         1.000    0.032
[4m3
[24m   0.063
[4m4
[24m

[38;5;250m 4
[39m 31-45     233 Female     135      0.579     0.835         1.000    0.032
[4m3
[24m   0.063
[4m4
[24m

[38;5;250m 5
[39m 46-64     379 Male       168      0.443     0.721         1.000    0.025
[4m5
[24m   0.050
[4m0
[24m

[38;5;250m 6
[39m 46-64     379 Female     211      0.557     0.721         1.000    0.025
[4m5
[24m   0.050
[4m0
[24m

[38;5;250m 7
[39m 65+       186 Male        87      0.468     0.394         0.984    0.036
[4m6
[24m   0.071
[4m7
[24m

[38;5;250m 8
[39m 65+       186 Female      99      0.532     0.394         0.984    0.036
[4m6
[24m   0.071
[4m7
[24m

[38;5;250m 9
[39m overall   989 Male       426      0.431     1.000         1.000    0.015
[4m7
[24m   0.030
[4m9
[24m

[38;5;250m10
[39m overall   989 Female     563      0.569     1.000         1.000    0.015
[4m7
[24m   0.030
[4m9
[24m
  • Our grouping variable s_age now has an additional level: overall.
  • sum_n column stores the number of observations in each level of the grouping variable.
  • The third column contains all levels of the variable for which the proportion test was calculated.
  • n and percentage columns represent the number of observations and percentage of observations of each level of variable for which proportion test is calculated within level of grouping variable.
  • prop_pval and prop_pval_adj contain p value and adjusted p value of proportion test for each grouping level in respect to overall level.
  • The adjustment method is controlled by p_adjust argument, which uses methods provided by p.adjust() function (use ?p.adjust to check available methods and their descriptions).
  • Last two columns: std_error and conf_int store standard errors and 95% confidence interval values.

Alternatively you can indicate one of the grouping variable levels to be treated as a reference level using the reference argument. In such a case you can dismiss overall stats by setting overall to FALSE. For instance if you set grp_var = s_age and reference = '46-64' this level will be treated as the base line for proportion tests.

ds_newsletter %>%
  srv_prop_test_ref(
    grp_var = s_age,
    sbj_var = s_gender,
    overall = F,
    reference = "46-64"
  )

[38;5;246m# A tibble: 8 × 9
[39m
  s_age sum_n s_gender     n percentage prop_pval prop_pval_adj std_error conf_int
  
[3m
[38;5;246m<fct>
[39m
[23m 
[3m
[38;5;246m<int>
[39m
[23m 
[3m
[38;5;246m<fct>
[39m
[23m    
[3m
[38;5;246m<int>
[39m
[23m      
[3m
[38;5;246m<dbl>
[39m
[23m     
[3m
[38;5;246m<dbl>
[39m
[23m         
[3m
[38;5;246m<dbl>
[39m
[23m     
[3m
[38;5;246m<dbl>
[39m
[23m    
[3m
[38;5;246m<dbl>
[39m
[23m

[38;5;250m1
[39m 18-30   191 Male        73      0.382     0.192         0.770    0.035
[4m2
[24m   0.068
[4m9
[24m

[38;5;250m2
[39m 18-30   191 Female     118      0.618     0.192         0.770    0.035
[4m2
[24m   0.068
[4m9
[24m

[38;5;250m3
[39m 31-45   233 Male        98      0.421     0.642         0.861    0.032
[4m3
[24m   0.063
[4m4
[24m

[38;5;250m4
[39m 31-45   233 Female     135      0.579     0.642         0.861    0.032
[4m3
[24m   0.063
[4m4
[24m

[38;5;250m5
[39m 46-64   379 Male       168      0.443     1             1        0.025
[4m5
[24m   0.050
[4m0
[24m

[38;5;250m6
[39m 46-64   379 Female     211      0.557     1             1        0.025
[4m5
[24m   0.050
[4m0
[24m

[38;5;250m7
[39m 65+     186 Male        87      0.468     0.646         0.861    0.036
[4m6
[24m   0.071
[4m7
[24m

[38;5;250m8
[39m 65+     186 Female      99      0.532     0.646         0.861    0.036
[4m6
[24m   0.071
[4m7
[24m

CAUTION

If you are using a grouping variable with just TWO levels (like s_gender which has just Male and Female) you can set overall to FALSE and omit the reference argument and it won’t throw an error. HOWEVER, your output will be different.

Let’s say your grouping variable is grp_var = s_gender and your variable of interest is sbj_var = s_age. In the case you omit the reference argument, the proportion test will be calculated for each sbj_var levels (e.g. proportion of Male 65+ vs. Female 65+). Additionally, the values for p values, adjusted p values for both rows of sbj_var will be the same.

On the other hand, if you decide to use the reference argument when there are only two levels of grouping variable, p values and adjusted p values for reference level rows will be set to 1. In other word, setting up the reference argument will work same as with using reference argument with more categories.

Please, refer to code below if this description is unclear.

ds_newsletter %>%
  srv_prop_test_ref(
    grp_var = s_gender, 
    sbj_var = s_age, 
    overall = F, 
    reference = "Male"
    )

[38;5;246m# A tibble: 8 × 9
[39m
  s_gender sum_n s_age     n percentage prop_pval prop_pval_adj std_error conf_int
  
[3m
[38;5;246m<fct>
[39m
[23m    
[3m
[38;5;246m<int>
[39m
[23m 
[3m
[38;5;246m<fct>
[39m
[23m 
[3m
[38;5;246m<int>
[39m
[23m      
[3m
[38;5;246m<dbl>
[39m
[23m     
[3m
[38;5;246m<dbl>
[39m
[23m         
[3m
[38;5;246m<dbl>
[39m
[23m     
[3m
[38;5;246m<dbl>
[39m
[23m    
[3m
[38;5;246m<dbl>
[39m
[23m

[38;5;250m1
[39m Male       426 18-30    73      0.171     1                 1    0.018
[4m3
[24m   0.035
[4m8
[24m

[38;5;250m2
[39m Male       426 31-45    98      0.230     1                 1    0.020
[4m4
[24m   0.040
[4m0
[24m

[38;5;250m3
[39m Male       426 46-64   168      0.394     1.000             1    0.023
[4m7
[24m   0.046
[4m4
[24m

[38;5;250m4
[39m Male       426 65+      87      0.204     1                 1    0.019
[4m5
[24m   0.038
[4m3
[24m

[38;5;250m5
[39m Female     563 18-30   118      0.210     0.154             1    0.017
[4m2
[24m   0.033
[4m6
[24m

[38;5;250m6
[39m Female     563 31-45   135      0.240     0.778             1    0.018
[4m0
[24m   0.035
[4m3
[24m

[38;5;250m7
[39m Female     563 46-64   211      0.375     0.575             1    0.020
[4m4
[24m   0.040
[4m0
[24m

[38;5;250m8
[39m Female     563 65+      99      0.176     0.294             1    0.016
[4m0
[24m   0.031
[4m4
[24m
ds_newsletter %>%
  srv_prop_test_ref(
    grp_var = s_gender, 
    sbj_var = s_age, 
    overall = F
    )

[38;5;246m# A tibble: 8 × 9
[39m
  s_gender sum_n s_age     n percentage prop_pval prop_pval_adj std_error conf_int
  
[3m
[38;5;246m<fct>
[39m
[23m    
[3m
[38;5;246m<int>
[39m
[23m 
[3m
[38;5;246m<fct>
[39m
[23m 
[3m
[38;5;246m<int>
[39m
[23m      
[3m
[38;5;246m<dbl>
[39m
[23m     
[3m
[38;5;246m<dbl>
[39m
[23m         
[3m
[38;5;246m<dbl>
[39m
[23m     
[3m
[38;5;246m<dbl>
[39m
[23m    
[3m
[38;5;246m<dbl>
[39m
[23m

[38;5;250m1
[39m Male       426 18-30    73      0.171     0.154         0.588    0.018
[4m3
[24m   0.035
[4m8
[24m

[38;5;250m2
[39m Male       426 31-45    98      0.230     0.778         0.778    0.020
[4m4
[24m   0.040
[4m0
[24m

[38;5;250m3
[39m Male       426 46-64   168      0.394     0.575         0.766    0.023
[4m7
[24m   0.046
[4m4
[24m

[38;5;250m4
[39m Male       426 65+      87      0.204     0.294         0.588    0.019
[4m5
[24m   0.038
[4m3
[24m

[38;5;250m5
[39m Female     563 18-30   118      0.210     0.154         0.588    0.017
[4m2
[24m   0.033
[4m6
[24m

[38;5;250m6
[39m Female     563 31-45   135      0.240     0.778         0.778    0.018
[4m0
[24m   0.035
[4m3
[24m

[38;5;250m7
[39m Female     563 46-64   211      0.375     0.575         0.766    0.020
[4m4
[24m   0.040
[4m0
[24m

[38;5;250m8
[39m Female     563 65+      99      0.176     0.294         0.588    0.016
[4m0
[24m   0.031
[4m4
[24m

srv_prop_test_within() – Calculate proportions within groups

This function calculates proportion test between levels of sbj_var within particular level of a grouping variable. Let’s say we want to know if the frequencies of age groups within each gender is equal. We can check it by running code below:

ds_newsletter %>%
  srv_prop_test_within(grp_var = s_gender, sbj_var = s_age)

[38;5;246m# A tibble: 8 × 9
[39m
  s_gender sum_n s_age     n percentage prop_pval prop_pval_adj std_error conf_int
  
[3m
[38;5;246m<fct>
[39m
[23m    
[3m
[38;5;246m<int>
[39m
[23m 
[3m
[38;5;246m<fct>
[39m
[23m 
[3m
[38;5;246m<int>
[39m
[23m      
[3m
[38;5;246m<dbl>
[39m
[23m     
[3m
[38;5;246m<dbl>
[39m
[23m         
[3m
[38;5;246m<dbl>
[39m
[23m     
[3m
[38;5;246m<dbl>
[39m
[23m    
[3m
[38;5;246m<dbl>
[39m
[23m

[38;5;250m1
[39m Male       426 18-30    73      0.171  6.89
[38;5;246me
[39m
[31m-11
[39m      6.89
[38;5;246me
[39m
[31m-11
[39m    0.012
[4m0
[24m   0.023
[4m5
[24m

[38;5;250m2
[39m Male       426 31-45    98      0.230  6.89
[38;5;246me
[39m
[31m-11
[39m      6.89
[38;5;246me
[39m
[31m-11
[39m    0.013
[4m4
[24m   0.026
[4m2
[24m

[38;5;250m3
[39m Male       426 46-64   168      0.394  6.89
[38;5;246me
[39m
[31m-11
[39m      6.89
[38;5;246me
[39m
[31m-11
[39m    0.015
[4m5
[24m   0.030
[4m5
[24m

[38;5;250m4
[39m Male       426 65+      87      0.204  6.89
[38;5;246me
[39m
[31m-11
[39m      6.89
[38;5;246me
[39m
[31m-11
[39m    0.012
[4m8
[24m   0.025
[4m1
[24m

[38;5;250m5
[39m Female     563 18-30   118      0.210  4.10
[38;5;246me
[39m
[31m-11
[39m      6.89
[38;5;246me
[39m
[31m-11
[39m    0.012
[4m9
[24m   0.025
[4m4
[24m

[38;5;250m6
[39m Female     563 31-45   135      0.240  4.10
[38;5;246me
[39m
[31m-11
[39m      6.89
[38;5;246me
[39m
[31m-11
[39m    0.013
[4m6
[24m   0.026
[4m6
[24m

[38;5;250m7
[39m Female     563 46-64   211      0.375  4.10
[38;5;246me
[39m
[31m-11
[39m      6.89
[38;5;246me
[39m
[31m-11
[39m    0.015
[4m4
[24m   0.030
[4m2
[24m

[38;5;250m8
[39m Female     563 65+      99      0.176  4.10
[38;5;246me
[39m
[31m-11
[39m      6.89
[38;5;246me
[39m
[31m-11
[39m    0.012
[4m1
[24m   0.023
[4m7
[24m

The columns meaning is similar to the one we obtain with srv_prop_test_ref() function, however now prop_pval and prop_pval_adj values are associated with the within grouping level frequency distribution.

srv_prop_stats() – Calculates index of variable level within group to overall population

This function returns a table with an additional index variable. The index variable compares the frequency of a level from sbj_var between grp_var and the overall population. For instance, let’s say we are interested in the s_age variable, grouped by the s_gender variable. The index of 18-30 Male will be calculated as follows:

Index18−30male=(n18−30male/nmale)(n18−30/ntotal)Index_{18-30male} = \frac{(n_{18-30 male}/n_{male})}{(n_{18-30}/n_{total})} It can be called in similar way as srv_pop_test_within function:

ds_newsletter %>% 
  srv_prop_stats(grp_var = s_gender, sbj_var = s_age)

[38;5;246m# A tibble: 8 × 10
[39m
  s_gender sum_n s_age     n percentage overall_n overall_sum_n overall_percentage index  pval
  
[3m
[38;5;246m<fct>
[39m
[23m    
[3m
[38;5;246m<int>
[39m
[23m 
[3m
[38;5;246m<fct>
[39m
[23m 
[3m
[38;5;246m<int>
[39m
[23m      
[3m
[38;5;246m<dbl>
[39m
[23m     
[3m
[38;5;246m<int>
[39m
[23m         
[3m
[38;5;246m<int>
[39m
[23m              
[3m
[38;5;246m<dbl>
[39m
[23m 
[3m
[38;5;246m<dbl>
[39m
[23m 
[3m
[38;5;246m<dbl>
[39m
[23m

[38;5;250m1
[39m Male       426 18-30    73      0.171       191           989              0.193  88.7 0.374

[38;5;250m2
[39m Male       426 31-45    98      0.230       233           989              0.236  97.6 0.875

[38;5;250m3
[39m Male       426 46-64   168      0.394       379           989              0.383 103.  0.737

[38;5;250m4
[39m Male       426 65+      87      0.204       186           989              0.188 109.  0.527

[38;5;250m5
[39m Female     563 18-30   118      0.210       191           989              0.193 109.  0.475

[38;5;250m6
[39m Female     563 31-45   135      0.240       233           989              0.236 102.  0.901

[38;5;250m7
[39m Female     563 46-64   211      0.375       379           989              0.383  97.8 0.783

[38;5;250m8
[39m Female     563 65+      99      0.176       186           989              0.188  93.5 0.596

The table also contains columns summarizing frequencies:

  • sum_n – frequency of grouping level,
  • n – frequency of sbj_var level within grp_var level,
  • percentage – proportion of n to sum_n,
  • overall_n – frequency of sbj_var level in overall population,
  • overall_sum_n – overall sample size,
  • overall_percentage – proportion of overall_n to overall_sum_n.

Last column contains p value of proportion test.

srv_prop_stats_crossing() – Calculates proportion statistics for all combinations of provided subject and grouping variables.

In case you need to calculate srv_prop_stats() for multiple combinations of grouping and subject variables, you can take srv_prop_stats_crossing() as a shortcut.

ds_newsletter_prop_stats_crossed_df <- ds_newsletter %>%
  srv_prop_stats_crossing(
    grp_vars = c(s_age, s_gender),
    sbj_vars = c(s_race, Region)
    )

ds_newsletter_prop_stats_crossed_df

[38;5;246m# A tibble: 4 × 5
[39m
  grp_var  grp_var_label                      sbj_var sbj_var_label                                           stats             
  
[3m
[38;5;246m<chr>
[39m
[23m    
[3m
[38;5;246m<chr>
[39m
[23m                              
[3m
[38;5;246m<chr>
[39m
[23m   
[3m
[38;5;246m<chr>
[39m
[23m                                                   
[3m
[38;5;246m<list>
[39m
[23m            

[38;5;250m1
[39m s_age    What is your age?                  s_race  Which racial or ethnic group do you most identify with? 
[38;5;246m<tibble [24 × 12]>
[39m

[38;5;250m2
[39m s_age    What is your age?                  Region  
[31mNA
[39m                                                      
[38;5;246m<tibble [16 × 12]>
[39m

[38;5;250m3
[39m s_gender With which gender do you identify? s_race  Which racial or ethnic group do you most identify with? 
[38;5;246m<tibble [12 × 12]>
[39m

[38;5;250m4
[39m s_gender With which gender do you identify? Region  
[31mNA
[39m                                                      
[38;5;246m<tibble [8 × 12]>
[39m 

Since we are interested in multiple grouping and subject variables, grp_vars and sbj_vars argument might be specified using tidyselect syntax (You can read more on tidyselect here). Suppose your data frame looks like this:

example_behavior_df <- data.frame(
  resp_id = 1:30,
  gender_demo = sample(c("M", "F"), 30, replace = T),
  region_demo = sample(c("North", "South", "East"), 30, replace = T, prob = c(0.15, 0.35, 0.5)),
  education_demo = sample(c("Primary", "Higher"), 30, replace = T, prob = c(0.7, 0.3)),
  q1_behavior = sample(1:5, 30, replace = T, prob = c(0.4, 0.1, 0.1, 0.3, 0.1)),
  q2_behavior = sample(1:5, 30, replace = T, prob = c(0.2, 0.2, 0.1, 0.2, 0.3)),
  q3_behavior = sample(1:5, 30, replace = T, prob = c(0.05, 0.15, 0.5, 0.15, 0.2))
  ) %>%
  mutate(across(ends_with("behavior"), as.factor))

example_behavior_df %>%
  head(6) %>%
  knitr::kable()
resp_id gender_demo region_demo education_demo q1_behavior q2_behavior q3_behavior
1 M East Higher 2 2 4
2 M East Primary 1 4 3
3 F East Primary 4 5 4
4 M South Primary 5 3 3
5 M East Primary 3 2 3
6 F East Primary 1 5 3

With this table it would be easy to select all behavior columns for subject variables and all demographic columns for grouping variables. Please refer to the code below:

example_behavior_df %>%
  srv_prop_stats_crossing(ends_with("_demo"), ends_with("behavior"))

[38;5;246m# A tibble: 9 × 5
[39m
  grp_var        grp_var_label sbj_var     sbj_var_label stats             
  
[3m
[38;5;246m<chr>
[39m
[23m          
[3m
[38;5;246m<chr>
[39m
[23m         
[3m
[38;5;246m<chr>
[39m
[23m       
[3m
[38;5;246m<chr>
[39m
[23m         
[3m
[38;5;246m<list>
[39m
[23m            

[38;5;250m1
[39m gender_demo    
[31mNA
[39m            q1_behavior 
[31mNA
[39m            
[38;5;246m<tibble [10 × 12]>
[39m

[38;5;250m2
[39m gender_demo    
[31mNA
[39m            q2_behavior 
[31mNA
[39m            
[38;5;246m<tibble [10 × 12]>
[39m

[38;5;250m3
[39m gender_demo    
[31mNA
[39m            q3_behavior 
[31mNA
[39m            
[38;5;246m<tibble [10 × 12]>
[39m

[38;5;250m4
[39m region_demo    
[31mNA
[39m            q1_behavior 
[31mNA
[39m            
[38;5;246m<tibble [15 × 12]>
[39m

[38;5;250m5
[39m region_demo    
[31mNA
[39m            q2_behavior 
[31mNA
[39m            
[38;5;246m<tibble [15 × 12]>
[39m

[38;5;250m6
[39m region_demo    
[31mNA
[39m            q3_behavior 
[31mNA
[39m            
[38;5;246m<tibble [15 × 12]>
[39m

[38;5;250m7
[39m education_demo 
[31mNA
[39m            q1_behavior 
[31mNA
[39m            
[38;5;246m<tibble [10 × 12]>
[39m

[38;5;250m8
[39m education_demo 
[31mNA
[39m            q2_behavior 
[31mNA
[39m            
[38;5;246m<tibble [10 × 12]>
[39m

[38;5;250m9
[39m education_demo 
[31mNA
[39m            q3_behavior 
[31mNA
[39m            
[38;5;246m<tibble [10 × 12]>
[39m

The output data frame contains columns with grouping and subject variable names and labels. The last column (stats) of output contains proper tables with results in a tibble format. Suppose you want to see the results for the subject variable Region and grouping variable s_age from the first example. In this case, you can access them by simply addressing row one and column five with double brackets, followed with 1 in double brackets:

ds_newsletter_prop_stats_crossed_df[[1,5]][[1]] %>%
  head()

[38;5;246m# A tibble: 6 × 12
[39m
  grp_var_cat sbj_var_cat                              n sum_n overall_n overall_sum_n percentage overall_percentage index   pval signif signif_signed
  
[3m
[38;5;246m<fct>
[39m
[23m       
[3m
[38;5;246m<fct>
[39m
[23m                                
[3m
[38;5;246m<int>
[39m
[23m 
[3m
[38;5;246m<int>
[39m
[23m     
[3m
[38;5;246m<int>
[39m
[23m         
[3m
[38;5;246m<int>
[39m
[23m      
[3m
[38;5;246m<dbl>
[39m
[23m              
[3m
[38;5;246m<dbl>
[39m
[23m 
[3m
[38;5;246m<dbl>
[39m
[23m  
[3m
[38;5;246m<dbl>
[39m
[23m  
[3m
[38;5;246m<dbl>
[39m
[23m         
[3m
[38;5;246m<dbl>
[39m
[23m

[38;5;250m1
[39m 18-30       White                                  136   191       784           989     0.712             0.793    89.8 0.017
[4m9
[24m      2            -
[31m2
[39m

[38;5;250m2
[39m 18-30       African American/Black/Caribbean Am…    33   191       106           989     0.173             0.107   161.  0.014
[4m2
[24m      2             2

[38;5;250m3
[39m 18-30       Native American or American Indian       3   191         6           989     0.015
[4m7
[24m            0.006
[4m0
[24m
[4m7
[24m 259.  0.166       0             0

[38;5;250m4
[39m 18-30       Asian or Pacific Islander                6   191        26           989     0.031
[4m4
[24m            0.026
[4m3
[24m  119.  0.630       0             0

[38;5;250m5
[39m 18-30       Mixed race and Others                    0   191         9           989     0                 0.009
[4m1
[24m
[4m0
[24m   0   0.369       0             0

[38;5;250m6
[39m 18-30       Hispanic or Latino                      13   191        58           989     0.068
[4m1
[24m            0.058
[4m6
[24m  116.  0.618       0             0

Later on you can deal with is as with an ordinary data.frame / tibble. Suppose you are interested in level Hispanic or Latino of s_race variable, grouped by s_gender variable. You want to limit your display to grp_var_cat, sbj_var_cat, percentage and index:

ds_newsletter_prop_stats_crossed_df[[4,5]][[1]] %>%
  filter(sbj_var_cat == "Hispanic or Latino") %>%
  select(grp_var_cat, sbj_var_cat, percentage, index)

[38;5;246m# A tibble: 0 × 4
[39m

[38;5;246m# ℹ 4 variables: grp_var_cat <fct>, sbj_var_cat <fct>, percentage <dbl>, index <dbl>
[39m

You can also apply some functions (like filter to all the tibbles), using purrr::map). Let’s say we want to filter results from all the tibbles for sbj_var_cat level White:

ds_newsletter_prop_stats_crossed_df %>%
  mutate(stats = purrr::map(stats, ~filter(., sbj_var_cat == "White"))) %>%
  tidyr::unnest(stats)

[38;5;246m# A tibble: 6 × 16
[39m
  grp_var  grp_var_label sbj_var sbj_var_label grp_var_cat sbj_var_cat     n sum_n overall_n overall_sum_n percentage overall_percentage index    pval
  
[3m
[38;5;246m<chr>
[39m
[23m    
[3m
[38;5;246m<chr>
[39m
[23m         
[3m
[38;5;246m<chr>
[39m
[23m   
[3m
[38;5;246m<chr>
[39m
[23m         
[3m
[38;5;246m<fct>
[39m
[23m       
[3m
[38;5;246m<fct>
[39m
[23m       
[3m
[38;5;246m<int>
[39m
[23m 
[3m
[38;5;246m<int>
[39m
[23m     
[3m
[38;5;246m<int>
[39m
[23m         
[3m
[38;5;246m<int>
[39m
[23m      
[3m
[38;5;246m<dbl>
[39m
[23m              
[3m
[38;5;246m<dbl>
[39m
[23m 
[3m
[38;5;246m<dbl>
[39m
[23m   
[3m
[38;5;246m<dbl>
[39m
[23m

[38;5;250m1
[39m s_age    What is your… s_race  Which racial… 18-30       White         136   191       784           989      0.712              0.793  89.8 0.017
[4m9
[24m 

[38;5;250m2
[39m s_age    What is your… s_race  Which racial… 31-45       White         181   233       784           989      0.777              0.793  98.0 0.655  

[38;5;250m3
[39m s_age    What is your… s_race  Which racial… 46-64       White         301   379       784           989      0.794              0.793 100.  1.000  

[38;5;250m4
[39m s_age    What is your… s_race  Which racial… 65+         White         166   186       784           989      0.892              0.793 113.  0.002
[4m1
[24m
[4m4
[24m

[38;5;250m5
[39m s_gender With which g… s_race  Which racial… Male        White         358   426       784           989      0.840              0.793 106.  0.044
[4m4
[24m 

[38;5;250m6
[39m s_gender With which g… s_race  Which racial… Female      White         426   563       784           989      0.757              0.793  95.5 0.113  

[38;5;246m# ℹ 2 more variables: signif <dbl>, signif_signed <dbl>
[39m

Last, but not least – it is also fairly straightforward now to leave only interesting combinations based on some other criteria. Let’s say we are looking for only those cross-combinations of subject category level and grouping category level that have at least one p_value smaller than 0.05 (AKA statistically significant difference). To get these combinations, you can use following code:

ds_newsletter_prop_stats_crossed_df %>%
  rowwise() %>%
  mutate(min_pval = min(stats$pval)) %>%
  ungroup() %>%
  #leave out label variables for better printing in vignette
  dplyr::select(-ends_with("label")) %>%
  filter(min_pval < 0.05)

[38;5;246m# A tibble: 2 × 4
[39m
  grp_var  sbj_var stats              min_pval
  
[3m
[38;5;246m<chr>
[39m
[23m    
[3m
[38;5;246m<chr>
[39m
[23m   
[3m
[38;5;246m<list>
[39m
[23m                
[3m
[38;5;246m<dbl>
[39m
[23m

[38;5;250m1
[39m s_age    s_race  
[38;5;246m<tibble [24 × 12]>
[39m  0.002
[4m1
[24m
[4m4
[24m

[38;5;250m2
[39m s_gender s_race  
[38;5;246m<tibble [12 × 12]>
[39m  0.044
[4m4
[24m 

srv_prop_stats_xlsx() – Alows to save results in xlsx format

If you need to store the results of srv_prop_stats_crossing() into a spreadsheet, you can use the srv_prop_stats_xlsx() function, which will create an xlsx output. Results for each grp_var will be stored in a separate sheet. The names of the sheets will be named after grp_var_label values. Thus, if you need different names for sheets in output file you need to change the values prior to calling srv_prop_stats_xlsx(). Cells in spreadsheet will be automatically formatted for a uniform appearance.

You can use this function as in the example below:

ds_newsletter_prop_stats_crossed_df %>%
  srv_prop_stats_xlsx(file = "my_file_with_stats_crossed_df.xlsx")

Additionally you can use these 4 parameters to alter default behavior of this function:

  • overwrite – set FALSE if you do not want to overwrite existing files
  • overall – set FALSE if you do not need overall column for subject variable stats
  • secondary_stats – set to NULL if you only need index value. By default this parameter is set to c("percentage", "n")
  • hide_stats_names – set to FALSE if you do not want to hide statistics names column.

You can also pass one of additional 9 arguments that alter default styling of the sheet cells:

  • overall_percent_font_color – use different font color for cells in overall column containing percent values.
  • overall_fill_color – use different color for filling cells in overall column.
  • group_percent_font_color – use different font color for cells in group columns containing percent values.
  • header_font_color – use different color for font in header section
  • header_fill_color – use different color for fill in header section
  • header_border_color – use different border color in border section
  • header_border_style – use different style of borders in header section
  • border_color – use different border color in the body of a table
  • border_style – use different border style in the body of a table.

All the color values needs to be character of valid color (see colours()) or hex value of color (begging with #). In the case of border styles please refer to ?openxlsx::createStyle.