Skip to contents

… or a very standard example with minimum explanations. Just run the code to get a sense on how the whole thing works. To learn about the terminology, underlying logic, and how to create more custom summaries, please see the other vignettes.

1. Fit a set of NMF models

Pack data for modelling

seg_pack <- 
  pack_model_data(
    data = example_seg_df[1:100,], # trim to save time
    id_col = response_id,
    weight_col = weight,
    x_cols = starts_with("m2_")
  )
seg_pack <- example_seg_pack

Fit models

seg_rack <- 
  seg_rack_nmf(
    data = seg_pack,
    n_seg = 3:5,
    nrun = 5,
    seed = 1L
  )

2. Add index summaries

Boxed indices for feature columns

seg_rack_extended <-
  seg_rack %>%
  rack_put_indices_boxed(
    cols = everything(),
    boxes = list(
      "Top 2" = c("Strongly agree", "Somewhat agree"),
      "Top 1" = c("Strongly agree"),
      "Bottom 1" = c("Strongly disagree")
    )
  )

Indices for demo columns

demo_data <- example_seg_df[1:100, c("s_age", "s_gender")]

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

3. Add typing tool summaries

… along with feature importance values.

seg_rack_extended <-
  seg_rack_extended %>% 
  rack_put_feature_importance() %>% 
  rack_put_tt_stats(n_var = c(8, 10, 12, 15))

That’s it! Create a spreadsheet with summaries via:

rack_render_everything(seg_rack_extended, "nmf_summaries.xlsx")

4. Choose the final segmentation solution and typing tool

You’ve made a decision on the segmentation solution. For example let it be the 3-segment solution. Extract the corresponding shelf.

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

Set descriptive segment names:

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

For the typing tool, let’s use glmnet with 12 features. Extract the corresponding model specification (this is not the final TT model yet).

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

Build the final model using all the available data.

tt_model <- shelf_make_tt_model(seg_shelf, spec)

Archive the TT model.

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

Next time you need to use this TT model:

tt_model <- tt_model_archive_read(yaml_path)

Let’s say this is new data to assign segments to.

# randomly shuffle original data
new_x <- mutate_all(example_tt_model$data$x, sample)

To apply the model to the new data:

tt_model_predict(example_tt_model, new_x)