tests presque OK
[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 = getForecast(data00, indices, "Average", "Zero", Inf, 24)
16 pred00_p = getForecast(data00, indices, "Average", "Persistence", Inf, 24)
17 for (i in seq_along(indices))
18 {
19 #zero jump: should predict true values minus 1
20 expect_equal(pred00_z$getSerie(i), rep(pred_order[i],24))
21 #persistence jump == 1: should predict true values
22 expect_equal(pred00_p$getSerie(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 wrong)
28 pred13_z = getForecast(data13, indices, "Average", "Zero", Inf, 24)
29 pred13_p = getForecast(data13, indices, "Average", "Persistence", Inf, 24)
30 prediction = c(rep(-13/24,11),rep(11/24,13))
31 for (i in seq_along(indices))
32 {
33 expect_equal(pred13_z$getSerie(i), prediction ) ##TODO: prendre en compte recollement
34 expect_equal(pred13_p$getSerie(i), prediction )
35 }
36
37 #A few extra checks
38 expect_identical( pred00_sd$getIndexInData(1), dateIndexToInteger("2007-04-01",data00) )
39 expect_identical( pred13_dd$getIndexInData(3), dateIndexToInteger("2007-04-03",data13) )
40 expect_identical( pred00_dd$getIndexInData(5), dateIndexToInteger("2007-04-05",data00) )
41 })
42
43 test_that("Persistence method behave as expected",
44 {
45 #Situation A: +Zero; always wrong
46 pred00_sd = getForecast(data00, indices, "Persistence", "Zero", Inf, 24, same_day=TRUE)
47 pred00_dd = getForecast(data00, indices, "Persistence", "Zero", Inf, 24, same_day=FALSE)
48 for (i in seq_along(indices))
49 {
50 expect_identical(pred00_sd$getSerie(i), rep(i,24))
51 expect_identical(pred00_dd$getSerie(i), rep(i,24))
52 }
53
54 pred13_sd = getForecast(data13, indices, "Persistence", "Zero", Inf, 24, same_day=TRUE)
55 pred13_dd = getForecast(data13, indices, "Persistence", "Zero", Inf, 24, same_day=FALSE)
56 for (i in seq_along(indices))
57 {
58 expect_identical(pred13_sd$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) )
59 expect_identical(pred13_dd$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) )
60 }
61
62 #Situation B: +Persistence, correct when predict_at==0
63 pred00_sd = getForecast(data00, indices, "Persistence", "Persistence", Inf, 24, same_day=TRUE)
64 pred00_dd = getForecast(data00, indices, "Persistence", "Persistence", Inf, 24, same_day=FALSE)
65 for (i in seq_along(indices))
66 {
67 expect_identical(pred00_sd$getSerie(i), rep(i%%7+1,24))
68 expect_identical(pred00_dd$getSerie(i), rep(i%%7+1,24))
69 }
70
71 pred13_sd = getForecast(data13, indices, "Persistence", "Persistence", Inf, 24, same_day=TRUE)
72 pred13_dd = getForecast(data13, indices, "Persistence", "Persistence", Inf, 24, same_day=FALSE)
73 for (i in seq_along(indices))
74 {
75 expect_identical(pred13_sd$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) )
76 expect_identical(pred13_dd$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) )
77 }
78
79 #A few extra checks
80 expect_identical( pred13_sd$getIndexInData(1), dateIndexToInteger("2007-04-01",data12) )
81 expect_identical( pred00_dd$getIndexInData(3), dateIndexToInteger("2007-04-03",data00) )
82 expect_identical( pred13_dd$getIndexInData(5), dateIndexToInteger("2007-04-05",data13) )
83 })
84
85 test_that("Neighbors+Zero method behave as expected",
86 {
87 })
88
89 test_that("Neighbors+Neighbors method behave as expected",
90 {
91 })