【问题标题】:How can I display time series graph with its forecasting line?如何显示带有预测线的时间序列图?
【发布时间】:2016-01-03 19:22:43
【问题描述】:

对象是来自用户的gettig参数,以使他们了解预测技术。因此,我想从移动平均线开始。尽管工作很简单,但我无法管理并且遇到了一些问题。

  1. 出现一个错误:错误:缺少需要 TRUE/FALSE 的值。

我不明白为什么会得到这个?

  1. 我想显示下一个期间的预测值。但是这个现成的公式不能提供吗?

`

library(shiny)

    shinyUI(pageWithSidebar(

    headerPanel("Forecasting Methods"),
     sidebarPanel(

      h3(strong("Moving Average",style = "color:black")),
      br(),
      sliderInput("ord","Order Size:",min = 1, max = 100, step= 1, value = 15),

    ),

    mainPanel(
      plotOutput(outputId = "ma1", width  = "700px",height = "400px"))

      ))

   library(shiny)
    library(ggplot2)
    library(forecast)
    library(TTR)

      shinyServer(function(input, output){


           output$ma1 <- renderPlot( 
           tmp <- data.frame(time = 1:100, sales = round(runif(100, 150, 879))),
           sm <- SMA(tmp[,"sales"],order=input$ord),
           y <-ggplot(tmp, aes(time, sales)) + geom_line() + geom_line(aes(time,sm),color="red") + xlab("Days") + ylab("Sales Quantity")+ ggtitle("Moving Average"),
            y

              )


})

【问题讨论】:

    标签: r ggplot2 shiny


    【解决方案1】:

    这是怎么回事:

    library(shiny)
    library(ggplot2)
    library(forecast)
    library(TTR)
    
    ui <- pageWithSidebar(    
      headerPanel("Forecasting Methods"),
      sidebarPanel(
    
        h3(strong("Moving Average",style = "color:black")),
        br(),
        sliderInput("ord","Order Size:",min = 1, max = 100, step= 1, value = 15)
    
      ),
    
      mainPanel(
        plotOutput(outputId = "ma1", width  = "700px",height = "400px"))
    )
    
    server <- function(input, output){
    
      n <- 0
      output$ma1 <- renderPlot({ 
        input$ord
        tmp <- data.frame(time = 1:100, sales = round(runif(100, 150, 879)) )
        sm <- SMA(tmp[,"sales"],order=input$ord)
        title <- sprintf("Moving Average (%d)",n)
        n <<- n+1
        y <-ggplot(tmp, aes(time, sales)) + 
          geom_line() + 
          geom_line(aes(time,sm),color="red") + 
          xlab("Days") + ylab("Sales Quantity")+ ggtitle(title)
        y
      })
    }
    
    shinyApp(ui, server)
    

    产量:

    至于您的程序 - 我无法准确重现您的错误:

    1 - 发布的程序不会运行。服务器函数代码块没有用大括号 ({}) 括起来,而是像 ui 函数代码一样结构化(逗号分隔语句)。这是错误的。 ui 函数代码不是像服务器代码那样的函数,而是输出 html/css/javascript 的一系列函数调用。从 R 控制台尝试它们,看看我的意思。

    2 - UI 函数至少有一个多余的逗号,我必须删除它才能使其工作。

    3 - 在初始化sm 数据帧的output$ma1 代码中使用input$ord 不足以导致函数响应,并在每次更新滑块时触发。不知道为什么这还不够,但是当我在函数前面添加另一个 input$ord 实例时,它起作用了。

    4- 我还在output$ma1 的标题中放置了一个计数器,以帮助我调试上面调试的缺乏反应性。

    5 - 我还将闪亮的ui.Rserver.R 文件合并到一个文件中,因为此示例很小,可以轻松查看所有内容。请注意,使用 Rstudio 选项卡式编辑器可能难以匹配 ui.Rserver.R 代码 - 如果您需要多个文件,则值得使用另一个编辑器(如 Atom 或 Notepad++)来帮助编写代码。

    【讨论】:

    • 也许也可以添加一些解释,这样以后的读者就不需要玩“找不同”了。
    • 好点,虽然我没有得到他的错误。但我可以做更多。
    • Shiny 有可怕的错误信息。有时我对此感到非常沮丧。但没有真正的竞争。
    • 我只想再问两个问题。首先,当我更改输入时,数据会更改。为了解决这个问题,我应该使用-例如-“set.seed(123)”吗? 2.玩k时,红线的起点也应该改变。例如,如果我设置k 8,应该从第8天开始,如果是20应该是20。因为它是移动平均线的特征,不是吗?
    • 为 1,是的,您可以明确设置种子,以便它始终生成相同的随机数序列,但可能只是在 output$ma1 = renderPlot( 代码之外(之前)生成 tmp 数组,以便它不是每次都重新生成会更好,更多你想要的。
    猜你喜欢
    • 2018-12-24
    • 1970-01-01
    • 2018-06-17
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多