【问题标题】:ggseasonplot in R forecast not showing forecast when last unit of time has only 1 observation当最后一个时间单位只有 1 个观测值时,R 预测中的 ggseasonplot 不显示预测
【发布时间】:2019-08-23 23:45:42
【问题描述】:

当使用 for Forecast 包中的 ggseasonplot 时,当每单位时间有 2 个或更多观测值(即一年中超过 2 个季度)时,它可以正常工作。

例如

austres  %>% ggseasonplot()

产量

但是,如果我们删除 Q2-1993(因此我们最后的观察结果是 Q1-1993),我们会在图例上看到 1993,但在图上看不到。它应该显示为一个点吗?如果是的话,我怎么把它放进去?

austres %>% window(end=c(1993,1)) %>% ggseasonplot()

【问题讨论】:

    标签: r ggplot2 time-series forecasting


    【解决方案1】:

    我已经找到了一种方法——但我不知道是否是 最简单的方法!

    我修改了ggseasonplot on github中的代码

    tspx <- tsp(austres %>% window(end=c(1993,1)))
    s <- round(frequency(x))
    x <- ts(austres, start = tspx[1], frequency = s)
    
    data <- data.frame(
      y = as.numeric(x),
      year = trunc(time(x)),
      cycle = as.numeric(cycle(x)),
      time = as.numeric((cycle(x) - 1) / s)
    )
    
    austres %>% window(end=c(1993,1)) %>% ggseasonplot()+
      layer(
        data[nrow(data)-1,] ,
        geom = "point",
        stat = "identity",
        position = "identity", aes(x= time,y=y,col="1993")
      )     
    

    这样做会产生这个情节,这正是我想要的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-15
      • 1970-01-01
      • 2020-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-09
      • 2014-04-03
      相关资源
      最近更新 更多