revise package structure: always predict from 1am to horizon, dataset not cut at...
[talweg.git] / pkg / tests / testthat / test-Forecaster.R
CommitLineData
6d97bfec
BA
1context("Check that forecasters behave as expected")
2
44a9990b
BA
3ts_data = system.file("testdata","ts_test.csv",package="talweg")
4exo_data = system.file("testdata","exo_test.csv",package="talweg")
d2ab47a7
BA
5data_p <<- getData(ts_data, exo_data, date_format="%Y-%m-%d %H:%M", limit=Inf)
6#Forecasts from monday to sunday (series 1 to 7)
7indices <<- seq(as.Date("2007-04-02"),as.Date("2007-04-08"),"days")
defcae03 8pred_order = c(7,1:6) #will facilitate tests
44a9990b
BA
9
10test_that("Average method behave as expected",
6d97bfec 11{
d2ab47a7
BA
12 pred00_z = computeForecast(data_p, indices, "Average", "Zero", 1, Inf, 24, ncores=1)
13 pred00_p = computeForecast(data_p, indices, "Average", "Persistence", 1, Inf, 24)
99f83c9a 14 for (i in 1:7)
44a9990b
BA
15 {
16 #zero jump: should predict true values minus 1
c36568fa 17 expect_equal( pred00_z$getForecast(i), rep(pred_order[i],24) )
44a9990b 18 #persistence jump == 1: should predict true values
c36568fa 19 expect_equal( pred00_p$getForecast(i), rep(i,24) )
44a9990b
BA
20 }
21
d2ab47a7 22 #NOTE: 24h-block become
44a9990b 23 #1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 (14h-->0h then 1h-->13h)
99f83c9a 24 #No jump between days, thus zero and persistence are equivalent (and correct)
d2ab47a7
BA
25 pred13_z = computeForecast(data_p, indices, "Average", "Zero", 14, Inf, 24)
26 pred13_p = computeForecast(data_p, indices, "Average", "Persistence", 14, Inf, 24)
99f83c9a 27 for (i in 1:7)
44a9990b 28 {
d2ab47a7
BA
29 expect_equal( pred13_z$getForecast(i), rep(i,24) )
30 expect_equal( pred13_p$getForecast(i), rep(i,24) )
44a9990b 31 }
613a986f 32
44a9990b 33 #A few extra checks
d2ab47a7
BA
34 expect_equal( pred00_p$getIndexInData(2), dateIndexToInteger("2007-04-03",data_p) )
35 expect_equal( pred00_z$getIndexInData(2), dateIndexToInteger("2007-04-03",data_p) )
36 expect_equal( pred13_p$getIndexInData(5), dateIndexToInteger("2007-04-06",data_p) )
37 expect_equal( pred13_z$getIndexInData(5), dateIndexToInteger("2007-04-06",data_p) )
44a9990b
BA
38})
39
40test_that("Persistence method behave as expected",
41{
99f83c9a 42 #Situation A: +Zero; (generally) correct if jump, wrong otherwise
d2ab47a7 43 pred00_sd = computeForecast(data_p, indices, "Persistence", "Zero", 1, Inf, 24,
c36568fa 44 ncores=1, same_day=TRUE)
d2ab47a7 45 pred00_dd = computeForecast(data_p, indices, "Persistence", "Zero", 1, Inf, 24,
c36568fa 46 ncores=1, same_day=FALSE)
99f83c9a 47 for (i in 1:7)
44a9990b 48 {
c36568fa
BA
49 expect_equal(pred00_sd$getForecast(i), rep(pred_order[i],24))
50 expect_equal(pred00_dd$getForecast(i), rep(pred_order[i],24))
44a9990b 51 }
613a986f 52
d2ab47a7 53 pred13_sd = computeForecast(data_p, indices, "Persistence", "Zero", 14, Inf, 24,
c36568fa 54 ncores=1, same_day=TRUE)
d2ab47a7 55 pred13_dd = computeForecast(data_p, indices, "Persistence", "Zero", 14, Inf, 24,
c36568fa 56 ncores=1, same_day=FALSE)
d2ab47a7 57 for (i in 1:7)
44a9990b 58 {
d2ab47a7
BA
59 expect_equal(pred13_sd$getForecast(i), rep(i,24) )
60 expect_equal(pred13_dd$getForecast(i), rep(i,24) )
44a9990b 61 }
613a986f 62
99f83c9a 63 #Situation B: +Persistence, (generally) correct
d2ab47a7 64 pred00_sd = computeForecast(data_p, indices, "Persistence", "Persistence", 1, Inf, 24,
c36568fa 65 ncores=1, same_day=TRUE)
d2ab47a7 66 pred00_dd = computeForecast(data_p, indices, "Persistence", "Persistence", 1, Inf, 24,
c36568fa 67 ncores=1, same_day=FALSE)
99f83c9a 68 for (i in 3:7)
613a986f 69 {
c36568fa
BA
70 expect_equal(pred00_sd$getForecast(i), rep(i,24))
71 expect_equal(pred00_dd$getForecast(i), rep(i,24))
613a986f 72 }
99f83c9a 73 #boundaries are special cases: OK if same day, quite wrong otherwise
c36568fa
BA
74 expect_equal(pred00_sd$getForecast(1), rep(1,24) )
75 expect_equal(pred00_dd$getForecast(1), rep(8,24) )
76 expect_equal(pred00_sd$getForecast(2), rep(2,24) )
77 expect_equal(pred00_dd$getForecast(2), rep(-5,24) )
613a986f 78
d2ab47a7 79 pred13_sd = computeForecast(data_p, indices, "Persistence", "Persistence", 14, Inf, 24,
c36568fa 80 ncores=1, same_day=TRUE)
d2ab47a7 81 pred13_dd = computeForecast(data_p, indices, "Persistence", "Persistence", 14, Inf, 24,
c36568fa 82 ncores=1, same_day=FALSE)
d2ab47a7 83 for (i in 1:7)
44a9990b 84 {
d2ab47a7
BA
85 expect_equal(pred13_sd$getForecast(i), rep(i,24) )
86 expect_equal(pred13_dd$getForecast(i), rep(i,24) )
44a9990b 87 }
6d97bfec 88
44a9990b 89 #A few extra checks
d2ab47a7
BA
90 expect_equal( pred00_sd$getIndexInData(3), dateIndexToInteger("2007-04-04",data_p) )
91 expect_equal( pred00_dd$getIndexInData(6), dateIndexToInteger("2007-04-07",data_p) )
92 expect_equal( pred13_sd$getIndexInData(3), dateIndexToInteger("2007-04-04",data_p) )
93 expect_equal( pred13_dd$getIndexInData(6), dateIndexToInteger("2007-04-07",data_p) )
44a9990b 94})
6d97bfec 95
99f83c9a 96test_that("Neighbors method behave as expected",
6d97bfec 97{
99f83c9a 98 #Situation A: +Zero; correct if jump, wrong otherwise
d2ab47a7 99 pred00 = computeForecast(data_p, indices, "Neighbors", "Zero", 1, Inf, 24,
c36568fa 100 simtype="mix", local=FALSE)
99f83c9a 101 for (i in 1:7)
c36568fa 102 expect_equal(pred00$getForecast(i), rep(pred_order[i],24))
6d97bfec 103
d2ab47a7 104 pred13 = computeForecast(data_p, indices, "Persistence", "Zero", 14, Inf, 24,
c36568fa 105 simtype="mix", local=FALSE)
99f83c9a 106 for (i in 1:7)
d2ab47a7 107 expect_equal(pred13$getForecast(i), rep(i,24) )
99f83c9a 108
c36568fa 109 #Situation B: +Neighbors == too difficult to eval in a unit test
d2ab47a7 110# pred00 = computeForecast(data_p, indices, "Neighbors", "Neighbors", 1, Inf, 24,
c36568fa
BA
111# simtype="endo", local=FALSE)
112# jumps = ...
113# for (i in 1:7)
114# expect_equal(pred00$getForecast(i), rep(pred_order[i]+jumps[i],24))
d2ab47a7 115# pred13 = computeForecast(data_p, indices, "Neighbors", "Neighbors", 14, Inf, 24,
c36568fa 116# simtype="endo", local=FALSE)
99f83c9a 117# for (i in 1:7)
c36568fa 118# expect_equal(pred13$getForecast(i), c( rep(i,11), rep(i%%7+1,13) ) )
99f83c9a
BA
119
120 #A few extra checks
d2ab47a7
BA
121 expect_equal( pred00$getIndexInData(1), dateIndexToInteger("2007-04-02",data_p) )
122 expect_equal( pred00$getIndexInData(4), dateIndexToInteger("2007-04-05",data_p) )
123 expect_equal( pred13$getIndexInData(1), dateIndexToInteger("2007-04-02",data_p) )
124 expect_equal( pred13$getIndexInData(4), dateIndexToInteger("2007-04-05",data_p) )
44a9990b 125})