-
Notifications
You must be signed in to change notification settings - Fork 3
/
How to use outliers to improve your forecasts.Rmd
220 lines (145 loc) · 5.15 KB
/
How to use outliers to improve your forecasts.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
---
title: "How to use outliers to improve your forecasts. Forecasting customer demand with holidays and special events in R"
author: "Anita Owens"
output:
html_document:
df_print: paged
toc: true
toc_float:
collapsed: false
smooth_scroll: false
toc_depth: 2
---
### Questions to answer:
**1.** What is the equation used to forecast daily customer visits?
**2.** How does day of week affect customer visits?
**3.** How does time of year affect customer visits?
**4.** What special factors affects customer visits and how can we use this information to improve our forecasts?
**5.** What other data might you want to collect to improve forecast accuracy?
## 1. Set up environment
```{r Load packages}
# Install pacman if needed
if (!require("pacman")) install.packages("pacman")
# load packages
pacman::p_load(pacman,
tidyverse, openxlsx, forecast, modeltime, parsnip, rsample, timetk, xts, cowplot)
```
## Dinner
```{r Check dinner data}
dinner <- read.xlsx("datasets/Dinner.xlsx", skipEmptyRows = TRUE)
str(dinner)
```
```{r Reformat Excel dates}
dinner$Date <- as.Date(dinner$Date, origin = "1899-12-30")
head(dinner$Date)
```
```{r Rename dinner columns}
#Rename columns
dinner <- dinner %>%
rename(weekday = Day.week,
cust_count = Cust.count.dinner
)
#Check column names
names(dinner)
```
```{r Plot customer dinner counts}
#From timetk package
dinner %>%
plot_time_series(Date, cust_count, .interactive = TRUE)
```
```{r Plot customer counts across month and day of the week}
b1 <- ggplot(dinner, aes(x=factor(Month), y = cust_count)) + geom_boxplot()
b2 <- ggplot(dinner, aes(x=factor(weekday), y = cust_count)) + geom_boxplot()
plot_grid(b1, b2, labels = "AUTO")
```
```{r Factor numeric variables}
#Create list of variables that need transformation
fac_vars <- c("Month", "weekday")
#Factor Variables
dinner[,fac_vars] <- lapply(dinner[,fac_vars], factor)
#check results
str(dinner)
```
```{r Fit regression model}
lm_01 <- # Identify outcome, state the rest; must specify dataset
lm(cust_count ~ Month + weekday, data = dinner)
# Summarize regression model
summary(lm_01)
```
```{r Compute accuracy statistics}
# Use accuracy() to compute RMSE statistics
accuracy(lm_01)
```
MAPE is at 15.49
Now we can answer questions like "How many customers should we expect based on the day of week?"
```{r Make predictions }
explanatory_data <- tibble(dinner)
#Put predictions inside a data frame
prediction_data<- explanatory_data %>%
mutate(forecast = predict(lm_01, explanatory_data))
head(prediction_data)
```
actual minus forecast
-a negative error means the actual result was below the forecast
if forecasts are too high
if forecasts are too low, company loses sales.
absolute value of forecast errors allows us to calculate the distance between forecast and actual
```{r Add errors to dataframe}
prediction_data <- prediction_data %>%
mutate(errors = cust_count - forecast)
#Do the errors add up to zero?
sum(prediction_data$errors)
#Are the errors normally distributed?
hist(prediction_data$errors)
#Standard deviation of the errors - We need to know how accurate our forecasts should be.
(one_sd_errors <- sd(prediction_data$errors))
(two_sd_errors <- sd(prediction_data$errors) *2)
```
```{r Finding the outliers}
#Add outlier column
prediction_data <- prediction_data %>%
mutate(outlier = case_when(abs(errors) > two_sd_errors ~ 1,
TRUE ~ 0))
prediction_data %>%
summarize(total_errors = sum(outlier)/n() * 100)
#view outlier rows only
prediction_data %>%
filter(outlier == 1) %>%
select(Date, cust_count, forecast, errors, outlier) %>% # subset columns
arrange(Date)
```
```{r Add outlier dummy variables to dataframe}
dinner <- dinner %>%
mutate(independ_day = case_when(Month == 7 & daymon == 4 ~ 1, TRUE ~ 0)) %>%
mutate(valentines = case_when(Month == 2 & daymon %in% c(14) ~ 1, TRUE ~ 0)) %>%
mutate(xmas_eve = case_when(Month == 12 & daymon == 24 ~ 1, TRUE ~ 0)) %>%
mutate(new_years_eve = case_when(Month == 12 & daymon == 31 ~ 1, TRUE ~ 0))
#CHECK RESULTS
head(dinner)
```
# Prophet model
```{r Prophet - model fit}
dinner_new <- dinner %>%
select(Date, cust_count)
#We need to rename columns for the prophet model
colnames(dinner_new)=c("ds", "y")
model <- prophet(dinner_new, seasonality.mode='multiplicative')
```
```{r}
future <- make_future_dataframe(model, periods = 30, freq = 'day')
forecast <- predict(model, future)
# If you want to see the forecast components, you can use the Prophet.plot_components method. By default you’ll see the trend, yearly seasonality, and weekly seasonality of the time series. If you include holidays, you’ll see those here, too.
prophet_plot_components(model, forecast)
```
```{r}
plot(model, forecast)
```
```{r}
```
### Questions to answer:
**1.** What is the equation used to forecast daily customer visits?
sales =
**2.** How does day of week affect customer visits?
**3.** How does time of year affect customer visits?
**4.** What special factors affects customer visits and how can we use this information to improve our forecasts?
**5.** What other data might you want to collect to improve forecast accuracy?