How to get forecasting output for all individual input in R

Please try, to make your questions with a self-contained REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

I think you are modeling the three Items as just one, you can reshape your data frame and model each one separately or nest your data and use purrrpackage to fit all the models.

df <- data.frame(stringsAsFactors = FALSE,
                 Item.Number = as.factor(c("004-0013", "DP-023-0059", "DP-023-0059",
                                           "502-00038R", "502-00038R", "502-00038R",
                                           "502-00038R", "502-00038R", "502-00038R", "502-00038R",
                                           "004-0013", "004-0013", "502-00038R", "DP-023-0059",
                                           "DP-023-0059", "DP-023-0059", "DP-023-0059",
                                           "502-00038R", "502-00038R", "502-00038R", "502-00038R",
                                           "502-00038R", "004-0013", "DP-023-0059", "DP-023-0059",
                                           "DP-023-0059", "DP-023-0059", "DP-023-0059",
                                           "DP-023-0059", "DP-023-0059", "DP-023-0059", "DP-023-0059",
                                           "DP-023-0059", "502-00038R", "502-00038R",
                                           "502-00038R", "502-00038R", "502-00038R", "502-00038R",
                                           "502-00038R", "502-00038R", "502-00038R", "DP-023-0059",
                                           "DP-023-0059", "DP-023-0059", "DP-023-0059",
                                           "DP-023-0059", "DP-023-0059", "DP-023-0059", "DP-023-0059",
                                           "DP-023-0059", "DP-023-0059", "DP-023-0059",
                                           "DP-023-0059", "DP-023-0059", "DP-023-0059", "502-00038R",
                                           "502-00038R", "502-00038R", "502-00038R",
                                           "DP-023-0059", "DP-023-0059", "DP-023-0059", "502-00038R",
                                           "502-00038R", "502-00038R", "502-00038R",
                                           "DP-023-0059", "502-00038R", "502-00038R", "502-00038R",
                                           "502-00038R", "DP-023-0059", "DP-023-0059", "502-00038R",
                                           "502-00038R", "004-0013", "004-0013", "004-0013",
                                           "004-0013", "004-0013", "004-0013", "004-0013",
                                           "004-0013", "004-0013", "004-0013", "004-0013", "004-0013",
                                           "004-0013", "004-0013", "004-0013", "004-0013",
                                           "004-0013", "004-0013", "004-0013", "004-0013",
                                           "004-0013", "004-0013", "004-0013", "004-0013", "004-0013",
                                           "004-0013", "004-0013", "004-0013", "004-0013",
                                           "004-0013", "004-0013", "004-0013", "004-0013")),
                 Month = c("2017-07-01", "2017-12-01", "2018-01-01", "2018-11-01",
                           "2019-01-01", "2018-06-01", "2018-08-01", "2018-05-01",
                           "2018-09-01", "2018-07-01", "2018-03-01", "2016-07-01",
                           "2018-12-01", "2018-12-01", "2017-04-01", "2018-11-01", "2016-05-01",
                           "2016-05-01", "2018-03-01", "2018-02-01", "2017-03-01",
                           "2016-03-01", "2016-08-01", "2017-06-01", "2016-11-01", "2017-07-01",
                           "2016-09-01", "2017-10-01", "2018-05-01", "2016-08-01",
                           "2016-06-01", "2018-03-01", "2016-02-01", "2017-04-01",
                           "2017-01-01", "2016-06-01", "2017-07-01", "2016-07-01", "2017-10-01",
                           "2018-04-01", "2017-05-01", "2016-04-01", "2016-03-01",
                           "2017-08-01", "2018-02-01", "2017-09-01", "2018-08-01", "2016-04-01",
                           "2018-06-01", "2016-01-01", "2018-07-01", "2018-04-01",
                           "2017-11-01", "2016-07-01", "2017-01-01", "2016-12-01", "2018-01-01",
                           "2016-02-01", "2016-09-01", "2016-08-01", "2017-02-01",
                           "2017-05-01", "2018-10-01", "2017-11-01", "2017-08-01",
                           "2016-12-01", "2017-02-01", "2018-09-01", "2016-01-01", "2017-06-01",
                           "2017-09-01", "2016-10-01", "2017-03-01", "2016-10-01",
                           "2017-12-01", "2016-11-01", "2018-11-01", "2018-12-01", "2017-02-01",
                           "2019-01-01", "2016-11-01", "2018-02-01", "2017-03-01",
                           "2017-05-01", "2017-08-01", "2018-07-01", "2016-09-01", "2016-02-01",
                           "2018-10-01", "2018-06-01", "2018-01-01", "2017-04-01",
                           "2017-10-01", "2016-10-01", "2018-05-01", "2017-01-01",
                           "2016-04-01", "2017-06-01", "2017-11-01", "2018-04-01", "2016-03-01",
                           "2016-12-01", "2016-06-01", "2016-01-01", "2017-12-01",
                           "2018-08-01", "2017-09-01", "2018-09-01", "2016-05-01"),
                 Qty = c(1L, 1L, 1L, 73L, 738L, 358L, 751L, 697L, 1400L, 210L, 4L,
                         4L, 1832L, 2L, 2L, 3L, 3L, 197L, 302L, 275L, 291L, 359L, 9L,
                         4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 365L, 297L, 590L,
                         380L, 418L, 438L, 288L, 369L, 237L, 6L, 5L, 5L, 5L, 5L, 5L, 7L,
                         6L, 6L, 6L, 6L, 6L, 6L, 6L, 1483L, 306L, 420L, 534L, 9L, 8L,
                         8L, 492L, 723L, 445L, 1544L, 9L, 619L, 679L, 829L, 517L, 8L, 8L,
                         313L, 867L, 14L, 15L, 14L, 19L, 30L, 13L, 9L, 4L, 15L, 13L,
                         16L, 17L, 6L, 7L, 6L, 11L, 17L, 21L, 13L, 12L, 24L, 11L, 12L,
                         14L, 13L, 12L, 16L, 6L, 9L, 12L, 21L, 6L, 12L)
)

library(tidyr)
library(dplyr)

df %>% 
    spread(Item.Number, Qty) %>%
    mutate(Month = as.Date(Month)) %>% 
    arrange(Month) %>% 
    head()
#>        Month 004-0013 502-00038R DP-023-0059
#> 1 2016-01-01        6        619           6
#> 2 2016-02-01       17        306           4
#> 3 2016-03-01       13        359           6
#> 4 2016-04-01       24        237           5
#> 5 2016-05-01       12        197           3
#> 6 2016-06-01       16        590           4

Created on 2019-03-08 by the reprex package (v0.2.1)

1 Like