Commit | Line | Data |
---|---|---|
6d97bfec BA |
1 | context("Check that forecasters behave as expected") |
2 | ||
44a9990b BA |
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) | |
defcae03 | 9 | #Forecast at sunday to saturday (series 7 to 1), for monday to sunday (series 1 to 7) |
44a9990b | 10 | indices <<- seq(as.Date("2007-04-01"),as.Date("2007-04-07"),"days") |
defcae03 | 11 | pred_order = c(7,1:6) #will facilitate tests |
44a9990b BA |
12 | |
13 | test_that("Average method behave as expected", | |
6d97bfec | 14 | { |
af3b84f4 | 15 | browser() |
99f83c9a BA |
16 | pred00_z = computeForecast(data00, indices, "Average", "Zero", Inf, 24) |
17 | pred00_p = computeForecast(data00, indices, "Average", "Persistence", Inf, 24) | |
18 | for (i in 1:7) | |
44a9990b BA |
19 | { |
20 | #zero jump: should predict true values minus 1 | |
99f83c9a | 21 | expect_equal( pred00_z$getSerie(i), rep(pred_order[i],24) ) |
44a9990b | 22 | #persistence jump == 1: should predict true values |
99f83c9a | 23 | expect_equal( pred00_p$getSerie(i), rep(i,24) ) |
44a9990b BA |
24 | } |
25 | ||
26 | #NOTE: days become | |
27 | #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 BA |
28 | #No jump between days, thus zero and persistence are equivalent (and correct) |
29 | pred13_z = computeForecast(data13, indices, "Average", "Zero", Inf, 24) | |
30 | pred13_p = computeForecast(data13, indices, "Average", "Persistence", Inf, 24) | |
31 | for (i in 1:7) | |
44a9990b | 32 | { |
99f83c9a BA |
33 | expect_equal( pred13_z$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) ) |
34 | expect_equal( pred13_p$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) ) | |
44a9990b | 35 | } |
613a986f | 36 | |
44a9990b | 37 | #A few extra checks |
99f83c9a BA |
38 | expect_equal( pred00_p$getIndexInData(2), dateIndexToInteger("2007-04-02",data00) ) |
39 | expect_equal( pred00_z$getIndexInData(2), dateIndexToInteger("2007-04-02",data00) ) | |
40 | expect_equal( pred13_p$getIndexInData(5), dateIndexToInteger("2007-04-05",data13) ) | |
41 | expect_equal( pred13_z$getIndexInData(5), dateIndexToInteger("2007-04-05",data13) ) | |
44a9990b BA |
42 | }) |
43 | ||
44 | test_that("Persistence method behave as expected", | |
45 | { | |
99f83c9a BA |
46 | #Situation A: +Zero; (generally) correct if jump, wrong otherwise |
47 | pred00_sd = computeForecast(data00, indices, "Persistence", "Zero", Inf, 24, same_day=TRUE) | |
48 | pred00_dd = computeForecast(data00, indices, "Persistence", "Zero", Inf, 24, same_day=FALSE) | |
49 | for (i in 1:7) | |
44a9990b | 50 | { |
99f83c9a BA |
51 | expect_equal(pred00_sd$getSerie(i), rep(pred_order[i],24)) |
52 | expect_equal(pred00_dd$getSerie(i), rep(pred_order[i],24)) | |
44a9990b | 53 | } |
613a986f | 54 | |
99f83c9a BA |
55 | pred13_sd = computeForecast(data13, indices, "Persistence", "Zero", Inf, 24, same_day=TRUE) |
56 | pred13_dd = computeForecast(data13, indices, "Persistence", "Zero", Inf, 24, same_day=FALSE) | |
57 | for (i in 2:6) | |
44a9990b | 58 | { |
99f83c9a BA |
59 | expect_equal(pred13_sd$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) ) |
60 | expect_equal(pred13_dd$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) ) | |
44a9990b | 61 | } |
99f83c9a BA |
62 | #boundaries are special cases: OK if same day, quite wrong otherwise |
63 | expect_equal(pred13_sd$getSerie(1), c( rep(1,11), rep(2,13) ) ) | |
64 | expect_equal(pred13_dd$getSerie(1), c( rep(1,11), rep(-5,13) ) ) | |
65 | expect_equal(pred13_sd$getSerie(7), c( rep(7,11), rep(1,13) ) ) | |
66 | expect_equal(pred13_dd$getSerie(7), c( rep(7,11), rep(8,13) ) ) | |
613a986f | 67 | |
99f83c9a | 68 | #Situation B: +Persistence, (generally) correct |
af3b84f4 BA |
69 | pred00_sd = computeForecast(data00, indices, "Persistence", "Persistence", Inf, 24, |
70 | same_day=TRUE) | |
71 | pred00_dd = computeForecast(data00, indices, "Persistence", "Persistence", Inf, 24, | |
72 | same_day=FALSE) | |
99f83c9a | 73 | for (i in 3:7) |
613a986f | 74 | { |
99f83c9a BA |
75 | expect_equal(pred00_sd$getSerie(i), rep(i,24)) |
76 | expect_equal(pred00_dd$getSerie(i), rep(i,24)) | |
613a986f | 77 | } |
99f83c9a BA |
78 | #boundaries are special cases: OK if same day, quite wrong otherwise |
79 | expect_equal(pred00_sd$getSerie(1), rep(1,24) ) | |
80 | expect_equal(pred00_dd$getSerie(1), rep(8,24) ) | |
81 | expect_equal(pred00_sd$getSerie(2), rep(2,24) ) | |
82 | expect_equal(pred00_dd$getSerie(2), rep(-5,24) ) | |
613a986f | 83 | |
af3b84f4 BA |
84 | pred13_sd = computeForecast(data13, indices, "Persistence", "Persistence", Inf, 24, |
85 | same_day=TRUE) | |
86 | pred13_dd = computeForecast(data13, indices, "Persistence", "Persistence", Inf, 24, | |
87 | same_day=FALSE) | |
99f83c9a | 88 | for (i in 2:6) |
44a9990b | 89 | { |
99f83c9a BA |
90 | expect_equal(pred13_sd$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) ) |
91 | expect_equal(pred13_dd$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) ) | |
44a9990b | 92 | } |
99f83c9a BA |
93 | #boundaries are special cases: OK if same day, quite wrong otherwise |
94 | expect_equal(pred13_sd$getSerie(1), c( rep(1,11), rep(2,13) ) ) | |
95 | expect_equal(pred13_dd$getSerie(1), c( rep(1,11), rep(-5,13) ) ) | |
96 | expect_equal(pred13_sd$getSerie(7), c( rep(7,11), rep(1,13) ) ) | |
97 | expect_equal(pred13_dd$getSerie(7), c( rep(7,11), rep(8,13) ) ) | |
6d97bfec | 98 | |
44a9990b | 99 | #A few extra checks |
99f83c9a BA |
100 | expect_equal( pred00_sd$getIndexInData(3), dateIndexToInteger("2007-04-03",data00) ) |
101 | expect_equal( pred00_dd$getIndexInData(6), dateIndexToInteger("2007-04-06",data00) ) | |
102 | expect_equal( pred13_sd$getIndexInData(3), dateIndexToInteger("2007-04-03",data13) ) | |
103 | expect_equal( pred13_dd$getIndexInData(6), dateIndexToInteger("2007-04-06",data13) ) | |
44a9990b | 104 | }) |
6d97bfec | 105 | |
99f83c9a | 106 | test_that("Neighbors method behave as expected", |
6d97bfec | 107 | { |
99f83c9a BA |
108 | #Situation A: +Zero; correct if jump, wrong otherwise |
109 | pred00 = computeForecast(data00, indices, "Neighbors", "Zero", Inf, 24, simtype="mix") | |
110 | for (i in 1:7) | |
111 | expect_equal(pred00$getSerie(i), rep(pred_order[i],24)) | |
6d97bfec | 112 | |
99f83c9a BA |
113 | pred13 = computeForecast(data13, indices, "Persistence", "Zero", Inf, 24, simtype="mix") |
114 | for (i in 1:7) | |
115 | expect_equal(pred13$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) ) | |
116 | ||
117 | #Situation B: +Neighbors, always predict bad (small, averaged) jump | |
118 | pred00 = computeForecast(data00, indices, "Neighbors", "Neighbors", Inf, 24, simtype="endo") | |
119 | #Concerning weights, there are 12+(1 if i>=2) gaps at -6 and 90-12+(i-2 if i>=3) gaps at 1 | |
120 | #Thus, predicted jump is respectively | |
121 | # (12*-6+78)/90 = 0.06666667 | |
122 | # (13*-6+78)/91 = 0 | |
123 | # (13*-6+79)/92 = 0.01086957 | |
124 | # (13*-6+80)/93 = 0.02150538 | |
125 | # (13*-6+81)/94 = 0.03191489 | |
126 | # (13*-6+82)/95 = 0.04210526 | |
127 | # (13*-6+83)/96 = 0.05208333 | |
128 | jumps = c(0.06666667, 0, 0.01086957, 0.02150538, 0.03191489, 0.04210526, 0.05208333) | |
129 | for (i in 1:7) | |
130 | expect_equal(pred00$getSerie(i), rep(pred_order[i]+jumps[i],24)) | |
131 | ||
132 | #Next lines commented out because too unpredictable results (tendency to flatten everything...) | |
133 | # pred13 = computeForecast(data13, indices, "Neighbors", "Neighbors", Inf, 24, simtype="endo") | |
134 | # for (i in 1:7) | |
135 | # expect_equal(pred13$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) ) | |
136 | ||
137 | #A few extra checks | |
138 | expect_equal( pred00$getIndexInData(1), dateIndexToInteger("2007-04-01",data00) ) | |
139 | expect_equal( pred00$getIndexInData(4), dateIndexToInteger("2007-04-04",data00) ) | |
140 | expect_equal( pred13$getIndexInData(1), dateIndexToInteger("2007-04-01",data13) ) | |
141 | expect_equal( pred13$getIndexInData(4), dateIndexToInteger("2007-04-04",data13) ) | |
44a9990b | 142 | }) |