1. Fitting a set of segmentation models
Iaroslav Domin
2026-06-17
Source:vignettes/ch_1_fitting_models.Rmd
ch_1_fitting_models.RmdModel data pack
Creating a model data pack
We start by creating a specially organized dataset, a so-called “data pack” to be fed into subsequent functions for training segmentation models.
seg_pack <- pack_model_data(
example_seg_df,
id_col = response_id,
weight_col = weight,
x_cols = starts_with("m2_1_")
)## ✔ Converted 7 <factor> columns to <labelled numeric>: m2_1_1, m2_1_2, m2_1_3, m2_1_4, m2_1_5, m2_1_6, and m2_1_7.
##
## ── Summary of mapping for 7 <labelled> columns ─────────────────────────────────
##
## [7 columns] All columns:
## 1: Strongly disagree
## 2: Somewhat disagree
## 3: Somewhat agree
## 4: Strongly agree
##
We do this by providing a dataframe along with instructions on how to identify:
id column - a numeric, character or factor variable without duplicates
weight column - a numeric variable such that all its values are non-missing and non-negative
feature columns - a set of numeric columns that we’ll be training our models on, or to put it in one word, features. All columns must be numeric and must not contain missing values. Note that in most of the cases we get segmentation features from SPSS files as labelled numerics. This works as well. If you want to convert factor columns to labelled numerics check out
fct_as_labelled()function.
Aforementioned instructions to identify these columns are nothing
other than tidyselect expressions (see dplyr::select()
documentation). So you can use quoted or unquoted names, ordinal numbers
or expressions including -, !,
starts_with(), where() etc.
There’s some default setup to save you typing:
if you don’t touch
weight_coland keep itNULL, it will be assumed that all weights are equal to 1.x_colsis set toeverything()by default, which means that all columns that were not identified as either id or weight column will be considered feature columns.
One more example on how one could create a different data pack
mtcars_pack <-
mtcars %>%
dplyr::mutate(rowname = rownames(.)) %>%
pack_model_data(id_col = "rowname", x_cols = -c(cyl, vs:carb))## ✔ Converted 0 <factor> columns to <labelled numeric>: .
## ℹ No <labelled> columns ware found!
What exactly is a model data pack?
So what is the output of the pack_model_data() function
and what about it makes it “data packing”?. Let’s take a look at
seg_pack
## # A tibble: 1,512 × 3
## id weight x$m2_1_1 $m2_1_2 $m2_1_3 $m2_1_4 $m2_1_5 $m2_1_6
## <chr> <dbl> <dbl+lbl> <dbl+l> <dbl+l> <dbl+l> <dbl+l> <dbl+l>
## 1 R_yyyU2RdwNtBtXHj 0.771 2 [Somewhat… 3 [Som… 2 [Som… 3 [Som… 2 [Som… 3 [Som…
## 2 R_3Jqm7E60eZmLHUL 0.879 4 [Strongly… 4 [Str… 4 [Str… 4 [Str… 4 [Str… 4 [Str…
## 3 R_TsVtU9pnig96wvf 1.08 3 [Somewhat… 3 [Som… 2 [Som… 3 [Som… 2 [Som… 3 [Som…
## 4 R_1Fn2QNuPz7eT8nG 1.42 4 [Strongly… 4 [Str… 3 [Som… 4 [Str… 3 [Som… 4 [Str…
## 5 R_ai3slhE8Ua9aDV7 0.707 2 [Somewhat… 3 [Som… 3 [Som… 3 [Som… 3 [Som… 3 [Som…
## 6 R_2uw8aNOkURhjVTk 1.41 3 [Somewhat… 4 [Str… 1 [Str… 4 [Str… 1 [Str… 3 [Som…
## 7 R_1PdETTmHmJpBbbh 1.46 1 [Strongly… 3 [Som… 3 [Som… 3 [Som… 1 [Str… 3 [Som…
## 8 R_enW7a74WZCtGehr 1.17 3 [Somewhat… 3 [Som… 2 [Som… 3 [Som… 2 [Som… 4 [Str…
## 9 R_1mCA8bWg0XAZ7Y2 1.14 3 [Somewhat… 3 [Som… 2 [Som… 3 [Som… 2 [Som… 2 [Som…
## 10 R_2V4HFTieiV5VhDG 0.566 4 [Strongly… 3 [Som… 4 [Str… 3 [Som… 4 [Str… 4 [Str…
## # ℹ 1,502 more rows
## # ℹ 1 more variable: x$m2_1_7 <dbl+lbl>
This looks like a normal dataframe although there’s one nuance: all
feature columns are packed under a common header x.
names(seg_pack)## [1] "id" "weight" "x"
So, as you can see, seg_pack formally has only three
columns, id, weight and x. These
names are always the same regardless of the column names in the original
flat dataframe.
x is in turn a dataframe-column meaning that it’s a
dataframe itself. If we extract it, we’ll see a flat dataframe
containing all the features. Features are taken as is from the original
dataframe and their names and attributes are not changed.
seg_pack$x## # A tibble: 1,512 × 7
## m2_1_1 m2_1_2 m2_1_3 m2_1_4 m2_1_5 m2_1_6 m2_1_7
## <dbl+lbl> <dbl+lbl> <dbl+l> <dbl+l> <dbl+l> <dbl+l> <dbl+l>
## 1 2 [Somewhat disagree] 3 [Somewhat ag… 2 [Som… 3 [Som… 2 [Som… 3 [Som… 3 [Som…
## 2 4 [Strongly agree] 4 [Strongly ag… 4 [Str… 4 [Str… 4 [Str… 4 [Str… 4 [Str…
## 3 3 [Somewhat agree] 3 [Somewhat ag… 2 [Som… 3 [Som… 2 [Som… 3 [Som… 3 [Som…
## 4 4 [Strongly agree] 4 [Strongly ag… 3 [Som… 4 [Str… 3 [Som… 4 [Str… 2 [Som…
## 5 2 [Somewhat disagree] 3 [Somewhat ag… 3 [Som… 3 [Som… 3 [Som… 3 [Som… 2 [Som…
## 6 3 [Somewhat agree] 4 [Strongly ag… 1 [Str… 4 [Str… 1 [Str… 3 [Som… 3 [Som…
## 7 1 [Strongly disagree] 3 [Somewhat ag… 3 [Som… 3 [Som… 1 [Str… 3 [Som… 3 [Som…
## 8 3 [Somewhat agree] 3 [Somewhat ag… 2 [Som… 3 [Som… 2 [Som… 4 [Str… 3 [Som…
## 9 3 [Somewhat agree] 3 [Somewhat ag… 2 [Som… 3 [Som… 2 [Som… 2 [Som… 2 [Som…
## 10 4 [Strongly agree] 3 [Somewhat ag… 4 [Str… 3 [Som… 4 [Str… 4 [Str… 4 [Str…
## # ℹ 1,502 more rows
To “unpack” the data pack you can use the
tidyr::unpack() function. In fact tidyr’s
pack() and unpack() functions is where this
terminology is inherited from.
tidyr::unpack(seg_pack, "x")## # A tibble: 1,512 × 9
## id weight m2_1_1 m2_1_2 m2_1_3 m2_1_4 m2_1_5 m2_1_6 m2_1_7
## <chr> <dbl> <dbl+l> <dbl+l> <dbl+l> <dbl+l> <dbl+l> <dbl+l> <dbl+l>
## 1 R_yyyU2RdwNtB… 0.771 2 [Som… 3 [Som… 2 [Som… 3 [Som… 2 [Som… 3 [Som… 3 [Som…
## 2 R_3Jqm7E60eZm… 0.879 4 [Str… 4 [Str… 4 [Str… 4 [Str… 4 [Str… 4 [Str… 4 [Str…
## 3 R_TsVtU9pnig9… 1.08 3 [Som… 3 [Som… 2 [Som… 3 [Som… 2 [Som… 3 [Som… 3 [Som…
## 4 R_1Fn2QNuPz7e… 1.42 4 [Str… 4 [Str… 3 [Som… 4 [Str… 3 [Som… 4 [Str… 2 [Som…
## 5 R_ai3slhE8Ua9… 0.707 2 [Som… 3 [Som… 3 [Som… 3 [Som… 3 [Som… 3 [Som… 2 [Som…
## 6 R_2uw8aNOkURh… 1.41 3 [Som… 4 [Str… 1 [Str… 4 [Str… 1 [Str… 3 [Som… 3 [Som…
## 7 R_1PdETTmHmJp… 1.46 1 [Str… 3 [Som… 3 [Som… 3 [Som… 1 [Str… 3 [Som… 3 [Som…
## 8 R_enW7a74WZCt… 1.17 3 [Som… 3 [Som… 2 [Som… 3 [Som… 2 [Som… 4 [Str… 3 [Som…
## 9 R_1mCA8bWg0XA… 1.14 3 [Som… 3 [Som… 2 [Som… 3 [Som… 2 [Som… 2 [Som… 2 [Som…
## 10 R_2V4HFTieiV5… 0.566 4 [Str… 3 [Som… 4 [Str… 3 [Som… 4 [Str… 4 [Str… 4 [Str…
## # ℹ 1,502 more rows
Fitting a set of NMF models
Creating a model rack
It’s time to introduce some more terminology! To fit a set of NMF
models for different number of segments we run the
seg_rack_nmf() function.
seg_rack <- seg_rack_nmf(
seg_pack,
n_seg = 3:5,
nrun = 5,
seed = 1L
)This function fits NMF models for each number of segments using the
NMF package, NMF::nmfEstimateRank() function in particular,
and creates what we call a model rack. Essentially it’s a
dataframe with a complex nested structure where:
NMF models are stored in the model column - each element of such a column is a model.
Data pack that was used for training the model is in the data column - each element contains the same
seg_packin our case. But potentially you may want to combine racks built on different column sets in which case data elements won’t all be the same.Segment assignments for corresponding models are stored in segments - each element is a vector length equal to the number of rows in the corresponding
data.Then you have n_seg and model_name - number of segments and short model identifiers. In our case it’ll be “nmf_3”, “nmf_4”, “nmf_5”.
Finally you’ll see columns containing different model statistics. consensus contains feature consensus matrices for each of the model. And measures is a dataframe-column containing various model measures like sparseness, silhouette or dispersion.
You can think of a model rack as a real-world paletted rack where each shelf corresponds to one model and contains different pieces of information about it.

Basic spreadsheet summary
As soon as we have a model rack, we can create a simple spreadsheet summary:
rack_render_everything(seg_rack, file = "basic_summary.xlsx")It will contain only three sheets:
Segment breakdowns - with counts for each model for each of the segments. Percentages take into account case weights which we’ve put in
seg_pack$weight. Or, to be more precise, weights for each of the models will be taken from the correspondingdataelement of the rack although in our case these elements are all the same.Segment assignments - is a table which lists segment assignments for each data case, one column with assignments per model.
measures - more or less just
measureselement of the rack printed, but with some useless columns dropped.
That’s it! To create more informative summaries with index stats, typing tool assessments etc, you need to put things to the rack. This will be discussed in other vignettes.