|
| 1 | +--- |
| 2 | +title: "ABC Segmentation" |
| 3 | +subtitle: "How to use segmentation strategies to simplify your analysis" |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +```{r} |
| 8 | +#| echo: false |
| 9 | +quarto::write_yaml_metadata_block( |
| 10 | + date=format(Sys.Date()) |
| 11 | + ) |
| 12 | +``` |
| 13 | + |
| 14 | + |
| 15 | +### Introduction |
| 16 | + |
| 17 | +In business and financial analytics, the **ABC classification** method is widely used to categorize items, customers, or other groups according to their relative contribution to a total metric. |
| 18 | + |
| 19 | +The `abc()` function provides a flexible and robust way to perform ABC segmentation, either based on **transaction counts** or the sum of a **numeric variable** such as sales margin or revenue. |
| 20 | + |
| 21 | +### Function Purpose |
| 22 | + |
| 23 | +`abc()`: |
| 24 | + |
| 25 | +- Segments a dataset into categories (A, B, C, etc.) based on cumulative contribution. |
| 26 | +- Allows for **custom break points** (e.g., top 10% = A, next 40% = B, etc.). |
| 27 | +- Works on both **grouped tibbles** and **database-backed objects**. |
| 28 | +- Returns a **segment object**, which is processed by `calculate()` to produce a table of results. |
| 29 | + |
| 30 | +### How It Works |
| 31 | + |
| 32 | +> `fpaR::abc()` requires a grouped tibble or lazy DBI object using `dplyr::group_by()` to specify the groups that drive the contribution |
| 33 | +
|
| 34 | +**Value Capture** |
| 35 | + |
| 36 | +- If `.value` is provided, the the column is aggregate per group; otherwise, it counts rows. |
| 37 | + |
| 38 | +**Category Values** |
| 39 | + |
| 40 | +- Provide the break points break points that are used to set the cumulative categories. Each break point will get a letter category starting with 'A' |
| 41 | +- If you want to see the stores that make up the top 40% of revenue follow by top 70% and then 90% you should put in `c(0.4,.7,.9,1)` |
| 42 | + |
| 43 | + |
| 44 | +```{r} |
| 45 | +#| label: tbl-abc-args |
| 46 | +#| echo: false |
| 47 | +#| eval: true |
| 48 | +
|
| 49 | +tibble::tibble( |
| 50 | + Argument = c(".data", "category_values", ".value"), |
| 51 | + Description = c( |
| 52 | + "A grouped tibble or DBI object (using dplyr::group_by())", |
| 53 | + "A numeric vector of breakpoints between 0 and 1, representing cumulative proportions for ABC categories.", |
| 54 | + "Optional. A column to sum for categorization. If not provided, the function counts the number of rows per group." |
| 55 | + ) |
| 56 | +) |> tinytable::tt() |
| 57 | +
|
| 58 | +``` |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +```{r} |
| 63 | +#| label: abc-example |
| 64 | +#| echo: true |
| 65 | +#| eval: true |
| 66 | +
|
| 67 | +# Example |
| 68 | +
|
| 69 | +contoso::sales |> |
| 70 | + dplyr::group_by(store_key) |> |
| 71 | + fpaR::abc( |
| 72 | + category_values = c(0.4,.7,.9,1), |
| 73 | + .value = margin |
| 74 | + ) |
| 75 | +``` |
| 76 | + |
| 77 | +The function returns a **segment object**. Use `calculate()` to generate the ABC classification table: |
| 78 | + |
| 79 | +```{r} |
| 80 | +#| label: var |
| 81 | +#| echo: true |
| 82 | +#| eval: false |
| 83 | +#| warning: false |
| 84 | +#| message: false |
| 85 | +#| include: true |
| 86 | +contoso::sales |> |
| 87 | + dplyr::group_by(store_key) |> |
| 88 | + fpaR::abc( |
| 89 | + category_values = c(0.4,.7,.9,1), |
| 90 | + .value = margin |
| 91 | + ) |> |
| 92 | + fpaR::calculate() |
| 93 | +``` |
| 94 | + |
| 95 | +```{r} |
| 96 | +#| label: tbl-output-no-echo |
| 97 | +#| echo: false |
| 98 | +#| eval: true |
| 99 | +#| warning: false |
| 100 | +#| message: false |
| 101 | +#| include: true |
| 102 | +#| column: screen |
| 103 | +
|
| 104 | +contoso::sales |> |
| 105 | + dplyr::filter( |
| 106 | + store_key!="999999" |
| 107 | + ) |> |
| 108 | + dplyr::group_by(store_key) |> |
| 109 | + fpaR::abc( |
| 110 | + category_values = c( 0.4,.7,.9,1), |
| 111 | + .value = margin |
| 112 | + ) |> |
| 113 | + fpaR::calculate() |> |
| 114 | + dplyr::collect() |> |
| 115 | + dplyr::arrange(row_id) |> |
| 116 | + head(10) |> |
| 117 | + tinytable::tt() |
| 118 | +``` |
| 119 | + |
| 120 | + |
| 121 | +This table contains grouped data with various metrics, highlighting the contribution of each group in terms of both value and transaction count. Below is an explanation of the key columns and how to interpret the results: |
| 122 | + |
| 123 | +### Understanding the Results |
| 124 | + |
| 125 | +- Store 540 has a margin of $7,812.11 ("ABC Margin"), which accounts for about 4% ("prop_total") of the total margin across all stores |
| 126 | + |
| 127 | +- The "cum_sum" column tracks the running total of values (e.g., revenue or count) for each store, showing the cumulative sum up to that row |
| 128 | + |
| 129 | +- The "cum_prop_total" column shows each store's contribution as a percentage of the total margin as you move down the table |
| 130 | + |
| 131 | +- The store with the highest contribution has a "row_id" of 1 and is assigned to the first category segment ('A') via the "category_name" column |
| 132 | + |
| 133 | +- The "max_row_id" shows that there are 57 additional stores in the same category ('A') |
| 134 | + |
| 135 | +- The "cum_unit_prop" column tracks the cumulative contribution from a transaction count perspective, similar to cum_prop_total but at the unit level |
| 136 | + |
| 137 | +- The category_value and category_name columns define the breakpoints you provided, assigning stores to categories (e.g., 'A', 'B', 'C') based on their cumulative contribution |
| 138 | + |
| 139 | +This is summarized in @tbl-output below |
| 140 | + |
| 141 | + |
| 142 | +```{r} |
| 143 | +#| label: tbl-output |
| 144 | +#| echo: false |
| 145 | +#| eval: true |
| 146 | +#| warning: false |
| 147 | +#| message: false |
| 148 | +#| column: screen |
| 149 | +#| include: true |
| 150 | +
|
| 151 | +tibble::tibble( |
| 152 | + Column_Name = c("cum_sum", "prop_total", "cum_prop_total", "row_id", "max_row_id", "cum_unit_prop", "category_value", "category_name"), |
| 153 | + Description = c( |
| 154 | + "The cumulative sum of the specified values (e.g., revenue, count, etc.), aggregated per group. Represents the total value up to that row.", |
| 155 | + "The proportion of the total for each row's value. Shows the percentage of the total represented by the current row's contribution.", |
| 156 | + "The cumulative proportion of the total, showing the running total percentage of the entire dataset as you move through the rows.", |
| 157 | + "The unique identifier for the row, often used to track or identify specific rows in the dataset. Typically sequential ID or index.", |
| 158 | + "The maximum row ID in the current group (if grouping is applied), representing the total number of rows in the group.", |
| 159 | + "The cumulative proportion of the unit values, similar to cum_prop_total, but typically used when the unit is treated as aggregate.", |
| 160 | + "The category value that corresponds to the cumulative proportion break points (e.g., top 10%, top 40%, etc.). Based on the break points provided.", |
| 161 | + "The name of the category assigned to each row based on the cumulative contribution. Categories are represented by letters (A, B, C, etc.)." |
| 162 | + ), |
| 163 | + Example_Values = c( |
| 164 | + "1000, 2500, 4000", |
| 165 | + "0.10, 0.25, 0.40", |
| 166 | + "0.10, 0.35, 0.75", |
| 167 | + "1, 2, 3", |
| 168 | + "5, 5, 5", |
| 169 | + "0.10, 0.30, 0.70", |
| 170 | + "0.4, 0.7, 0.9", |
| 171 | + "\"A\", \"B\", \"C\"" |
| 172 | + ) |
| 173 | +) |> |
| 174 | + tinytable::tt() |
| 175 | +
|
| 176 | +
|
| 177 | +``` |
| 178 | + |
| 179 | + |
0 commit comments