Skip to content

Commit b22016f

Browse files
committed
fixes for CRAN submission
1 parent f3bb04c commit b22016f

File tree

13 files changed

+111
-61
lines changed

13 files changed

+111
-61
lines changed

NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ S3method(plot,tsissm.simulate)
2121
S3method(predict,tsissm.estimate)
2222
S3method(predict,tsissm.selection)
2323
S3method(print,summary.tsissm.estimate)
24+
S3method(print,tsissm.diagnose)
2425
S3method(residuals,tsissm.estimate)
2526
S3method(sigma,tsissm.estimate)
2627
S3method(simulate,tsissm.estimate)
@@ -171,7 +172,6 @@ importFrom(utils,head)
171172
importFrom(utils,setTxtProgressBar)
172173
importFrom(utils,tail)
173174
importFrom(utils,txtProgressBar)
174-
importFrom(utils,write.table)
175175
importFrom(viridisLite,viridis)
176176
importFrom(xts,as.xts)
177177
importFrom(xts,is.xts)

R/estimation.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ estimate.tsissm.autospec <- function(object, control = NULL, trace = FALSE, ...)
113113
tmp_toc <- Sys.time() - tmp_tic
114114
est_time_one <- tmp_toc
115115
estimated_time <- round(as.numeric(est_time_one/n_cores) * NROW(args_grid), 2) %/% 60
116-
print(paste0("no. of models to evaluate: ", NROW(args_grid)))
117-
print(paste0("estimated evaluation time (mins): ", estimated_time))
116+
cat(paste0("no. of models to evaluate: ", NROW(args_grid)))
117+
cat(paste0("estimated evaluation time (mins): ", estimated_time))
118118
prog_trace <- progressor(n)
119119
}
120120
tic <- Sys.time()

R/methods.R

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#' @description Extract the fitted values from an estimated model.
66
#' @param object an object of class \dQuote{tsissm.estimate}.
77
#' @param ... not currently used.
8+
#' @returns an xts object of the fitted values.
89
#' @aliases fitted
910
#' @method fitted tsissm.estimate
1011
#' @rdname fitted
@@ -941,6 +942,8 @@ BIC.tsissm.selection <- function(object, ...)
941942
#' @param actual the actual data matched to the dates of the forecasts.
942943
#' @param alpha the coverage level for distributional forecast metrics.
943944
#' @param ... not currently used.
945+
#' @returns a data.frame of performance metrics including MAPE, MSLRE, BIAS and AIC
946+
#' for the estimate object and MAPE, MSLRE, BIAS, MASE, MIS and CRPS for predict object.
944947
#' @aliases tsmetrics
945948
#' @method tsmetrics tsissm.predict
946949
#' @rdname tsmetrics
@@ -979,21 +982,8 @@ tsmetrics.tsissm.estimate <- function(object, ...)
979982
MAPE <- mape(object$spec$target$y_orig, fitted(object))
980983
BIAS <- bias(object$spec$target$y_orig, fitted(object))
981984
MSLRE <- mslre(object$spec$target$y_orig, fitted(object))
982-
# yt <- object$spec$transform$transform(object$spec$target$y_orig, lambda = lambda)
983-
# ft <- object$spec$transform$transform(as.numeric(fitted(object)), lambda = lambda)
984-
# r <- yt - ft
985-
cat("\ntsissm: Performance Metrics")
986-
cat("\n----------------------------------\n")
987-
cat(paste0("AIC\t: ", round(AIC,2), " (n = ", nr,")"))
988-
cat("\n")
989-
cat(paste0("MAPE\t: ", round(MAPE,5)))
990-
cat("\n")
991-
cat(paste0("BIAS\t: ", round(BIAS,5)))
992-
cat("\n")
993-
cat(paste0("MSLRE\t: ", round(MSLRE, 5)))
994-
metrics = c(AIC, MAPE, BIAS, MSLRE)
995-
names(metrics) <- c("AIC","MAPE","BIAS","MSLRE")
996-
return(invisible(metrics))
985+
metrics <- data.frame("MAPE" = MAPE, "MSLRE" = MSLRE, "BIAS" = BIAS, "AIC" = AIC)
986+
return(metrics)
997987
}
998988

999989
# vcov ---------------------------------------------------
@@ -1070,7 +1060,7 @@ sigma.tsissm.estimate <- function(object, ...) {
10701060
#' @description Generates a list of model equations in LaTeX.
10711061
#' @param object an object of class \dQuote{tsissm.estimate}.
10721062
#' @param ... not currently used.
1073-
#' @return A list of equations in LaTeX which can be used in documents. This is
1063+
#' @returns A list of equations in LaTeX which can be used in documents. This is
10741064
#' a list with 3 slots for the observation, state and variance equations,
10751065
#' @details This method is called in the summary when the format output option
10761066
#' chosen is \dQuote{flextable}.

R/plots.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
#'
1616
plot.tsissm.estimate <- function(x, y = NULL, ...)
1717
{
18-
opar <- par(mfrow = c(1,1))
18+
opar <- par(no.readonly = TRUE)
19+
on.exit(par(opar))
1920
tsd <- tsdecompose(x)
2021
# fitted+actual and then states
2122
a <- x$spec$target$y_orig
@@ -35,15 +36,15 @@ plot.tsissm.estimate <- function(x, y = NULL, ...)
3536
mtext(colnames(tsd)[i], side = 4, adj = 0.5, padj = 0.5, cex = 0.7, font = 2, family = "mono")
3637
grid()
3738
}
38-
suppressWarnings(par(opar))
3939
}
4040

4141
#' @method plot tsissm.simulate
4242
#' @rdname plot
4343
#' @export
4444
plot.tsissm.simulate <- function(x, y = NULL, ...)
4545
{
46-
opar <- par(mfrow = c(1,1))
46+
opar <- par(no.readonly = TRUE)
47+
on.exit(par(opar))
4748
components <- tsdecompose(x)
4849
n <- length(components) + 1
4950
colx <- (.viridis_fun(option = "H", begin = 0.4, end = 0.9, alpha = 0.5)(n))
@@ -57,7 +58,6 @@ plot.tsissm.simulate <- function(x, y = NULL, ...)
5758
plot(components[[component_names[i]]], gradient_color = colx[i], main = "", ylab = "", xlab = "", x_axes = FALSE, cex.axis = 0.8, interval_color = "steelblue", median_width = 1, interval_type = 1, interval_width = 1)
5859
mtext(component_names[i], side = 4, adj = 0.5, padj = 0.5, cex = 0.7, font = 2, family = "mono")
5960
}
60-
suppressWarnings(par(opar))
6161
}
6262

6363
#' @method plot tsissm.profile
@@ -66,7 +66,8 @@ plot.tsissm.simulate <- function(x, y = NULL, ...)
6666
plot.tsissm.profile <- function(x, y = NULL, type = c("coef","mape","mase","crps"), ...)
6767
{
6868
Simulation <- Variable <- NULL
69-
opar <- par(mfrow = c(1,1))
69+
opar <- par(no.readonly = TRUE)
70+
on.exit(par(opar))
7071
type <- match.arg(type[1], c("coef","mape","mase","crps"))
7172
if (type == "coef") {
7273
true_values <- data.table(Variable = names(x$true_coef),
@@ -99,6 +100,5 @@ plot.tsissm.profile <- function(x, y = NULL, type = c("coef","mape","mase","crps
99100
colx <- rev(.viridis_fun(alpha = 0.5, begin = 0, end = 0.7, option = "H")(ncol(tmp)))
100101
boxplot(round(tmp, 2), xlab = "Horizon", ylab = "CRPS", col = colx, outline = FALSE, main = "CRPS by Horizon")
101102
}
102-
suppressWarnings(par(opar))
103103
return(invisible(x))
104104
}

R/tsdiagnose.R

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
#' @param object an object of class \dQuote{tsissm.estimate}.
55
#' @param plot whether to generate diagnostic plots to accompany summary.
66
#' @param ... not currently used.
7-
#' @return A list of tables (printed out and returned invisibly) with
8-
#' Ljung-Box test for residual autocorrelation, parameter and model bounds
9-
#' diagnostics and outlier dates using the Rosner test (\code{\link[EnvStats]{rosnerTest}}).
7+
#' @returns A list of tests including the weighted Ljung-Box test for residual
8+
#' autocorrelation, system forecastability test, and outlier dates using the
9+
#' Rosner test (\code{\link[EnvStats]{rosnerTest}}). Optionally generates a plot
10+
#' of relevant diagnostics.
1011
#' @aliases tsdiagnose
1112
#' @method tsdiagnose tsissm.estimate
1213
#' @rdname tsdiagnose
@@ -16,29 +17,13 @@
1617
tsdiagnose.tsissm.estimate <- function(object, plot = FALSE, ...)
1718
{
1819
if (sum(object$spec$arma$order) > 0) {
19-
cat("\nARMA roots (<1)")
20-
cat("\n------------------------------------------\n")
2120
armav <- coef(object)
2221
armav <- armav[grepl("theta|psi", names(armav))]
2322
rt <- .armaroots(armav)
24-
if (object$spec$arma$order[1] > 0) {
25-
cat("Inverse AR roots:", 1/abs(rt$root$ar))
26-
cat("\n")
27-
}
28-
if (object$spec$arma$order[2] > 0) {
29-
cat("Inverse MA roots:", 1/abs(rt$root$ma))
30-
cat("\n")
31-
}
3223
} else {
3324
rt <- NULL
3425
}
35-
cat("\nForecastability")
36-
cat("\n------------------------------------------\n")
3726
e <- abs(eigen(object$model$D, symmetric = FALSE)$values)
38-
cat("Real Eigenvalues (D):", round(e,3))
39-
cat("\n")
40-
cat("\nWeighted Ljung-Box Test [scaled residuals]")
41-
cat("\n------------------------------------------\n")
4227
df <- sum(object$spec$arma$order)
4328
sigma <- sigma(object)
4429
r <- as.numeric(na.omit(residuals(object, transformed = TRUE)/sigma))
@@ -53,25 +38,72 @@ tsdiagnose.tsissm.estimate <- function(object, plot = FALSE, ...)
5338
lbsr <- data.table(Lag = c("Lag[1]", paste0("Lag[",b2j,"]"), paste0("Lag[",b3j,"]"), paste0("Lag[",b4j,"]")),
5439
statistic = c(b1$statistic[[1]], b2$statistic[[1]], b3$statistic[[1]],b4$statistic[[1]]),
5540
pvalue = c(b1$p.value[[1]], b2$p.value[[1]],b3$p.value[[1]], b4$p.value[[1]]))
56-
print(lbsr, row.names = FALSE, digits = 3)
5741
rtest <- .rosner_test(as.numeric(na.omit(residuals(object, transformed = TRUE))), k = 10)
5842
if (any(rtest$Outlier)) {
59-
out.index <- object$spec$target$index[which(object$spec$good == 1)][rtest$Obs.Num[rtest$Outlier]]
60-
cat("\nOutlier Diagnostics (based on Rosner Test)")
61-
cat("\n------------------------------------------")
62-
cat("\nOutliers:", as.character(out.index))
43+
outliers_index <- object$spec$target$index[which(object$spec$good == 1)][rtest$Obs.Num[rtest$Outlier]]
6344
} else {
64-
out.index <- NULL
45+
outliers_index <- NULL
6546
}
6647
if (plot) {
48+
opar <- par(no.readonly = TRUE)
49+
on.exit(par(opar))
6750
par(mfrow = c(2,2), mar = c(3,3,3,3))
6851
if (df > 0) .plotarmaroots(.armaroots(armav))
6952
acf(as.numeric(r), type = "correlation", main = "Scaled Residuals Autocorrelation")
70-
hist(r, breaks = "fd", main = "Scaled Residuals Histogram", probability = T)
53+
hist(r, breaks = "fd", main = "Scaled Residuals Histogram", probability = TRUE)
7154
box()
7255
qqnorm(r)
7356
qqline(r, col = 2)
7457
}
75-
L <- list(armaroots = rt, D.eigenvalues = e, lb_test = lbsr, outliers = rtest$all.stats, outlier_index = out.index)
76-
return(invisible(L))
58+
L <- list(arma_test = rt,
59+
stability_test = e,
60+
weighted_box_test = lbsr,
61+
rosner_test = rtest,
62+
outliers_index = outliers_index)
63+
class(L) <- "tsissm.diagnose"
64+
return(L)
65+
}
66+
67+
68+
#' Model Diagnostics Print method
69+
#'
70+
#' @description Print method for class \dQuote{tsissm.diagnose}
71+
#' @param x an object of class \dQuote{tsissm.duagnose} generated from
72+
#' calling \code{\link[tsissm]{tsdiagnose}}.
73+
#' @param ... not currently used.
74+
#' @returns Invisibly returns the original object and prints the output to console.
75+
#' @aliases print.tsissm.diagnose
76+
#' @method print tsissm.diagnose
77+
#' @rdname print.tsissm.diagnose
78+
#' @export
79+
#'
80+
#'
81+
print.tsissm.diagnose <- function(x, ...)
82+
{
83+
arma_test <- x$arma_test
84+
if (!is.null(arma_test)) {
85+
cat("\nARMA roots (<1)")
86+
cat("\n------------------------------------------\n")
87+
if (!is.null(arma_test$root$ar)) {
88+
cat("Inverse AR roots:", 1/abs(arma_test$root$ar))
89+
cat("\n")
90+
}
91+
if (!is.null(arma_test$root$ma)) {
92+
cat("Inverse MA roots:", 1/abs(arma_test$root$ma))
93+
cat("\n")
94+
}
95+
}
96+
cat("\nForecastability")
97+
cat("\n------------------------------------------\n")
98+
cat("Real Eigenvalues (D):", round(x$stability_test,3))
99+
cat("\n")
100+
cat("\nWeighted Ljung-Box Test [scaled residuals]")
101+
cat("\n------------------------------------------\n")
102+
print(x$weighted_box_test, row.names = FALSE, digits = 3)
103+
if (any(x$rosner_test$Outlier)) {
104+
cat("\nOutlier Diagnostics (based on Rosner Test)")
105+
cat("\n------------------------------------------")
106+
cat("\nOutliers:", as.character(x$outliers_index))
107+
}
108+
return(invisible(x))
77109
}

R/tsissm-package.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#' @import data.table
66
#' @importFrom RTMB AD ADoverload ADREPORT REPORT matrix eigen as.vector.advector
77
#' @importFrom TMB MakeADFun
8-
#' @importFrom utils head tail data txtProgressBar setTxtProgressBar write.table
8+
#' @importFrom utils head tail data txtProgressBar setTxtProgressBar
99
#' @importFrom stats sd var acf na.pass pchisq pgamma qqline qqnorm simulate na.omit median fitted coef quantile residuals predict logLik cov cor qt pnorm AIC BIC nobs sigma ar arima dnorm printCoefmat vcov density
1010
#' @importFrom graphics grid legend lines par plot points abline axis axis.Date axis.POSIXct box polygon layout mtext title hist boxplot
1111
#' @importFrom grDevices gray n2mfrow

R/tsprofile.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ profile_fun <- function(sim, object, h, control, trace)
7474
toc <- Sys.time()
7575
dtime <- difftime(toc, tic, units = "secs")
7676
if (trace) {
77-
print(paste0("\nCompleted Profiling in ", round(as.numeric(dtime),2)," secs."))
78-
print(paste0("\nCompiling Performance Metrics..."))
77+
cat(paste0("\nCompleted Profiling in ", round(as.numeric(dtime),2)," secs."))
78+
cat(paste0("\nCompiling Performance Metrics..."))
7979
}
8080
C <- rbindlist(lapply(1:length(prof), function(i) prof[[i]]$L1))
8181
M <- rbindlist(lapply(1:length(prof), function(i) prof[[i]]$L2))

man/fitted.Rd

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

man/print.tsissm.diagnose.Rd

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

man/tsdiagnose.Rd

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

0 commit comments

Comments
 (0)