forked from atthegates25/ML_Lab
/
lab_code.R
176 lines (154 loc) · 4.79 KB
/
lab_code.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
library(stringr)
library(data.table)
library(dplyr)
library(ggplot2)
library(lubridate)
#orders <- read.csv("./data/orders.csv")
#returns <- read.csv("./data/returns.csv")
orders <- fread("./data/orders.csv")
returns <- fread("./data/returns.csv")
####part 1: basic eda####
colnames(orders) <- tolower(colnames(orders))
colnames(returns) <- tolower(colnames(returns))
colnames(returns)[2] <- "order.id"
str(orders)
str(returns)
summary(orders)
##problem 1##
orders$profit <- as.numeric(gsub("[$, ]", "", orders$profit))
orders$sales <- as.numeric(gsub("[$, ]", "", orders$sales))
str(orders$profit)
str(orders$sales)
##problem 2##
orders$date <- as.Date(orders$order.date, "%m/%d/%y")
str(orders$order.date)
str(orders$date)
#extract year, month, and day
orders$year <- year(orders$date)
orders$month <- month(orders$date)
#check all months present
sort(unique(orders$year))
sort(unique(orders$month))
str(orders$date)
unique(orders$category)
#q1,
#sales at aggregate level
orders %>%
group_by(year, month) %>%
summarise(tot_orders = sum(quantity)) %>%
arrange(year, month) %>%
mutate(obs = row_number(),
yearmo = paste(year, month, sep = "/")) %>%
ggplot(aes(obs, tot_orders)) +
geom_line() +
theme_bw() +
labs(title = "Evolution of orders across time",
x = "Time",
y = "Total orders") +
theme(plot.title = element_text(hjust = 0.5))
#orders across time
orders %>%
group_by(category, month) %>%
summarise(tot_orders = sum(quantity)) %>%
ggplot(aes(x = month, y = tot_orders)) +
geom_bar(aes(fill = category), stat = "identity") +
theme_bw() +
labs(title = "Evolution of orders across time",
x = "Time",
y = "Total orders",
fill = "Category") +
theme(plot.title = element_text(hjust = 0.5),
legend.position = "bottom")
#q2,
#composition of orders across time
orders %>%
group_by(category, month) %>%
summarise(tot_orders = sum(quantity)) %>%
ggplot(aes(x = month, y = tot_orders)) +
geom_bar(aes(fill = category), stat = "identity", position = "fill") +
theme_bw() +
labs(title = "Evolution of orders across time",
x = "Time",
y = "Total orders",
fill = "Category") +
theme(plot.title = element_text(hjust = 0.5),
legend.position = "bottom")
#sales trend by category
orders %>%
group_by(category, year, month) %>%
summarise(tot_orders = sum(quantity)) %>%
arrange(year, month) %>%
mutate(obs = row_number(),
yearmo = paste(year, month, sep = "/")) %>%
ggplot(aes(obs, tot_orders, fill = category)) +
geom_line() +
theme_bw() +
labs(title = "Evolution of orders across time",
x = "Time",
y = "Total orders") +
theme(plot.title = element_text(hjust = 0.5))
##problem 3##
orders_wret <- orders %>%
left_join(returns, by = "order.id") %>%
mutate(returned = ifelse(is.na(returned) == TRUE, 0, 1))
#q1,
orders_wret %>%
filter(returned == 1) %>%
group_by(year) %>%
summarise(profit_loss = sum(profit)) %>%
ggplot(aes(year, profit_loss)) +
geom_bar(stat = "identity") +
theme_bw() +
labs(title = "Profit loss across time",
x = "Time",
y = "Profit loss") +
theme(plot.title = element_text(hjust = 0.5))
#q2,
#more than one return
nrow(orders_wret %>%
filter(returned == 1) %>%
group_by(customer.id) %>%
summarise(n = n_distinct(order.id)) %>%
filter(n > 1))
#more than five returns
nrow(orders_wret %>%
filter(returned == 1) %>%
group_by(customer.id) %>%
summarise(n = n_distinct(order.id)) %>%
filter(n > 5))
#q3,
#returns by region
orders_wret %>%
group_by(region.x, returned) %>%
summarise(no_returns = n_distinct(order.id)) %>%
mutate(colproduct = returned * no_returns,
region = region.x) %>%
group_by(region) %>%
summarise(tot_sales = sum(no_returns),
tot_returns = sum(colproduct),
pct_return = tot_returns/tot_sales) %>%
arrange(desc(pct_return)) %>%
mutate(pct_return = round(100*pct_return, 2))
#q4,
#returns by category
orders_wret %>%
group_by(category, returned) %>%
summarise(no_returns = sum(quantity)) %>%
mutate(colproduct = returned * no_returns) %>%
group_by(category) %>%
summarise(tot_sales = sum(no_returns),
tot_returns = sum(colproduct),
pct_return = tot_returns/tot_sales) %>%
arrange(desc(pct_return)) %>%
mutate(pct_return = round(100*pct_return, 2))
#returns by sub-category
orders_wret %>%
group_by(sub.category, returned) %>%
summarise(no_returns = sum(quantity)) %>%
mutate(colproduct = returned * no_returns) %>%
group_by(sub.category) %>%
summarise(tot_sales = sum(no_returns),
tot_returns = sum(colproduct),
pct_return = tot_returns/tot_sales) %>%
arrange(desc(pct_return)) %>%
mutate(pct_return = round(100*pct_return, 2))