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