Skip to contents
library("g6tr.list")
library("dplyr")
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
data("list_experiment_toy")

Pretty Client Outputs

Making Pretty Deliverables

As you saw in previous vignette, the output of the list_statistics_bulk() function is not ready to be presented to clients. Fortunately we have a workhorse function: list_render_sheets() that will take care of putting all the results in wide format into formatted Excell spreadsheets.

Prequisites

Lets quickly recap what you need to do to get all the inputs for our function to work.

First we need to read and tidy our data:

## Create a demographic data frame
demo_toy <- list_experiment_toy[1:300, ] %>%
  select("response_id", "d_party", "s_gender", "s_region") %>%
  mutate(num_var = rnorm(nrow(.), 3, 0.4))
 
## Create a data frame holding weights
demo_weights <- list_experiment_toy %>%
  select("response_id", "weights")

## Run read %>% assert %>% tidy in pipe to make code
list_data_tidy <- list_read_data_bulk(list_experiment_toy, "response_id") %>%
  list_assert_data_bulk(demo_toy, id = "response_id", n_items = 4L) %>%
  list_tidy_data_bulk(id = response_id)

Next we need to run models:

list_model_bulk(
  list_data_tidy, seed = 666, 
  file_prefix = "list_model_toy", path = "models", 
  iter = 400, cores = 2, chains = 2
)

In the last step we calculate statistics for the models output.

output_statistics <- list_statistics_bulk(
  "models", "list_model_", list_data_tidy,
  weights_df = demo_weights, weights_var = "weights"
)

There are two important elements of the list_statistics() output that will be used by our rendering functions: point_estimates$mean_probabilities and prob_by_group. Here is how they look for individual bundle:

output_statistics$bundle_1$point_estimates$mean_probabilities
#> # A tibble: 1 × 4
#>   probability_public probability_private probability_delta N_Overall
#>                <dbl>               <dbl>             <dbl>     <dbl>
#> 1              0.211               0.531             0.321       279

head(output_statistics$bundle_1$prob_by_group)
#> # A tibble: 6 × 6
#>   demo_variable demo_value  private_probability public_probability
#>   <chr>         <chr>                     <dbl>              <dbl>
#> 1 d_party       Democrat                  0.475              0.218
#> 2 d_party       Independent               0.480              0.132
#> 3 d_party       Republican                0.674              0.288
#> 4 d_party       Other                     0.458              0.134
#> 5 s_gender      Female                    0.520              0.170
#> 6 s_gender      Male                      0.551              0.280
#> # ℹ 2 more variables: delta_probability <dbl>, N <dbl>

Rendering Sheets

For each List Experiment study we have two deliverables: within and between statistics. The first one contains comparison of differences between private and public opinion WITHIN particular demographic level. In the latter we compare the difference of private and public opinion of particular demographic level to the difference BETWEEN private and public opinion observed for overall population. As mentioned in the beginning, we have a workhorse that take the output of list_statistic_bulk() function and exports it as a nicely formatted Excell spreadsheets. For the simplest use, you can just call the function with single argument – the result of the list_statistics_bulk() function.

rendered_sheets <- list_render_sheets(output_statistics, file = NULL)

However there are also few other parameters that can adjust the function behavior to your needs:

  • wb – here you can specify an empty Workbook object created with openxlsx package that will be used to store the sheets
  • file – the name of the file where the output will be dumped
  • low_p, mid_p, high_p – these arguments have default values of 0.75, 0.92 and 0.95 respectively. The indicate the lower values of probabilities that we consider low, medium or high.
  • colors_p – this argument contains vector of colors that will be used as a background for cells that probability is higher than low_p, mid_p or high_p value. The vector should contain 6 colors. First three will be used to indicate the cells where the difference between statistic for demographic group and Overall population is smaller than 0, and the last 3 will be used to highlight the cells for which the difference was greater than 0. The colors defaults are #F9C8BB, #F39278, #EF5D36, #C4E9DA, #8AD3B4 and #50BD90.

We used list_statistics_bulk() with default higher parameter, i.e. 0.03. In the within sheet we will be highlighting results for which probability that the true difference between private and public probability is indeed higher than 0.03 or lower than -0.03. The color scheme will works as follows:

There is higher chance that the difference between private and public opinion is negative:

  • first color of colors_p vector if the probability is higher than low_p
  • second color of colors_p vector if the probability is higher than med_p
  • third color of colors_p vector if the probability is higher than high_p

There is higher chance that the difference between private and public opinion is positive:

  • fourth color of colors_p vector if the probability is higher than low_p
  • fifth color of colors_p vector if the probability is higher than med_p
  • sixth color of colors_p vector if the probability is higher than high_p

Lets assume that for Males the difference between private and public opinion is equal to 0.12. The probability that this difference is higher than 0.03 is 0.93. Assuming that we used default settings, the probability is higher than 0.92 so the cell will get #8AD3B4 color.

In the case of between sheet the scheme of coloring works similarly. The difference is that now we will be highlighting probabilities that the difference between demographic level and overall population difference between private and public probability is lower or higher than the 0.03 threshold. The color scheme will works as follows:

The difference in statistic between the demographic group and the Overall population is negative:

  • first color of colors_p vector if the probability is higher than low_p
  • second color of colors_p vector if the probability is higher than med_p
  • third color of colors_p vector if the probability is higher than high_p

The difference in statistic between the demographic group and the Overall population is positive:

  • fourth color of colors_p vector if the probability is higher than low_p
  • fifth color of colors_p vector if the probability is higher than med_p
  • sixth color of colors_p vector if the probability is higher than high_p

Lets say you want to give pink color to lower probability equal or higher than 0.5, lightblue color to probability equal or higher than 0.9 and blue to probability equal or higher than 0.98 (if the difference is positive) and green, yellow and red if the difference is negative. You also want to automatically save the results to render_sheets.xlsx file. You can call the function like in the example below.

list_render_sheets(
  output_statistics, 
  file = "rendered_sheets.xlsx",
  low_p = 0.5, mid_p = 0.9, high_p = 0.98,
  colors_p = c("green", "yellow", "red", "pink", "lightblue", "blue"),
  saveGD = FALSE
)

Alterative Way

Alternatively you might want to render only the within or between sheet. This way is also straightforward, but requires additional step. First you need to restructure the output of list_statistics_bulk(). We have handy wrappers for this – list_client_output_whitin and list_client_output_between. Both functions have just one argument which takes the results of list_statistics_bulk(). However both functions deal with only single bundle, so you need to run them with lapply to handle multiple bundles.

output_within <- lapply(output_statistics, list_client_output_within)
output_between <- lapply(output_statistics, list_client_output_between)

The code above creates a list containing just three elements: within/between, weighted_N and reshaped_stats. Lets take look what they contain:

output_within$bundle_1$within[, 1:6]
#> # A tibble: 1 × 6
#>   Overall__Overall__private Overall__Overall__public Overall__Overall__delta
#>                       <dbl>                    <dbl>                   <dbl>
#> 1                     0.531                    0.211                   0.321
#> # ℹ 3 more variables: `Overall__Overall__abs delta` <dbl>,
#> #   d_party__Democrat__private <dbl>, d_party__Democrat__public <dbl>
output_between$bundle_1$between[, 1:6]
#> # A tibble: 1 × 6
#>   Overall__private__d_party Democrat__private__d_party Independent__private__d…¹
#>                       <dbl>                      <dbl>                     <dbl>
#> 1                     0.531                      0.475                     0.480
#> # ℹ abbreviated name: ¹​Independent__private__d_party
#> # ℹ 3 more variables: Republican__private__d_party <dbl>,
#> #   Other__private__d_party <dbl>, Overall__public__d_party <dbl>

For each bundle we get one row and multiple columns. They are ordered by demographic variables and levels in the case of whitin results and by demographic level and probability in the case of between results. For each level we get private and public probability, their difference and the absolute difference.

head(output_within$bundle_1$weighted_N)
#> # A tibble: 6 × 2
#>   demo_value      N
#>   <chr>       <dbl>
#> 1 Overall       279
#> 2 Democrat      113
#> 3 Independent    79
#> 4 Republican     78
#> 5 Other           9
#> 6 Female        176

This element contains just the weighted Ns for each of the demographic levels.

head(output_within$bundle_1$reshaped_stats)
#> # A tibble: 6 × 7
#>   demo_variable demo_value  within_delta_higher within_delta_lower ordering_var 
#>   <chr>         <chr>                     <dbl>              <dbl> <fct>        
#> 1 Overall       Overall                    0.99               0    Overall__Ove…
#> 2 d_party       Democrat                   0.94               0.05 d_party__Dem…
#> 3 d_party       Independent                0.99               0.01 d_party__Ind…
#> 4 d_party       Republican                 0.97               0.01 d_party__Rep…
#> 5 d_party       Other                      0.76               0.11 d_party__Oth…
#> 6 s_gender      Female                     0.99               0    s_gender__Fe…
#> # ℹ 2 more variables: threshold <chr>, diff_sign <chr>

Finally we have a table with probabilities for differences within demographic levels and between demographic level and overall population. This element will be used to find out which cells should be colored in the Excell sheet.

In the next step you can run the whitin or between function with the above results. If you are not going to pass clean Workbook object, you will need to add wb_null = TRUE argument. The function also allows you to set low_p, mid_p, high_p and colors_p argument.

render_within <- list_render_sheets_within(output_within, wb_null = TRUE)
#or create a Workbook argument and pass it to function
wb <- openxlsx::createWorkbook()
render_between <- list_render_sheets_between(
  output_between, wb,
  low_p = 0.5, mid_p = 0.9, high_p = 0.98,
  colors_p = c("green", "yellow", "red", "pink", "lightblue", "blue")
  )

Both functions however, do not dump the results to Excel spreadsheet automatically so you need a third step for this.

openxlsx::saveWorkbook(render_within, "render_within.xlsx", overwrite = TRUE)
openxlsx::saveWorkbook(render_between, "render_between.xlsx", overwrite = TRUE)
Within sheet preview

Within sheet preview

Between sheet preview

Between sheet preview