Skip to contents

To keep up with the examples you’ll need to have created a minimal model rack. You can go through the code in the first chapter of the TLDR vignette or simply run

library(g6tr.segmentation)
library(dplyr)
library(tidyr)
library(g6tr.survey)

seg_df <- example_seg_df
seg_pack <- example_seg_pack
seg_rack <- example_seg_rack

to take example data provided by the package.

Boxed indices

We’ll start with “boxed” index summaries. “Boxed” here indicates that we combine certain levels into “boxes” and calculate proportions and indices for such boxes versus all the other levels. Consider one column, seg_pack$x$m2_1_1. It is a labelled numeric with the following labels:

attr(seg_pack$x$m2_1_1, "labels")
##    Strongly agree    Somewhat agree Somewhat disagree Strongly disagree 
##                 4                 3                 2                 1

This set of options is a variation of a Likert scale and typically the most interesting box for us would be the combination of the top 2 levels, namely “Strongly agree” and “Somewhat agree”.

Let’s “manually” calculate segment indices for the Top 2 box of this one variable against segments assigned by the “nmf_3” model.

  1. Prepare a simple dataframe
data_for_indices <- 
  seg_rack$data[[1]] %>% 
  unpack("x") %>% 
  select(id, weight, m2_1_1) %>% 
  bind_cols(segment = seg_rack$segments[[1]]) %>% 
  mutate(box_top_2 = m2_1_1 %in% 4:3)

data_for_indices
## # A tibble: 100 × 5
##    id                weight m2_1_1                segment box_top_2
##    <chr>              <dbl> <dbl+lbl>             <fct>   <lgl>    
##  1 R_yyyU2RdwNtBtXHj  0.771 2 [Somewhat disagree] 3       FALSE    
##  2 R_3Jqm7E60eZmLHUL  0.879 4 [Strongly agree]    3       TRUE     
##  3 R_TsVtU9pnig96wvf  1.08  3 [Somewhat agree]    3       TRUE     
##  4 R_1Fn2QNuPz7eT8nG  1.42  4 [Strongly agree]    2       TRUE     
##  5 R_ai3slhE8Ua9aDV7  0.707 2 [Somewhat disagree] 3       FALSE    
##  6 R_2uw8aNOkURhjVTk  1.41  3 [Somewhat agree]    3       TRUE     
##  7 R_1PdETTmHmJpBbbh  1.46  1 [Strongly disagree] 2       FALSE    
##  8 R_enW7a74WZCtGehr  1.17  3 [Somewhat agree]    3       TRUE     
##  9 R_1mCA8bWg0XAZ7Y2  1.14  3 [Somewhat agree]    3       TRUE     
## 10 R_2V4HFTieiV5VhDG  0.566 4 [Strongly agree]    3       TRUE     
## # ℹ 90 more rows

box_top_2 is TRUE when corresponding response belongs to the Top 2 box, and is FALSE when it doesn’t.

  1. Calculate weighted indices for box_top_2 vs segment using a function from g6tr.survey.
indices_box_top_2_all <- 
  srv_prop_stats(data_for_indices, segment, box_top_2, wt = weight)
indices_box_top_2_all
## # A tibble: 6 × 10
##   segment sum_n box_top_2     n percentage overall_n overall_sum_n
##   <fct>   <dbl> <fct>     <dbl>      <dbl>     <dbl>         <dbl>
## 1 1        23.7 FALSE      6.05      0.256      36.7          103.
## 2 1        23.7 TRUE      17.6       0.744      65.8          103.
## 3 2        49.6 FALSE     20.2       0.407      36.7          103.
## 4 2        49.6 TRUE      29.4       0.593      65.8          103.
## 5 3        29.2 FALSE     10.4       0.357      36.7          103.
## 6 3        29.2 TRUE      18.8       0.643      65.8          103.
## # ℹ 3 more variables: overall_percentage <dbl>, index <dbl>, pval <dbl>
  1. We are only interested in the stats which correspond to “hitting the box”
indices_box_top_2_all %>% 
  filter(box_top_2 == "TRUE") %>% 
  select(-box_top_2)
## # A tibble: 3 × 9
##   segment sum_n     n percentage overall_n overall_sum_n overall_percentage
##   <fct>   <dbl> <dbl>      <dbl>     <dbl>         <dbl>              <dbl>
## 1 1        23.7  17.6      0.744      65.8          103.              0.642
## 2 2        49.6  29.4      0.593      65.8          103.              0.642
## 3 3        29.2  18.8      0.643      65.8          103.              0.642
## # ℹ 2 more variables: index <dbl>, pval <dbl>

This is essentially what we call index statistics for the Top 2 box of a column versus segment assignments of a model.

But in practice we are interested in these summaries for multiple models and for multiple columns, typically for all of the feature columns. And apart from the Top 2 box we might want to see some other like, the Top 1 and Bottom 1 boxes. We have a function for this purpose. Take a look:

seg_rack_extended <-
  seg_rack %>%
  # put "Top 2" and "Top 1" indices for battery "3" to "indices_boxed"
  rack_put_indices_boxed(
    cols = matches("_3_"),
    boxes = list(
      "Top 2" = c("Strongly agree", "Somewhat agree"),
      "Top 1" = c("Strongly agree")
    )
  )

Here we define:

  • cols - a set of columns to calculate indices for. It is done with a tidyselect expression matches("_3_"), so it takes all the features containing "_3_" in the name.

  • boxes - a set of level boxes defined as a named list

As a result we get a rack with one more column, indices_boxed. Each element of this column is a dataframe with indices calculated for a corresponding model. Let’s take a look at one of the dataframes.

seg_rack_extended$indices_boxed[[1]]
## # A tibble: 18 × 7
##    segment segment_total population_total feature label            `Top 2`$index
##    <fct>           <dbl>            <dbl> <chr>   <chr>                    <dbl>
##  1 1                23.7             103. m2_3_17 I think trying …         111. 
##  2 2                49.6             103. m2_3_17 I think trying …          97.8
##  3 3                29.2             103. m2_3_17 I think trying …          94.4
##  4 1                23.7             103. m2_3_18 I tend to value…         105. 
##  5 2                49.6             103. m2_3_18 I tend to value…         102. 
##  6 3                29.2             103. m2_3_18 I tend to value…          93.3
##  7 1                23.7             103. m2_3_19 I am happy to c…          96.5
##  8 2                49.6             103. m2_3_19 I am happy to c…         103. 
##  9 3                29.2             103. m2_3_19 I am happy to c…          97.8
## 10 1                23.7             103. m2_3_21 As I grow older…         134. 
## 11 2                49.6             103. m2_3_21 As I grow older…          57.1
## 12 3                29.2             103. m2_3_21 As I grow older…         146. 
## 13 1                23.7             103. m2_3_22 I tend to see t…         140. 
## 14 2                49.6             103. m2_3_22 I tend to see t…          85.4
## 15 3                29.2             103. m2_3_22 I tend to see t…          92.5
## 16 1                23.7             103. m2_3_23 I like knowing …         102. 
## 17 2                49.6             103. m2_3_23 I like knowing …         104. 
## 18 3                29.2             103. m2_3_23 I like knowing …          91.1
## # ℹ 6 more variables: `Top 2`$pval <dbl>, $segment_perc <dbl>,
## #   $segment_n <dbl>, $population_perc <dbl>, $population_n <dbl>,
## #   `Top 1` <tibble[,6]>

Here in rows we have all the segment-feature combinations (for "_3_" features) and in columns, among everything else, we have data-frame columns called “Top 2” and “Top 1” which contain stats specific to the corresponding boxes:

names(seg_rack_extended$indices_boxed[[1]]$`Top 2`) # Top 1 has the same
## [1] "index"           "pval"            "segment_perc"    "segment_n"      
## [5] "population_perc" "population_n"

There might be situations when some variables have different levels and thus boxes will be defined differently for them. It’s not the case with our toy data, but nobody stops us from making questionable decisions and calculating boxed indices for a "_2_" set of features separately and also without the “Top 1” box. We can do it as follows:

seg_rack_extended <-
  seg_rack_extended %>%
  rack_put_indices_boxed(
    cols = matches("_2_"),
    boxes = list(
      "Top 2" = c("Strongly agree", "Somewhat agree")
    )
  ) 

In this case summaries for "_2_" features will not override the existing summaries for “3” features, but instead will be appended beneath them.

seg_rack_extended$indices_boxed[[1]]$feature
##  [1] "m2_3_17" "m2_3_17" "m2_3_17" "m2_3_18" "m2_3_18" "m2_3_18" "m2_3_19"
##  [8] "m2_3_19" "m2_3_19" "m2_3_21" "m2_3_21" "m2_3_21" "m2_3_22" "m2_3_22"
## [15] "m2_3_22" "m2_3_23" "m2_3_23" "m2_3_23" "m2_2_8"  "m2_2_8"  "m2_2_8" 
## [22] "m2_2_9"  "m2_2_9"  "m2_2_9"  "m2_2_10" "m2_2_10" "m2_2_10" "m2_2_11"
## [29] "m2_2_11" "m2_2_11" "m2_2_12" "m2_2_12" "m2_2_12" "m2_2_13" "m2_2_13"
## [36] "m2_2_13" "m2_2_14" "m2_2_14" "m2_2_14" "m2_2_16" "m2_2_16" "m2_2_16"

As a final stroke, let’s pretend “Top 1” box of "_2_" features is something so exceptional that we want to put it aside. There’s a parameter called “id”. When you set some short description to it, for example “other”, it puts the new index summaries into a different column, “indices_boxed_other” in this case.

seg_rack_extended <- 
  seg_rack_extended %>%
  # put "Top 1" indices for battery "2" to "indices_boxed_other"
  rack_put_indices_boxed(
    cols = matches("_2_"),
    boxes = list(
      "Top 1" = c("Strongly agree")
    ),
    id = "other"
  )

Detailed indices

There’s a different type of index summaries which we call “detailed” and this word is chosen mainly to oppose the “boxed” option. In this summary we don’t combine feature levels into boxes and calculate index statistics for each of the levels separately. Here’s how we could add detailed index summaries for a set of demo features to a rack:

seg_rack_extended <- 
  seg_rack_extended %>% 
  rack_put_indices_detailed(
    data = example_demo_data,
    cols = everything(),
    id = "demo"
  ) 

As you see, first we prepare a dataframe with the necessary columns, demo_data because these columns do not exist in the rack. Another option could be to create a rack column called “demo”, put the demo dataframe into each of the elements and then set data = "demo" to point rack_put_indices_detailed() to it.

Note that rack_put_indices_boxed() also has the data argument and it’s default value is “data$x” (as it is for *_detailed()). Which basically means that by default we take the features the models were trained on.

Let’s take a glance at the rack column which rack_put_indices_detailed() creates, “indices_detailed_demo” in our case:

seg_rack_extended$indices_detailed_demo[[1]]
## # A tibble: 2 × 5
##   grp_var grp_var_label sbj_var  sbj_var_label stats             
##   <chr>   <chr>         <chr>    <chr>         <list>            
## 1 segment NA            s_age    Age group     <tibble [12 × 12]>
## 2 segment NA            s_gender Gender        <tibble [6 × 12]>

This is an output of g6tr.survey::srv_prop_stats_crossing(), a nested dataframe with one row per one column. Each element of the stats column is a dataframe of index stats for the corresponding feature.

Try running rack_render_everything() to see how detailed index summaries are printed to spreadsheet format.

Tall and Wide spreadsheet summaries

At any step of index calculations described above you are free to generate spreadsheet summaries.

rack_render_everything(seg_rack_extended, file = "summary_with_some_indices.xlsx")

If you open the resulting xlsx file, you’ll see tabs named “Index tall” and tabs named “Index wide”. The former would resemble the way summaries are stored in the rack. “Wide” summaries in turn contain much less information and only show index values for the first box whatever it happens to be.