can.u
can.u

Reputation: 545

Exponential Smoothing in R and representing with Shiny

I want to build forecast techniques that exponential smoothing method is one of my selection. However, I have some issues with representing the ggplot and the result/report of the calculation.

Initially, I am generating random dataset in order to be used for this technique where alpha and number of periods to be forecasted are determined by the user. For instance; i have 100 days and next 4 days are willing to be estimated with their lines -fit, upper and lower-. Then I want to learn the values of this data as a table.

When I try to visualize the plot, the error is: ggplot2 doesn't know how to deal with data of class mtstsmatrix

Secondly, I would like to monitor the data like: enter image description here

 require(shiny)
 require(ggplot2)
 require(forecast)
 require(TTR)

 shinyServer(function(input, output, session){

   set.seed(123)
   output$es1 <- renderPlot({ 

  tmp <- data.frame(time = 1:100, sales = round(runif(100, 150, 879)) )
  tmp.mean <- HoltWinters(x=tmp$sales, alpha = input$alpha, beta = FALSE,gamma=FALSE)
  tmp.pred <- predict(tmp.mean,n.ahead = input$h, prediction.interval = TRUE)

 y <- ggplot(tmp, aes(time, sales)) + 
          geom_line() +   
          geom_line(data=tmp.pred, aes(y=tmp.pred[,1]),color="red") +  
          geom_line(data=tmp.pred, aes(y=tmp.pred[,2]),color="blue") + 
          xlab("Days") + 
          ylab("Sales Quantity")+ 
          ggtitle(title)
  y })

 output$infoes <- renderDataTable({ 

tmp <- data.frame(time = 1:100, sales = round(runif(100, 150, 879)) )
tmp.mean <- HoltWinters(x=tmp$sales, alpha = input$alpha, beta = FALSE,gamma=FALSE)
tmp.pred <- predict(tmp.mean,n.ahead = input$h, prediction.interval = TRUE)
tmp.pred

  })

ui

 require(shiny)
 require(ggplot2)
 require(forecast)
 require(TTR)

   shinyUI(pageWithSidebar(    
     headerPanel("Forecasting Methods"),
     sidebarPanel(

   h3(strong("Exponential Smoothing",style = "color:black")),
   br(),
   sliderInput("h","Number of periods for forecasting:",
        min = 1, max = 20,     step= 1, value = 4),
   sliderInput("alpha","Alpha (Smoothing Parameter):",
        min = 0.05, max = 1, step= 0.05, value = 0.01)

    ),

 mainPanel(
    tabsetPanel( id="tabs",
         tabPanel("Exponential Smoothing",
                   value="panel",
                   plotOutput(outputId = "es1",
                   width  = "900px",height = "400px"),
                   dataTableOutput(outputId="infoes"))
    ))))

Upvotes: 0

Views: 947

Answers (1)

mlegge
mlegge

Reputation: 6913

You had a to make tmp.pred palatable for ggplot as was said in the comments. You also don't have to create the same data in multiple statements, a reactive command is good for that:

enter image description here

ui.R (unchanged)

require(shiny)
require(ggplot2)
require(forecast)
require(TTR)

shinyUI(pageWithSidebar(    
  headerPanel("Forecasting Methods"),
  sidebarPanel(

    h3(strong("Exponential Smoothing",style = "color:black")),
    br(),
    sliderInput("h","Number of periods for forecasting:",
                min = 1, max = 20,     step= 1, value = 4),
    sliderInput("alpha","Alpha (Smoothing Parameter):",
                min = 0.05, max = 1, step= 0.05, value = 0.01)

  ),

  mainPanel(
    tabsetPanel( id="tabs",
                 tabPanel("Exponential Smoothing",
                          value="panel",
                          plotOutput(outputId = "es1",
                                     width  = "900px",height = "400px"),
                          dataTableOutput(outputId="infoes"))
    ))))

server.R

require(shiny)
require(ggplot2)
require(forecast)
require(TTR)

shinyServer(function(input, output, session){

  set.seed(123)

  predset <- reactive({
    tmp <- data.frame(time = 1:100, sales = round(runif(100, 150, 879)) )
    tmp.mean <- HoltWinters(x=tmp$sales, alpha = input$alpha, beta = FALSE,gamma=FALSE)
    tmp.pred <- data.frame(predict(tmp.mean,n.ahead = input$h, prediction.interval = TRUE), time = tmp[nrow(tmp), "time"] + 1:input$h)  
    list(tmp = tmp, tmp.pred = tmp.pred)
  })

  output$es1 <- renderPlot({

    tmp <- predset()$tmp
    tmp.pred <- predset()$tmp.pred

    y <- ggplot(tmp, aes(time, sales)) + 
      geom_line() +   
      geom_line(data=tmp.pred, aes(y=upr),color="red") +  
      geom_line(data=tmp.pred, aes(y=fit),color="blue") +
      geom_line(data=tmp.pred, aes(y=lwr),color="red") +
      xlab("Days") + 
      ylab("Sales Quantity")+ 
      ggtitle("title")
    y })

  output$infoes <- renderDataTable({ 
    predset()$tmp.pred
  })
})

Upvotes: 1

Related Questions