-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.R
127 lines (106 loc) · 4.27 KB
/
server.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
# ----------------------------------------------------------------------------
# PRECIOUS METALS PRICE FORECAST
# File: server.R
# (c) 2015 - Enrique Pérez Herrero
# 20/Dec/2015
# GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007
# ----------------------------------------------------------------------------
source('helpers.R')
metals.df <- getPreciousMetals(precious_metals, currency_list)
start.date <- head(metals.df$date, 1)
end.date <- tail(metals.df$date, 1)
# Forecast interval parameter
forecast.days <- 90
shinyServer(function(input, output) {
# Metal Prices plot using ggplot2
output$prices.plot <- renderPlot({
tag <- paste(input$metal_id, input$metal_curr, sep = '.')
ggplot(data = metals.df , aes_string('date', tag)) +
geom_line() +
stat_smooth(
method = input$method, formula = y ~ x, size = 1
) +
xlab('')
})
# Forecasted data with auto.arima plot
output$prediction.plot <- renderPlot({
tag <- paste(input$metal_id, input$metal_curr, sep = '.')
selected.metal <- ts(zoo(metals.df[tag],
order.by = metals.df$date))
arima.fit <- auto.arima(selected.metal,
approximation = FALSE,
trace = FALSE)
pred <- forecast(arima.fit, h = forecast.days)
arimaForecastPlot(pred, start = start.date, ylabel = tag)
})
# Data first differences plot
output$diff.plot <- renderPlot({
tag <- paste(input$metal_id, input$metal_curr, sep = '.')
selected.metal <-
ts(zoo(metals.df[tag], order.by = metals.df$date))
ifelse(
input$chcklog,
selected.metal <- diff(log10(selected.metal)),
selected.metal <- diff(selected.metal)
)
diff.df <- data.frame(date = seq(
from = start.date + 1, to = end.date , by = "1 day"
),
price = selected.metal)
ggplot(data = diff.df , aes_string('date', tag)) +
geom_line() +
xlab('')
})
output$text.sd <- renderText({
paste0("Start Date: ",
start.date <- head(metals.df$date, 1))
})
output$text.ed <- renderText({
paste0("End Date: ",
end.date <- tail(metals.df$date, 1))
})
# ARIMA results from auto.arima
output$text.arima <- renderPrint({
tag <- paste(input$metal_id, input$metal_curr, sep = '.')
selected.metal <- ts(zoo(metals.df[tag],
order.by = metals.df$date))
arima.fit <- auto.arima(selected.metal,
approximation = FALSE,
trace = FALSE)
summary(arima.fit)
})
output$metals.table <- renderDataTable(
metals.df[, grepl(paste0(input$metal_id, '|date'), names(metals.df))]
, options = list(pageLength = 10)
)
output$residuals.plot <- renderPlot({
tag <- paste(input$metal_id, input$metal_curr, sep = '.')
selected.metal <- ts(zoo(metals.df[tag],
order.by = metals.df$date))
arima.fit <- auto.arima(selected.metal,
approximation = FALSE,
trace = FALSE)
par(mfrow = c(1, 2))
acf(ts(arima.fit$residuals), main = 'ACF Residual')
pacf(ts(arima.fit$residuals), main ='PACF Residual')
})
forecast.table <- reactive({
tag <- paste(input$metal_id, input$metal_curr, sep = '.')
selected.metal <- ts(zoo(metals.df[tag],
order.by = metals.df$date))
arima.fit <- auto.arima(selected.metal,
approximation = FALSE,
trace = FALSE)
pred <- forecast(arima.fit, h = forecast.days)
pred <- as.data.frame(pred)
pred <- cbind(date = 0, pred[-1])
pred$date <-
seq(from = end.date + 1,
to = end.date + forecast.days,
by = "1 day")
return(pred)
})
output$table.arima <- renderDataTable({
forecast.table()
}, options = list(pageLength = 10))
})