Skip to contents

Once you've decided on a final TT model specification you should fit it on all the data available for best results. model objects in a tt_stats table are fit on a train subset which usually contains ~75% of the rows. Or, in other words, they had only seen a part of available information.

Given a segmentation model shelf, first you extract a TT model specification from the tt_stats field of the shelf using tt_stats_extract_spec(). Then you apply shelf_make_tt_model()

This function takes

  • your shelf extracted from a rack with rack_extract_shelf

  • a model specification extracted from tt_stats field of the shelf

and fits a model according to the specification on all the observations in shelf$data.

shelf_make_complete_tt_model() is a wrapper for fitting the best by accuracy complete typing tool, i.e. model using all available features.

To apply a fitted TT model, use tt_model_predict.

Usage

shelf_make_complete_tt_model(shelf)

tt_stats_extract_spec(tt_stats, ind = 1)

shelf_make_tt_model(shelf, spec)

Arguments

shelf

A rack shelf, or in other words, a row of a model rack extracted with rack_extract_shelf().

tt_stats

A tt_stats element of a shelf.

ind

Index of the row to extract.

spec

A model specification. See examples on how to extract one from a TT summary.

Examples

library(dplyr)

# suppose we've decided on the segmentation solution with 4 segments
shelf <-
  example_seg_rack_extended %>% 
  filter(n_seg == 4) %>% 
  rack_extract_shelf()

# set descriptive segment names, they will be captured by the TT model
levels(shelf$segments) <- c("Yellow", "Red", "Green", "Blue")

# for the typing tool, let's select the spec with the least number of
# features used among those with assessed accuracy > 0.75
spec <- 
  shelf$tt_stats %>% 
  filter(accuracy > 0.8) %>% 
  arrange(n_var) %>% 
  slice(1) %>% 
  tt_stats_extract_spec()

tt_model <- shelf_make_tt_model(shelf, spec)

# data is stored in the `data` field of the model object
tt_model$data
#> # A tibble: 100 × 4
#>    id                weight segment x$m2_5_45 $m2_6_54 $m2_1_5 $m2_6_56 $m2_3_21
#>    <chr>              <dbl> <fct>   <dbl+lbl> <dbl+lb> <dbl+l> <dbl+lb> <dbl+lb>
#>  1 R_yyyU2RdwNtBtXHj  0.771 Green   2 [Somew… 2 [Some… 2 [Som… 2 [Some… 2 [Some…
#>  2 R_3Jqm7E60eZmLHUL  0.879 Yellow  4 [Stron… 4 [Stro… 4 [Str… 4 [Stro… 4 [Stro…
#>  3 R_TsVtU9pnig96wvf  1.08  Green   2 [Somew… 3 [Some… 2 [Som… 3 [Some… 2 [Some…
#>  4 R_1Fn2QNuPz7eT8nG  1.42  Red     1 [Stron… 1 [Stro… 3 [Som… 1 [Stro… 1 [Stro…
#>  5 R_ai3slhE8Ua9aDV7  0.707 Green   1 [Stron… 3 [Some… 3 [Som… 2 [Some… 2 [Some…
#>  6 R_2uw8aNOkURhjVTk  1.41  Green   1 [Stron… 4 [Stro… 1 [Str… 2 [Some… 1 [Stro…
#>  7 R_1PdETTmHmJpBbbh  1.46  Red     4 [Stron… 1 [Stro… 1 [Str… 1 [Stro… 3 [Some…
#>  8 R_enW7a74WZCtGehr  1.17  Yellow  2 [Somew… 4 [Stro… 2 [Som… 2 [Some… 2 [Some…
#>  9 R_1mCA8bWg0XAZ7Y2  1.14  Green   2 [Somew… 2 [Some… 2 [Som… 2 [Some… 2 [Some…
#> 10 R_2V4HFTieiV5VhDG  0.566 Yellow  4 [Stron… 4 [Stro… 4 [Str… 4 [Stro… 4 [Stro…
#> # ℹ 90 more rows

# best by accuracy complete TT (using all available features)
shelf_make_complete_tt_model(example_seg_shelf)
#> parsnip model object
#> 
#> 
#> Call:  glmnet::glmnet(x = maybe_matrix(x), y = y, family = "multinomial",      alpha = ~double(1)) 
#> 
#>     Df  %Dev  Lambda
#> 1   54  0.00 301.900
#> 2   54  0.60 275.000
#> 3   54  0.65 250.600
#> 4   54  0.72 228.300
#> 5   54  0.79 208.100
#> 6   54  0.86 189.600
#> 7   54  0.94 172.700
#> 8   54  1.04 157.400
#> 9   54  1.13 143.400
#> 10  54  1.24 130.700
#> 11  54  1.36 119.100
#> 12  54  1.49 108.500
#> 13  54  1.63  98.850
#> 14  54  1.79  90.060
#> 15  54  1.96  82.060
#> 16  54  2.14  74.770
#> 17  54  2.34  68.130
#> 18  54  2.56  62.080
#> 19  54  2.80  56.560
#> 20  54  3.06  51.540
#> 21  54  3.34  46.960
#> 22  54  3.65  42.790
#> 23  54  3.98  38.990
#> 24  54  4.34  35.520
#> 25  54  4.73  32.370
#> 26  54  5.15  29.490
#> 27  54  5.61  26.870
#> 28  54  6.10  24.480
#> 29  54  6.63  22.310
#> 30  54  7.21  20.330
#> 31  54  7.82  18.520
#> 32  54  8.48  16.880
#> 33  54  9.19  15.380
#> 34  54  9.94  14.010
#> 35  54 10.75  12.770
#> 36  54 11.61  11.630
#> 37  54 12.51  10.600
#> 38  54 13.48   9.657
#> 39  54 14.50   8.799
#> 40  54 15.57   8.018
#> 41  54 16.69   7.305
#> 42  54 17.87   6.656
#> 43  54 19.11   6.065
#> 44  54 20.39   5.526
#> 45  54 21.73   5.035
#> 46  54 23.11   4.588
#> 47  54 24.54   4.180
#> 48  54 26.01   3.809
#> 49  54 27.52   3.471
#> 50  54 29.07   3.162
#> 51  54 30.64   2.881
#> 52  54 32.25   2.625
#> 53  54 33.88   2.392
#> 54  54 35.53   2.180
#> 55  54 37.19   1.986
#> 56  54 38.86   1.810
#> 57  54 40.55   1.649
#> 58  54 42.23   1.502
#> 59  54 43.92   1.369
#> 60  54 45.60   1.247
#> 61  54 47.27   1.136
#> 62  54 48.93   1.036
#> 63  54 50.58   0.944
#> 64  54 52.21   0.860
#> 65  54 53.83   0.783
#> 66  54 55.42   0.714
#> 67  54 56.99   0.650
#> 68  54 58.54   0.593
#> 69  54 60.06   0.540
#> 70  54 61.55   0.492
#> 71  54 63.01   0.448
#> 72  54 64.45   0.408
#> 73  54 65.86   0.372
#> 74  54 67.23   0.339
#> 75  54 68.57   0.309
#> 76  54 69.88   0.281
#> 77  54 71.16   0.256
#> 78  54 72.41   0.234
#> 79  54 73.62   0.213
#> 80  54 74.80   0.194
#> 81  54 75.94   0.177
#> 82  54 77.05   0.161
#> 83  54 78.13   0.147
#> 84  54 79.18   0.134
#> 85  54 80.19   0.122
#> 86  54 81.17   0.111
#> 87  54 82.11   0.101
#> 88  54 83.02   0.092
#> 89  54 83.90   0.084
#> 90  54 84.75   0.077
#> 91  54 85.56   0.070
#> 92  54 86.34   0.064
#> 93  54 87.09   0.058
#> 94  54 87.80   0.053
#> 95  54 88.49   0.048
#> 96  54 89.14   0.044
#> 97  54 89.77   0.040
#> 98  54 90.36   0.036
#> 99  54 90.93   0.033
#> 100 54 91.47   0.030