forked from GonzaloAGarcia/CRIXForecastApp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CRIXForecastApp.R
232 lines (195 loc) · 6.23 KB
/
CRIXForecastApp.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
library("shiny")
library("shinyjs")
library("tidyverse")
library("dplyr")
library("xts")
library("dygraphs")
library("lubridate")
library("tibbletime")
library("forecast")
library("zoo")
library("timetk")
library("reshape2")
library("keras")
library("htmltools")
source("CRIXdataloader/CRIXdataloader.R")
source("r/analysis.R")
ETS <-
readRDS(file = "models/ets.rds")
coin <- get_crypto()
coin$date <- ymd(coin$date)
log_returns <- diff.xts(coin[, 2], lag = 1, log = T)
log_returns <- xts(log_returns, order.by = coin$date)
log_returns <- na.omit(log_returns)
ui <- fluidPage(
tags$head(tags$style(".rightAlign{float:right;}")), # Align right the plots from Analysis page
useShinyjs(),
theme = 'bootstrap.min.css',
# Navigation Bar
navbarPage(
"Seiðr",
# Forecast Tab
tabPanel(
"Forecast",
sidebarPanel(
textInput(
"coin",
"Entity:",
"CRIX"
),
numericInput("periods_to_forecast",
"Select Number of Days to Forecast:",
value = 1),
uiOutput('no_value'), # No error caused by empty periods_to_forecast
uiOutput('ptf_text'), # Prompt user to enter a value if periods_to_forecast is empty
selectInput(
"model",
"Select Model:",
c(
"LSTM" = 'lstm',
"Naive" = 'naive_lr',
"Mean" = "mean_lr",
"ETS" = "ets_lr"
)
),
uiOutput('lstm_loading_time_text'), # Note about LSTM loading times
# Show ETS parameters. Hidden if model != ETS
conditionalPanel(
condition = "input.model == 'ets_lr'",
textInput("spec_ets",
"Method",
value = ETS$method),
textInput("error",
"Error",
value = ETS$components[1]),
textInput("trend",
"Trend",
value = ETS$components[2]),
textInput("seasonality",
"Seasonality",
value = ETS$components[3]),
numericInput("alpha",
"Alpha",
signif(ETS[["par"]][["alpha"]], 3))
),
actionButton("update_plot", "Plot") # Button to update plot.
),
mainPanel(dygraphOutput("series")) # App Output
),
# Analysis Tab
tabPanel(
"Analysis",
sidebarPanel(
textInput(
"coin_analysis",
"Entity:",
"CRIX"
),
selectInput(
"analysis",
"Select Analysis:",
c(
"Autocorrelation" = "acf",
"Partial Autocorrelation" = "pacf",
"Monthly Decomposition" = "mdecomp"
)
),
actionButton("update_analysis", "Plot")
),
# Determines which analysis is shown based on user input.
mainPanel(
conditionalPanel(
condition = "input.analysis == 'mdecomp'",
dygraphOutput("seasonal",height='230px')
),
conditionalPanel(
condition = "input.analysis == 'mdecomp'",
dygraphOutput("trend",height='230px')
),
conditionalPanel(
condition = "input.analysis == 'mdecomp'",
dygraphOutput("remainder",height='230px')
),
conditionalPanel(
condition = "input.analysis == 'acf' | input.analysis == 'pacf'",
plotOutput("acfpacf")
)
)),
tabPanel("References"),
tabPanel("Terms of Use"),
tabPanel("Contact"),
tabPanel("About"),
# Dark background for the NavBar
inverse = TRUE
)
)
server <- function(input, output) {
# Backend of "Plot" button
observeEvent(input$update_plot, {
if (is.na(input$periods_to_forecast)) {
} else {
output$series <- renderDygraph({
interactive_graph(
periods_to_forecast = isolate(floor(input$periods_to_forecast)),
for_model = isolate(input$model)
)
})}
})
# Backend of "Prompt user to enter a value if periods_to_forecast is empty" text
output$ptf_text <-
renderUI({
if (is.na(input$periods_to_forecast)){
br()
} else if(input$model == 'lstm' & input$periods_to_forecast > 1){
return(p("Warning: The LSTM model was designed to forecast exactly one step into the future. The chosen forecasting horizon is greater than one.", style = "color:red"))
}
})
# Backend of LSTM loading times text
output$lstm_loading_time_text <-
renderUI({
if(input$model == 'lstm'){
return(p("Note: Loading the necessary libraries for prediction with the LSTM model might take a few seconds.", style = "color:gray"))
}
})
# Backend of empty periods_to_forecast
result<-reactive({
validate(
need(input$periods_to_forecast, "Please input a value.")
)
})
output$no_value <- renderPrint({
result()
})
# Disable coin Input
disable('coin')
disable('coin_analysis')
# Disable user Input on ETS parameters
disable('spec_ets')
disable('error')
disable('trend')
disable('seasonality')
disable('alpha')
# Analysis Backend
observeEvent(input$update_analysis, {
if (isolate(input$analysis) == "mdecomp") {
output$seasonal <- renderDygraph({
analysis(for_analysis = isolate(input$analysis), part="seasonal")
})
output$trend <- renderDygraph({
analysis(for_analysis = isolate(input$analysis), part="trend")
})
output$remainder <- renderDygraph({
analysis(for_analysis = isolate(input$analysis), part="remainder")
})
} else if (isolate(input$analysis) == "acf") {
output$acfpacf <- renderPlot({
analysis(for_analysis = isolate(input$analysis))
})
} else if (isolate(input$analysis) == "pacf") {
output$acfpacf <- renderPlot({
analysis(for_analysis = isolate(input$analysis))
})
}
})
}
shinyApp(ui = ui, server = server)