Advertisement
Guest User

Untitled

a guest
Jun 24th, 2020
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 6.21 KB | None | 0 0
  1. library(shiny)
  2. library(rsconnect)
  3. library(shinyjs)
  4. ui <- shinyUI(fluidPage(
  5.     titlePanel("index predictive plot and predictive table"),
  6.     mainPanel(
  7.         textInput("index1", label = "code") ,
  8.         textOutput("yahooop"),
  9.         actionButton("goButton", "Go!"),
  10.        
  11.         tabsetPanel(
  12.             tabPanel(
  13.                 "op.index",
  14.                 plotOutput("open.predict.plot1"),
  15.                 tableOutput("open.value1"),
  16.                 textOutput("open.table1"),
  17.                 tableOutput("open.predict.table1"),
  18.                 textOutput("open.table2")
  19.             ),
  20.             tabPanel(
  21.                 "close.index",
  22.                 plotOutput("close.predict.plot1"),
  23.                 tableOutput("close.value1"),
  24.                 textOutput("close.table1"),
  25.                 tableOutput("close.predict.table1"),
  26.                 textOutput("close.table2"),
  27.                 textOutput("income")
  28.             )
  29.         )
  30.         ,
  31.         width = 15,
  32.         height = 15
  33.     )
  34. ))
  35.  
  36. server <- shinyServer(function(input, output, session) {
  37.     library(rsconnect)
  38.     library(tseries)
  39.     library(quantmod)
  40.     library(forecast)
  41.     library(car)
  42.     library(haven)
  43.     library(ggplot2)
  44.    
  45.    
  46.     yahoo1 <- reactive({
  47.         paste("Please enter the code for YAHOO finance")
  48.     })
  49.     output$yahooop <- renderText({
  50.         yahoo1()
  51.     })
  52.    
  53.     observeEvent(input$goButton, {
  54.         library(tseries)
  55.         library(quantmod)
  56.         library(forecast)
  57.         library(car)
  58.         library(haven)
  59.        
  60.         index3 <-
  61.             getSymbols(input$index1, auto.assign = FALSE)
  62.         index2 <- input$index1
  63.         index.open <- na.omit(data.frame(index3[, 1]))
  64.         index.close <- na.omit(data.frame(index3[, 4]))
  65.         index.open.test <-
  66.             data.frame(index.open[1:(nrow(index.open) - 365), ])
  67.         index.close.test <-
  68.             data.frame(index.close[1:(nrow(index.open) - 365), ])
  69.         index.open.train <-
  70.             data.frame(index.open[1:(nrow(index.open)), ])
  71.         index.close.train <-
  72.             data.frame(index.close[1:(nrow(index.open)), ])
  73.         index.open.year <- data.frame(tail(index.open, 365))
  74.         index.close.year <-
  75.             data.frame(tail(index.close, 365))
  76.         colnames(index.open.year) = "OP.value"
  77.         colnames(index.open.train) = "OP.value"
  78.         colnames(index.close.year) = "close.value"
  79.         colnames(index.close.train) = "close.value"
  80.         mod1 <-
  81.             auto.arima(
  82.                 index.open.train,
  83.                 seasonal = TRUE,
  84.                 ic = "aic",
  85.                 test = "adf",
  86.                 seasonal.test = "seas",
  87.                 allowdrift = TRUE,
  88.                 allowmean = TRUE,
  89.                 stepwise = FALSE,
  90.                 approximation = FALSE
  91.             )
  92.         mod2 <-
  93.             auto.arima(
  94.                 index.close.train,
  95.                 seasonal = TRUE,
  96.                 ic = "aic",
  97.                 test = "adf",
  98.                 seasonal.test = "seas",
  99.                 allowdrift = TRUE,
  100.                 allowmean = TRUE,
  101.                 stepwise = FALSE,
  102.                 approximation = FALSE
  103.             )
  104.         predict.open <-
  105.             forecast(
  106.                 index.open.test[[1]],
  107.                 model = mod1,
  108.                 h = 365,
  109.                 include.mean = TRUE
  110.             )
  111.         predict.close <-
  112.             forecast(
  113.                 index.close.test[[1]],
  114.                 model = mod2,
  115.                 h = 365,
  116.                 include.mean = TRUE
  117.             )
  118.         t1 <- data.frame(index.open.year$OP.value)
  119.         t2 <- data.frame(index.close.year$close.value)
  120.         p2 <- data.frame(predict.close$mean)
  121.         p1 <- data.frame(predict.open$mean)
  122.         d1 = 0
  123.         for (x in 1:2) {
  124.             d1 <-
  125.                 ((t2[x,]) - (t1[x,]) - ((p2[x,]) - (p1[x,])))
  126.         }
  127.         output$open.predict.plot1 <-
  128.             renderPlot(plot(
  129.                 forecast(index.open.year[[1]] , model = mod1, h = 5),
  130.                 main = paste(c(index2), ".open", "ARIMA predicton plot")
  131.             ))
  132.        
  133.         output$open.value1 <-
  134.             renderTable(tail(index.open.year, 5), rownames = TRUE)
  135.         open.formulaText1 <- reactive({
  136.             paste("it is the index opening index in the past five days")
  137.         })
  138.         output$open.table1 <-
  139.             renderText({
  140.                 open.formulaText1()
  141.             })
  142.         output$open.predict.table1 <-
  143.             renderTable(data.frame(forecast(
  144.                 index.open.year[[1]],
  145.                 model = mod2,
  146.                 h = 5
  147.             )))
  148.        
  149.         open.formulaText2 <- reactive({
  150.             paste(
  151.                 "The expected value of the opening index  in the next five days and its 80%, 95% confidence interval"
  152.             )
  153.         })
  154.         output$open.table2 <-
  155.             renderText({
  156.                 open.formulaText2()
  157.             })
  158.        
  159.         output$close.predict.plot1 <-
  160.             renderPlot(plot(
  161.                 forecast(index.close.year[[1]], 30 , model = mod2, h = 5),
  162.                 main = paste(c(index2), ".close", " ARIMA predicton plot")
  163.             ))
  164.         output$close.value1 <-
  165.             renderTable(tail(index.close.year, 5), rownames = TRUE)
  166.         close.formulaText1 <- reactive({
  167.             paste("it is the index closeing index in the past five days")
  168.         })
  169.         output$close.table1 <-
  170.             renderText({
  171.                 close.formulaText1()
  172.             })
  173.         output$close.predict.table1 <-
  174.             renderTable(data.frame(forecast(
  175.                 tail(index.close.year, 30)[[1]] ,
  176.                 model = mod2,
  177.                 h = 5
  178.             )))
  179.        
  180.         close.formulaText2 <- reactive({
  181.             paste(
  182.                 "The expected value of the closeing index  in the next five days and its 80%, 95% confidence interval"
  183.             )
  184.         })
  185.         output$close.table2 <-
  186.             renderText({
  187.                 close.formulaText2()
  188.             })
  189.         income <- reactive({
  190.             paste("the net income is", d1)
  191.         })
  192.     })
  193. })
  194. shinyApp(ui = ui, server = server)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement