after merg
[talweg.git] / pkg / tests / testthat / test-Forecaster.R
index 09b6f0a..3f5cf9c 100644 (file)
@@ -2,18 +2,15 @@ context("Check that forecasters behave as expected")
 
 ts_data = system.file("testdata","ts_test.csv",package="talweg")
 exo_data = system.file("testdata","exo_test.csv",package="talweg")
-data00 <<- getData(ts_data, exo_data, input_tz="GMT", date_format="%Y-%m-%d %H:%M",
-       working_tz="GMT", predict_at=0, limit=Inf)
-data13 <<- getData(ts_data, exo_data, input_tz="GMT", date_format="%Y-%m-%d %H:%M",
-       working_tz="GMT", predict_at=13, limit=Inf)
-#Forecast at sunday to saturday (series 7 to 1), for monday to sunday (series 1 to 7)
-indices <<- seq(as.Date("2007-04-01"),as.Date("2007-04-07"),"days")
+data_p <<- getData(ts_data, exo_data, date_format="%Y-%m-%d %H:%M", limit=Inf)
+#Forecasts from monday to sunday (series 1 to 7)
+indices <<- seq(as.Date("2007-04-02"),as.Date("2007-04-08"),"days")
 pred_order = c(7,1:6) #will facilitate tests
 
 test_that("Average method behave as expected",
 {
-       pred00_z = computeForecast(data00, indices, "Average", "Zero", Inf, 24)
-       pred00_p = computeForecast(data00, indices, "Average", "Persistence", Inf, 24)
+       pred00_z = computeForecast(data_p, indices, "Average", "Zero", 1, Inf, 24, ncores=1)
+       pred00_p = computeForecast(data_p, indices, "Average", "Persistence", 1, Inf, 24)
        for (i in 1:7)
        {
                #zero jump: should predict true values minus 1
@@ -22,30 +19,30 @@ test_that("Average method behave as expected",
                expect_equal( pred00_p$getForecast(i), rep(i,24) )
        }
 
-       #NOTE: days become
+       #NOTE: 24h-block become
        #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)
        #No jump between days, thus zero and persistence are equivalent (and correct)
-       pred13_z = computeForecast(data13, indices, "Average", "Zero", Inf, 24)
-       pred13_p = computeForecast(data13, indices, "Average", "Persistence", Inf, 24)
+       pred13_z = computeForecast(data_p, indices, "Average", "Zero", 14, Inf, 24)
+       pred13_p = computeForecast(data_p, indices, "Average", "Persistence", 14, Inf, 24)
        for (i in 1:7)
        {
-               expect_equal( pred13_z$getForecast(i), c( rep(i,11), rep(i%%7+1,13) ) )
-               expect_equal( pred13_p$getForecast(i), c( rep(i,11), rep(i%%7+1,13) ) )
+               expect_equal( pred13_z$getForecast(i), rep(i,24) )
+               expect_equal( pred13_p$getForecast(i), rep(i,24) )
        }
 
        #A few extra checks
-       expect_equal( pred00_p$getIndexInData(2), dateIndexToInteger("2007-04-02",data00) )
-       expect_equal( pred00_z$getIndexInData(2), dateIndexToInteger("2007-04-02",data00) )
-       expect_equal( pred13_p$getIndexInData(5), dateIndexToInteger("2007-04-05",data13) )
-       expect_equal( pred13_z$getIndexInData(5), dateIndexToInteger("2007-04-05",data13) )
+       expect_equal( pred00_p$getIndexInData(2), dateIndexToInteger("2007-04-03",data_p) )
+       expect_equal( pred00_z$getIndexInData(2), dateIndexToInteger("2007-04-03",data_p) )
+       expect_equal( pred13_p$getIndexInData(5), dateIndexToInteger("2007-04-06",data_p) )
+       expect_equal( pred13_z$getIndexInData(5), dateIndexToInteger("2007-04-06",data_p) )
 })
 
 test_that("Persistence method behave as expected",
 {
        #Situation A: +Zero; (generally) correct if jump, wrong otherwise
-       pred00_sd = computeForecast(data00, indices, "Persistence", "Zero", Inf, 24,
+       pred00_sd = computeForecast(data_p, indices, "Persistence", "Zero", 1, Inf, 24,
                ncores=1, same_day=TRUE)
-       pred00_dd = computeForecast(data00, indices, "Persistence", "Zero", Inf, 24,
+       pred00_dd = computeForecast(data_p, indices, "Persistence", "Zero", 1, Inf, 24,
                ncores=1, same_day=FALSE)
        for (i in 1:7)
        {
@@ -53,25 +50,20 @@ test_that("Persistence method behave as expected",
                expect_equal(pred00_dd$getForecast(i), rep(pred_order[i],24))
        }
 
-       pred13_sd = computeForecast(data13, indices, "Persistence", "Zero", Inf, 24,
+       pred13_sd = computeForecast(data_p, indices, "Persistence", "Zero", 14, Inf, 24,
                ncores=1, same_day=TRUE)
-       pred13_dd = computeForecast(data13, indices, "Persistence", "Zero", Inf, 24,
+       pred13_dd = computeForecast(data_p, indices, "Persistence", "Zero", 14, Inf, 24,
                ncores=1, same_day=FALSE)
-       for (i in 2:6)
+       for (i in 1:7)
        {
-               expect_equal(pred13_sd$getForecast(i), c( rep(i,11), rep(i%%7+1,13) ) )
-               expect_equal(pred13_dd$getForecast(i), c( rep(i,11), rep(i%%7+1,13) ) )
+               expect_equal(pred13_sd$getForecast(i), rep(i,24) )
+               expect_equal(pred13_dd$getForecast(i), rep(i,24) )
        }
-       #boundaries are special cases: OK if same day, quite wrong otherwise
-       expect_equal(pred13_sd$getForecast(1), c( rep(1,11), rep(2,13) ) )
-       expect_equal(pred13_dd$getForecast(1), c( rep(1,11), rep(-5,13) ) )
-       expect_equal(pred13_sd$getForecast(7), c( rep(7,11), rep(1,13) ) )
-       expect_equal(pred13_dd$getForecast(7), c( rep(7,11), rep(8,13) ) )
 
        #Situation B: +Persistence, (generally) correct
-       pred00_sd = computeForecast(data00, indices, "Persistence", "Persistence", Inf, 24,
+       pred00_sd = computeForecast(data_p, indices, "Persistence", "Persistence", 1, Inf, 24,
                ncores=1, same_day=TRUE)
-       pred00_dd = computeForecast(data00, indices, "Persistence", "Persistence", Inf, 24,
+       pred00_dd = computeForecast(data_p, indices, "Persistence", "Persistence", 1, Inf, 24,
                ncores=1, same_day=FALSE)
        for (i in 3:7)
        {
@@ -84,55 +76,50 @@ test_that("Persistence method behave as expected",
        expect_equal(pred00_sd$getForecast(2), rep(2,24) )
        expect_equal(pred00_dd$getForecast(2), rep(-5,24) )
 
-       pred13_sd = computeForecast(data13, indices, "Persistence", "Persistence", Inf, 24,
+       pred13_sd = computeForecast(data_p, indices, "Persistence", "Persistence", 14, Inf, 24,
                ncores=1, same_day=TRUE)
-       pred13_dd = computeForecast(data13, indices, "Persistence", "Persistence", Inf, 24,
+       pred13_dd = computeForecast(data_p, indices, "Persistence", "Persistence", 14, Inf, 24,
                ncores=1, same_day=FALSE)
-       for (i in 2:6)
+       for (i in 1:7)
        {
-               expect_equal(pred13_sd$getForecast(i), c( rep(i,11), rep(i%%7+1,13) ) )
-               expect_equal(pred13_dd$getForecast(i), c( rep(i,11), rep(i%%7+1,13) ) )
+               expect_equal(pred13_sd$getForecast(i), rep(i,24) )
+               expect_equal(pred13_dd$getForecast(i), rep(i,24) )
        }
-       #boundaries are special cases: OK if same day, quite wrong otherwise
-       expect_equal(pred13_sd$getForecast(1), c( rep(1,11), rep(2,13) ) )
-       expect_equal(pred13_dd$getForecast(1), c( rep(1,11), rep(-5,13) ) )
-       expect_equal(pred13_sd$getForecast(7), c( rep(7,11), rep(1,13) ) )
-       expect_equal(pred13_dd$getForecast(7), c( rep(7,11), rep(8,13) ) )
 
        #A few extra checks
-       expect_equal( pred00_sd$getIndexInData(3), dateIndexToInteger("2007-04-03",data00) )
-       expect_equal( pred00_dd$getIndexInData(6), dateIndexToInteger("2007-04-06",data00) )
-       expect_equal( pred13_sd$getIndexInData(3), dateIndexToInteger("2007-04-03",data13) )
-       expect_equal( pred13_dd$getIndexInData(6), dateIndexToInteger("2007-04-06",data13) )
+       expect_equal( pred00_sd$getIndexInData(3), dateIndexToInteger("2007-04-04",data_p) )
+       expect_equal( pred00_dd$getIndexInData(6), dateIndexToInteger("2007-04-07",data_p) )
+       expect_equal( pred13_sd$getIndexInData(3), dateIndexToInteger("2007-04-04",data_p) )
+       expect_equal( pred13_dd$getIndexInData(6), dateIndexToInteger("2007-04-07",data_p) )
 })
 
 test_that("Neighbors method behave as expected",
 {
        #Situation A: +Zero; correct if jump, wrong otherwise
-       pred00 = computeForecast(data00, indices, "Neighbors", "Zero", Inf, 24,
-               simtype="mix", local=FALSE)
+       pred00 = computeForecast(data_p, indices, "Neighbors", "Zero", 1, Inf, 24,
+               simtype="mix", local=FALSE, window=c(1,1))
        for (i in 1:7)
                expect_equal(pred00$getForecast(i), rep(pred_order[i],24))
 
-       pred13 = computeForecast(data13, indices, "Persistence", "Zero", Inf, 24,
-               simtype="mix", local=FALSE)
+       pred13 = computeForecast(data_p, indices, "Persistence", "Zero", 14, Inf, 24,
+               simtype="mix", local=FALSE, window=c(1,1))
        for (i in 1:7)
-               expect_equal(pred13$getForecast(i), c( rep(i,11), rep(i%%7+1,13) ) )
+               expect_equal(pred13$getForecast(i), rep(i,24) )
 
        #Situation B: +Neighbors == too difficult to eval in a unit test
-#      pred00 = computeForecast(data00, indices, "Neighbors", "Neighbors", Inf, 24,
+#      pred00 = computeForecast(data_p, indices, "Neighbors", "Neighbors", 1, Inf, 24,
 #              simtype="endo", local=FALSE)
 #      jumps = ...
 #      for (i in 1:7)
 #              expect_equal(pred00$getForecast(i), rep(pred_order[i]+jumps[i],24))
-#      pred13 = computeForecast(data13, indices, "Neighbors", "Neighbors", Inf, 24,
+#      pred13 = computeForecast(data_p, indices, "Neighbors", "Neighbors", 14, Inf, 24,
 #              simtype="endo", local=FALSE)
 #      for (i in 1:7)
 #              expect_equal(pred13$getForecast(i), c( rep(i,11), rep(i%%7+1,13) ) )
 
        #A few extra checks
-       expect_equal( pred00$getIndexInData(1), dateIndexToInteger("2007-04-01",data00) )
-       expect_equal( pred00$getIndexInData(4), dateIndexToInteger("2007-04-04",data00) )
-       expect_equal( pred13$getIndexInData(1), dateIndexToInteger("2007-04-01",data13) )
-       expect_equal( pred13$getIndexInData(4), dateIndexToInteger("2007-04-04",data13) )
+       expect_equal( pred00$getIndexInData(1), dateIndexToInteger("2007-04-02",data_p) )
+       expect_equal( pred00$getIndexInData(4), dateIndexToInteger("2007-04-05",data_p) )
+       expect_equal( pred13$getIndexInData(1), dateIndexToInteger("2007-04-02",data_p) )
+       expect_equal( pred13$getIndexInData(4), dateIndexToInteger("2007-04-05",data_p) )
 })