如何在r闪亮中应用Holt的冬季预测方法?

时间:2016-02-18 19:56:50

标签: r shiny forecasting

我想通过闪亮调整Holt Winter的方法作为预测方法。但是我无法绘制真实和预测的数据。但是我得到一个错误,表明时间序列没有或少于2个句点。

以下是代码:

ui.R

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

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

h3(strong("Holt's Winter",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),
sliderInput("beta","Beta (Smoothing Parameter):",
            min = 0.05, max = 1, step= 0.05, value = 0.01),
sliderInput("gamma","Gamma (Smoothing Parameter):",
            min = 0.05, max = 1, step= 0.05, value = 0.01)),

  mainPanel(
    tabsetPanel( id="tabs",
             tabPanel("Holt's Winter",
                      value="panel",
                      plotOutput(outputId = "hw",
                                 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 = , beta = input$beta,gamma=input$gamma)
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$hw <- 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 })

0 个答案:

没有答案