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
1617tsdiagnose.tsissm.estimate <- function (object , plot = FALSE , ... )
1718{
1819 if (sum(object $ spec $ arma $ order ) > 0 ) {
19- cat(" \n ARMA 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(" \n Forecastability" )
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(" \n Weighted 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(" \n Outlier Diagnostics (based on Rosner Test)" )
61- cat(" \n ------------------------------------------" )
62- cat(" \n Outliers:" , 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(" \n ARMA 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(" \n Forecastability" )
97+ cat(" \n ------------------------------------------\n " )
98+ cat(" Real Eigenvalues (D):" , round(x $ stability_test ,3 ))
99+ cat(" \n " )
100+ cat(" \n Weighted 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(" \n Outlier Diagnostics (based on Rosner Test)" )
105+ cat(" \n ------------------------------------------" )
106+ cat(" \n Outliers:" , as.character(x $ outliers_index ))
107+ }
108+ return (invisible (x ))
77109}
0 commit comments