2016-01-05 38 views
0

我想要建立指数平滑方法是我选择的预测技术之一。但是,我有一些代表ggplot和计算结果/报告的问题。R中的指数平滑和Shiny

最初,我正在生成随机数据集以便用于此技术,其中要预测的alpha和期数由用户确定。例如;我有100天,并且接下来的4天愿意用他们的线 - 上限和下限估计。然后,我想以表格的形式了解这些数据的值。

当我试图想象的情节,错误的是:GGPLOT2不知道如何处理类mtstsmatrix的数据

其次,我想监视的数据等: 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")) 
    )))) 
+2

您需要指定要解决的问题。如果可以,请发布屏幕截图。如果您正在生成随机数据,则应在测试过程中指定种子,以便始终获得相同的数据。 –

+0

我将编辑我的帖子以便清楚。 –

+0

tmp.pred可能不是一个数据框,所以这就是为什么你会得到“错误是:ggplot2不知道如何处理类mtstsmatrix的数据” – MLavoie

回答

1

你有做tmp.pred可口的ggplot因为有人说我评论。你也不必创建多个语句相同的数据,一个reactive命令是很好的为:

enter image description here

ui.R(不变)

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")) 
    )))) 

服务器。 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 
    }) 
}) 
+0

你节省了我的一天。谢谢!! –