I have a daily seasonal time series of 1461 observations, which I seasonally differenced and I am running multiple different models. My trained data is of 1023 obs and the test data is of 438 obs.
Here's train and test data (without any seasonal differencing).
clean_train
Time Series:
Start = c(2015, 4, 01)
Frequency = 365
[1] 49.00000 58.00000 36.00000 44.00000 64.00000 46.00000 46.00000 39.00000 58.00000 61.00000 51.00000
[12] 52.00000 48.00000 24.00000 43.00000 56.00000 44.00000 47.00000 44.00000 48.00000 31.00000 32.00000
[23] 45.00000 43.00000 40.00000 45.00000 50.00000 38.00000 29.00000 48.00000 51.00000 44.00000 40.00000
[34] 33.00000 34.00000 33.00000 49.00000 46.00000 49.00000 44.00000 41.00000 22.00000 40.00000 43.00000
[45] 46.00000 45.00000 49.00000 47.00000 28.00000 47.00000 44.00000 51.00000 48.00000 37.00000 49.00000
[56] 50.00000 40.00000 44.00000 48.00000 37.00000 47.00000 48.00000 35.00000 41.00000 46.00000 43.00000
[67] 66.00000 51.00000 46.00000 26.00000 32.00000 46.00000 40.00000 30.00000 49.00000 42.00000 27.00000
[78] 38.00000 43.00000 36.00000 44.00000 44.00000 50.00000 37.00000 30.00000 46.00000 44.00000 41.00000
[89] 52.00000 49.00000 23.00000 30.00000 44.00000 30.00000 35.00000 46.00000 39.00000 37.00000 35.00000
[100] 40.00000 31.00000 35.00000 40.00000 34.00000 27.00000 31.00000 43.00000 39.00000 38.00000 39.00000
[111] 36.00000 31.00000 32.00000 45.00000 40.00000 44.00000 35.00000 45.00000 37.00000 25.00000 50.00000
[122] 34.00000 27.00000 47.00000 36.00000 22.00000 20.00000 50.00000 49.00000 46.00000 26.00000 38.00000
[133] 24.00000 40.00000 39.00000 49.00000 42.00000 36.00000 35.00000 24.00000 27.00000 42.00000 33.00000
[144] 34.00000 32.00000 52.00000 25.00000 37.00000 29.00000 34.00000 25.00000 36.00000 26.00000 30.00000
[155] 35.00000 35.00000 38.00000 34.00000 33.00000 39.00000 25.00000 33.00000 46.00000 40.00000 32.00000
[166] 33.00000 34.00000 26.00000 27.00000 39.00000 40.00000 28.00000 47.00000 43.00000 34.00000 36.00000
[177] 44.00000 46.00000 47.00000 45.00000 51.00000 31.00000 50.00000 41.00000 45.00000 43.00000 42.00000
[188] 42.00000 41.00000 36.00000 44.00000 41.00000 43.00000 40.00000 42.00000 45.00000 45.00000 53.00000
[199] 33.00000 43.00000 53.00000 45.00000 41.00000 32.00000 44.00000 46.00000 46.00000 44.00000 39.00000
[210] 32.00000 33.00000 48.00000 43.00000 51.00000 47.00000 41.00000 42.00000 35.00000 52.00000 43.00000
[221] 34.00000 36.00000 49.00000 27.00000 30.00000 49.00000 35.00000 51.00000 45.00000 50.00000 30.00000
[232] 28.00000 46.00000 30.00000 44.00000 35.00000 47.00000 34.00000 34.00000 58.00000 38.00000 41.00000
[243] 48.00000 44.00000 30.00000 32.00000 47.00000 37.00000 64.00000 41.00000 52.00000 33.00000 47.00000
[254] 58.00000 58.00000 54.00000 61.00000 73.00000 45.00000 46.00000 55.00000 47.00000 53.00000 51.00000
[265] 45.00000 52.00000 48.00000 59.00000 42.00000 59.00000 81.00000 79.00000 57.00000 60.00000 89.00000
[276] 54.00000 66.00000 69.00000 61.00000 57.00000 44.00000 55.00000 58.00000 68.00000 53.00000 69.00000
[287] 40.00000 41.00000 72.00000 55.00000 49.00000 44.00000 51.00000 37.00000 43.00000 85.00000 44.00000
[298] 51.00000 46.00000 45.00000 46.00000 40.00000 56.00000 58.00000 53.00000 55.00000 56.00000 37.00000
[309] 30.00000 66.00000 49.00000 47.00000 47.00000 54.00000 41.00000 44.00000 62.00000 51.00000 55.00000
[320] 55.00000 60.00000 45.00000 57.00000 68.00000 48.00000 44.00000 49.00000 61.00000 47.00000 48.00000
[331] 54.00000 47.00000 45.00000 71.00000 61.00000 38.00000 57.00000 62.00000 59.00000 82.00000 66.00000
[342] 62.00000 47.00000 52.00000 50.00000 45.00000 68.00000 55.00000 79.00000 50.00000 54.00000 54.00000
[353] 59.00000 50.00000 54.00000 74.00000 44.00000 58.00000 56.00000 61.00000 70.00000 77.00000 62.00000
[364] 62.00000 50.00000 64.00000 58.00000 71.00000 53.00000 64.00000 49.00000 47.00000 69.00000 54.00000
[375] 58.00000 64.00000 71.00000 43.00000 56.00000 66.00000 48.00000 56.00000 55.00000 43.00000 29.00000
[386] 44.00000 46.00000 38.00000 45.00000 37.00000 41.00000 30.00000 43.00000 53.00000 46.00000 45.00000
[397] 37.00000 43.00000 29.00000 34.00000 47.00000 39.00000 47.00000 52.00000 48.00000 46.00000 32.00000
[408] 41.00000 40.00000 45.00000 28.00000 53.00000 23.00000 45.00000 46.00000 37.00000 47.00000 45.00000
[419] 41.00000 34.00000 42.00000 37.00000 58.00000 47.00000 41.00000 38.00000 43.00000 28.00000 50.00000
[430] 42.00000 49.00000 43.00000 39.00000 28.00000 30.00000 41.00000 46.00000 45.00000 32.00000 35.00000
[441] 28.00000 35.00000 49.00000 35.00000 38.00000 45.00000 34.00000 36.00000 25.00000 45.00000 41.00000
[452] 38.00000 40.00000 48.00000 33.00000 36.00000 37.00000 47.00000 34.00000 43.00000 40.00000 22.00000
[463] 31.00000 48.00000 40.00000 33.00000 30.00000 37.00000 36.00000 31.00000 44.00000 39.00000 39.00000
[474] 50.00000 34.00000 25.00000 39.00000 48.00000 47.00000 34.00000 34.00000 29.00000 42.00000 30.00000
[485] 45.00000 32.00000 34.00000 31.00000 35.00000 34.73238 30.00000 45.00000 38.00000 39.00000 33.00000
[496] 50.00000 27.00000 24.00000 46.00000 27.00000 36.00000 52.00000 42.00000 23.00000 27.00000 41.00000
[507] 33.00000 32.00000 46.00000 45.00000 26.00000 23.00000 32.00000 43.00000 39.00000 29.00000 39.00000
[518] 30.00000 36.00000 40.00000 30.00000 24.00000 49.00000 50.00000 24.00000 29.00000 32.00000 25.00000
[529] 33.00000 36.00000 44.00000 28.00000 26.00000 30.00000 35.00000 40.00000 29.00000 39.00000 35.00000
[540] 43.00000 51.00000 33.00000 33.00000 39.00000 47.00000 32.00000 32.00000 44.00000 46.00000 46.00000
[551] 41.00000 50.00000 29.00000 33.00000 41.00000 38.00000 39.00000 40.00000 43.00000 32.00000 31.00000
[562] 49.00000 64.00000 36.00000 38.00000 50.00000 32.00000 35.00000 48.00000 37.00000 43.00000 31.00000
[573] 51.00000 35.00000 34.00000 47.00000 35.00000 40.00000 42.00000 35.00000 37.00000 42.00000 41.00000
[584] 41.00000 31.00000 43.00000 36.00000 27.00000 35.00000 41.00000 51.00000 51.00000 32.00000 39.00000
[595] 35.00000 39.00000 35.00000 32.00000 45.00000 43.00000 42.00000 28.00000 39.00000 46.00000 44.00000
[606] 39.00000 50.00000 53.00000 50.00000 37.00000 68.00000 37.00000 49.00000 49.00000 36.00000 48.00000
[617] 45.00000 63.00000 52.00000 51.00000 66.00000 64.00000 34.00000 44.00000 66.00000 40.00000 49.00000
[628] 57.00000 55.00000 40.00000 46.00000 56.00000 54.00000 39.00000 59.00000 82.00000 59.00000 45.00000
[639] 83.00000 61.00000 43.00000 64.00000 82.00000 59.00000 60.00000 77.00000 57.00000 53.00000 60.00000
[650] 61.00000 29.00000 39.00000 59.00000 46.00000 42.00000 45.00000 39.00000 37.00000 41.00000 48.00000
[661] 55.00000 48.00000 46.00000 52.00000 41.00000 39.00000 48.00000 47.00000 58.00000 55.00000 59.00000
[672] 37.00000 42.00000 62.00000 28.00000 58.00000 35.00000 60.00000 52.00000 42.00000 63.00000 50.00000
[683] 45.00000 52.00000 56.00000 42.00000 39.00000 67.00000 52.00000 35.00000 50.00000 51.00000 39.00000
[694] 41.00000 55.00000 44.00000 47.00000 37.00000 51.00000 45.00000 42.00000 60.00000 63.00000 42.00000
[705] 52.00000 48.00000 39.00000 33.00000 57.00000 43.00000 43.00000 47.00000 49.00000 37.00000 48.00000
[716] 55.00000 58.00000 44.00000 76.00000 53.00000 52.00000 63.00000 66.00000 70.00000 54.00000 47.00000
[727] 70.00000 56.00000 60.00000 67.00000 48.00000 55.00000 47.00000 67.00000 44.00000 49.00000 58.00000
[738] 45.00000 54.00000 61.00000 53.00000 37.00000 44.00000 56.00000 42.00000 60.00000 44.00000 34.00000
[749] 39.00000 44.00000 50.00000 55.00000 42.00000 55.00000 56.00000 39.00000 36.00000 49.00000 44.00000
[760] 44.00000 57.00000 39.00000 34.00000 37.00000 55.00000 40.00000 34.00000 51.00000 37.00000 31.00000
[771] 42.00000 39.00000 57.00000 32.00000 49.00000 43.00000 40.00000 47.00000 54.00000 45.00000 41.00000
[782] 48.00000 55.00000 41.00000 46.00000 35.00000 56.00000 55.00000 56.00000 45.00000 36.00000 31.00000
[793] 41.00000 38.00000 39.00000 51.00000 49.00000 38.00000 41.00000 44.00000 43.00000 39.00000 41.00000
[804] 41.00000 23.00000 37.00000 52.00000 31.00000 43.00000 44.00000 34.00000 35.00000 30.00000 50.00000
[815] 49.00000 43.00000 50.00000 43.00000 40.00000 27.00000 49.00000 46.00000 44.00000 36.00000 39.00000
[826] 30.00000 45.00000 55.00000 39.00000 45.00000 44.00000 34.00000 40.00000 30.00000 42.00000 43.00000
[837] 43.00000 39.00000 46.00000 22.00000 39.00000 45.00000 49.00000 26.00000 34.00000 47.00000 32.00000
[848] 25.00000 50.00000 44.00000 38.00000 42.00000 34.00000 33.00000 33.00000 31.00000 38.00000 33.00000
[859] 35.00000 38.00000 33.00000 30.00000 46.00000 41.00000 43.00000 30.00000 42.00000 33.00000 33.00000
[870] 44.00000 37.00000 41.00000 46.00000 45.00000 35.00000 42.00000 45.00000 35.00000 55.00000 37.00000
[881] 46.00000 34.00000 26.00000 48.00000 45.00000 42.00000 37.00000 40.00000 27.00000 32.00000 35.00000
[892] 40.00000 36.00000 36.00000 43.00000 38.00000 23.00000 40.00000 34.00000 43.00000 49.00000 47.00000
[903] 31.00000 43.00000 44.00000 44.00000 43.00000 39.00000 35.00000 33.00000 39.00000 47.00000 50.00000
[914] 29.00000 40.00000 37.00000 30.00000 26.00000 41.00000 40.00000 40.00000 38.00000 55.00000 33.00000
[925] 29.00000 58.00000 55.00000 52.00000 48.00000 58.00000 40.00000 33.00000 35.00000 48.00000 39.00000
[936] 43.00000 40.00000 29.00000 28.00000 36.00000 51.00000 38.00000 43.00000 56.00000 38.00000 36.00000
[947] 53.00000 39.00000 34.00000 35.00000 56.00000 45.00000 35.00000 51.00000 39.00000 46.00000 40.00000
[958] 49.00000 38.00000 30.00000 55.00000 60.00000 43.00000 52.00000 42.00000 37.00000 43.00000 39.00000
[969] 46.00000 38.00000 57.00000 50.00000 42.00000 37.00000 51.00000 37.00000 41.00000 54.00000 64.00000
[980] 39.00000 53.00000 36.00000 45.00000 61.00000 39.00000 66.00000 53.00000 56.00000 68.00000 49.00000
[991] 63.00000 64.00000 78.00000 59.00000 65.00000 79.00000 77.00000 62.00000 56.00000 77.00000 97 92 86 82 96 77 97 87 81 90 74 80 82 90 55 53 76 84 51 76 65 50 55
> clean_test
Time Series:
Start = c(2018, 01, 18)
End = c(2019, 4)
Frequency = 365
[1] 72.00000 64.00000 73.00000 65.00000 50.00000 52.00000 49.00000 70.00000 73.00000 43.00000 70.00000
[12] 69.00000 61.00000 55.00000 71.00000 62.00000 56.00000 66.00000 70.00000 48.00000 56.00000 77.00000
[23] 57.00000 72.00000 64.00000 69.00000 74.00000 63.00000 82.00000 76.00000 65.00000 68.00000 49.00000
[34] 72.00000 58.00000 73.00000 60.00000 57.00000 50.00000 58.00000 44.00000 55.00000 68.00000 60.00000
[45] 56.00000 44.00000 58.00000 50.00000 47.00000 62.00000 55.00000 49.00000 59.00000 55.00000 47.00000
[56] 33.00000 63.00000 47.00000 53.00000 45.00000 59.00000 41.00000 53.00000 65.00000 62.00000 63.00000
[67] 43.00000 66.00000 49.00000 57.00000 62.00000 47.00000 56.00000 64.00000 43.00000 52.00000 53.00000
[78] 58.00000 65.00000 47.00000 54.00000 58.00000 43.00000 43.00000 61.00000 51.00000 42.00000 42.00000
[89] 68.00000 28.00000 32.00000 51.00000 36.00000 50.00000 55.00000 48.00000 41.00000 38.00000 54.00000
[100] 42.00000 49.00000 54.00000 52.00000 40.00000 46.00000 47.00000 51.00000 54.00000 50.00000 46.00000
[111] 39.00000 42.00000 37.00000 46.00000 43.00000 42.00000 53.00000 33.00000 31.00000 45.00000 51.00000
[122] 45.00000 56.00000 46.00000 45.00000 34.00000 46.00000 35.00000 46.00000 57.00000 55.00000 42.00000
[133] 33.00000 44.00000 53.00000 47.00000 52.00000 51.00000 29.00000 30.00000 43.00000 36.00000 36.00000
[144] 50.00000 35.00000 35.00000 37.00000 56.00000 47.00000 48.00000 41.00000 48.00000 31.00000 26.00000
[155] 47.00000 49.00000 49.00000 33.00000 46.00000 30.00000 32.00000 51.00000 52.00000 40.00000 49.00000
[166] 51.00000 35.00000 42.00000 41.00000 33.00000 43.00000 46.00000 47.00000 30.00000 34.00000 38.00000
[177] 36.00000 35.00000 38.00000 43.00000 30.00000 31.00000 43.00000 43.00000 42.00000 38.00000 39.00000
[188] 20.00000 27.00000 53.00000 36.00000 43.00000 45.00000 30.00000 32.00000 32.00000 44.00000 40.00000
[199] 36.00000 33.00000 34.00000 24.00000 37.00000 42.00000 45.00000 33.00000 41.00000 44.00000 36.00000
[210] 39.00000 42.00000 41.00000 43.00000 39.00000 41.00000 28.00000 34.00000 39.00000 36.00000 42.00000
[221] 38.00000 33.00000 21.00000 25.00000 33.00000 43.00000 46.00000 31.00000 42.00000 31.00000 45.00000
[232] 40.00000 43.00000 39.00000 40.00000 50.00000 35.00000 37.00000 49.00000 52.00000 44.00000 39.00000
[243] 43.00000 39.00000 38.00000 49.00000 50.00000 50.00000 47.00000 43.00000 44.00000 36.00000 64.00000
[254] 42.00000 46.00000 54.00000 70.00000 43.00000 50.00000 47.00000 64.00000 43.00000 42.00000 48.00000
[265] 23.00000 46.00000 54.00000 43.00000 43.00000 53.00000 53.00000 42.00000 41.00000 55.00000 35.00000
[276] 46.00000 53.00000 51.00000 40.00000 45.00000 37.00000 41.00000 37.00000 40.00000 59.00000 43.00000
[287] 35.00000 55.00000 52.00000 57.00000 64.00000 56.00000 46.00000 42.00000 55.00000 65.00000 51.00000
[298] 62.00000 55.00000 36.00000 41.00000 59.00000 45.00000 39.00000 53.00000 49.00000 44.00000 43.00000
[309] 43.00000 45.00000 51.00000 60.00000 55.00000 50.00000 39.00000 60.00000 52.00000 47.00000 50.00000
[320] 44.00000 50.00000 48.00000 54.00000 58.00000 49.00000 63.00000 48.00000 47.00000 46.00000 65.00000
[331] 72.00000 67.00000 55.00000 59.00000 46.00000 51.00000 61.00000 60.00000 67.00000 52.00000 48.00000
[342] 70.00000 79.00000 82.00000 95.00000 78.00000 69.00000 63.00000 78.00000 66.00000 79.00000 76.00000
[353] 55.00000 89.00000 84.00000 64.00000 43.00000 76.00000 68.00000 55.00000 64.00000 64.00000 51.00000
[364] 57.00000 60.00000 61.00000 68.00000 54.00000 90.00000 40.00000 53.00000 76.00000 68.00000 54.00000
[375] 78.00000 82.00000 55.00000 61.00000 65.00000 65.00000 78.00000 59.00000 78.00000 61.00000 54.00000
[386] 64.00000 77.00000 79.00000 48.00000 74.00000 54.00000 65.00000 62.00000 64.00000 57.00000 61.00000
[397] 52.00000 50.00000 69.00000 83.00000 83.00000 68.00000 60.00000 73.00000 53.00000 63.00000 58.00000
[408] 92.00000 57.00000 56.00000 62.00000 60.00000 74.00000 69.00000 66.00000 66.00000 60.00000 63.00000
[419] 56.00000 49.00000 72.00000 73.00000 64.00000 61.00000 54.00000 43.00000 51.00000 56.00000 52.00000
[430] 67.00000 64.00000 41.00000 42.00000 45.00000 38.00000 39.00000 47.00000 50.77037
Amongst them I am trying to run the meanf()
model with the differenced data, then undifferencing it to plot and to check accuracy and such.
This is my initial code:
train_seasdiff <- diff(clean_train, lag=7)
test_seasdiff <- diff(clean_test, lag = 7)
mean_diff <- meanf(train_seasdiff, h=14, level= 95)
Now, I am in doubt regarding the composition of the diffinv()
I should use, so I have two options:
# undoes the seasonal diff on the forecast
# Option 1
inv_f <- diffinv(mean_diff$mean, lag=7, xi=clean_train[1017:1023])
# Option 2
inv_f <- diffinv(mean_diff$mean, lag=1, xi=clean_train[1023])
The plots that come out of these two commands are the following:
And the code is:
plot(clean_train,
type="l",
xaxt="n",
main = 'Mean Forecast Level',
xlab = 'Date',
ylab = 'Number of patients',
xlim = c(2017.78,2017.85),
ylim =c(20, 100))
lines(clean_test, col = 'red', lty = 2)
lines(inv_f, col="blue")
Which option should I use? I am a bit confused with the model, the lags, the seasonality. I am aware the meanf
model just gets the mean of the series as forecast, so this would be a lineal forecast in the case of a stationary series, but since I had to un-difference my data to see the actual forecast, I am not sure which shape it should take.