Function SSModel
creates a state space object object of class
SSModel
which can be used as an input object for various functions of
KFAS
package.
SSMarima(
ar = NULL,
ma = NULL,
d = 0,
Q,
stationary = TRUE,
index,
n = 1,
state_names = NULL,
ynames
)SSMcustom(Z, T, R, Q, a1, P1, P1inf, index, n = 1, state_names = NULL)
SSMcycle(
period,
Q,
type,
index,
a1,
P1,
P1inf,
damping = 1,
n = 1,
state_names = NULL,
ynames
)
SSModel(formula, data, H, u, distribution, tol = .Machine$double.eps^0.5)
SSMregression(
rformula,
data,
type,
Q,
index,
R,
a1,
P1,
P1inf,
n = 1,
remove.intercept = TRUE,
state_names = NULL,
ynames
)
SSMseasonal(
period,
Q,
sea.type = c("dummy", "trigonometric"),
type,
index,
a1,
P1,
P1inf,
n = 1,
state_names = NULL,
ynames,
harmonics
)
SSMtrend(
degree = 1,
Q,
type,
index,
a1,
P1,
P1inf,
n = 1,
state_names = NULL,
ynames
)
Object of class SSModel
, which is a list with the following
components:
A n x p matrix containing the observations.
A p x m x 1 or p x m x n array corresponding to the system matrix of observation equation.
A p x p x 1 or p x p x n array corresponding to the covariance matrix of observational disturbances epsilon.
A m x m x 1 or m x m x n array corresponding to the first system matrix of state equation.
A m x k x 1 or m x k x n array corresponding to the second system matrix of state equation.
A k x k x 1 or k x k x n array corresponding to the covariance matrix of state disturbances eta
A m x 1 matrix containing the expected values of the initial states.
A m x m matrix containing the covariance matrix of the nondiffuse part of the initial state vector.
A m x m matrix containing the covariance
matrix of the diffuse part of the initial state vector.
If P1[i,i]
is non-zero then P1inf[i,i]
is automatically set to zero.
A n x p matrix of an additional parameters in case of non-Gaussian model.
A vector of length p giving the distributions of the observations.
A tolerance parameter for Kalman filtering.
Original call to the function.
In addition, object of class
SSModel
contains following attributes:
Names of the list components.
Integer valued scalars defining the dimensions of the model components.
Types of the states in the model.
Types of the state disturbances in the model.
Integer vector stating whether Z
,H
,T
,R
or Q
is
time-varying (indicated by 1 in tv
and 0 otherwise).
If you manually change the dimensions of the matrices you must change this attribute also.
For arima component, a numeric vector containing the autoregressive coeffients.
For arima component, a numericvector containing the moving average coeffients.
For arima component, a degree of differencing.
For arima, cycle and seasonal component, a length(index)
.
For trend component, list of length degree
containing the
For arima component, logical value indicating whether a stationarity of the arima part is assumed. Defaults to TRUE.
A vector indicating for which series the corresponding components are constructed.
Length of the series, only used internally for dimensionality check.
A character vector giving the state names.
names of the times series, used internally.
For a custom component, system matrix or array of observation equation.
For a custom component, system matrix or array of transition equation.
For a custom and regression components, optional
Optional
Optional
Optional P1inf[i,i]>0
, corresponding
row and column of P1
should be zero.
For a cycle and seasonal components, the length of the cycle/seasonal pattern.
For cycle, seasonal, trend and regression components, character
string defining if "distinct"
or "common"
states are used for
different series.
A damping factor for cycle component. Defaults to 1. Note that there are no checks for the range of the factor.
An object of class formula
containing the
symbolic description of the model. The intercept term can be removed with
-1
as in lm
. In case of trend or differenced arima component the
intercept is removed automatically in order to keep the model identifiable.
See package vignette and examples in KFAS
for special functions
used in model construction.
An optional data frame, list or environment containing the variables in the model.
Covariance matrix or array of disturbance terms
Additional parameters for non-Gaussian models. See details in
KFAS
.
A vector of distributions of the observations. Default is
rep("gaussian", p)
, where p
is the number of series.
A tolerance parameter used in checking whether Finf
or F
is numerically zero.
Defaults to .Machine$double.eps^0.5
. If F < tol * max(abs(Z[Z > 0]))^2
,
then F is deemed to be zero (i.e. differences are due to numerical precision).
This has mostly effect only on determining when to end exact diffuse phase. Tweaking this
and/or scaling model parameters/observations can sometimes help with numerical issues.
For regression component, right hand side formula or list of of such formulas defining the custom regression part.
Remove intercept term from regression model. Default
is TRUE
. This tries to ensure that there are no extra intercept
terms in the model.
For seasonal component, character string defining whether to
use "dummy"
or "trigonometric"
form of the seasonal
component.
For univariate trigonometric seasonal, argument
harmonics
can be used to specify which subharmonics
are added to the model. Note that for multivariate model you can call
SSMseasonal
multiple times with different values of index
.
For trend component, integer defining the degree of the polynomial trend. 1 corresponds to local level, 2 for local linear trend and so forth.
Formula of the model can contain the usual regression part and additional
functions defining different types of components of the model, named as
SSMarima
, SSMcustom
, SSMcycle
, SSMregression
,
SSMseasonal
and SSMtrend
.
For more details, see package vignette (the mathematical notation is somewhat non-readable in ASCII).
artransform
KFAS
for more examples.
# add intercept to state equation by augmenting the state vector:
# diffuse initialization for the intercept, gets estimated like other states:
# for known fixed intercept, just set P1 = P1inf = 0 (default in SSMcustom).
intercept <- 0
model_int <- SSModel(Nile ~ SSMtrend(1, Q = 1469) +
SSMcustom(Z = 0, T = 1, Q = 0, a1 = intercept, P1inf = 1), H = 15099)
model_int$T
model_int$T[1, 2, 1] <- 1 # add the intercept value to level
out <- KFS(model_int)
# An example of a time-varying variance
model_drivers <- SSModel(log(cbind(front, rear)) ~ SSMtrend(1, Q = list(diag(2))),
data = Seatbelts, H = array(NA, c(2, 2, 192)))
ownupdatefn <- function(pars, model){
diag(model$Q[, , 1]) <- exp(pars[1:2])
model$H[,,1:169] <- diag(exp(pars[3:4])) # break in variance
model$H[,,170:192] <- diag(exp(pars[5:6]))
model
}
fit_drivers <- fitSSM(model_drivers, inits = rep(-1, 6),
updatefn = ownupdatefn, method = "BFGS")
fit_drivers$model$H[,,1]
fit_drivers$model$H[,,192]
# An example of shift in the level component
Tt <- array(diag(2), c(2, 2, 100))
Tt[1,2,28] <- 1
Z <- matrix(c(1,0), 1, 2)
Q <- diag(c(NA, 0), 2)
model <- SSModel(Nile ~ -1 + SSMcustom(Z, Tt, Q = Q, P1inf = diag(2)),
H = matrix(NA))
model <- fitSSM(model, c(10,10), method = "BFGS")$model
model$Q
model$H
conf_Nile <- predict(model, interval = "confidence", level = 0.9)
pred_Nile <- predict(model, interval = "prediction", level = 0.9)
ts.plot(cbind(Nile, pred_Nile, conf_Nile[, -1]), col = c(1:2, 3, 3, 4, 4),
ylab = "Predicted Annual flow", main = "River Nile")
# dynamic regression model
set.seed(1)
x1 <- rnorm(100)
x2 <- rnorm(100)
b1 <- 1 + cumsum(rnorm(100, sd = 1))
b2 <- 2 + cumsum(rnorm(100, sd = 0.1))
y <- 1 + b1 * x1 + b2 * x2 + rnorm(100, sd = 0.1)
model <- SSModel(y ~ SSMregression(~ x1 + x2, Q = diag(NA,2)), H = NA)
fit <- fitSSM(model, inits = c(0,0,0), method = "BFGS")
model <- fit$model
model$Q
model$H
out <- KFS(model)
ts.plot(out$alphahat[,-1], b1, b2, col = 1:4)
# SSMregression with multivariate observations
x <- matrix(rnorm(30), 10, 3) # one variable per each series
y <- x + rnorm(30)
model <- SSModel(y ~ SSMregression(list(~ X1, ~ X2, ~ X3), data = data.frame(x)))
# more generally SSMregression(sapply(1:3, function(i) formula(paste0("~ X",i))), ...)
# three covariates per series, with same coefficients:
y <- x[,1] + x[,2] + x[,3] + matrix(rnorm(30), 10, 3)
model <- SSModel(y ~ -1 + SSMregression(~ X1 + X2 + X3, remove.intercept = FALSE,
type = "common", data = data.frame(x)))
# the above cases can be combined in various ways, you can call SSMregression multiple times:
model <- SSModel(y ~ SSMregression(~ X1 + X2, type = "common") +
SSMregression(~ X2), data = data.frame(x))
# examples of using data argument
y <- x <- rep(1, 3)
data1 <- data.frame(x = rep(2, 3))
data2 <- data.frame(x = rep(3, 3))
f <- formula(~ -1 + x)
# With data missing the environment of formula is checked,
# and if not found in there a calling environment via parent.frame is checked.
c(SSModel(y ~ -1 + x)["Z"]) # 1
c(SSModel(y ~ -1 + x, data = data1)["Z"]) # 2
c(SSModel(y ~ -1 + SSMregression(~ -1 + x))["Z"]) # 1
c(SSModel(y ~ -1 + SSMregression(~ -1 + x, data = data1))["Z"]) # 2
c(SSModel(y ~ -1 + SSMregression(~ -1 + x), data = data1)["Z"]) # 2
SSModel(y ~ -1 + x + SSMregression(~ -1 + x, data = data1))["Z"] # 1 and 2
SSModel(y ~ -1 + x + SSMregression(~ -1 + x), data = data1)["Z"] # both are 2
SSModel(y ~ -1 + x + SSMregression(~ -1 + x, data = data1), data = data2)["Z"] # 3 and 2
SSModel(y ~ -1 + x + SSMregression(f))["Z"] # 1 and 1
SSModel(y ~ -1 + x + SSMregression(f), data = data1)["Z"] # 2 and 1
SSModel(y ~ -1 + x + SSMregression(f,data = data1))["Z"] # 1 and 2
rm(x)
c(SSModel(y ~ -1 + SSMregression(f, data = data1))$Z) # 2
if (FALSE) {
# This fails as there is no x in the environment of f
try(c(SSModel(y ~ -1 + SSMregression(f), data = data1)$Z))
}
Run the code above in your browser using DataLab