【问题标题】:Exponential Smoothing in R and representing with ShinyR中的指数平滑并用Shiny表示
【发布时间】:2026-01-20 15:55:01
【问题描述】:

我想建立预测技术,指数平滑法是我的选择之一。但是,我在表示 ggplot 和计算结果/报告时遇到了一些问题。

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

当我尝试可视化绘图时,错误是:ggplot2 不知道如何处理类 mtstsmatrix 的数据

其次,我想监控以下数据:

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

【问题讨论】:

  • 您需要指定要修复的问题。如果可以,请发布屏幕截图。此外,如果您正在生成随机数据,则应在测试期间指定种子,以便始终获得相同的数据。
  • 我将编辑我的帖子以明确。
  • tmp.pred 可能不是数据框,所以这就是为什么你会得到“错误是:ggplot2 不知道如何处理 mtstsmatrix 类的数据”

标签: r ggplot2 shiny forecasting smoothing


【解决方案1】:

正如 cmets 中所说,您必须使 tmp.pred 适合 ggplot。您也不必在多个语句中创建相同的数据,reactive 命令非常适合:

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

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

【讨论】:

  • 你拯救了我的一天。谢谢!!