-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprice_dispersion_analysis.R
523 lines (452 loc) · 15.7 KB
/
price_dispersion_analysis.R
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
## Start ----
## Script name: Price Dispersion Analysis
##
## Purpose of the script: Explore pricing data of Xbox360 products
## as set by certain stores from 2012 - 2020
## Home Assignment;
## Data Analysis and Visualization course;
## Dalarna University
##
## Authors: Saumya Gupta, M.M. Usman Zahid
##
## Date Created: 2021-02-28
##
## Copyright (c) 2021 Saumya Gupta
##
## Email: [email protected], [email protected]
## before proceeding, we recommend Windows Rstudio user to use Ctrl+Shift+O
## to see document outline
## set working directory.
# setwd(
# 'C:/Users/gupta/OneDrive/Documents/MS-DS/AMI23A/Part3/HomeAssignment/PriceDispersionAnalysis/'
# )
## load up required packages
library(ggplot2)
library(ggdendro)
library(ggpubr)
library(gridExtra)
library(data.table)
library(dplyr)
library(tidyverse)
library(dtw)
library(NbClust)
## create DTW Suite suggested method to help create dissimilarity matrix out of
## time series of different lengths
dtwOmitNA <- function (x, y)
{
a <- na.omit(x)
b <- na.omit(y)
return(dtw(a, b, distance.only = TRUE)$normalizedDistance)
}
## create new entry in registry with two aliases
pr_DB$set_entry(FUN = dtwOmitNA, names = c("dtwOmitNA"))
## create method to cluster stores and produce cluster visualizations
findDTWHCluster <- function(id, product_ts, k_val = 3) {
## convert to data frame for ease in operations
product_ts <- as.data.frame(product_ts)
## get store IDs as row names
row.names(product_ts) <- product_ts$store_id
product_ts$store_id <- NULL
## get distance matrix using DTW distance measure for unequal time series
d <- dist(product_ts, method = "dtwOmitNA")
## cluster hierarchically using 'complete' agglomeration method
hc <- stats::hclust(d, method = "complete")
## cut tree using asked number of groups
hclus <- cutree(hc, k = k_val) %>%
as.data.frame(.) %>%
rename(., cluster_group = .) %>%
rownames_to_column("store_id")
## show cluster hierarchy using dendrogram
hcdata <- dendro_data(hc)
# cols <- c("#a9a9a9", "#1f77b4")
p1 <- hcdata %>%
ggdendrogram(., rotate = TRUE, leaf_labels = FALSE) +
geom_hline(yintercept = 1, color = "red") +
annotate(
"text",
x = 15.5,
y = 0.85,
label = "Cluster 2",
size = 2
) +
annotate(
"text",
x = 8,
y = 0.85,
label = "Cluster 1",
size = 2
) +
theme(text = element_text(size = 8),
plot.margin = margin(0, 0,-0.3, 0, "cm"))
p1 <- annotate_figure(p1,
bottom = text_grob("Height", size = 8))
## get store IDs as row names in clustered stores data
row.names(hclus) <- hclus$store_id
hclus$store_id <- NULL
## combine non clustered time series with assigned cluster groups
stores_clustered_wide <-
merge(product_ts, hclus, by = 0) %>% rename("store_id" = "Row.names")
## reshape data to facilitate plotting
stores_clustered_long <-
stores_clustered_wide %>% pivot_longer(
starts_with("201"),
names_to = "date",
values_to = "normalized_adjusted_price",
values_drop_na = TRUE
) %>%
mutate(date = as.Date(date)) %>%
as.data.frame()
## get real prices for plotting
product_real_price <- data_Xbox360 %>%
filter(product_id == id) %>%
select(store_id, date, cpi_adjusted_price)
## plot un-clustered stores first, for comparison
ggplot(product_real_price,
aes(date, cpi_adjusted_price, color = store_id)) +
geom_step(size = 1) +
ggtitle(paste("Product #", product, "Non clustered Stores")) +
xlab("Time") +
ylab("CPI-Adjusted Price (in SEK)") +
theme_minimal()
## merge clustered data with real prices
stores_clustered_long <-
merge(stores_clustered_long,
product_real_price,
by = c("store_id", "date")) %>%
mutate(Cluster = as.factor(cluster_group),
date = as.Date(date))
## get cluster statistics on cluster groups
clustered_stores <- stores_clustered_long %>%
group_by(Cluster, date) %>%
summarise(
mean_price = mean(cpi_adjusted_price),
q.25 = quantile(cpi_adjusted_price, 0.25),
q.75 = quantile(cpi_adjusted_price, 0.75),
min_price = min(cpi_adjusted_price),
max_price = max(cpi_adjusted_price)
)
## produce cluster result
cluster_fig <- ggplot(clustered_stores,
aes(date,
mean_price,
group = Cluster,
colour = Cluster)) +
geom_step(size = 1) +
geom_ribbon(aes(
ymin = q.25,
ymax = q.75,
fill = Cluster
),
alpha = 0.25,
color = 'transparent') +
ggtitle(paste("Product #", id)) +
xlab("Time") +
ylab("CPI-Adjusted Price (in SEK)") +
theme_minimal() +
theme(text = element_text(size = 8), legend.position = "none")
grid.arrange(cluster_fig, p1, ncol = 2)
## un-comment below to just get time series w/o dendrogram
# print(cluster_fig)
}
## create cluster validation function using NbClust to
## validate clusters and returns optimal partitioning information
bestNbClust <- function(id, product_ts) {
product_data_NbClust <- as.data.frame(product_ts)
# Get store IDs as row names
row.names(product_data_NbClust) <- product_data_NbClust$store_id
product_data_NbClust$store_id <- NULL
# Get distance matrix using dtw distance measure for unequal time series
d_NbClust <- dist(product_data_NbClust, method = "dtwOmitNA")
res <-
NbClust(
diss = d_NbClust,
distance = NULL,
min.nc = 2,
max.nc = 6,
method = "ward.D2",
index = "mcclain"
)
res$Best.nc
# return (res)
}
## Data Read ----
## load up data containing all product categories
data_total <- fread("../home_assignment_data_pricing.csv")
## filter data needed for exploration
data_Xbox360 <- data_total %>% filter(category == 'Xbox 360')
## encode variables to factor variables to help plot better figures
data_Xbox360$product_id = as.factor(data_Xbox360$product_id)
data_Xbox360$store_id = as.factor(data_Xbox360$store_id)
## Result 1 ----
### Fig. 1 ----
## show data distribution over time
ggplot(data_Xbox360,
aes(date)) +
geom_histogram(bins = 28,
color = 'white') +
xlab('Time') +
ylab('Number of data points') +
ggtitle('Data Distribution') +
theme_minimal()
## get number of stores and products over time
data_Xbox360[, c('date', 'product_id', 'store_id')] %>%
group_by(date) %>%
mutate(products = n_distinct(product_id),
stores = n_distinct(store_id)) %>%
select(date, products, stores) %>%
distinct() %>%
pivot_longer(
cols = c("products", "stores"),
names_to = "entity",
values_to = "count"
) -> everyday_product_store_count
### Fig. 2 (Report Fig. 1)----
## show number of products and stores over time
ggplot(everyday_product_store_count, aes(date, count, color = entity)) +
geom_line(size = 1) +
scale_color_manual(
"",
labels = c("Products", "Retailers"),
values = c("blue", "darkorange")
) +
xlab('Time') +
ylab('Count') +
ggtitle('Products and Retailers Over Time (2012 - 2017)') +
theme_minimal() +
theme(text = element_text(size = 8))
### Fig. 3 ----
## show all prices over time (execution takes time)
# ggplot(data_Xbox360[,
# c("date", "store_id", "product_id", "cpi_adjusted_price")],
# aes(date, cpi_adjusted_price, color = store_id)) +
# geom_point(size = 0.1, alpha = 0.25) +
# ylim(0, 1500) +
# xlab('Time') +
# ylab('CPI-Adjusted Price') +
# ggtitle('Prices Over Time (2012 - 2017)') +
# theme_minimal()
# theme(legend.position = "none")
## get all price aggregates over time
data_Xbox360 %>%
select(cpi_adjusted_price, date) %>%
group_by(date) %>%
summarise(
# mean = mean(cpi_adjusted_price),
# median = median(cpi_adjusted_price),
# standard_deviation = sd(cpi_adjusted_price),
CoV = (sd(cpi_adjusted_price) / mean(cpi_adjusted_price)) * 1000,
Min = min(cpi_adjusted_price),
Max = max(cpi_adjusted_price)
) %>%
pivot_longer(cols = starts_with(c("M", "CoV", "sta")),
names_to = "aggregate",
values_to = "value") -> everyday_price_aggregates
### Fig. 4 ----
## show all price aggregates over time
ggplot(everyday_price_aggregates,
aes(date, value, color = aggregate)) +
geom_step() +
scale_y_continuous(
"CPI-Adjusted Price (in SEK)",
sec.axis = sec_axis(trans = ~ . * 0.1,
name = "(*0.1) Zoomed Axis for CoV (%)")
) +
# scale_color_brewer("",
# palette = "Dark2") +
xlab("Time") +
ggtitle('Prices (all products)') +
theme_minimal()
### Fig. 5 ----
## show case of product #1260
data_Xbox360 %>%
filter(product_id == 2687705,
store_id == 1260,
date >= '2015-01-01',
date <= '2015-12-31') %>%
ggplot(aes(date, cpi_adjusted_price)) +
geom_step() +
xlab("2015") +
ylab("CPI-Adjusted Price (in SEK)") +
ggtitle('Price (Product #2687705 - Store #1260) (2015)') +
theme_minimal()
## get price aggregates excluding weird data points
data_Xbox360 %>%
select(cpi_adjusted_price, date) %>%
filter(cpi_adjusted_price < 4000) %>%
group_by(date) %>%
summarise(
# mean = mean(cpi_adjusted_price),
# median = median(cpi_adjusted_price),
Min = min(cpi_adjusted_price),
Max = max(cpi_adjusted_price),
# standard_deviation = sd(cpi_adjusted_price),
CoV = (sd(cpi_adjusted_price) / mean(cpi_adjusted_price)) * 1000
) %>%
pivot_longer(cols = starts_with(c("M", "CoV", "sta")),
names_to = "aggregate",
values_to = "value") -> everyday_price_aggregates_wo_outlier
## show all price aggregates over time excluding weird data points
plot1 <- ggplot(everyday_price_aggregates_wo_outlier,
aes(date, value, color = aggregate)) +
geom_step() +
scale_y_continuous("",
sec.axis = sec_axis(trans = ~ . * 0.1)) +
# scale_color_brewer("",
# palette = "Dark2") +
xlab("Time") +
ggtitle('A. Prices (all products)') +
theme_minimal() +
theme(text = element_text(size = 8),
legend.title = element_blank())
## check for single product
data_Xbox360 %>%
filter(product_id == 3186029) %>%
select(cpi_adjusted_price, date) %>%
group_by(date) %>%
summarise(
# mean = mean(cpi_adjusted_price),
# median = median(cpi_adjusted_price),
Min = min(cpi_adjusted_price),
Max = max(cpi_adjusted_price),
# standard_deviation = sd(cpi_adjusted_price),
CoV = (sd(cpi_adjusted_price) / mean(cpi_adjusted_price)) * 1000
) %>%
pivot_longer(cols = starts_with(c("M", "CoV", "sta")),
names_to = "aggregate",
values_to = "value") -> everyday_price_aggregates_3186029
## show result for single product
plot2 <- ggplot(everyday_price_aggregates_3186029,
aes(date, value, color = aggregate)) +
geom_step() +
scale_y_continuous("",
sec.axis = sec_axis(trans = ~ . * 0.1)) +
# scale_color_brewer("",
# palette = "Dark2") +
xlab("Time") +
ggtitle('B. Prices (product # 3186029)') +
theme_minimal() +
theme(
text = element_text(size = 8),
plot.margin = margin(0, 0,-0.2, 0, "cm"),
legend.title = element_blank()
)
## combine plot 1 & 2
multi_fig <- ggarrange(
plot1,
plot2,
nrow = 2,
common.legend = TRUE,
legend = "bottom"
)
### Fig. 6 (Report Fig. 2)----
## annotate some axis text
annotate_figure(
multi_fig,
left = text_grob("CPI-Adjusted Price (in SEK)", size = 8, rot = 90),
right = text_grob(
"(*0.1) Zoomed Axis for CoV (%)",
size = 8,
rot = -90
)
)
## find summary stats on mean coefficient of variation for all products
product_with_mean_CoV <- data_Xbox360 %>%
group_by(product_id, date) %>%
summarise(CoV =
sd(cpi_adjusted_price) / mean(cpi_adjusted_price)) %>%
group_by(product_id) %>%
summarise(mean_CoV = mean(CoV))
summary(product_with_mean_CoV %>% select(mean_CoV))
product_stats <-
merge(product_with_mean_CoV, product_with_store_count, by = "product_id") %>%
arrange(desc(mean_CoV))
## get number of stores for each product
data_Xbox360 %>%
group_by(product_id) %>%
summarise(store_count = n_distinct(store_id)) %>%
arrange(desc(store_count)) -> product_with_store_count
## get number of products for each store
data_Xbox360 %>%
group_by(store_id) %>%
summarise(product_count = n_distinct(product_id)) %>%
arrange(desc(product_count)) -> store_with_product_count
## get store ts for all products in file using loop
# for (product in levels(data_Xbox360$product_id)) {
# my_plot <- ggplot(
# data_Xbox360 %>% filter(product_id == product),
# aes(date, cpi_adjusted_price, color = store_id)
# ) +
# geom_line() +
# ggtitle(paste("Product #", product)) +
# xlab("Time") +
# ylab("CPI-Adjusted Price (in SEK)")
# ggsave(paste("product_", product, ".png"), my_plot)
# }
## show price randomizing/undercutting practiced by stores for product # 1769910
plot3 <- ggplot(
data_Xbox360 %>% filter(product_id == 1769910),
aes(date, cpi_adjusted_price, color = store_id)
) +
geom_line() +
ggtitle(paste("A. Product #", 1769910)) +
xlab("") +
ylab("") +
theme_minimal() +
theme(text = element_text(size = 8)) +
theme(legend.position = "none",
plot.margin = unit(c(0.1, 0.2,-0.1,-0.2), "cm"))
## show price randomizing/undercutting practiced by stores for product # 1769910
plot4 <- ggplot(
data_Xbox360 %>% filter(product_id == 1341635),
aes(date, cpi_adjusted_price, color = store_id)
) +
geom_line() +
ggtitle(paste("B. Product #", 1341635)) +
xlab("Time") +
ylab("") +
theme_minimal() +
theme(text = element_text(size = 8)) +
theme(legend.position = "none",
plot.margin = unit(c(-0.1, 0.2, 0,-0.2), "cm"))
### Fig. 7 (Report Fig. 3) ----
## combine plot 3 & 4
grid.arrange(
plot3,
plot4,
nrow = 2,
left = text_grob("CPI-Adjusted Price (in SEK)", size = 8, rot = 90)
)
## Result 2 ----
### Scaling Prices ----
## normalizing cpi_adjusted_prices
data_Xbox360 <- data_Xbox360 %>%
mutate(normalized_cpi_adjusted_price = scale(cpi_adjusted_price))
### Clustering ----
## get store-wise time series for all products in list using loop
product_ts_list <- list()
for (product in product_with_store_count$product_id[1:30]) {
product_ts_list[[product]] <- data_Xbox360 %>%
filter(product_id == product) %>%
select(store_id, normalized_cpi_adjusted_price, date) %>%
arrange(date) %>%
pivot_wider(id_cols = store_id,
names_from = date,
values_from = normalized_cpi_adjusted_price) %>%
as.data.table()
}
#### Fig. 8, 9 (Report Fig. 4) ----
## cluster product # 1341635 (change function a bit to get exact figure)
findDTWHCluster(1341635,
product_ts_list$`1341635`,
2)
#### Fig. 10, 11 (Report Fig. 5) ----
## cluster product # 916993
findDTWHCluster(916993,
product_ts_list$`916993`,
2)
### Cluster Validation ----
## validate clusters for product # 1341635
bestNbClust(1341635, product_ts_list$`1341635`)
## validate clusters for product # 916993
bestNbClust(916993, product_ts_list$`916993`)
## End ----