-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeSeriesTerritories.R
81 lines (52 loc) · 1.5 KB
/
timeSeriesTerritories.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
library(tseries)
library(ggplot2)
library(forecast)
# Test
library(nortest)
library(FinTS)
i <- 2
nombres <- c('EMEA', 'NORTHAME')
ruta <- paste0("C:/Users/maste/Desktop/Data_Science/Basic_Data_Analysis/data/sales_", nombres[i], ".csv")
df1 <- read.csv(ruta)
df1$SALES <- df1$SALES/1000
serie <- ts(df1$SALES, start = c(2003, 1), frequency = 12)
# Original
#par(mar=c(1,1,1,1))
plot(serie,
col = "mediumturquoise",
main = paste("Serie de Tiempo de", nombres[i]),
xlab= "Years",
ylab = "Sales in K")
# Plot with no tendency
seriet <- diff(serie,lag=1,differences = 1)
par(mar=c(1,1,1,1))
plot(seriet, col = "coral", main = "Serie")
# Hypothesis testing
k<-trunc(length(df1$SALES)^(1/4))
adf.test(seriet,k=k)
# p & q
#p
pacf(seriet, lag.max=30, main = "PACF", col = "firebrick4")
#q
acf(seriet, lag.max=30, main = "ACF", col ="firebrick4")
#q = 1,3,9
auto_model <- auto.arima(serie)
print(forecast(auto_model, h=12))
#par(mar=c(1,1,1,1))
plot(forecast(auto_model,h=12),
type ="l",
col = "lightseagreen",
main = paste("Predicciones de", nombres[i]),
xlab= "Years",
ylab = "Sales in K",
axes=TRUE)
print(summary(auto_model))
# Assumptions verification
# Test Ljung-Box, where h0: No autocorrelation
Box.test(auto_model$residuals, type = "Ljung-Box")
# Test ArchText, where h0: Constant variance
ArchTest(auto_model$residuals)
# Test t, where h0: mean = 0
t.test(auto_model$residuals)
# Test Lilliefors, where h0: e~Normal
lillie.test(auto_model$residuals)