1 Introduction

Sales are activities related to selling or the number of goods sold in a given targeted time period. The delivery of a service for a cost is also considered a sale. A period during which goods are sold for a reduced price may also be referred to as a “sale”.

The seller, or the provider of the goods or services, completes a sale in an interaction with a buyer, which may occur at the point of sale or in response to a purchase order from a customer. There is a passing of title (property or ownership) of the item, and the settlement of a price, in which agreement is reached on a price for which transfer of ownership of the item will occur. The seller, not the purchaser, typically executes the sale and it may be completed prior to the obligation of payment. In the case of indirect interaction, a person who sells goods or service on behalf of the owner is known as a salesman or saleswoman or salesperson, but this often refers to someone selling goods in a store/shop, in which case other terms are also common, including salesclerk, shop assistant, and retail clerk.

2 Data Preparation

2.1 Import Library

library(dplyr)
library(lubridate)
library(forecast)
library(ggplot2)
library(tseries)
## Warning: package 'tseries' was built under R version 4.3.3
library(GGally)

2.2 Import Data

sales <- read.csv("data_input/sales.csv")
head(sales)

Our data contain 3 columns, which are:

  • X = Index number
  • Date = Date of sales happen
  • Sales = Sales that occured at that time

3 Exploratory Data Analysis

In time series, we only use Date and Sales as our variables. Which means we will remove X column because we doesn’t use that. We will also change our Date format from character into date format. We will also arrange the data ascending by Date.

sales_clean <- sales %>% 
  select(-X) %>% 
  mutate(Date = ymd(Date)) %>% 
  arrange(Date)

head(sales_clean)

Next we will check is there any missing value inside our cleaned data.

colSums(is.na(sales_clean))
##  Date Sales 
##     0     0

There is no missing value.

In time series, we also doesn’t want our data to have a period that was skipped. Therefor we will create a complete sequence of the date and compare it to our data.

complete_date <- seq.Date(from = min(sales_clean$Date),
                          to = max(sales_clean$Date),
                          by = "day")

all(complete_date == sales_clean$Date)
## [1] TRUE

TRUE value means our data doens’t have any skipped period.

Before creating Time Series Model, we will convert our cleaned data into time series object by using ts() function. We will put set our start with the earliest date in the data. For the frequency we will use 365 because our data consist of days that covers the whole year.

sales_ts <- ts(data = sales_clean$Sales,
               start = c(2021,10,1),
               frequency = 365)
sales_ts
## Time Series:
## Start = c(2021, 10) 
## End = c(2022, 9) 
## Frequency = 365 
##   [1] 29.10955 30.72044 30.30517 27.39089 22.84112 19.00244 23.90459 27.54414
##   [9] 30.56706 30.08703 25.64404 22.77784 21.09076 23.42618 28.48936 31.35395
##  [17] 31.52051 25.64484 21.98663 19.82563 21.10152 29.04911 32.23471 29.63427
##  [25] 28.46975 20.56920 21.07548 23.81732 30.27827 33.18964 30.88138 26.92816
##  [33] 21.48578 19.39893 24.00659 29.25184 33.30057 32.27881 26.51267 22.42126
##  [41] 20.68117 23.28449 27.73922 34.37105 30.91678 26.81193 21.82077 22.85721
##  [49] 23.44061 29.58275 31.87481 32.16334 27.08919 22.24293 22.40154 25.83284
##  [57] 30.21201 33.42275 31.49211 27.58726 23.10110 22.42017 24.94136 28.76921
##  [65] 33.64770 32.07465 26.66980 24.58635 22.22242 26.15645 31.57459 33.94926
##  [73] 33.96584 27.41517 24.87591 22.79491 25.58371 30.61664 33.85873 33.23260
##  [81] 27.83485 25.72439 24.29538 25.26826 33.03375 36.41617 34.70521 29.17008
##  [89] 24.10281 25.23417 26.75133 33.11794 35.07855 34.85307 30.05637 26.23014
##  [97] 24.54022 29.29038 32.37241 35.62227 36.10959 28.70224 24.60308 25.84912
## [105] 26.68138 34.53912 35.15666 33.82898 32.32294 27.70408 27.09728 29.11055
## [113] 32.08427 37.83034 34.65843 31.55246 27.52082 25.42471 29.16858 34.21770
## [121] 36.64670 34.17703 31.39824 28.24995 25.23515 28.75487 33.21034 38.46954
## [129] 36.29873 31.85746 26.50365 26.81897 28.58017 34.02733 36.33443 36.65287
## [137] 32.37659 27.41527 27.02573 28.51144 32.85424 37.75967 36.49311 32.78503
## [145] 30.35671 27.92420 29.04168 35.81251 36.35437 36.21485 32.43176 30.03691
## [153] 26.58497 29.47807 34.94704 37.35680 38.15307 31.77007 27.52610 27.24190
## [161] 30.15647 37.32503 39.31970 37.46399 31.97456 29.86793 27.02951 29.45973
## [169] 36.93352 39.03722 38.64729 33.86873 30.23040 27.72870 30.32026 36.77709
## [177] 38.26687 37.38689 33.44447 29.74104 28.37573 30.32955 35.80188 37.19688
## [185] 39.05167 32.64794 28.96918 29.13189 31.31494 38.33851 38.47742 39.04349
## [193] 34.56072 29.25547 29.95300 32.23296 37.91728 40.94378 41.28967 36.28653
## [201] 30.40438 29.54034 33.85417 38.15076 41.11041 37.85948 35.27567 30.38553
## [209] 30.40965 33.00636 38.75567 41.13750 40.61276 35.18358 30.52912 30.06967
## [217] 33.43749 38.57465 43.42959 40.13418 35.04405 31.47758 30.36613 34.28599
## [225] 37.00470 41.58354 40.68294 36.58218 31.57625 30.94180 32.73044 38.40217
## [233] 41.32742 41.29249 35.54382 33.30476 33.02421 32.43452 39.67175 42.89719
## [241] 40.58900 36.65273 32.74068 31.58193 34.54549 37.91949 43.72261 42.65605
## [249] 36.58664 31.75714 32.75079 34.62872 40.08745 42.60095 42.61797 38.44475
## [257] 32.84797 31.19636 33.97157 40.90587 42.08142 41.76962 37.50369 33.87100
## [265] 30.99344 36.09328 41.16939 43.70870 42.31555 38.54740 34.67261 30.50713
## [273] 38.21042 41.38559 43.31787 42.58548 39.29374 34.50746 31.59904 38.66900
## [281] 41.23495 45.34045 42.63439 40.68638 35.25991 34.58857 35.90925 42.90664
## [289] 45.36010 44.97828 38.87191 34.84254 36.63364 36.24449 41.90954 46.15717
## [297] 44.12416 40.43295 35.27412 35.04978 36.34798 44.05363 45.25211 43.69626
## [305] 40.86638 35.56285 33.69546 36.65779 43.43927 45.56070 44.59273 41.62774
## [313] 35.24674 34.64904 37.96963 43.18985 46.02811 44.78955 40.83837 36.61626
## [321] 35.01012 37.89151 43.72001 45.52936 44.26908 40.93771 36.91590 38.33645
## [329] 38.34981 44.73876 47.51747 44.58749 42.37325 36.23968 33.77055 40.01083
## [337] 42.38960 47.57121 45.44242 43.60955 38.84207 36.32634 39.06667 43.28142
## [345] 47.02936 46.19608 41.93531 38.28027 37.70824 40.45416 44.08135 46.38249
## [353] 48.19097 41.96055 37.82127 36.95853 38.61144 44.71752 47.69062 47.79679
## [361] 43.69846 38.82734 38.76157 41.14447 45.52981

Then we will check our data patern using autoplot.

sales_ts %>% autoplot()

From the plot above, our data trends is increasing but doesn’t have any seasonal on it because we can’t use decompose() function.

4 Cross Validation

Before we create our model, we need to split our data into train and test data. In time series case, we can’t just randomly split our data. But we need our data to be sequential. Therefor we will take 20% of our last data as our test and the rest for our train data.

sales_train <- head(sales_clean, -73)
sales_test <- tail(sales_clean, 73)

5 Forecasting

5.1 Exponential Smoothing

Because our data has trends but doens’t have any seasonal, we will use Double Exponential Smoothing

sales_ses <- HoltWinters(sales_ts,
                         gamma = F)
sales_ses
## Holt-Winters exponential smoothing with trend and without seasonal component.
## 
## Call:
## HoltWinters(x = sales_ts, gamma = F)
## 
## Smoothing parameters:
##  alpha: 0.1198238
##  beta : 0.1854204
##  gamma: FALSE
## 
## Coefficients:
##          [,1]
## a 42.69361181
## b  0.02290783

Then we will tune our model a bit with alpha 0.1 and beta 0.2

sales_ses_tun <- HoltWinters(sales_ts,
                             alpha = 0.1,
                             beta = 0.2,
                             gamma = F)
sales_ses_tun
## Holt-Winters exponential smoothing with trend and without seasonal component.
## 
## Call:
## HoltWinters(x = sales_ts, alpha = 0.1, beta = 0.2, gamma = F)
## 
## Smoothing parameters:
##  alpha: 0.1
##  beta : 0.2
##  gamma: FALSE
## 
## Coefficients:
##          [,1]
## a 42.69522634
## b  0.02206103

5.1.1 Evaluation

accuracy(sales_ses$fitted[ ,1], sales_ts)
##                  ME     RMSE      MAE       MPE    MAPE     ACF1 Theil's U
## Test set -0.1968965 4.649764 4.114551 -2.498784 12.7939 0.615672  1.274346
accuracy(sales_ses_tun$fitted[ ,1], sales_ts)
##                  ME     RMSE      MAE      MPE     MAPE      ACF1 Theil's U
## Test set -0.2188468 4.660118 4.101363 -2.57223 12.78257 0.6253871  1.287211

Based on MAPE, our best model is sales_ses_tun with 12.78% error rate with alpha = 0.1 and beta = 0.2.

6 ARIMA

We can’t create ARIMA model because our data range is too long.

7 Conclusion

With error rate at 12.78%, our sales forecasting data is good enough to predict sales that will happen in the future.