-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProductA_timeseries.R
73 lines (48 loc) · 1.94 KB
/
ProductA_timeseries.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
# Time- Series Analysis
library(tidyverse)
library(forecast)
library(evobiR)
library(tseries)
library(urca)
library(TSstudio)
library(Metrics)
productA <- read.csv("ProductA.csv") # Read sales quantity from csv file
# Splitting of time-series data into training and testing set
split_point <- floor(0.8 * nrow(productA))
productA_training <- productA[1:split_point,]
productA_testing <- productA[(split_point + 1):nrow(productA), ]
# Defining a time series object using the tarining data set
productA_ts <- ts(productA_training$Quantity, start = 1, frequency = 7)
autoplot(productA_ts) + ggtitle("Product A sales") + labs(x = 'time', y = 'Sales')
productA_clean <- SlidingWindow("mean",productA_training$Quantity,3,1) # Cleaning of data set using sliding window
productA_ts <- ts(productA_clean, start = 1, frequency = 7)
autoplot(productA_ts) + ggtitle("Product A sales") + labs(x = 'time', y = 'Sales')
ggAcf(productA_ts) + ggtitle("ACF of sales")
ggPacf(productA_ts) + ggtitle("PACF of sales")
d_productA_ts <- diff(productA_ts)
ggAcf(d_productA_ts) + ggtitle("ACF")
ggPacf(d_productA_ts) + ggtitle("PACF")
# Decomposition of time series object
ts_decompose(productA_ts, type = "additive", showline = TRUE)
# Tests for stationarity
adf.test(productA_ts)
adf.test(productA_ts, k=1)
adf.test(productA_ts, k=2)
pp.test(productA_ts)
pp.test(d_productA_ts)
kpss.test(productA_ts)
kpss.test(d_productA_ts)
# ARIMA modelling
fit_productA_ts <- auto.arima(productA_ts)
summary(fit_productA_ts)
forecast <- data.frame(forecast(fit_productA_ts, h = nrow(productA)-split_point))
forecast <- forecast$Point.Forecast
productA_testing <- productA_testing$Quantity
mape(productA_testing,forecast)
view(forecast)
time_index <- 1:(nrow(productA)-split_point)
df <- data.frame(time_index, productA_testing, forecast)
plot(df$time_index, df$productA_testing, type = "l", col = "blue",
xlab = "Time Index",
ylab = "Values")
lines(df$time_index, df$forecast, col = "red")