How to get forecasting output for all individual input in R

forecast
#1

I have a dataset with 3 different Item Numbers, with corresponding 36 months Quantity value. When I run the forecast output, it shows only the cumulative/only the first Item Numbers forecast. I want the output to show the forecast of each individual Item Number(That is Item1 - "004-0013" -> 6 months forecast, followed by Item2- "DP-023-0059"-> 6 months forecast and lastly Item3 -"502-00038R"-> 6 months forecast). Thanks in advance for any help. p.s: This is my first post, so if there is an error in the format of the post, please do let me know.

library('ggplot2')
library('forecast')
library('tseries')
a <- read.csv("high.csv", stringsAsFactors = F)
a$Month <- as.Date(a$Month)

Qty_ts = ts(a[,c('Qty')])

a$Qty_ma12 = ma(a$Qty, order = 3)

Qty_ma = ts(na.omit(a$Qty_ma), start = c(2016,1),end = c(2019),frequency = 
12)
decomp = stl(Qty_ma, s.window = "periodic")
deseasonal_Qty <- seasadj(decomp)
plot(decomp) 

adf.test(Qty_ma, alternative = "stationary")

Acf(Qty_ma, main="")
Pacf(Qty_ma,main="")

Qty_d1 = diff(deseasonal_Qty, differences =1)
plot(Qty_d1)
adf.test(Qty_d1, alternative = "stationary")

Acf(Qty_d1, main ='ACF for differenced Series')
Pacf(Qty_d1,main ='PACF for Differenced Series')

auto.arima(deseasonal_Qty, seasonal = FALSE)

fit<- auto.arima(deseasonal_Qty, seasonal =FALSE)
tsdisplay(residuals(fit), lag.max=45, main='(0,1,1) Model Residuals')

fit2 = arima(deseasonal_Qty, order =c(1,1,0))
fit2
tsdisplay(residuals(fit2), lag.max=15, main='Seasonal model Residuals')

fcast<- forecast(fit2, h=6)
plot(fcast)
fcast

####Data###

Item.Number     Month  Qty
1      004-0013  7/1/2017    1
2   DP-023-0059 12/1/2017    1
3   DP-023-0059  1/1/2018    1
4    502-00038R 11/1/2018   73
5    502-00038R  1/1/2019  738
6    502-00038R  6/1/2018  358
7    502-00038R  8/1/2018  751
8    502-00038R  5/1/2018  697
9    502-00038R  9/1/2018 1400
10   502-00038R  7/1/2018  210
11     004-0013  3/1/2018    4
12     004-0013  7/1/2016    4
13   502-00038R 12/1/2018 1832
14  DP-023-0059 12/1/2018    2
15  DP-023-0059  4/1/2017    2
16  DP-023-0059 11/1/2018    3
17  DP-023-0059  5/1/2016    3
18   502-00038R  5/1/2016  197
19   502-00038R  3/1/2018  302
20   502-00038R  2/1/2018  275
21   502-00038R  3/1/2017  291
22   502-00038R  3/1/2016  359
23     004-0013  8/1/2016    9
24  DP-023-0059  6/1/2017    4
25  DP-023-0059 11/1/2016    4
26  DP-023-0059  7/1/2017    4
27  DP-023-0059  9/1/2016    4
28  DP-023-0059 10/1/2017    4
29  DP-023-0059  5/1/2018    4
30  DP-023-0059  8/1/2016    4
31  DP-023-0059  6/1/2016    4
32  DP-023-0059  3/1/2018    4
33  DP-023-0059  2/1/2016    4
34   502-00038R  4/1/2017  365
35   502-00038R  1/1/2017  297
36   502-00038R  6/1/2016  590
37   502-00038R  7/1/2017  380
38   502-00038R  7/1/2016  418
39   502-00038R 10/1/2017  438
40   502-00038R  4/1/2018  288
41   502-00038R  5/1/2017  369
42   502-00038R  4/1/2016  237
43  DP-023-0059  3/1/2016    6
44  DP-023-0059  8/1/2017    5
45  DP-023-0059  2/1/2018    5
46  DP-023-0059  9/1/2017    5
47  DP-023-0059  8/1/2018    5
48  DP-023-0059  4/1/2016    5
49  DP-023-0059  6/1/2018    7
50  DP-023-0059  1/1/2016    6
51  DP-023-0059  7/1/2018    6
52  DP-023-0059  4/1/2018    6
53  DP-023-0059 11/1/2017    6
54  DP-023-0059  7/1/2016    6
55  DP-023-0059  1/1/2017    6
56  DP-023-0059 12/1/2016    6
57   502-00038R  1/1/2018 1483
58   502-00038R  2/1/2016  306
59   502-00038R  9/1/2016  420
60   502-00038R  8/1/2016  534
61  DP-023-0059  2/1/2017    9
62  DP-023-0059  5/1/2017    8
63  DP-023-0059 10/1/2018    8
64   502-00038R 11/1/2017  492
65   502-00038R  8/1/2017  723
66   502-00038R 12/1/2016  445
67   502-00038R  2/1/2017 1544
68  DP-023-0059  9/1/2018    9
69   502-00038R  1/1/2016  619
70   502-00038R  6/1/2017  679
71   502-00038R  9/1/2017  829
72   502-00038R 10/1/2016  517
73  DP-023-0059  3/1/2017    8
74  DP-023-0059 10/1/2016    8
75   502-00038R 12/1/2017  313
76   502-00038R 11/1/2016  867
77     004-0013 11/1/2018   14
78     004-0013 12/1/2018   15
79     004-0013  2/1/2017   14
80     004-0013  1/1/2019   19
81     004-0013 11/1/2016   30
82     004-0013  2/1/2018   13
83     004-0013  3/1/2017    9
84     004-0013  5/1/2017    4
85     004-0013  8/1/2017   15
86     004-0013  7/1/2018   13
87     004-0013  9/1/2016   16
88     004-0013  2/1/2016   17
89     004-0013 10/1/2018    6
90     004-0013  6/1/2018    7
91     004-0013  1/1/2018    6
92     004-0013  4/1/2017   11
93     004-0013 10/1/2017   17
94     004-0013 10/1/2016   21
95     004-0013  5/1/2018   13
96     004-0013  1/1/2017   12
97     004-0013  4/1/2016   24
98     004-0013  6/1/2017   11
99     004-0013 11/1/2017   12
100    004-0013  4/1/2018   14
101    004-0013  3/1/2016   13
102    004-0013 12/1/2016   12
103    004-0013  6/1/2016   16
104    004-0013  1/1/2016    6
105    004-0013 12/1/2017    9
106    004-0013  8/1/2018   12
107    004-0013  9/1/2017   21
108    004-0013  9/1/2018    6
109    004-0013  5/1/2016   12
0 Likes

#2

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

closed #3

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

0 Likes