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
seg_shelf <- example_seg_shelf

to take the example data provided with the package.

Feature importance

A Typing tool is essentially a model to efficiently predict segments of new respondents using a minimal subset of the original feature set. So first what we do is we assess importance of the features.

There are multiple ways to do this, but the general idea is that features with higher importance values are those which damage the accuracy of a model the most if they are removed. Here’s how we add importance scores to the rack;

seg_rack_extended <-
  seg_rack %>% 
  rack_put_feature_importance()

Take a look at the importance scores for the first NMF model:

seg_rack_extended$feature_importance[[1]]
## # A tibble: 54 × 3
##    feature label                                                      importance
##    <chr>   <chr>                                                           <dbl>
##  1 m2_1_5  I feel like I’m too much of a burden on the people I care…      0.214
##  2 m2_6_54 I usually pay the minimum monthly payment on my credit ca…      0.200
##  3 m2_5_45 I just buy things that I might need and then return what …      0.181
##  4 m2_6_56 Credit cards are designed to hurt card holders.                 0.166
##  5 m2_4_32 I  feel confident about my ability to manage money              0.132
##  6 m2_4_30 I’m a saver not a spender                                       0.111
##  7 m2_6_48 I spend more than I otherwise would when paying with a cr…      0.105
##  8 m2_1_1  Achieving personal success is most important to me              0    
##  9 m2_1_2  I tend to accomplish small life goals at a time rather th…      0    
## 10 m2_1_3  I have a specific role model that has had a significant i…      0    
## # ℹ 44 more rows

Run rack_render_everything() to see how importance scores are rendered to a spreadsheet.

Typing tool stats

Now to the main thing, Typing Tool assessments. Consider one segmentation model and the segments it assigns to the data cases. The task is to find a supervised model such that being trained on a small subset of original features predicts segment assignments sufficiently accurately.

Let’s focus on one segmentation model and get a bit into implementation internals. First let’s extract one row of the rack, or using the package terminology, shelf.

seg_shelf <- rack_extract_shelf(seg_rack_extended, 1)
seg_shelf$model_name
## [1] nmf_3
## Levels: nmf_3 nmf_4 nmf_5
seg_shelf$model
## <Object of class: NMFfitX1 >
##   Method: brunet 
##   Runs:  5 
##   RNG:
##    10407L, -1535484873L, 1222746892L, 1963142301L, 158053050L, -1240755981L, -292394600L 
##   Total timing:
##    user  system elapsed 
##   4.638   0.014   4.584

We start with defining a set of model specifications to try:

model_specs <- tt_get_model_specs()
model_specs
## # A tibble: 6 × 2
##   name     spec     
##   <chr>    <list>   
## 1 xgboost  <spec[+]>
## 2 lda      <spec[+]>
## 3 lightgbm <spec[+]>
## 4 rf       <spec[+]>
## 5 glmnet   <spec[+]>
## 6 svm_rbf  <spec[+]>

Each element of the spec column is a model specification object from the parsnip package. It’s not a model yet, but rather a standardized interface to call a model from a different package.

Next we prepare data cuts to train and test our models on. As mentioned earlier, we have a method to assess importance of the features and the task is to take only small subset of the best of them. So what we are going to do is to prepare smaller data packs with a limited set of features and to split the into train/test parts to be able to assess the TT models.

data_cuts <- shelf_make_tt_data_cuts(seg_shelf, n_var = c(5, 8, 10, 12, 15))
data_cuts
## # A tibble: 5 × 3
##   n_var train             test             
##   <dbl> <list>            <list>           
## 1     5 <tibble [74 × 3]> <tibble [26 × 3]>
## 2     8 <tibble [74 × 3]> <tibble [26 × 3]>
## 3    10 <tibble [74 × 3]> <tibble [26 × 3]>
## 4    12 <tibble [74 × 3]> <tibble [26 × 3]>
## 5    15 <tibble [74 × 3]> <tibble [26 × 3]>

The train element corresponding to n_var == 5 only the first 5 most important features and only a subset of rows. Corresponding test element contains the rest of the rows.

data_cuts$train[[1]]
## # A tibble: 74 × 3
##    segment weight x$m2_1_5              $m2_6_54      $m2_5_45 $m2_6_56 $m2_4_32
##    <fct>    <dbl> <dbl+lbl>             <dbl+lbl>     <dbl+lb> <dbl+lb> <dbl+lb>
##  1 Green    0.879 4 [Strongly agree]    4 [Strongly … 4 [Stro… 4 [Stro… 4 [Stro…
##  2 Green    0.707 3 [Somewhat agree]    3 [Somewhat … 1 [Stro… 2 [Some… 3 [Some…
##  3 Green    1.41  1 [Strongly disagree] 4 [Strongly … 1 [Stro… 2 [Some… 3 [Some…
##  4 Green    1.17  2 [Somewhat disagree] 4 [Strongly … 2 [Some… 2 [Some… 3 [Some…
##  5 Green    1.14  2 [Somewhat disagree] 2 [Somewhat … 2 [Some… 2 [Some… 3 [Some…
##  6 Green    0.566 4 [Strongly agree]    4 [Strongly … 4 [Stro… 4 [Stro… 4 [Stro…
##  7 Green    1.50  3 [Somewhat agree]    3 [Somewhat … 3 [Some… 3 [Some… 3 [Some…
##  8 Green    0.893 4 [Strongly agree]    2 [Somewhat … 1 [Stro… 2 [Some… 3 [Some…
##  9 Green    0.847 2 [Somewhat disagree] 4 [Strongly … 1 [Stro… 2 [Some… 3 [Some…
## 10 Green    1.30  3 [Somewhat agree]    3 [Somewhat … 3 [Some… 3 [Some… 3 [Some…
## # ℹ 64 more rows

Having a set of model specifications and and a set of data cuts, we can create all combinations of former with latter:

model_grid <- crossing(model_specs, data_cuts)
model_grid
## # A tibble: 30 × 5
##    name   spec      n_var train             test             
##    <chr>  <list>    <dbl> <list>            <list>           
##  1 glmnet <spec[+]>     5 <tibble [74 × 3]> <tibble [26 × 3]>
##  2 glmnet <spec[+]>     8 <tibble [74 × 3]> <tibble [26 × 3]>
##  3 glmnet <spec[+]>    10 <tibble [74 × 3]> <tibble [26 × 3]>
##  4 glmnet <spec[+]>    12 <tibble [74 × 3]> <tibble [26 × 3]>
##  5 glmnet <spec[+]>    15 <tibble [74 × 3]> <tibble [26 × 3]>
##  6 lda    <spec[+]>     5 <tibble [74 × 3]> <tibble [26 × 3]>
##  7 lda    <spec[+]>     8 <tibble [74 × 3]> <tibble [26 × 3]>
##  8 lda    <spec[+]>    10 <tibble [74 × 3]> <tibble [26 × 3]>
##  9 lda    <spec[+]>    12 <tibble [74 × 3]> <tibble [26 × 3]>
## 10 lda    <spec[+]>    15 <tibble [74 × 3]> <tibble [26 × 3]>
## # ℹ 20 more rows

We then fit corresponding TT models on train for each for the rows and assess them on test. But! You don’t need to do it manually and there’s a function already to perform such calculations for each model of the rack

seg_rack_extended <- 
  seg_rack_extended %>% 
  rack_put_tt_stats()

It is not necessary to put feature importances to the rack as they are relatively cheap to calculate and will be calculated automatically if absent. If you take a look at an element of the tt_stats column you see the model_grid skeleton. But in addition it’ll also contain predictions of the TT models and their assessments such as accuracy and others.

Run rack_render_everything(seg_rack_extended, "nmf_summary.xlsx") to see what a Typing Tool spreadsheet summary looks like.