From 99b0dd141e92c1ce82e5ead378ba42b62777e611 Mon Sep 17 00:00:00 2001 From: "jouni.helske@jyu.fi" Date: Thu, 24 Apr 2014 23:44:32 +0300 Subject: [PATCH] Corrected a major bug in SSMcycle Basically SSMcycle gave wrong system matrix T for all models, as nondiagonal entries contained - sign in wrong place... No idea how this has been unnoticed for so long. --- ChangeLog | 1 + R/SSMcycle.R | 6 +++--- R/predict.SSModel.R | 5 +++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1e5144f..641d9eb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -7,6 +7,7 @@ Changes from Version 1.0.3 to 1.0.4: * Corrected a bug in approximation algorithm which caused the approximation to fail for seemingly random models. * Fixed a bug in SSMcycle which caused error with common components. + * Fixed bug in SSMcycle which resulted erroneus system matrix T in all cases. * Fixed a bug in SSMseasonal which caused error in SSModel when using common components. * SSMseasonal with trigonometric seasonal now works properly when period is odd. * Fixed a bug in coef.KFS which caused function to return smoothed states even with argument diff --git a/R/SSMcycle.R b/R/SSMcycle.R index 31bd559..4963ba3 100644 --- a/R/SSMcycle.R +++ b/R/SSMcycle.R @@ -15,8 +15,8 @@ SSMcycle <- function(period, type, Q, index, a1, P1, P1inf, n, ynames) { stop("type must be 'distinct' or 'common'.") } - if (!(length(period) == 1 & period > 1 & abs(period - round(period)) == 0)) - stop("period of the cycle component must be integer larger than 1. ") + if (!(length(period) == 1 & period > 0)) + stop("Period of the cycle component must be larger than 0. ") lambda <- 2 * pi/period @@ -24,7 +24,7 @@ SSMcycle <- function(period, type, Q, index, a1, P1, P1inf, n, ynames) { Z <- matrix(0, p, m) T <- matrix(0, m, m) Z_univariate <- matrix(c(1,0), 1, 2) - T_univariate <- matrix(c(cos(lambda), sin(lambda), -sin(lambda), cos(lambda)), 2, 2) + T_univariate <- matrix(c(cos(lambda), -sin(lambda), sin(lambda), cos(lambda)), 2, 2) if (type != 2) { for (i in 1:p) { Z[i, ((i - 1) * 2 + 1):(i * 2)] <- Z_univariate diff --git a/R/predict.SSModel.R b/R/predict.SSModel.R index 6c61dde..4b3d881 100644 --- a/R/predict.SSModel.R +++ b/R/predict.SSModel.R @@ -138,7 +138,7 @@ predict.SSModel <- function(object, newdata, n.ahead, interval = c("none", "conf } } else { - if(!is.null(n.ahead) || !missing(n.ahead)){ + if(!missing(n.ahead) && !is.null(n.ahead)){ tv <- logical(5) tv[1] <- dim(object$Z)[3] > 1 @@ -203,7 +203,8 @@ predict.SSModel <- function(object, newdata, n.ahead, interval = c("none", "conf } out <- KFS(model = object, smoothing = "signal",maxiter=maxiter) for (i in 1:p) { - pred[[i]] <- cbind(fit=out$thetahat[timespan, i]+(if(object$distribution[i]=="poisson") log(object$u[timespan, i]) else 0), + pred[[i]] <- cbind(fit=out$thetahat[timespan, i]+ + (if(object$distribution[i]=="poisson") log(object$u[timespan, i]) else 0), switch(interval, none = NULL, out$thetahat[timespan, i] + (if(object$distribution[i]=="poisson") log(object$u[timespan, i]) else 0) + qnorm((1 - level)/2) * sqrt(out$V_theta[i, i, timespan]) %o% c(1, -1)),