-
Notifications
You must be signed in to change notification settings - Fork 0
/
Investigating_Items.Rmd
340 lines (281 loc) · 13.8 KB
/
Investigating_Items.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
---
title: ""
output:
html_document:
df_print: paged
code_folding: hide
---
```{r, include = FALSE}
library(readxl)
df <- read_excel(here::here("online_retail_II.xlsx"))
library(dplyr)
library(stringr)
df <- df %>%
filter(str_length(StockCode) == 5 |
str_detect(StockCode, "^\\d{5}[a-zA-Z]{1,2}$") |
str_detect(StockCode, "PADS|DCGS|SP|gift")) %>%
filter(Price != 0) %>%
mutate(CustomerID = as.character(`Customer ID`),
Country = na_if(Country, "Unspecified"), .keep = "unused", .after = Price)
```
After having examined the data from an invoice point of view, we continue here in the same vein but focusing on the items, keeping in mind that we don’t have access to the complete inventory but only to the items that have been invoiced during the time frame present in our data (a little bit more than a year).
As in the previous analysis, we decided not to remove [duplicated rows](https://pykaalexandro.github.io/An-Online-Retailer-Investigation/Data_Wrangling_Part2_-missing_values_duplicated_rows-.html#-_duplicated_rows), as we prefer not to lose information about quantity even if that might inflate the number of purchases.
<br>
# - *most popular items*
So let’s start by counting their presence (for both confirmed and cancelled invoices) to assess the ones appearing most frequently.
```{r}
df %>%
count(StockCode, Description, sort = TRUE, name = "Number of Occurrences")
```
If we divide them by status we get these numbers instead,
```{r}
df %>%
mutate(Status = if_else(str_detect(Invoice, "C"), "Cancelled", "Confirmed")) %>%
group_by(StockCode, Description, Status) %>%
summarise("Number of Occurrences" = n(), .groups = "drop") %>%
tidyr::pivot_wider(names_from = "Status", values_from = "Number of Occurrences", values_fill = 0) %>%
relocate("Confirmed", .after = Description) %>%
arrange(desc(Confirmed))
```
and we can show as well the total quantities of the confirmed and of the cancelled orders, plus the percentage of the cancelled over the sum of the two.
```{r}
df %>%
mutate(Status = if_else(str_detect(Invoice, "C"), "Cancelled", "Confirmed")) %>%
group_by(StockCode, Description) %>%
summarise("Total Quantity Purchased" = sum(Quantity[Status == "Confirmed"]),
"Total Quantity Cancelled" = sum(abs(Quantity[Status == "Cancelled"])),
"Percentage Over Total" = formattable::percent(`Total Quantity Cancelled` / sum(`Total Quantity Purchased`, `Total Quantity Cancelled`)), .groups = "drop") %>%
arrange(desc(`Total Quantity Purchased`))
```
We might then be interested in knowing, for every item, the weekly, confirmed, sales volumes and to compare them to the item's minimum, median and maximum overall values.
```{r}
df %>%
filter(!str_starts(Invoice, "C")) %>%
mutate("Year / Week" = format(InvoiceDate, "%y/%V")) %>%
group_by(StockCode, Description, `Year / Week`) %>%
summarise("Quantity Sold" = sum(Quantity), .groups = "drop_last") %>%
mutate("Minimum Quantity Sold" = min(`Quantity Sold`),
"Median Quantity Sold" = median(`Quantity Sold`),
"Maximum Quantity Sold" = max(`Quantity Sold`)) %>%
ungroup()
```
<br>
We can observe the trend of an item (INFLATABLE POLITICAL GLOBE in this case) on a temporal scale, with a dotted line that represents the median as a comparison.
```{r}
library(ggplot2)
df %>%
filter(!str_starts(Invoice, "C")) %>%
mutate("Year / Week" = format(InvoiceDate, "%y/%V")) %>%
group_by(StockCode, Description, `Year / Week`) %>%
summarise("Quantity Sold" = sum(Quantity), .groups = "drop_last") %>%
mutate("Minimum Quantity Sold" = min(`Quantity Sold`),
"Median Quantity Sold" = median(`Quantity Sold`),
"Maximum Quantity Sold" = max(`Quantity Sold`)) %>%
ungroup() %>%
filter(StockCode == "10002") %>%
ggplot(aes(`Year / Week`, `Quantity Sold`, group = StockCode)) +
geom_line() +
geom_abline(aes(intercept = `Median Quantity Sold`, slope = 0), linetype = 3) +
scale_x_discrete(breaks = ~ .[c(FALSE, TRUE, FALSE)]) +
labs(x = NULL,
y = NULL,
title = "Weekly Sales Volumes for StockCode 10002, INFLATABLE POLITICAL GLOBE")
```
<br>
# - *most expensive items*
Besides the most popular items, another thing we are interest into are the most expensive ones, that we will rank using their median price.
```{r}
df %>%
group_by(StockCode, Description) %>%
summarise("Median Price" = median(Price),
"Number of Invoices" = n(),
"Median Quantity per Invoice" = median(Quantity), .groups = "drop") %>%
arrange(desc(`Median Price`))
```
If we segment our inventory in `10` different bins of equal size (of approximately `30 £`) we notice that the bin with the cheapest items is the most populated, by a large margin.
```{r}
df %>%
group_by(StockCode, Description) %>%
mutate("Median Price" = median(Price)) %>%
group_by("Price Bins" = cut(`Median Price`, 10), .drop = FALSE) %>%
summarise("Number of Distinct Items" = n_distinct(StockCode),
"Number of Purchases" = n())
df %>%
group_by(StockCode, Description) %>%
mutate("Median Price" = median(Price)) %>%
group_by("Price Bins" = cut(`Median Price`, 10), .drop = FALSE) %>%
summarise("Number of Purchases" = n() + 1) %>%
#we add 1 to next use a logarithmic scale on the y axis
ggplot(aes(`Price Bins`, `Number of Purchases`)) +
geom_col() +
scale_y_log10(labels = scales::label_number()) +
labs(x = NULL,
y = NULL,
title = "Number of Purchases for Price Bin, Logarithmic Scale")
```
<br>
Ultimately, if we are interested in knowing when an item changed price, we can use this table,
```{r}
df %>%
select(InvoiceDate, StockCode, Description, Price) %>%
group_by(StockCode, Description) %>%
mutate("Price Change?" = if_else(Price != lag(Price), TRUE, FALSE)) %>%
ungroup() %>%
arrange(StockCode)
```
<br>
# - *most profitable items*
Let’s isolate now our best products in term of revenues, filtering out the cancelled invoices.
```{r}
df %>%
filter(!str_starts(Invoice, "C")) %>%
mutate(Revenue = Quantity * Price) %>%
group_by(StockCode, Description) %>%
summarise("Total Revenues" = sum(Revenue),
"Number of Purchases" = n(),
"Median Price of Purchase" = median(Price),
"Median Quantity of Purchase" = median(Quantity), .groups = "drop") %>%
ungroup() %>%
arrange(desc(`Total Revenues`))
```
The bin with the cheapest items makes up for more than `90%` of revenues as well,
```{r}
df %>%
filter(!str_starts(Invoice, "C")) %>%
mutate(Revenue = Quantity * Price) %>%
group_by(StockCode, Description) %>%
mutate("Median Price" = median(Price)) %>%
group_by("Price Bins" = cut(`Median Price`, 10), .drop = FALSE) %>%
summarise("Total Revenues" = sum(Revenue)) %>%
mutate("Percentage over Total Revenues" = formattable::percent(`Total Revenues` / sum(`Total Revenues`)))
```
That is true also for the lost revenues of the cancelled invoices.
```{r}
df %>%
filter(str_starts(Invoice, "C")) %>%
mutate("Lost Revenues" = abs(Quantity) * Price) %>%
group_by(StockCode, Description) %>%
summarise("Total Lost Revenues" = sum(`Lost Revenues`),
"Number of Cancellations" = n(),
"Median Price" = median(Price),
"Median Quantity" = median(abs(Quantity)), .groups = "drop") %>%
ungroup() %>%
arrange(desc(`Total Lost Revenues`))
df %>%
filter(str_starts(Invoice, "C")) %>%
mutate("Lost Revenues" = abs(Quantity) * Price) %>%
group_by(StockCode, Description) %>%
mutate("Median Price" = median(Price)) %>%
group_by("Price Bins" = cut(`Median Price`, 10), .drop = FALSE) %>%
summarise("Total Lost Revenues" = sum(`Lost Revenues`)) %>%
mutate("Percentage over Total Lost Revenues" = formattable::percent(`Total Lost Revenues` / sum(`Total Lost Revenues`)))
```
There are two bins for which the lost revenues are, in percentage, much higher than the others.
```{r}
df %>%
mutate(Status = if_else(!str_detect(Invoice, "C"), "Revenues", "Revenues Lost for Cancellations"),
Revenue = Quantity * Price,) %>%
group_by(StockCode, Description, Status) %>%
mutate("Median Price" = median(Price)) %>%
group_by("Price Bins" = cut(`Median Price`, 10), Status, .drop = FALSE) %>%
summarise("Total Revenues" = sum(abs(Revenue)), .groups = "drop") %>%
mutate(Status = coalesce(Status, "Revenues")) %>%
#to eliminate NAs in the `Status` column for the 3 bins with no items
tidyr::pivot_wider(names_from = Status, values_from = `Total Revenues`, values_fill = 0) %>%
rowwise() %>%
mutate("Percentage over Total" = formattable::percent(`Revenues Lost for Cancellations` / sum(`Revenues`, `Revenues Lost for Cancellations`)))
```
<br>
# - *items more frequently cancelled*
The cancellation of invoices is an important phenomenon, so let's show the cancellation rates for all the items, with the ones always cancelled on top.
```{r}
df %>%
mutate(Status = if_else(str_starts(Invoice, "C"), "Cancelled Purchases", "Confirmed Purchases")) %>%
group_by(StockCode, Description, Status) %>%
summarise("Number of Occurrences" = n(), .groups = "drop_last") %>%
tidyr::pivot_wider(names_from = Status, values_from = `Number of Occurrences`, values_fill = 0) %>%
mutate("Cancellation Rate" = formattable::percent(`Cancelled Purchases` / sum(`Confirmed Purchases`, `Cancelled Purchases`))) %>%
ungroup() %>%
relocate(`Cancellation Rate`, `Confirmed Purchases`, `Cancelled Purchases`, .after = Description) %>%
arrange(desc(`Cancellation Rate`), desc(`Cancelled Purchases`))
```
<br>
The following table and graph show the distribution of `Cancellation Rate`, to more easily assess how many items are always or never cancelled.
```{r}
df %>%
mutate(Status = if_else(str_starts(Invoice, "C"), "Cancelled Purchases", "Confirmed Purchases")) %>%
group_by(StockCode, Description, Status) %>%
summarise("Number of Occurrences" = n(), .groups = "drop_last") %>%
tidyr::pivot_wider(names_from = Status, values_from = `Number of Occurrences`, values_fill = 0) %>%
mutate("Cancellation Rate" = formattable::percent(`Cancelled Purchases` / sum(`Confirmed Purchases`, `Cancelled Purchases`))) %>%
ungroup() %>%
count(`Cancellation Rate`, name = "Number of Distinct Items") %>%
mutate("Percentage of Distinct Items" = formattable::percent(`Number of Distinct Items` / sum(`Number of Distinct Items`))) %>%
arrange(desc(`Number of Distinct Items`), desc(`Cancellation Rate`))
df %>%
mutate(Status = if_else(str_starts(Invoice, "C"), "Cancelled Purchases", "Confirmed Purchases")) %>%
group_by(StockCode, Description, Status) %>%
summarise("Number of Occurrences" = n(), .groups = "drop_last") %>%
tidyr::pivot_wider(names_from = Status, values_from = `Number of Occurrences`, values_fill = 0) %>%
mutate("Cancellation Rate" = formattable::percent(`Cancelled Purchases` / sum(`Confirmed Purchases`, `Cancelled Purchases`))) %>%
ungroup() %>%
ggplot(aes(`Cancellation Rate`)) +
geom_histogram(bins = 100) +
scale_x_continuous(labels = scales::label_percent()) +
labs(x = NULL,
y = NULL,
title = "Number of Distinct Items for Integer Percentages of Cancellation Rates")
```
<br>
# - *quantities per month*
We can then produce a table with the monthly quantities sold for every item, useful to investigate seasonal trends.
```{r}
df %>%
filter(!str_starts(Invoice, "C")) %>%
mutate(Month = format(InvoiceDate, "%y/%m")) %>%
group_by(StockCode, Description, Month) %>%
summarise(Quantity = sum(Quantity), .groups = "drop") %>%
tidyr::pivot_wider(names_from = Month, values_from = Quantity, names_sort = TRUE) %>%
mutate("Total Quantity" = rowSums(across(where(is.numeric)), na.rm = TRUE),
"Monthly Average" = round(rowMeans(across(where(is.numeric)), na.rm = TRUE), 2),
"Number of Missing Months" = rowSums(is.na(pick(everything()))), .after = "Description") %>%
arrange(desc(`Total Quantity`))
```
<br>
# - *country breakdown*
Let's take a look now at what items are popular in more countries, showing first the number of invoices (confirmed and cancelled both) they appear in
```{r}
df %>%
count(Country, StockCode, Description, name = "Number of Occurrences") %>%
tidyr::pivot_wider(names_from = Country, values_from = `Number of Occurrences`) %>%
mutate("Number of Countries Invoiced In" = nrow(count(df, Country)) - rowSums(is.na(pick(everything()))), .after = Description) %>%
arrange(desc(`Number of Countries Invoiced In`))
```
<br>
and then the quantities effectively sold.
```{r}
df %>%
filter(!str_starts(Invoice, "C")) %>%
group_by(Country, StockCode, Description) %>%
summarise("Total Quantity" = sum(Quantity), .groups = "drop") %>%
tidyr::pivot_wider(names_from = Country, values_from = `Total Quantity`) %>%
mutate("Number of Countries Sold In" = nrow(count(df, Country)) - rowSums(is.na(pick(everything()))), .after = Description) %>%
arrange(desc(`Number of Countries Sold In`))
```
<br>
We can also be interested in knowing if an item has different prices across the different countries it is sold in, using the median one as the metric.
```{r}
df %>%
filter(!str_starts(Invoice, "C")) %>%
group_by(Country, StockCode, Description) %>%
summarise("Rounded Median Price" = round(median(Price), 2), .groups = "drop") %>%
tidyr::pivot_wider(names_from = Country, values_from = `Rounded Median Price`) %>%
left_join(df %>%
filter(!str_starts(Invoice, "C")) %>%
group_by(StockCode, Description) %>%
summarise("Global Rounded Median Price" = round(median(Price), 2), .groups = "drop"), by = c("StockCode", "Description")) %>%
relocate(`Global Rounded Median Price`, .after = Description)
```
<br>
# - *main takeaways*
We produced various tables that can be explored further to gain insights on what items perform best for certain common metrics like the most commonly sold and the ones yielding highest revenues. The problematic phenomenon of cancellations has also been examined, together with a breakdown by countries.