tests presque OK
[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{
44a9990b
BA
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
defcae03 20 expect_equal(pred00_z$getSerie(i), rep(pred_order[i],24))
44a9990b 21 #persistence jump == 1: should predict true values
defcae03 22 expect_equal(pred00_p$getSerie(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)
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 {
defcae03 33 expect_equal(pred13_z$getSerie(i), prediction ) ##TODO: prendre en compte recollement
44a9990b
BA
34 expect_equal(pred13_p$getSerie(i), prediction )
35 }
613a986f 36
44a9990b
BA
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
43test_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 }
613a986f 53
44a9990b
BA
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 }
613a986f 61
44a9990b
BA
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)
613a986f
BA
65 for (i in seq_along(indices))
66 {
44a9990b
BA
67 expect_identical(pred00_sd$getSerie(i), rep(i%%7+1,24))
68 expect_identical(pred00_dd$getSerie(i), rep(i%%7+1,24))
613a986f
BA
69 }
70
44a9990b
BA
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 }
6d97bfec 78
44a9990b
BA
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})
6d97bfec
BA
84
85test_that("Neighbors+Zero method behave as expected",
86{
44a9990b 87})
6d97bfec
BA
88
89test_that("Neighbors+Neighbors method behave as expected",
90{
44a9990b 91})