代码之家  ›  专栏  ›  技术社区  ›  Davide Lorino

在[R]中绘制类(预测)的时间序列列表

  •  3
  • Davide Lorino  · 技术社区  · 7 年前

    我正在尝试使用预测时间序列数据列表绘制时间序列图的分面网格(理想情况下是3x3)。数据嵌套在列表中,属于forecast::forecast类。

     > class(forecasts)
    [1] "list"
    > class(forecasts$`1_1`)
    [1] "forecast"
    > head(forecasts, 2)
    $`1_1`
             Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
    Dec 2016       7.370299 7.335176 7.405422 7.316583 7.424015
    
    $`1_10`
             Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
    Dec 2016       7.396656 7.359845 7.433467 7.340359 7.452953
    

    我想绘制数据,到目前为止我已经尝试过:

    > map2(forecasts, names(forecasts), 
    +      function(forecast, time_series) plot(forecast, 
    +                                       main= "Blank", 
    +                                       bty="n",
    +                                       ylab="Monthly Revenue",
    +                                       xlab="Time"))
    

    它返回这个:

    我似乎不知道如何添加相应的列表标签名称,所以我在其中放置了一个字符串“blank”作为占位符。

    如果有人有任何解决方案来绘制预测格式时间序列数据列表,我将非常感谢。

        > names(forecasts)
     [1] "1_1"   "1_10"  "1_2"   "1_3"   "1_4"   "1_5"   "1_6"   "1_7"   "1_8"   "1_9"   "10_1" 
    [12] "10_10" "10_2"  "10_3"  "10_4"  "10_5"  "10_7"  "10_8"  "10_9"  "2_1"   "2_10"  "2_2"  
    [23] "2_3"   "2_4"   "2_5"   "2_6"   "2_7"   "2_8"   "2_9"   "3_1"   "3_10"  "3_2"   "3_3"  
    [34] "3_4"   "3_5"   "3_6"   "3_7"   "3_8"   "3_9"   "4_1"   "4_10"  "4_2"   "4_3"   "4_4"  
    [45] "4_5"   "4_6"   "4_7"   "4_8"   "4_9"   "5_1"   "5_10"  "5_2"   "5_3"   "5_4"   "5_5"  
    [56] "5_6"   "5_7"   "5_8"   "5_9"   "6_1"   "7_1"   "7_10"  "7_2"   "7_3"   "7_4"   "7_5"  
    [67] "7_6"   "7_7"   "7_8"   "7_9"   "8_1"   "8_10"  "8_2"   "8_3"   "8_4"   "8_5"   "8_6"  
    [78] "8_7"   "8_8"   "8_9"   "9_1"   "9_10"  "9_2"   "9_3"   "9_4"   "9_5"   "9_6"   "9_7"  
    [89] "9_9" 
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Tung    7 年前

    你可以用其中一个

    par(mfrow = c(3, 3))
    
    map2(forecasts, names(forecasts), 
          ~ plot(       .x, 
                 main = .y, 
                 bty  = "n",
                 ylab = "Monthly Revenue",
                 xlab = "Time"))
    
    # same as map2 but returns nothing
    # suitable for plotting & writing output to files
    walk2(forecasts, names(forecasts), 
          ~ plot(       .x, 
                 main = .y, 
                 bty  = "n",
                 ylab = "Monthly Revenue",
                 xlab = "Time"))
    
    pwalk(list(forecasts, names(forecasts)), 
          ~ plot(       ..1, 
                 main = ..2, 
                 bty  = "n",
                 ylab = "GDP",
                 xlab = "Year"))
    
    # to save some typing
    iwalk(forecasts, 
          ~ plot(       .x, 
                 main = .y, 
                 bty  = "n",
                 ylab = "Monthly Revenue",
                 xlab = "Time"))
    

    附笔: time_series 在您的函数中不需要,因为您在后面的函数中不使用它 plot 呼叫

    推荐文章