1

I’m running unit tests on the forecast package, specifically for the forecast.ar() function. I have a block of code that works just fine when run on its own.

require(testthat)
require(forecast)
set.seed(55)
simseries <- stats::arima.sim(model = list(ar = c(-0.42, 0.832, 0.1, -0.34)), n = 400)
arfit <- stats::ar(simseries)
fc1 <- forecast(arfit)$mean
fc2 <- forecast(arfit, bootstrap = TRUE, npaths = 100, fan = TRUE)$mean
expect_true(all(fc1 == fc2))

However, when I embed this in a test_that() function (after starting a new clean R session), I get an error that the ts is empty. I’ve determined that the error occurs on the two lines generating fc1 and fc2; simseries is saved correctly.

require(testthat)
require(forecast)
test_that("tests for forecast.ar", {
set.seed(55)
simseries <- stats::arima.sim(model = list(ar = c(-0.42, 0.832, 0.1, -0.34)), n = 400)
arfit <- stats::ar(simseries)
fc1 <- forecast(arfit)$mean
fc2 <- forecast(arfit, bootstrap = TRUE, npaths = 100, fan = TRUE)$mean
expect_true(all(fc1 == fc2))
})

Why is this happening? I thought it might be a namespace issue with using the ar and arima.sim functions from the stats package, so I included the stats namespace in the function call, but that does not fix the problem.

R 3.1.1 forecast 6.1 testthat 0.10.0

  • The parameters for `forecast.arima(..., bootstrap, npath, fan)` are only relevant for prediction intervals, while you are only examining the point-predictions. What is the result if you remove these? Also, is it possible that the return to `fc1$method` and `fc2$method` are different (yes, I know not technically part of your return)? ... That is to say, can you simplify the call and get a correct result / isolate the problem? – alexwhitworth Sep 23 '15 at 02:34
  • If I comment out the three forecast lines (fc1, fc2, expect_true) and try adding these one at at time, the error occurs the first time `forecast()` is called. Moreover, testing that fc1 matches the forecast returned when forecast is called with `bootstrap = TRUE` and the other additional arguments is the purpose of this unit test, i.e. to make sure the point estimate forecasts always match. – Eddie Fernandez Sep 23 '15 at 15:15
  • I was going to say that these tests are best for the package author... but it appears that [you're working with Prof Hyndman](https://github.com/robjhyndman/forecast/commit/cde05751c803d60e18c486ae4c4851a54b766805) – alexwhitworth Sep 23 '15 at 15:57
  • ... ie- that is to say that I'm not sure. I haven't much experience with `testthat` -- it's on my to-do list for the next version of a couple of my own packages.... You might check [the source code](https://github.com/hadley/testthat) – alexwhitworth Sep 23 '15 at 16:00

0 Answers0