Skip to contents

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

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

seg_rack_extended <- example_seg_rack_extended

Select the final segmentation solution

Once we have created a summary spreadsheet with rack_render_everything() (see previous chapter) we have everything to make our decision on the final segmentation solution to use. Typically our research managers will make this decision. I’ll omit any reasoning here and will just pretend that we selected the 3-segment solution. Let’s focus on it:

seg_shelf <- 
  seg_rack_extended %>% 
  filter(n_seg == 3) %>% 
  rack_extract_shelf()

This seg_shelf object does not contain any new information, we’ve just extracted the rack row corresponding to the 3-segment model for the ease of use.

Next thing we should do, is set meaningful segment names. By default it’s just numbers, and typically for the final solutions we’d have names like “Science in Society”, “Gut-Thinkers” etc. In this particular case let’s not think too much about it:

levels(seg_shelf$segments) <- c("Yellow", "Red", "Green")

Done, we’ve set some “meaningful” names.

Prepare the final typing tool model

Now let’s prepare a typing tool for this segmentation model. In a similar fashion how a rack contains a set of models along with various attributes and assessments of these models, tt_stats element of a shelf describes a set of typing tool models for a particular segmentation model.

seg_shelf$tt_stats
## # A tibble: 30 × 10
##    name   spec      n_var train    test     model    pred     accuracy brier
##    <chr>  <list>    <dbl> <list>   <list>   <list>   <list>      <dbl> <dbl>
##  1 glmnet <spec[+]>     5 <tibble> <tibble> <fit[+]> <tibble>    0.713 0.175
##  2 glmnet <spec[+]>     8 <tibble> <tibble> <fit[+]> <tibble>    0.798 0.147
##  3 glmnet <spec[+]>    10 <tibble> <tibble> <fit[+]> <tibble>    0.747 0.153
##  4 glmnet <spec[+]>    12 <tibble> <tibble> <fit[+]> <tibble>    0.784 0.139
##  5 glmnet <spec[+]>    15 <tibble> <tibble> <fit[+]> <tibble>    0.789 0.137
##  6 lda    <spec[+]>     5 <tibble> <tibble> <fit[+]> <tibble>    0.712 0.213
##  7 lda    <spec[+]>     8 <tibble> <tibble> <fit[+]> <tibble>    0.752 0.199
##  8 lda    <spec[+]>    10 <tibble> <tibble> <fit[+]> <tibble>    0.752 0.203
##  9 lda    <spec[+]>    12 <tibble> <tibble> <fit[+]> <tibble>    0.752 0.198
## 10 lda    <spec[+]>    15 <tibble> <tibble> <fit[+]> <tibble>    0.743 0.182
## # ℹ 20 more rows
## # ℹ 1 more variable: confusion_matrix <list>

Again, I’ll omit any particular reasoning regarding the choice of a typing tool model. Let’s say we decide on the “glmnet” model with 12 features. Similarly to how we extract a shelf from a rack, here we extract a model specification from the tt_stats element.

spec <- 
  seg_shelf$tt_stats %>% 
  filter(name == "glmnet", n_var == 12) %>% 
  tt_stats_extract_spec()

spec$model already contains a model object, but this model is fitted on the so-called train data, i.e. a portion (75%) of all the responses we have. To get the best results on new data we should retrain the model on all responses. This is what we do:

tt_model <- shelf_make_tt_model(seg_shelf, spec)

This model uses all the responses from seg_shelf$data, but note, only the 12 most important features. The object, tt_model is of class “model_fit” from parsnip package and it can be used as such:

parsnip::predict.model_fit(tt_model, new_data = seg_shelf$data$x)
## # A tibble: 100 × 1
##    .pred_class
##    <fct>      
##  1 Green      
##  2 Green      
##  3 Green      
##  4 Red        
##  5 Green      
##  6 Red        
##  7 Red        
##  8 Green      
##  9 Green      
## 10 Green      
## # ℹ 90 more rows

But apart from the standard fields that parsnip creates we also attach:

  • data: tt_model$data contains the data that was used for fitting, unused features are dropped.

  • accuracy, brier and confusion_matrix - model metrics taken directly from spec.

Keeping the original data with the model object allows us to use it for checking new data. What if feature levels are labelled in reverse order, or what if new data contains some values outside of the original ranges? If we apply the prediction method provided with the g6tr.segmentation package it’ll make sure to check for these issues.

Simulate new data by randomly shuffling original data:

new_x <- mutate_all(example_tt_model$data$x, sample)
tt_model_predict(example_tt_model, new_x)

Mess up the data and watch it complaining:

new_x_corrupted <- new_x
new_x_corrupted$m2_1_5[7] <- 7
new_x_corrupted$m2_1_5[8] <- -1
names(attr(new_x_corrupted$m2_1_5, "labels")) <-
  rev(names(attr(new_x$m2_1_5, "labels")))

tt_model_predict(example_tt_model, new_x_corrupted)

Archive a typing tool model

The time to use the typing tool we’ve prepared might come only in a distant future. That’s why we need to properly organize our typing tool models. For this purpose we have a helper function:

tt_model_archive(
  model = tt_model,
  name = "example_tt_3_seg",
  title = "3-segment solution for g6tr.segmentation test data",
  model_s3_path = "test/models/tt_3_seg.rds"
) -> yaml_path

It takes:

  • a model object,
  • a model code name,
  • a more verbose model title,
  • a path on S3 to store the model at.

The function saves the model object at a given S3 path and creates a yaml file that captures the S3 location and some of the information about the model. yaml file path is constructed as atheneum/tt_archive/{ISO 8601 date}_{name}.yaml and is relative to the monorepo root, not the project root.

With regards to the model details that are captured in the yaml, these are meant for the future you or other analysts to quickly identify what project is it related to, what features does it use, what segments does it output and how well is it expected to perform.

The future you can read the model object with

tt_model <- tt_model_archive_read(yaml_path)