Skip to contents

Introduction

Once we’ve decided on a segmentation solution, one of the next steps is to address how to predict segments for new, unseen respondents. The segmentation model we use, Non-negative Matrix Factorization (NMF), like many other segmentation models, cannot be directly applied for this purpose. It only determines segments for the respondents on whose data it was trained and cannot process new data. Therefore, we need to build a separate predictive model trained on the original features to predict segment assignments for new data.

The main scenario for applying such a model is what’s referred to as a typing tool. We aim to predict segments for new respondents accurately while asking as few questions as possible. Another, less common scenario, involves having a group of respondents with complete data who were not included in the original segmentation. In this case, we want to determine where these respondents would have been assigned according to the segmentation model. Both of these cases will be discussed in detail further in this article.

For now, let’s assume we’ve chosen some certain segmentation solution. Regardless of the scenario, the first step is to extract the corresponding shelf from the model rack:

library(g6tr.segmentation)
library(dplyr, warn.conflicts = FALSE)

shelf <-
  example_seg_rack_extended %>% 
  filter(n_seg == 4) %>% 
  rack_extract_shelf()

# set some segment names
levels(shelf$segments) <- c("Yellow", "Red", "Green", "Blue")

Fitting a typing tool model

To determine the specifications for the typing tool model, we prepare the so-called typing tool stats. These stats include performance metrics for a standard set of model types — random forest, SVM, GLM, LDA and others. Each model is applied to various subsets of the original data, where we keep different numbers of the most important features (e.g., 5…10, 8-10-12-15-18). For each combination, a model is evaluated using a train/test split.

The rack_put_tt_stats() function generates these typing tool stats when applied to a rack. In our case, it was used on example_seg_rack_extended, so you’ll find the typing tool stats attached to the shelf object.

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.751 0.120
##  2 glmnet <spec[+]>     8 <tibble> <tibble> <fit[+]> <tibble>    0.739 0.125
##  3 glmnet <spec[+]>    10 <tibble> <tibble> <fit[+]> <tibble>    0.733 0.129
##  4 glmnet <spec[+]>    12 <tibble> <tibble> <fit[+]> <tibble>    0.719 0.117
##  5 glmnet <spec[+]>    15 <tibble> <tibble> <fit[+]> <tibble>    0.728 0.135
##  6 lda    <spec[+]>     5 <tibble> <tibble> <fit[+]> <tibble>    0.748 0.128
##  7 lda    <spec[+]>     8 <tibble> <tibble> <fit[+]> <tibble>    0.724 0.150
##  8 lda    <spec[+]>    10 <tibble> <tibble> <fit[+]> <tibble>    0.643 0.151
##  9 lda    <spec[+]>    12 <tibble> <tibble> <fit[+]> <tibble>    0.707 0.134
## 10 lda    <spec[+]>    15 <tibble> <tibble> <fit[+]> <tibble>    0.646 0.163
## # ℹ 20 more rows
## # ℹ 1 more variable: confusion_matrix <list>

If the typing tool stats were not already prepared, we could still generate them for our shelf with the following code:

shelf$tt_stats <- shelf_make_tt_stats(shelf)

Out of these different model specifications, we need to choose one for the final typing tool model. The criteria for selection can vary. Here are some examples:

  • the most accurate model among those using a maximum of 12 features
  • the model using the fewest features among those with an assessed accuracy of at least 80%
  • in some cases, we might prefer balanced options that offer good accuracy (though not the best) while using a relatively small number of features

Specifications are extracted using the tt_stats_extract_spec() function. By passing the TT stats table and the index of the desired specification to this function, it will extract a specification object. Alternatively, you can rearrange the TT stats table so that your desired specification appears in the first row; the function will then extract the first specification by default.

Here’s how we can extract a specification based on the second criterion mentioned earlier.

spec <- 
  shelf$tt_stats %>% 
  filter(accuracy > 0.8) %>% 
  arrange(n_var) %>% 
  slice(1) %>% 
  tt_stats_extract_spec()

You may notice that the spec object already contains a model element. While we could use this model, it was built for the test/train split assessment process and is based on a subset of the data. To achieve better expected accuracy, the final model should be built on all available data.

To do this, we take the original shelf object and our specification spec, and use the shelf_make_tt_model() function to fit the model on all the data in the shelf.

tt_model <- shelf_make_tt_model(shelf, spec)

tt_model is our final typing tool model object.

Archiving a typing tool model

After building a typing tool model, there is a possibility that this model may be needed for a different project in the future. To facilitate this, we prefer to keep track of our models in a centralized archive.

Here is an example of how to archive a model:

tt_model_archive(
  model = tt_model,
  name = "example_tt__seg",
  title = "4-segment solution for project Alpha",
  model_s3_path = "alpha/models/tt_alpha_4_seg.rds"
) -> yaml_path

This code:

  1. Saves the model object at the path specified in the model_s3_path parameter in our monorepo-data repository.

  2. Creates a YAML file with the model description under “atheneum/tt_archive” in the monorepo.

  3. Returns the path to the YAML file.

Later, you can retrieve this file or any other file in the archive folder and read the corresponding model object using the tt_model_archive_read() function.

Predicting Segments from a Dataframe

If you have a dataframe with new data that you want to predict segments for, use the tt_model_predict() function. You can also use the predict() method with new data supplied to the new_data parameter:

# not recommended
predict(tt_model, new_data = shelf$data$x[1:10,])

This works because tt_model is an object of the "model_fit" class from the parsnip package. However, we recommend using tt_model_predict() because it is designed to check the input data against the original training data and warn you if the input data ranges or labels differ.

tt_model_predict(tt_model, x = shelf$data$x[1:10,])

Typing Tool Spreadsheet

Some clients prefer to use the typing tool model in the form of a spreadsheet. This will be a Google Sheets file where they can interactively input data and get segment assignments. The only thing needed to build a TT spreadsheet file is a tt_model object:

tt_model_render_tt_xlsx(tt_model, "example_tt.xlsx")

# commands to upload the file to Google Drive
# ...

Please note, this function only works for glmnet models. The good news is that glmnet is typically one of the most performant options for NMF segmentations.

Complete typing tool model

As mentioned earlier, there could be a scenario where respondents with complete data were not included in the initial segmentation and we aim to identify their segment assignments based on the existing model.

In this situation, we no longer need to minimize the number of features used in the final model. Instead, the most appropriate strategy is to use all available features.

To facilitate this, we provide the shelf_make_complete_tt_stats() function, which creates a TT stats table similar to shelf_make_tt_stats(), but without the option to set different numbers of features to try. All specifications will use all available features.

If you prefer not to manually pick a specification to build the model and simply want the one with the highest assessed accuracy, you can use the shelf_make_complete_tt_model() function, which only requires you to provide a shelf object.

tt_model_complete <- shelf_make_complete_tt_model(shelf)

tt_model_predict(tt_model_complete, x = shelf$data$x[1:10,])