tests/testthat/test-forecast2.R

test_that("test rwf()", {
  rwfc <- rwf(airmiles)$mean
  expect_true(all(rwfc == naive(airmiles)$mean))
  expect_true(all(rwfc < rwf(airmiles, drift = TRUE)$mean))
  expect_true(all(rwf(airmiles, fan = TRUE)$mean == rwfc))
  expect_length(rwf(airmiles, lambda = 0.15)$mean, 10)
  expect_false(identical(
    rwf(airmiles, lambda = 0.15, biasadj = FALSE)$mean,
    rwf(airmiles, lambda = 0.15, biasadj = TRUE)$mean
  ))
  # Constant series should not error
  series <- ts(rep(950, 20), frequency = 4)
  constantForecast <- expect_no_error(rwf(series))
  expect_true(is.constant(constantForecast$mean))
})

test_that("test forecast.HoltWinters()", {
  hwmod <- stats::HoltWinters(UKgas)
  expect_true(all(forecast(hwmod, fan = TRUE)$mean == forecast(hwmod)$mean))
  expect_error(forecast(hwmod, level = -10))
  expect_error(forecast(hwmod, level = 110))
  # Forecasts transformed manually with Box-Cox should match
  # forecasts when lambda is passed as an argument
  hwmodbc <- stats::HoltWinters(BoxCox(UKgas, lambda = 0.25))
  hwfc <- forecast(hwmodbc, lambda = 0.25, biasadj = FALSE)$mean
  hwfc2 <- forecast(hwmodbc, lambda = 0.25, biasadj = TRUE)$mean
  hwbcfc <- InvBoxCox(forecast(hwmodbc)$mean, lambda = 0.25)
  expect_true(all(hwfc == hwbcfc))
  expect_false(identical(hwfc, hwfc2))
})

test_that("test for forecast.StructTS()", {
  structtsmod <- stats::StructTS(wineind)
  fc1 <- forecast(structtsmod)$mean
  expect_true(all(fc1 == forecast(structtsmod, fan = TRUE)$mean))
  expect_error(forecast(structtsmod, level = -10))
  expect_error(forecast(structtsmod, level = 110))
  # Forecasts transformed manually with Box-Cox should match
  # forecasts when lambda is passed as an argument
  bcseries <- BoxCox(woolyrnq, lambda = 0.19)
  fc2 <- InvBoxCox(forecast(stats::StructTS(bcseries))$mean, lambda = 0.19)
  fc3 <- forecast(
    stats::StructTS(bcseries),
    lambda = 0.19,
    biasadj = FALSE
  )$mean
  fc4 <- forecast(stats::StructTS(bcseries), lambda = 0.19, biasadj = TRUE)$mean
  expect_true(all(fc2 == fc3))
  expect_false(identical(fc3, fc4))
})

test_that("test croston()", {
  expect_error(croston(rnorm(100)))
  fc <- croston(c(0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0))
  expect_equal(c(fc$mean), rep(0.43, 10))
  expect_error(croston_model(c(0, 0, 0, 0, 0)))
  expect_error(croston_model(c(0, 1, 0, 0, 0)))
  expect_no_error(croston_model(c(0, 1, 0, 0, 1)))
})

test_that("test hw()", {
  expect_output(
    print(summary(holt(wineind))),
    regexp = "Forecast method: Holt's method"
  )
  expect_output(
    print(summary(holt(wineind, damped = TRUE))),
    regexp = "Forecast method: Damped Holt's method"
  )
})

test_that("test holt()", {
  expect_output(
    print(summary(hw(wineind))),
    regexp = "Forecast method: Holt-Winters' additive method"
  )
})

test_that("test naive() and snaive()", {
  # WWWusage has frequency = 1, so naive and snaive should match
  expect_true(all(snaive(WWWusage, h = 10)$mean == naive(WWWusage)$mean))
  expect_true(all(snaive(WWWusage, h = 10)$upper == naive(WWWusage)$upper))
  expect_true(all(snaive(WWWusage, h = 10)$lower == naive(WWWusage)$lower))
  # Constant series should not error
  series <- ts(rep(950, 20), frequency = 4)
  constantForecast <- expect_no_error(snaive(series))
  expect_true(is.constant(constantForecast$mean))
})

Try the forecast package in your browser

Any scripts or data that you put into this service are public.

forecast documentation built on March 18, 2026, 9:07 a.m.