【问题标题】:repeated forecasts gives same values重复的预测给出相同的值
【发布时间】:2018-07-11 11:25:06
【问题描述】:

我有一个公司不同部门的月度绩效数据集(以百分比表示)

   Date    |Sector  |Value
2016-01-01 |Sect 1  |-20
2016-02-01 |Sect 1  |10
2016-01-01 |Sect 2  |23
2016-02-01 |Sect 1  |10 

截至 2018 年 6 月,数据有 20 个行业和月度数据。现在我想预测下个月的价值。我使用了以下代码:

combine_ts <- function(data, h=1, frequency= 12, start= c(2016,5), 
end=c(2018,6)) 
{
  results <- list()
  sectgrowthsub <- data[!duplicated(sectgrowthdf2[,2]),]
  sectgrowthts <- ts(sectgrowthsub[,3], frequency = frequency, start = start, 
  end = end)


  for (i in 1:(nrow(sectgrowthsub))) {    
  results[[i]] <- data.frame(Date = 
  format(as.Date(time(forecast(auto.arima(sectgrowthts), h)$mean)), "%b-%y"),
                         SectorName = rep(sectgrowthsub[,2], h),
                         PointEstimate = forecast(auto.arima(sectgrowthts), 
                         h=h)$mean[i])

    }

return(data.table::rbindlist(results)) 
}
fore <- combine_ts(sectgrowthsub) 

在这种情况下的问题是所有部门的价值预测都是相同的。 非常感谢您的帮助

【问题讨论】:

  • 你检查auto.arima使用的参数了吗?默认情况下,它使用 (0, 0, 0),从而使您的预测成为您之前值的简单平均值
  • 参数为(1,0,0)。但除此之外,为什么所有部门的价值预测都相同是主要问题。
  • 因为您将相同的数据传递给循环的每次迭代。您需要通过分组变量 secgrowthsub 拆分/分隔 sectgrowthts,然后您会看到差异。
  • 啊!我现在明白了。关于如何以这种方式传递它的任何想法@Nate?我一直在尝试使用相应的一组值传递不同的扇区,但我一直在传递所有扇区的集合。感谢任何帮助,因为我介于 R 的初学者和新手之间。

标签: r time-series


【解决方案1】:

我冒昧地把问题简化了一点,去掉了函数,以便更好地展示单独建模组的过程:

library(magrittr)
library(forecast)

dat <- data.frame(value = c(rnorm(36, 5),
                            rnorm(36, 50)),
                  group = rep(1:2, each = 36))

# make a list where each element is a group's timeseries
sect_list <- dat %>%
    split(dat$group) %>%
    lapply(function(x, frequency, start) {
        ts(x[["value"]], frequency = 12, start = 1 ) })

# then forecast on each groups timeseries
fc <- lapply(sect_list, function(x) { data.frame(PointEstimate = forecast(x, h=1)$mean ) }) %>%
    do.call(rbind, .) # turn into one big data.frame

fc

PointEstimate
1      5.120082
2     49.752510

如果您对此有任何疑问,请告诉我。

【讨论】:

    猜你喜欢
    • 2017-04-25
    • 2019-02-21
    • 1970-01-01
    • 1970-01-01
    • 2016-10-29
    • 2020-06-11
    • 2018-09-02
    • 1970-01-01
    • 2016-07-30
    相关资源
    最近更新 更多