Skip to content

Commit

Permalink
fix minor bugs and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoBosh committed Feb 22, 2020
1 parent f5585e5 commit 91dfbd4
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
3 changes: 2 additions & 1 deletion R/autocovariances.R
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ partialAutocorrelations <- function(x, maxlag, lag_0 = TRUE, ...){
## TODO: this may fail if 'x' doesn't contain info about autocovariances.
## Maybe just put NA instead?
wrk <- autocovariances(x, maxlag = 0, ...)
res <- as(obj, "FlexibleLagged")
## BUGFIX: was: res <- as(obj, "FlexibleLagged")
res <- new("FlexibleLagged", data = pacr)
res[[0]] <- wrk[[0]]
res
}
Expand Down
32 changes: 32 additions & 0 deletions tests/testthat/test-Old_sarima.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## TODO: need proper tests here
test_that("functions in sarima.R work ok", {
## define a seasonal ARIMA model
m1 <- new("SarimaModel", iorder = 1, siorder = 1, ma = -0.3, sma = -0.1, nseasons = 12)

model0 <- modelCoef(m1, "ArmaModel")
model1 <- as(model0, "list")

ap.1 <- xarmaFilter(model1, x = AirPassengers, whiten = TRUE)
ap.2 <- xarmaFilter(model1, x = AirPassengers, eps = ap.1, whiten = FALSE)
ap <- AirPassengers
ap[-(1:13)] <- 0 # check that the filter doesn't use x, except for initial values.
ap.2a <- xarmaFilter(model1, x = ap, eps = ap.1, whiten = FALSE)
ap.2a - ap.2 ## indeed = 0
##ap.3 <- xarmaFilter(model1, x = list(init = AirPassengers[1:13]), eps = ap.1, whiten = TRUE)

## now set some non-zero initial values for eps
eps1 <- numeric(length(AirPassengers))
eps1[1:13] <- rnorm(13)
ap.A <- xarmaFilter(model1, x = AirPassengers, eps = eps1, whiten = TRUE)
ap.Ainv <- xarmaFilter(model1, x = ap, eps = ap.A, whiten = FALSE)
AirPassengers - ap.Ainv # = 0

## compare with sarima.f (an old function)
## compute predictions starting at from = 14
pred1 <- sarima.f(past = AirPassengers[1:13], n = 131, ar = model1$ar, ma = model1$ma)
pred2 <- xarmaFilter(model1, x = ap, whiten = FALSE)
pred2 <- pred2[-(1:13)]
all(pred1 == pred2) ##TRUE
expect_true(all(pred1 == pred2))

})
30 changes: 30 additions & 0 deletions tests/testthat/test-autocovariances.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
test_that("functions in autocorrelations.org work ok", {
acv1 <- autocovariances(list(ar = c(0.5), ma = c(0.8), sigma2 = 2), maxlag = 5)
expect_true(is(acv1, "Autocovariances"))

v1 <- rnorm(100)
autocorrelations(v1)
v1.acf <- autocorrelations(v1, maxlag = 10)

v1.acf[1:10] # drop lag zero value (and the class)
autocorrelations(v1, maxlag = 10, lag_0 = FALSE) # same

expect_output(show(autocorrelations(v1, maxlag = 10) ))
expect_output(show(autocorrelations(v1, maxlag = 10, lag_0 = FALSE) ))
expect_output(show(partialAutocorrelations(v1) ))
expect_output(show(partialAutocorrelations(v1, maxlag = 10) ))
Expand All @@ -18,4 +22,30 @@ test_that("functions in autocorrelations.org work ok", {
expect_output(show(partialVariances(v1, maxlag = 6) ))
## pv1 <- partialVariances(v1)

expect_true(is.numeric(partialAutocorrelations(v1, lag_0 = FALSE)))
partialAutocorrelations(v1, lag_0 = "var")

##
n <- 5000
x <- sarima:::rgarch1p1(n, alpha = 0.3, beta = 0.55, omega = 1, n.skip = 100)
x.acf <- autocorrelations(x)
x.pacf <- partialAutocorrelations(x)

acfGarchTest(x.acf, x = x, nlags = c(5,10,20))
acfGarchTest(x.pacf, x = x, nlags = c(5,10,20))

# do not compute CI's:
acfGarchTest(x.pacf, x = x, nlags = c(5,10,20), interval = NULL)

## ## plot methods call acfGarchTest() suitably if 'x' is given:
## plot(x.acf, data = x)
## plot(x.pacf, data = x)

## ## use 90% limits:
## plot(x.acf, data = x, interval = 0.90)

acfWnTest(x.acf, x = x, nlags = c(5,10,20))
nvarOfAcfKP(x, maxlag = 20)
whiteNoiseTest(x.acf, h0 = "arch-type", x = x, nlags = c(5,10,20))

})
3 changes: 3 additions & 0 deletions tests/testthat/test-fkf.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test_that("temporary disable FKF, see fkf.R", {
expect_error(fkf(), "FKF methods are not currently available")
})

0 comments on commit 91dfbd4

Please sign in to comment.