-
Notifications
You must be signed in to change notification settings - Fork 1
/
TimeSeriesFunctions_APACHE_0.4.R
82 lines (71 loc) · 3.01 KB
/
TimeSeriesFunctions_APACHE_0.4.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
library(elastic)
library(prophet)
stringMethods <- c('PROPHET')
directoryToSave <- "forecastModels"
getAvailableMethods <- function() {
return(stringMethods)
}
elasticConnection <- function(host, path, user, pwd, port) {
# CONNECTION TO ELASTICSEARCH NODE
connect(es_host = host, es_path = path, es_user= user, es_pwd = pwd,
es_port = port, es_transport_schema = "http")
ping()
}
searchElement <- function(name, index, tsfrequency, returnDF) {
# SEARCH FOR A NORMALIZED ELEMENT AND RETURN THE ASSOCIATED TIME SERIES
searchString <- ifelse(grepl("metrics", index, fixed=TRUE), 'metric:',
ifelse(grepl("factors", index, fixed=TRUE), 'factor:', 'strategic_indicator:'))
esearch <- Search(index = index, q = paste(searchString, name, sep = ''),
sort = "evaluationDate:asc", source = "value,evaluationDate", size = 10000)$hits$hits
valuesEsearch <- sapply(esearch, function(x) as.numeric(x$`_source`$value))
if (returnDF == FALSE) {
timeseries <- ts(valuesEsearch, frequency = tsfrequency, start = 0)
return(timeseries)
} else {
datesEsearch <- sapply(esearch, function(x) as.character(x$`_source`$evaluationDate))
datesEsearch <- as.Date(datesEsearch)
df <- data.frame("ds" = datesEsearch, "y" = valuesEsearch)
return(df)
}
}
saveModel <- function(name, index, method, model) {
cleanName <- gsub("[^[:alnum:] ]", "", name)
dir.create(directoryToSave)
filename <- paste(cleanName, index, method, sep = '_')
filename <- paste(directoryToSave, filename, sep = '/')
saveRDS(model, file = filename)
}
loadModel <- function(name, index, method) {
cleanName <- gsub("[^[:alnum:] ]", "", name)
filename <- paste(cleanName, index, method, sep = '_')
filename <- paste(directoryToSave, filename, sep = '/')
return(readRDS(filename))
}
checkModelExists <- function(name, index, method) {
cleanName <- gsub("[^[:alnum:] ]", "", name)
filename <- paste(cleanName, index, method, sep = '_')
filename <- paste(directoryToSave, filename, sep = '/')
return(ifelse(file.exists(filename), TRUE, FALSE))
}
trainProphetModel <- function(name, index) {
df <- searchElement(name, index, 7, returnDF = TRUE)
model <- prophet(df, daily.seasonality = 'auto', weekly.seasonality = 'auto')
saveModel(name, index, stringMethods[1], model)
return(model)
}
forecastProphet <- function(model, horizon) {
future <- make_future_dataframe(model, periods = horizon, freq = 'day', include_history = FALSE)
f <- predict(model, future)
flist <- list("lower1" = f$yhat_lower, "lower2" = f$yhat_lower, "mean" = f$yhat,
"upper1" = f$yhat_upper, "upper2" = f$yhat_upper)
return(flist)
}
forecastProphetWrapper <- function(name, index, horizon) {
model <- NULL
if(checkModelExists(name, index, stringMethods[1])) {
model <- loadModel(name, index, stringMethods[1])
} else {
model <- trainProphetModel(name, index)
}
return(forecastProphet(model, horizon))
}