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