Created
June 24, 2020 16:58
-
-
Save chenpanliao/a75eab4baea9a1c0549b1a225c7b5bc1 to your computer and use it in GitHub Desktop.
shinyApp.R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
library(rsconnect) | |
library(shinyjs) | |
ui <- shinyUI(fluidPage( | |
titlePanel("index predictive plot and predictive table"), | |
mainPanel( | |
textInput("index1", label = "code") , | |
textOutput("yahooop"), | |
actionButton("goButton", "Go!"), | |
tabsetPanel( | |
tabPanel( | |
"op.index", | |
plotOutput("open.predict.plot1"), | |
tableOutput("open.value1"), | |
textOutput("open.table1"), | |
tableOutput("open.predict.table1"), | |
textOutput("open.table2") | |
), | |
tabPanel( | |
"close.index", | |
plotOutput("close.predict.plot1"), | |
tableOutput("close.value1"), | |
textOutput("close.table1"), | |
tableOutput("close.predict.table1"), | |
textOutput("close.table2"), | |
textOutput("income") | |
) | |
) | |
, | |
width = 15, | |
height = 15 | |
) | |
)) | |
server <- shinyServer(function(input, output, session) { | |
library(rsconnect) | |
library(tseries) | |
library(quantmod) | |
library(forecast) | |
library(car) | |
library(haven) | |
library(ggplot2) | |
yahoo1 <- reactive({ | |
paste("Please enter the code for YAHOO finance") | |
}) | |
output$yahooop <- renderText({ | |
yahoo1() | |
}) | |
observeEvent(input$goButton, { | |
library(tseries) | |
library(quantmod) | |
library(forecast) | |
library(car) | |
library(haven) | |
index3 <- | |
getSymbols(input$index1, auto.assign = FALSE) | |
index2 <- input$index1 | |
index.open <- na.omit(data.frame(index3[, 1])) | |
index.close <- na.omit(data.frame(index3[, 4])) | |
index.open.test <- | |
data.frame(index.open[1:(nrow(index.open) - 365), ]) | |
index.close.test <- | |
data.frame(index.close[1:(nrow(index.open) - 365), ]) | |
index.open.train <- | |
data.frame(index.open[1:(nrow(index.open)), ]) | |
index.close.train <- | |
data.frame(index.close[1:(nrow(index.open)), ]) | |
index.open.year <- data.frame(tail(index.open, 365)) | |
index.close.year <- | |
data.frame(tail(index.close, 365)) | |
colnames(index.open.year) = "OP.value" | |
colnames(index.open.train) = "OP.value" | |
colnames(index.close.year) = "close.value" | |
colnames(index.close.train) = "close.value" | |
mod1 <- | |
auto.arima( | |
index.open.train, | |
seasonal = TRUE, | |
ic = "aic", | |
test = "adf", | |
seasonal.test = "seas", | |
allowdrift = TRUE, | |
allowmean = TRUE, | |
stepwise = FALSE, | |
approximation = FALSE | |
) | |
mod2 <- | |
auto.arima( | |
index.close.train, | |
seasonal = TRUE, | |
ic = "aic", | |
test = "adf", | |
seasonal.test = "seas", | |
allowdrift = TRUE, | |
allowmean = TRUE, | |
stepwise = FALSE, | |
approximation = FALSE | |
) | |
predict.open <- | |
forecast( | |
index.open.test[[1]], | |
model = mod1, | |
h = 365, | |
include.mean = TRUE | |
) | |
predict.close <- | |
forecast( | |
index.close.test[[1]], | |
model = mod2, | |
h = 365, | |
include.mean = TRUE | |
) | |
t1 <- data.frame(index.open.year$OP.value) | |
t2 <- data.frame(index.close.year$close.value) | |
p2 <- data.frame(predict.close$mean) | |
p1 <- data.frame(predict.open$mean) | |
d1 = 0 | |
for (x in 1:2) { | |
d1 <- | |
((t2[x,]) - (t1[x,]) - ((p2[x,]) - (p1[x,]))) | |
} | |
output$open.predict.plot1 <- | |
renderPlot(plot( | |
forecast(index.open.year[[1]] , model = mod1, h = 5), | |
main = paste(c(index2), ".open", "ARIMA predicton plot") | |
)) | |
output$open.value1 <- | |
renderTable(tail(index.open.year, 5), rownames = TRUE) | |
open.formulaText1 <- reactive({ | |
paste("it is the index opening index in the past five days") | |
}) | |
output$open.table1 <- | |
renderText({ | |
open.formulaText1() | |
}) | |
output$open.predict.table1 <- | |
renderTable(data.frame(forecast( | |
index.open.year[[1]], | |
model = mod2, | |
h = 5 | |
))) | |
open.formulaText2 <- reactive({ | |
paste( | |
"The expected value of the opening index in the next five days and its 80%, 95% confidence interval" | |
) | |
}) | |
output$open.table2 <- | |
renderText({ | |
open.formulaText2() | |
}) | |
output$close.predict.plot1 <- | |
renderPlot(plot( | |
forecast(index.close.year[[1]], 30 , model = mod2, h = 5), | |
main = paste(c(index2), ".close", " ARIMA predicton plot") | |
)) | |
output$close.value1 <- | |
renderTable(tail(index.close.year, 5), rownames = TRUE) | |
close.formulaText1 <- reactive({ | |
paste("it is the index closeing index in the past five days") | |
}) | |
output$close.table1 <- | |
renderText({ | |
close.formulaText1() | |
}) | |
output$close.predict.table1 <- | |
renderTable(data.frame(forecast( | |
tail(index.close.year, 30)[[1]] , | |
model = mod2, | |
h = 5 | |
))) | |
close.formulaText2 <- reactive({ | |
paste( | |
"The expected value of the closeing index in the next five days and its 80%, 95% confidence interval" | |
) | |
}) | |
output$close.table2 <- | |
renderText({ | |
close.formulaText2() | |
}) | |
income <- reactive({ | |
paste("the net income is", d1) | |
}) | |
}) | |
}) | |
shinyApp(ui = ui, server = server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment