fix tests
[talweg.git] / pkg / tests / testthat / test-Forecaster.R
1 context("Check that forecasters behave as expected")
2
3 ts_data = system.file("testdata","ts_test.csv",package="talweg")
4 exo_data = system.file("testdata","exo_test.csv",package="talweg")
5 data00 <<- getData(ts_data, exo_data, input_tz="GMT", date_format="%Y-%m-%d %H:%M",
6 working_tz="GMT", predict_at=0, limit=Inf)
7 data13 <<- getData(ts_data, exo_data, input_tz="GMT", date_format="%Y-%m-%d %H:%M",
8 working_tz="GMT", predict_at=13, limit=Inf)
9 #Forecast at sunday to saturday (series 7 to 1), for monday to sunday (series 1 to 7)
10 indices <<- seq(as.Date("2007-04-01"),as.Date("2007-04-07"),"days")
11 pred_order = c(7,1:6) #will facilitate tests
12
13 test_that("Average method behave as expected",
14 {
15 pred00_z = computeForecast(data00, indices, "Average", "Zero", Inf, 24)
16 pred00_p = computeForecast(data00, indices, "Average", "Persistence", Inf, 24)
17 for (i in 1:7)
18 {
19 #zero jump: should predict true values minus 1
20 expect_equal( pred00_z$getForecast(i), rep(pred_order[i],24) )
21 #persistence jump == 1: should predict true values
22 expect_equal( pred00_p$getForecast(i), rep(i,24) )
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)
27 #No jump between days, thus zero and persistence are equivalent (and correct)
28 pred13_z = computeForecast(data13, indices, "Average", "Zero", Inf, 24)
29 pred13_p = computeForecast(data13, indices, "Average", "Persistence", Inf, 24)
30 for (i in 1:7)
31 {
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) ) )
34 }
35
36 #A few extra checks
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) )
41 })
42
43 test_that("Persistence method behave as expected",
44 {
45 #Situation A: +Zero; (generally) correct if jump, wrong otherwise
46 pred00_sd = computeForecast(data00, indices, "Persistence", "Zero", Inf, 24,
47 ncores=1, same_day=TRUE)
48 pred00_dd = computeForecast(data00, indices, "Persistence", "Zero", Inf, 24,
49 ncores=1, same_day=FALSE)
50 for (i in 1:7)
51 {
52 expect_equal(pred00_sd$getForecast(i), rep(pred_order[i],24))
53 expect_equal(pred00_dd$getForecast(i), rep(pred_order[i],24))
54 }
55
56 pred13_sd = computeForecast(data13, indices, "Persistence", "Zero", Inf, 24,
57 ncores=1, same_day=TRUE)
58 pred13_dd = computeForecast(data13, indices, "Persistence", "Zero", Inf, 24,
59 ncores=1, same_day=FALSE)
60 for (i in 2:6)
61 {
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) ) )
64 }
65 #boundaries are special cases: OK if same day, quite wrong otherwise
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) ) )
70
71 #Situation B: +Persistence, (generally) correct
72 pred00_sd = computeForecast(data00, indices, "Persistence", "Persistence", Inf, 24,
73 ncores=1, same_day=TRUE)
74 pred00_dd = computeForecast(data00, indices, "Persistence", "Persistence", Inf, 24,
75 ncores=1, same_day=FALSE)
76 for (i in 3:7)
77 {
78 expect_equal(pred00_sd$getForecast(i), rep(i,24))
79 expect_equal(pred00_dd$getForecast(i), rep(i,24))
80 }
81 #boundaries are special cases: OK if same day, quite wrong otherwise
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) )
86
87 pred13_sd = computeForecast(data13, indices, "Persistence", "Persistence", Inf, 24,
88 ncores=1, same_day=TRUE)
89 pred13_dd = computeForecast(data13, indices, "Persistence", "Persistence", Inf, 24,
90 ncores=1, same_day=FALSE)
91 for (i in 2:6)
92 {
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) ) )
95 }
96 #boundaries are special cases: OK if same day, quite wrong otherwise
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) ) )
101
102 #A few extra checks
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) )
107 })
108
109 test_that("Neighbors method behave as expected",
110 {
111 #Situation A: +Zero; correct if jump, wrong otherwise
112 pred00 = computeForecast(data00, indices, "Neighbors", "Zero", Inf, 24,
113 simtype="mix", local=FALSE)
114 for (i in 1:7)
115 expect_equal(pred00$getForecast(i), rep(pred_order[i],24))
116
117 pred13 = computeForecast(data13, indices, "Persistence", "Zero", Inf, 24,
118 simtype="mix", local=FALSE)
119 for (i in 1:7)
120 expect_equal(pred13$getForecast(i), c( rep(i,11), rep(i%%7+1,13) ) )
121
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))
128 # pred13 = computeForecast(data13, indices, "Neighbors", "Neighbors", Inf, 24,
129 # simtype="endo", local=FALSE)
130 # for (i in 1:7)
131 # expect_equal(pred13$getForecast(i), c( rep(i,11), rep(i%%7+1,13) ) )
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) )
138 })