Skip to content

Commit f5ea40d

Browse files
committed
small fixes
1 parent 2845917 commit f5ea40d

File tree

10 files changed

+62
-8
lines changed

10 files changed

+62
-8
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ Encoding: UTF-8
1818
RoxygenNote: 7.3.2
1919
VignetteBuilder: knitr
2020
LazyData: true
21+
URL: https://github.com/tsmodels/tsissm
2122
Config/testthat/edition: 3

R/backtest.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ tsbacktest.tsissm.spec <- function(object, start = floor(length(object$target$y_
8282
{
8383
parameter <- b <- forecast_dates <- NULL
8484
data <- xts(object$target$y_orig, object$target$index)
85+
86+
if (start > NROW(data)) stop("\nstart is greater than length of data")
87+
if (end > NROW(data)) stop("\nend is greater than length of data")
88+
89+
8590
if (object$xreg$include_xreg) {
8691
use_xreg <- TRUE
8792
xreg <- xts(object$xreg$xreg, object$target$index)

R/estimation.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ estimate.tsissm.spec <- function(object, control = issm_control(algorithm = "SLS
6969
f$tmb <- opt$tmb
7070
f$opt <- opt$solver_out
7171
}
72+
f$status <- opt$solver_out$status
7273
f$score_promise <- opt$scores
7374
f$elapsed <- difftime(Sys.time(), tic, units = "mins")
7475
f$hessian <- opt$hessian

R/hresiduals.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ hresiduals_issm <- function(object, h = 12, nsim = 1000, start = 1, transformed
99
y <- c(0, object$spec$target$y)
1010
actual <- y[(start + 1):(start + h)]
1111
} else {
12-
y <- object$spec$target$y_orig
12+
y <- c(0, object$spec$target$y_orig)
1313
actual <- y[(start + 1):(start + h)]
1414
}
1515
xseed <- object$model$states[start, ,drop = FALSE]
1616
if (!object$spec$xreg$include_xreg) {
1717
xreg = matrix(0, ncol = 1, nrow = h)
1818
} else {
19-
xreg <- object$spec$xreg$x[(start + 1):(start + h)]
19+
m <- NCOL(object$spec$xreg$xreg)
20+
xreg <- rbind(matrix(0, ncol = m, nrow = 1), object$spec$xreg$xreg)[(start + 1):(start + h), ,drop = FALSE]
2021
}
2122
forecast_mu <- tsmoments.tsissm.estimate(object, h = h, newxreg = xreg, init_states = xseed, transform = !transformed)$mean
2223
e <- actual - forecast_mu

R/simulation.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ simulate.tsissm.estimate <- function(object, nsim = 1, seed = NULL, h = 1, newxr
113113
colnames(newxreg) <- colnames(object$spec$xreg$xreg)
114114
}
115115
}
116-
newxreg <- rbind(matrix(0, ncol = ncol(newxreg), nrow = 1), newxreg)
116+
newxreg <- rbind(matrix(0, ncol = ncol(newxreg), nrow = 1), coredata(newxreg))
117117

118118
res <- object$model$error[-1]
119119
sigma_res <- object$model$sigma
@@ -236,7 +236,7 @@ simulate.tsissm.estimate <- function(object, nsim = 1, seed = NULL, h = 1, newxr
236236
colnames(newxreg) <- colnames(object$spec$xreg$xreg)
237237
}
238238
}
239-
newxreg <- rbind(matrix(0, ncol = ncol(newxreg), nrow = 1), newxreg)
239+
newxreg <- rbind(matrix(0, ncol = ncol(newxreg), nrow = 1), coredata(newxreg))
240240

241241
res <- object$model$error
242242
garch_sigma <- object$model$sigma

R/specification.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -505,10 +505,11 @@ init_res <- function(y, spec) {
505505
} else {
506506
B <- box_cox(lambda = lambda)
507507
yt <- B$transform(y, frequency = seasonal_frequency)
508-
res <- try(residuals(arima(yt, order = c(1,1,2), seasonal = list(order = c(1, 1, 1), period = seasonal_frequency))), silent = TRUE)
508+
if (seasonal_frequency > 1) slist <- c(0,0,0) else slist <- c(1,1,1)
509+
res <- try(residuals(arima(yt, order = c(1,1,2), seasonal = list(order = slist, period = seasonal_frequency))), silent = TRUE)
509510
}
510511
if (inherits(res,'try-error')) {
511-
res <- residuals(tslinear(yt, trend = TRUE, seasonal = TRUE, frequency = seasonal_frequency))
512+
res <- residuals(tslinear(yt, trend = TRUE, seasonal = ifelse(seasonal_frequency>1, TRUE, FALSE), frequency = seasonal_frequency))
512513
}
513514
}
514515
res <- as.numeric(res)
@@ -682,4 +683,4 @@ check_variance <- function(variance) {
682683
variance <- "constant"
683684
}
684685
return(variance)
685-
}
686+
}

R/tsprofile.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ profile_fun <- function(sim, object, h, control, trace)
6060
if (inherits(mod, 'try-error')) {
6161
return(list(L1 = NULL, L2 = NULL, L3 = NULL))
6262
}
63-
if (mod$opt$status < 0) {
63+
if (mod$status < 0) {
6464
return(list(L1 = NULL, L2 = NULL, L3 = NULL))
6565
}
6666
p <- predict(mod, h = h)

man/tsissm-package.Rd

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/longtests/test-backtest.R

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
test_that("backtest", {
2+
data("spy", package = "tsissm")
3+
y <- as.xts(spy)
4+
xreg <- auto_regressors(y["2014/"], frequency = 1, lambda = 0, sampling = "days", method = "full",
5+
check.rank = TRUE, discard.cval = 3.5, maxit.iloop = 10, maxit.oloop = 10, types = "LS")
6+
exc <- which(xreg$xreg["2020-02-03"] == 1)
7+
xreg$xreg <- xreg$xreg[,-exc]
8+
xreg$init <- xreg$init[-exc]
9+
spec_dynamic <- issm_modelspec(y["2014/"], slope = TRUE, seasonal = FALSE, ar = 1, ma = 0, xreg = xreg$xreg,
10+
lambda = 0, variance = "dynamic", distribution = "jsu")
11+
b <- tsbacktest(spec_dynamic, start = 2000, end = 2327, rolling = TRUE, h = 1, estimate_every = 30, trace = FALSE, seed = 100)
12+
expect_s3_class(b, "tsissm.backtest")
13+
})
14+
15+
test_that("profile", {
16+
data("spy", package = "tsissm")
17+
y <- as.xts(spy)
18+
xreg <- auto_regressors(y["2014/"], frequency = 1, lambda = 0, sampling = "days", method = "full",
19+
check.rank = TRUE, discard.cval = 3.5, maxit.iloop = 10, maxit.oloop = 10, types = "LS")
20+
exc <- which(xreg$xreg["2020-02-03"] == 1)
21+
xreg$xreg <- xreg$xreg[,-exc]
22+
xreg$init <- xreg$init[-exc]
23+
spec_dynamic <- issm_modelspec(y["2014/"], slope = TRUE, seasonal = FALSE, ar = 1, ma = 0, xreg = xreg$xreg,
24+
lambda = 0, variance = "dynamic", distribution = "jsu")
25+
mod <- estimate(spec_dynamic)
26+
p <- tsprofile(mod, h = 10, nsim = 100, seed = 100)
27+
expect_s3_class(p, "tsissm.profile")
28+
})
29+

tests/longtests/test-hresiduals.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
test_that("hresiduals", {
2+
hr <- hresiduals(mod_dynamic_benchmark, h = 3, transformed = TRUE, index_start = 0)
3+
rr <- residuals(mod_dynamic_benchmark, transformed = TRUE)
4+
z1 <- as.numeric(hr$`1`)
5+
z2 <- as.numeric(rr)
6+
expect_equal(z1, z2, tolerance = 1e-8)
7+
})
8+
9+

0 commit comments

Comments
 (0)