I would like to achieve the following task. Using a linear mixed model, I would like to check whether "Month" (see dat table) has a significant effect on the "Response" variable. As for some of the tanks, data comes from different months, I included it as a random factor in my model. Please note, that sampling the same tank in different months does not change the "Response" variable. For some tank-month combinations there are multiple records, as we are included the compartment of the tank that was sampled (e.g. NW =north west).
Here the data:
print(dat)
Tank Month ID Response
1 AEW1 Jul AEW01SOBFJul2008 1.80522937
2 AEW10 Jul AEW10NWBFJul2008 2.13374401
3 AEW10 Jul AEW10NWBFJul2008 2.13374401
4 AEW11 Jun AEW11SWBFJun2008 1.65010205
5 AEW14 Jun AEW14SWBFJun2008 1.75459326
6 AEW15 Jun AEW15SOBFJun2008 2.82200903
7 AEW15 Jun AEW15SOBFJun2008 2.82200903
8 AEW18 Jul AEW18SOBFJul2008 0.39349330
9 AEW19 Jul AEW19NWBFJul2008 0.65886661
10 AEW20 Jul AEW20NWBFJul2008 1.07838018
11 AEW24 Jun AEW24NOBFJun2008 2.56677635
12 AEW27 Jul AEW27SWBFJul2008 2.64019328
13 AEW27 Jul AEW27SWBFJul2008 2.64019328
14 AEW29 Jul AEW29SOBFJul2008 2.06251217
15 AEW30 Jul AEW30NWBFJul2008 1.17010646
16 AEW31 Jun AEW31SWBFJun2008 2.25518873
17 AEW32 Jun AEW32SOBFJun2008 2.38707614
18 AEW33 Jun AEW33SOBFJun2008 2.30498448
19 AEW33 Jun AEW33SOBFJun2008 2.30498448
20 AEW36 Jul AEW36NOBFJul2008 1.92368247
21 AEW37 Jun AEW37NOBFJun2008 0.99387013
22 AEW39 Jul AEW39NOBFJul2008 1.24163732
23 AEW4 Jul AEW04SWBFJul2008 1.56327732
24 AEW42 Jun AEW42SWBFJun2008 1.26012579
25 AEW44 Jun AEW44SWBFJun2008 0.75985267
26 AEW48 Aug AEW48SOBFAug2008 1.57920494
27 AEW50 Jul AEW50NOBFJul2008 0.90052629
28 AEW8 Jul AEW08NOBFJul2008 0.00000000
29 AEW8 Jul AEW08NOBFJul2008 0.00000000
30 AEW9 Jul AEW09NOBFJul2008 0.48529647
31 HEW10 Jun HEW10SWBFJun2008 0.06412823
32 HEW10 Aug HEW10SOBFAug2008 0.06412823
33 HEW12 Jul HEW12NOBFJul2008 0.00000000
34 HEW13 Aug HEW13NWBFAug2008 2.24515850
35 HEW13 Jul HEW13SOBFJul2008 2.24515850
36 HEW13 Jul HEW13NOBFJul2008 2.24515850
37 HEW13 Jun HEW13SOBFJun2008 2.24515850
38 HEW13 Jun HEW13NWBFJun2008 2.24515850
39 HEW14 Jul HEW14SOBFJul2008 1.64783184
40 HEW18 Jun HEW18NWBFJun2008 1.32435721
41 HEW18 Jun HEW18NWBFJun2008 1.32435721
42 HEW19 Jul HEW19SWBFJul2008 1.01761003
43 HEW19 Jul HEW19SWBFJul2008 1.01761003
44 HEW22 Aug HEW22SWBFAug2008 0.63861037
45 HEW23 Jun HEW23SWBFJun2008 1.38472769
46 HEW23 Jun HEW23NWBFJun2008 1.38472769
47 HEW28 Jun HEW28NOBFJun2008 1.44377199
48 HEW3 Jun HEW03SWBFJun2008 2.19793633
49 HEW3 Jul HEW03SWBFJul2008 2.19793633
50 HEW30 Aug HEW30NWBFAug2008 0.76260579
51 HEW31 Jul HEW31SWBFJul2008 1.07879539
52 HEW35 Jun HEW35NWBFJun2008 0.86098152
53 HEW35 Jun HEW35NWBFJun2008 0.86098152
54 HEW36 Aug HEW36SOBFAug2008 0.36533352
55 HEW39 Jun HEW39SOBFJun2008 0.09283168
56 HEW4 Jun HEW04SWBFJun2008 1.89046783
57 HEW41 Aug HEW41NWBFAug2008 0.31996275
58 HEW41 Aug HEW41NWBFAug2008 0.31996275
59 HEW41 Jul HEW41NWBFJul2008 0.31996275
60 HEW41 Jul HEW41NWBFJul2008 0.31996275
61 HEW42 Jul HEW42NWBFJul2008 0.53998250
62 HEW43 Jun HEW43SWBFJun2008 1.85594061
63 HEW43 Jun HEW43SWBFJun2008 1.85594061
64 HEW44 Jun HEW44SOBFJun2008 1.79972095
65 HEW44 Jun HEW44SOBFJun2008 1.79972095
66 HEW49 Jun HEW49SWBFJun2008 1.25229249
67 HEW5 Aug HEW05SWBFAug2008 0.95559764
68 HEW50 Jun HEW50NWBFJun2008 0.42309531
69 HEW50 Jun HEW50NWBFJun2008 0.42309531
70 HEW7 Jul HEW07NWBFJul2008 0.69484213
71 HEW7 Jun HEW07NWBFJun2008 0.69484213
72 HEW8 Jul HEW08SWBFJul2008 1.15617440
73 SEW1 Aug SEW01NWBFAug2008 1.90030109
74 SEW1 Sep SEW01SWBFSep2008 1.90030109
75 SEW11 Aug SEW11NWBFAug2008 2.11940912
76 SEW12 Aug SEW12SOBFAug2008 2.29658624
77 SEW12 Jul SEW12SOBFJul2008 2.29658624
78 SEW17 Aug SEW17NOBFAug2008 1.49277937
79 SEW17 Jul SEW17NOBFJul2008 1.49277937
80 SEW17 Sep SEW17NOBFSep2008 1.49277937
81 SEW17 Aug SEW17SOBFAug2008 1.49277937
82 SEW18 Aug SEW18SOBFAug2008 1.70247509
83 SEW19 Aug SEW19SOBFAug2008 2.11617036
84 SEW20 Jul SEW20SWBFJul2008 1.87718089
85 SEW20 Jul SEW20SOBFJul2008 1.87718089
86 SEW22 Aug SEW22NOBFAug2008 0.77473833
87 SEW23 Aug SEW23NWBFAug2008 0.96183454
88 SEW23 Aug SEW23NOBFAug2008 0.96183454
89 SEW24 Jul SEW24SWBFJul2008 0.64090368
90 SEW24 Jul SEW24NWBFJul2008 0.64090368
91 SEW29 Jul SEW29SOBFJul2008 1.54699664
92 SEW29 Aug SEW29SWBFAug2008 1.54699664
93 SEW29 Aug SEW29SOBFAug2008 1.54699664
94 SEW34 Aug SEW34NWBFAug2008 1.79425003
95 SEW36 Jul SEW36SOBFJul2008 1.20337761
96 SEW4 Aug SEW04SWBFAug2008 1.59611963
97 SEW40 Sep SEW40SOBFSep2008 1.36486039
98 SEW40 Aug SEW40SWBFAug2008 1.36486039
99 SEW43 Sep SEW43SOBFSep2008 1.03169382
100 SEW44 Aug SEW44SWBFAug2008 0.79705660
101 SEW45 Jul SEW45NWBFJul2008 0.34130398
102 SEW46 Aug SEW46SOBFAug2008 0.20690386
103 SEW47 Aug SEW47SWBFAug2008 0.01564703
104 SEW47 Sep SEW47SWBFSep2008 0.01564703
105 SEW48 Aug SEW48SWBFAug2008 0.46745254
106 SEW5 Aug SEW05SWBFAug2008 0.68900435
107 SEW50 Aug SEW50NWBFAug2008 1.10731406
108 SEW7 Aug SEW07SWBFAug2008 0.08552432
109 SEW8 Jul SEW08NWBFJul2008 0.18731374
The model I generated so far is: Mod1 <- lmer(Response ~ Month + (1|Tank), data=dat)
Again, I included "Tank" because we sampled some tanks in several months but that does not change the response variable. Consequently, the response variable is fixed for each tank. Nevertheless, multiple data points originate from the same tank and I tried to account for that by including it as a random factor.
Fitting Mod1 results in the following message:
Warning messages:
1: In checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, :
Model failed to converge with max|grad| = 0.306567 (tol = 0.002, component 1)
2: In checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, :
Model is nearly unidentifiable: very large eigenvalue
- Rescale variables?
The question is now, whether the model is overly complex and whether I can drop "Tank" as a random factor, as measuring tanks repeatedly did not have an effect on the response variable.
Thus, the question is, would a simple linear model Mod1 <- lm(Response ~ Month, data =dat) be valid? And if not, how can I solve the 2 convergence issues.
Any help is very much appreciated! :)
Related
I'm trying to forecast an accumulated monthly time series (see data below) with the auto.arima function with exogenous regressors. I have two issues.
1) My first issue is that when I fit the model and use the forecast function to predict the second half of 2019 the forecast starts from zero as can be seen in this forecast plot.
This only happens when I include a matrix of exogenous regressors and not when I use a single time series as regressor as can be seen in this plot.
Why is that? My code is:
regnskab <- ts(data$Regnskab, frequency = 12, start = c(2014,1), end = c(2019,6))
budget <- ts(data$Budget, frequency = 12, start = c(2014,1), end = c(2019,6))
dagtilbud <- ts(data$Dagtilbud, frequency = 12, start = c(2014,1), end = c(2019,6))
skole <- ts(data$Skole, frequency = 12, start = c(2014,1), end = c(2019,6))
sundhed <- ts(data$Sundhed, frequency = 12, start = c(2014,1), end = c(2019,6))
miljø <- ts(data$Miljø, frequency = 12, start = c(2014,1), end = c(2019,6))
tsmatrix <- cbind(budget, dagtilbud, miljø, skole, sundhed)
fit <- auto.arima(regnskab, xreg = tsmatrix)
fcast <- forecast(fit, h = 6, xreg = tsmatrix)
autoplot(fcast)
summary(fcast)
2) My second issue is that I want a forecast for 6 months forward, but the h=6 option does not apply when including exogenous regressors. Can this be solved in any way? Again, it is not a problem without exogenous regressors.
I hope you can help and sorry for the data spamming!
A summary of my model:
> summary(fcast)
Forecast method: Regression with ARIMA(1,0,0)(1,0,0)[12] errors
Model Information:
Series: regnskab
Regression with ARIMA(1,0,0)(1,0,0)[12] errors
Coefficients:
ar1 sar1 budget dagtilbud miljø skole sundhed
0.7466 0.6693 0.0101 2.0861 0.1037 2.5240 7.7623
s.e. 0.0935 0.1042 0.0077 0.6967 1.7672 0.7535 2.6611
sigma^2 estimated as 1.884: log likelihood=-114.84
AIC=245.68 AICc=248.21 BIC=263.2
Error measures:
ME RMSE MAE MPE MAPE MASE ACF1
Training set -0.01739231 1.297694 0.9002519 -0.1065542 0.9060671 0.3687968 -0.03222251
> regnskab
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2014 19.11281 36.68003 54.66383 74.93864 94.10328 113.36373 134.96638 152.75095 170.79800 189.55430 207.00803 227.82096
2015 18.90205 37.20079 55.73305 75.44689 94.74538 115.03997 136.79829 155.41164 173.69889 191.96484 210.42391 231.52982
2016 20.12939 38.51516 56.32522 78.04822 97.46681 116.58424 139.43255 157.83048 175.26727 195.06259 213.73833 234.45281
2017 20.43082 38.55219 57.50119 78.07558 97.50132 119.13735 141.71973 161.49281 180.32002 199.27769 216.92571 239.40683
2018 19.35194 37.40571 55.36897 76.33412 95.90922 117.41442 140.03545 159.10527 177.88068 194.43207 215.28905 245.85670
2019 20.85722 40.01691 59.97383 81.92719 103.15225 123.81454
> tsmatrix
budget dagtilbud miljø skole sundhed
Jan 2014 230.0605 2.616639 0.597125 3.193017 0.456470
Feb 2014 230.0605 5.025708 1.047983 6.402845 1.012468
Mar 2014 230.0605 7.548424 1.458105 9.816814 1.602384
Apr 2014 230.0605 10.350321 1.957022 13.446215 2.263646
May 2014 230.0605 12.913356 2.439587 17.100957 2.873934
Jun 2014 230.0605 15.380146 2.915020 20.791343 3.498350
Jul 2014 230.0605 17.931069 3.434464 23.701276 3.987042
Aug 2014 230.0605 20.441732 3.837721 27.319389 4.597127
Sep 2014 230.0605 22.839922 4.295486 30.859254 5.185271
Oct 2014 230.0605 25.234620 4.761740 34.350629 5.819948
Nov 2014 230.0605 27.554525 5.163576 37.688182 6.416112
Dec 2014 230.0605 30.109529 5.742699 42.095747 7.313195
Jan 2015 234.5089 2.404843 0.643976 3.185265 0.477921
Feb 2015 234.5089 5.090533 1.094641 6.654691 1.040235
Mar 2015 234.5089 7.319261 1.462134 10.168618 1.659232
Apr 2015 234.5089 10.040823 1.943120 14.082780 2.356247
May 2015 234.5089 12.470742 2.431818 17.827494 2.963360
Jun 2015 234.5089 14.846720 3.019969 21.612527 3.615607
Jul 2015 234.5089 17.543682 3.540084 24.702634 4.126374
Aug 2015 234.5089 19.786612 3.984587 28.330977 4.741392
Sep 2015 234.5089 22.037785 4.362497 31.942762 5.367815
Oct 2015 234.5089 24.347196 4.805391 35.423452 6.019133
Nov 2015 234.5089 26.751255 5.250481 38.964450 6.642436
Dec 2015 234.5089 29.276667 5.789919 43.428855 7.555361
Jan 2016 237.2361 2.538133 0.721184 3.352676 0.508847
Feb 2016 237.2361 4.906975 1.377086 6.804320 1.100914
Mar 2016 237.2361 7.184724 1.719629 10.290800 1.744743
Apr 2016 237.2361 9.895237 2.333842 14.223635 2.480869
May 2016 237.2361 12.316509 2.850905 17.957433 3.115473
Jun 2016 237.2361 14.578536 3.404785 21.759111 3.858713
Jul 2016 237.2361 17.215216 3.867858 24.949928 4.359129
Aug 2016 237.2361 19.399769 4.406750 28.503968 5.030926
Sep 2016 237.2361 21.702215 4.792190 32.112449 5.674259
Oct 2016 237.2361 24.112579 5.238401 35.625806 6.328084
Nov 2016 237.2361 26.453919 5.677270 39.158270 6.977991
Dec 2016 237.2361 28.969565 6.098136 43.558768 7.974787
Jan 2017 241.9089 2.538901 0.917354 3.488151 0.535639
Feb 2017 241.9089 4.847981 1.450172 6.857674 1.138782
Mar 2017 241.9089 7.281994 1.899543 10.394615 1.808938
Apr 2017 241.9089 10.031959 2.388542 14.335895 2.554613
May 2017 241.9089 12.411935 2.893036 18.042788 3.206503
Jun 2017 241.9089 14.982942 3.282057 22.137085 3.959622
Jul 2017 241.9089 17.567382 3.770244 25.392706 4.540047
Aug 2017 241.9089 19.738993 4.484434 29.108498 5.196528
Sep 2017 241.9089 22.273634 5.051894 32.693173 5.870257
Oct 2017 241.9089 24.636583 5.456458 36.203329 6.544383
Nov 2017 241.9089 27.259158 5.793056 39.867875 7.249982
Dec 2017 241.9089 29.831986 6.079033 44.273697 8.269454
Jan 2018 246.0944 2.467981 0.985846 3.377469 0.544258
Feb 2018 246.0944 4.877189 1.383190 6.815726 1.167431
Mar 2018 246.0944 7.367918 1.738033 10.486250 1.848972
Apr 2018 246.0944 10.148353 2.249466 14.439246 2.614913
May 2018 246.0944 12.687311 2.844656 18.194669 3.328234
Jun 2018 246.0944 15.482606 3.616200 22.433048 4.108966
Jul 2018 246.0944 17.715938 3.982451 25.305411 4.689087
Aug 2018 246.0944 20.077201 4.696088 29.018017 5.396796
Sep 2018 246.0944 22.659831 5.158706 32.860215 6.087975
Oct 2018 246.0944 24.719623 5.586616 36.143198 6.713136
Nov 2018 246.0944 27.750904 6.069519 40.237747 7.501346
Dec 2018 246.0944 30.326036 6.308786 44.733470 8.564162
Jan 2019 251.9230 2.653607 0.932776 3.501389 0.595458
Feb 2019 251.9230 5.070721 1.445741 6.991538 1.243721
Mar 2019 251.9230 7.542256 1.825956 10.737607 1.941444
Apr 2019 251.9230 10.301781 2.330015 14.647082 2.733956
May 2019 251.9230 13.193286 2.999816 18.671285 3.455616
Jun 2019 251.9230 15.423716 3.516735 22.612031 4.145206
The xreg matrix in the forecast function should be for the future time periods. If you want h=6, then give a matrix of 6 rows corresponding to those 6 periods.
How do I convert this data set into a time series format in R? Lets call the data set Bob. This is what it looks like
1/2013 25
2/2013 865
3/2013 26
4/2013 33
5/2013 74
6/2013 24
Are you looking for something like this....?
> dat <- read.table(text = "1/2013 25
2/2013 865
3/2013 26
4/2013 33
5/2013 74
6/2013 24
", header=FALSE) # your data
> ts(dat$V2, start=c(2013, 1), frequency = 12) # time series object
Jan Feb Mar Apr May Jun
2013 25 865 26 33 74 24
Assuming that your starting point is the data frame DF defined reproducibly in the Note at the end this converts it to a zoo series z as well as a ts series tt.
library(zoo)
z <- read.zoo(DF, FUN = as.yearmon, format = "%m/%Y")
tt <- as.ts(z)
z
## Jan 2013 Feb 2013 Mar 2013 Apr 2013 May 2013 Jun 2013
## 25 865 26 33 74 24
tt
## Jan Feb Mar Apr May Jun
## 2013 25 865 26 33 74 24
Note
Lines <- "1/2013 25
2/2013 865
3/2013 26
4/2013 33
5/2013 74
6/2013 24"
DF <- read.table(text = Lines)
I have a lot of time series and i want a forecast for every single one for ten months. For some of them it works, for the most i always get the same forecast for every month.
The time series consists monthly data. For example:
> ts(Menge[Nummer==8 & Jahr>2014 & Index<61 ], frequency=12)
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1 6 225 0 114 21 25 5 256 1 6 1 8
2 13 35 180 215 20 48 20 31 283 130 3 1
3 53 31 0 142 60 76 10 28 298 29 5 14
The output of dput is:
dput(Menge[Nummer==8 & Jahr>2014 & Index<61 ])
c(6, 225, 0, 114, 21, 25, 5, 256, 1, 6, 1, 8, 13, 35, 180, 215,
20, 48, 20, 31, 283, 130, 3, 1, 53, 31, 0, 142, 60, 76, 10, 28,
298, 29, 5, 14)
When i decompose the time series i get seasonality and a trend:
> decompose(ts(Menge[Nummer==8 & Jahr>2014 & Index<61 ], frequency=12))
$x
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1 6 225 0 114 21 25 5 256 1 6 1 8
2 13 35 180 215 20 48 20 31 283 130 3 1
3 53 31 0 142 60 76 10 28 298 29 5 14
$seasonal
Jan Feb Mar Apr May Jun Jul Aug Sep Oct
1 -35.142361 -30.496528 25.065972 106.899306 -32.163194 -10.371528 -57.725694 76.336806 78.878472 4.295139
2 -35.142361 -30.496528 25.065972 106.899306 -32.163194 -10.371528 -57.725694 76.336806 78.878472 4.295139
3 -35.142361 -30.496528 25.065972 106.899306 -32.163194 -10.371528 -57.725694 76.336806 78.878472 4.295139
Nov Dec
1 -63.100694 -62.475694
2 -63.100694 -62.475694
3 -63.100694 -62.475694
$trend
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1 NA NA NA NA NA NA 55.95833 48.33333 47.91667 59.62500 63.79167 64.70833
2 66.29167 57.54167 59.91667 76.83333 82.08333 81.87500 83.25000 84.75000 77.08333 66.54167 65.16667 68.00000
3 68.75000 68.20833 68.70833 65.12500 61.00000 61.62500 NA NA NA NA NA NA
$random
Jan Feb Mar Apr May Jun Jul Aug
1 NA NA NA NA NA NA 6.7673611 131.3298611
2 -18.1493056 7.9548611 95.0173611 31.2673611 -29.9201389 -23.5034722 -5.5243056 -130.0868056
3 19.3923611 -6.7118056 -93.7743056 -30.0243056 31.1631944 24.7465278 NA NA
Sep Oct Nov Dec
1 -125.7951389 -57.9201389 0.3090278 5.7673611
2 127.0381944 59.1631944 0.9340278 -4.5243056
3 NA NA NA NA
$figure
[1] -35.142361 -30.496528 25.065972 106.899306 -32.163194 -10.371528 -57.725694 76.336806 78.878472 4.295139
[11] -63.100694 -62.475694
$type
[1] "additive"
attr(,"class")
[1] "decomposed.ts"
But the forecast is always the same:
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Jan 4 68.41899 -47.17701 184.015 -108.3698 245.2078
Feb 4 68.41899 -47.17701 184.015 -108.3698 245.2078
Mar 4 68.41899 -47.17701 184.015 -108.3698 245.2078
Apr 4 68.41899 -47.17701 184.015 -108.3698 245.2078
May 4 68.41899 -47.17701 184.015 -108.3698 245.2078
Jun 4 68.41899 -47.17701 184.015 -108.3698 245.2078
Jul 4 68.41899 -47.17701 184.015 -108.3698 245.2078
Aug 4 68.41899 -47.17701 184.015 -108.3698 245.2078
Sep 4 68.41899 -47.17701 184.015 -108.3698 245.2078
Oct 4 68.41899 -47.17701 184.015 -108.3698 245.2078
The data are three years (2015-2017). The forecast should be for the first ten months of 2018 (so that i can prove how good is the fitting in reality).
I did about 1000 forecasts (by changing "number" i get another time series) and very often i just got the same values, sometimes the point forecast is the same, but the Lo and Hi values change a little bit, in some cases i get different values for every month.
I observed the data in some cases but can't find a reason, why in some cases the forecasts are the same and in other cases not. Especially because i get saisonality and trends by decomposing the time series.
The whole code is:
setwd("Z:/Bestellvorschlag/Lagerdrehung") #workspace festlegen
x= read.csv("Daten Aufbereitet.csv", header=TRUE, sep=";") #read the data
attach(x)
library(forecast)
Zeilenanzahl<-length(x[,1]) #number of rows
AnzahlArtikel<-x[Zeilenanzahl,1] #number of articles
ForecastMatrix<-matrix(0,9*AnzahlArtikel,8) #i want nine forecasts for every article
#with the columns Nummer, Monat,Forecast, lower80, lower 95, upper 80, upper 95, Menge
for (i in 1:AnzahlArtikel) { #do it for all numbers; each number is another product
#extract mean(point forecast), lower und upper bounds
TS<- ts(Menge[Nummer==i & Jahr>2014 & Index<61 ], frequency=12)
mean<-unlist(forecast(TS,9)[2])
upper<-unlist(forecast(TS,9)[5])
lower<-unlist(forecast(TS,9)[6])
#write the data in a matrix
for (j in 1:9) {
ForecastMatrix[9*(i-1)+j,1]<-i
ForecastMatrix[9*(i-1)+j,2]<-j
ForecastMatrix[9*(i-1)+j,3]<-mean[j]
ForecastMatrix[9*(i-1)+j,4]<-lower[j]
ForecastMatrix[9*(i-1)+j,5]<-lower[9+j]
ForecastMatrix[9*(i-1)+j,6]<-upper[j]
ForecastMatrix[9*(i-1)+j,7]<-upper[9+j]
ForecastMatrix[9*(i-1)+j,8]<-Menge[Nummer==i & Jahr==2018 & Monat==j] #the real value
}
}
#write the data in a .csv
write.table(ForecastMatrix, file = "Forecastmatrix.csv", sep= ";")
you are getting same forecast value because your data is white noise (no trend, season and cycle), if the model find such data it will simply take avg and print same result for all forecast, therefore you are getting same forecast.
By the way i think you didn't create model ex. arima.
You should first build model then forecast.
I've used r to get some forecast result.
library(forecast)
fc<-forecast(fit.ets)
fc
I got a result like this
Points Forecast Lo 80 Hi 80 Lo 95 Hi 95
19.5, 1.8895 xxx xxx xxx xxx
20.0 xxxx xxx xxx xxx xxx
...
I want to get the Points Column to plot my data, how can I get this column?
You haven't copied and pasted correctly. The output is like this:
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
19.5 1.8895 xxx xxx xxx xxx
20.0 xxxx xxx xxx xxx xxx
...
The left hand column gives the time periods. The second column gives the "Point Forecast" -- that is, the estimated mean or median of each future observation.
If you just want the future times, you can get them using the time() function:
time(fc$mean)
Contrary to what you say in the comments, ets() does not change the x-axis scale, it simply fits a model. When you pass that model to forecast.ets(), the resulting times are a continuation of the sequence of time periods from the data that you provide.
For example:
> USAccDeaths
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1973 9007 8106 8928 9137 10017 10826 11317 10744 9713 9938 9161 8927
1974 7750 6981 8038 8422 8714 9512 10120 9823 8743 9129 8710 8680
1975 8162 7306 8124 7870 9387 9556 10093 9620 8285 8466 8160 8034
1976 7717 7461 7767 7925 8623 8945 10078 9179 8037 8488 7874 8647
1977 7792 6957 7726 8106 8890 9299 10625 9302 8314 8850 8265 8796
1978 7836 6892 7791 8192 9115 9434 10484 9827 9110 9070 8633 9240
> library(forecast)
> ets.fit <- ets(USAccDeaths)
> fc <- forecast(ets.fit)
> fc
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Jan 1979 8252.1 7945.0 8559.2 7782.4 8721.8
Feb 1979 7473.7 7162.8 7784.6 6998.3 7949.2
Mar 1979 8281.0 7903.8 8658.3 7704.0 8858.0
Apr 1979 8527.1 8107.5 8946.6 7885.4 9168.7
....
Nov 1980 8794.6 7960.6 9628.6 7519.1 10070.1
Dec 1980 9072.3 8195.5 9949.0 7731.4 10413.2
> time(fc$mean)
Jan Feb Mar Apr May Jun Jul Aug Sep
1979 1979.0 1979.1 1979.2 1979.3 1979.3 1979.4 1979.5 1979.6 1979.7
1980 1980.0 1980.1 1980.2 1980.3 1980.3 1980.4 1980.5 1980.6 1980.7
Oct Nov Dec
1979 1979.8 1979.8 1979.9
1980 1980.8 1980.8 1980.9
using the data below, and the line of code below, I am trying to produce a stacked area plot showing planned spend by project across the quarters specified in the data. Capex on the Y axis, quarters on the X axis. I have looked at many examples here and elsewhere, and I just cannot understand why it is failing. I'd like to post a screenshot of the result - but cant see a way to do that. Basically, it has the legend, and the axes look correct. But the main area of the chart is simply a grey grid, empty.
Code:
ggplot(short, aes(x=Quarter,y=Capex, fill=ProjectName, )) + geom_area(position = "stack") + ylim (1, 100000)
data:
ProjectName Quarter Capex
a F01 Jul 41709
a F02 Aug 41696
a F03 Sep 41667
a F04 Oct 41712
a F05 Nov 41676
a F06 Dec 41674
a F07 Jan 41694
a F08 Feb 41693
a F09 Mar 41698
a F10 Apr 41710
a F11 May 41694
a F12 Jun 41671
b F01 Jul 265197
b F02 Aug 265200
b F03 Sep 265187
b F04 Oct 265190
b F05 Nov 265179
b F06 Dec 265170
b F07 Jan 265167
b F08 Feb 265174
b F09 Mar 265187
b F10 Apr 265169
b F11 May 265186
b F12 Jun 265208
c F01 Jul 233335
c F02 Aug 233352
c F03 Sep 233344
c F04 Oct 233344
c F05 Nov 233344
c F06 Dec 233350
c F07 Jan 32
c F08 Feb 31
c F09 Mar 23
c F10 Apr 5046
c F11 May 5005
c F12 Jun 50
d F01 Jul 40
d F02 Aug 43
d F03 Sep 30
d F04 Oct 5038
d F05 Nov 45
d F06 Dec 8
d F07 Jan 45
d F08 Feb 20034
d F09 Mar 40
d F10 Apr 40
d F11 May 2
d F12 Jun 500045
e F01 Jul 300011
I'm pretty sure you want a stacked bar chart, not an area chart? Is this what you're after?
ggplot(short, aes(x=Quarter,y=Capex, fill=ProjectName, )) +
geom_bar(stat = "identity")
I'm not sure why you've got those y axis limits, they cut off your data, but this should be done with scale_y_continuous(limits = c(min, max)).
As a note, it's better to use the output from dput(data) when sharing your data, as it brings the structure of the data along with it. Have a look at How to make a great R reproducible example?