-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstore1.R
executable file
·105 lines (75 loc) · 2.21 KB
/
store1.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
library(caret)
library(ggplot2)
library(pls)
library(data.table)
library(rpart)
library(bst)
library(plyr)
library(MASS)
nLag <- 12
khorizon <- 1
loja1 <- read.csv("./databases/store1.csv", header = TRUE, sep = ";", quote = "\"")
plot(loja1$Date,loja1$Sales)
acf(loja1$Sales)
plot(loja1$Sales, type = 'l')
base <- loja1
base$Sales = (base$Sales-min(base$Sales))/(max(base$Sales)-min(base$Sales))
base <- setDT(base)[, paste0('Sales', 1:nLag) := shift(Sales, 1:nLag)][]
base <- base[(nLag+1):nrow(base),]
# Start the clock!
ptm <- proc.time()
timeSlices <- createTimeSlices(1:nrow(base),
initialWindow =nrow(base)*2/3, horizon = khorizon , fixedWindow = FALSE)
str(timeSlices,max.level = 1)
trainSlices <- timeSlices[[1]]
testSlices <- timeSlices[[2]]
predTest <- c(1,2)
predTest <- predTest[0]
trueTest <- c(1,2)
trueTest <- trueTest[0]
for(i in 1:length(trainSlices)){
plsFitTime <- train(Sales ~ .,
data = base[trainSlices[[i]],],
method = "treebag"
)
pred <- predict(plsFitTime,base[testSlices[[i]],])
true <- base$Sales[testSlices[[i]]]
if(i==1){
predTest <- c(predTest,pred)
trueTest <- c(trueTest,true)
}else{
predTest <- c(predTest,pred[khorizon])
trueTest <- c(trueTest,true[khorizon])
}
}
mseTest <- mean( (predTest- trueTest)^2, na.rm = TRUE)
div <- abs((trueTest - predTest)/trueTest)
mapeTest <- mean(div[is.finite(div)], na.rm = TRUE)
mapeTest
mseTest
plsFitTime
plot(predTest,type="l",col="red")
lines(trueTest,col="green")
# Stop the clock
proc.time() - ptm
# #start train
# ptm <- proc.time()
# predTrain <- c(1,2)
# predTrain <- predTrain[0]
# trueTrain <- c(1,2)
# trueTrain <- trueTrain[0]
# baseTrain <- base[1:nrow(base)*2/3,]
# for(i in 1:nrow(baseTrain)){
# pred <- predict(plsFitTime,baseTrain[i,])
# true <- baseTrain$Sales[i]
# predTrain <- c(predTrain,pred)
# trueTrain <- c(trueTrain,true)
# }
# mseTrain <- mean( (predTrain- trueTrain)^2, na.rm = TRUE)
# div <- abs((trueTrain - predTrain)/trueTrain)
# mapeTrain <- mean(div[is.finite(div)], na.rm = TRUE)
# mapeTrain
# mseTrain
# proc.time() - ptm
# plot(predTrain,type="l",col="red")
# lines(trueTrain,col="green")