Ksmooth | R Documentation |
Returns the smoother values for various linear state space models. The predicted and filtered values and the likelihood at the given parameter values are also returned (via Kfilter
).
Ksmooth(y, A, mu0, Sigma0, Phi, sQ, sR, Ups = NULL, Gam = NULL,
input = NULL, S = NULL, version = 1)
y |
data matrix (n |
A |
can be constant or an array with dimension |
mu0 |
initial state mean vector (p |
Sigma0 |
initial state covariance matrix (p |
Phi |
state transition matrix (p |
sQ |
state error pre-matrix (see details) |
sR |
observation error pre-matrix (see details) |
Ups |
state input matrix (p |
Gam |
observation input matrix (q |
input |
NULL (default) if not needed or a
matrix (n |
S |
covariance matrix between state and observation errors; not necessary to specify if not needed and only used if version=2; see details |
version |
either 1 (default) or 2; version 2 allows for correlated errors |
The states x_t
are p-dimensional, the data y_t
are q-dimensional, and
the inputs u_t
are r-dimensional for t=1, \dots, n
. The initial state is x_0 \sim N(\mu_0, \Sigma_0)
.
The measurement matrices A_t
can be constant or time varying. If time varying, they should be entered as an array of dimension dim = c(q,p,n)
. Otherwise, just enter the constant value making sure it has the appropriate q \times p
dimension.
Version 1 (default): The general model is
x_t = \Phi x_{t-1} + \Upsilon u_{t} + sQ\, w_t \quad w_t \sim iid\ N(0,I)
y_t = A_t x_{t-1} + \Gamma u_{t} + sR\, v_t \quad v_t \sim iid\ N(0,I)
where w_t \perp v_t
. Consequently the state noise covariance matrix is
Q = sQ\, sQ'
and the observation noise covariance matrix is
R = sR\, sR'
and sQ, sR
do not have to be square as long as everything is
conformable. Notice the specification of the state and observation covariances has changed from the original scripts.
NOTE: If it is easier to model in terms of Q
and R
, simply input the square root matrices
sQ = Q %^% .5
and sR = R %^% .5
.
Version 2 (correlated errors): The general model is
x_{t+1} = \Phi x_{t} + \Upsilon u_{t+1} + sQ\, w_t \quad w_t \sim iid\ N(0,I)
y_t = A_t x_{t-1} + \Gamma u_{t} + sR\, v_t \quad v_t \sim iid\ N(0,I)
where S = {\rm Cov}(w_t, v_t)
, and NOT {\rm Cov}(sQ\, w_t, sR\, v_t)
.
NOTE: If it is easier to model in terms of Q
and R
, simply input the square root matrices
sQ = Q %^% .5
and sR = R %^% .5
.
Note that in either version, sQ\, w_t
has to be p-dimensional, but w_t
does not, and sR\, v_t
has to be q-dimensional, but v_t
does not.
Time varying values are returned as arrays.
Xs |
state smoothers |
Ps |
smoother mean square error |
X0n |
initial mean smoother |
P0n |
initial smoother covariance |
J0 |
initial value of the J matrix |
J |
the J matrices |
Xp |
state predictors |
Pp |
mean square prediction error |
Xf |
state filters |
Pf |
mean square filter error |
like |
negative of the log likelihood |
innov |
innovation series |
sig |
innovation covariances |
Kn |
the value of the last Gain |
D.S. Stoffer
You can find demonstrations of astsa capabilities at FUN WITH ASTSA.
The most recent version of the package can be found at https://github.com/nickpoison/astsa/.
In addition, the News and ChangeLog files are at https://github.com/nickpoison/astsa/blob/master/NEWS.md.
The webpages for the texts and some help on using R for time series analysis can be found at https://nickpoison.github.io/.
Kfilter
# generate some data
set.seed(1)
sQ = 1; sR = 3; n = 100
mu0 = 0; Sigma0 = 10; x0 = rnorm(1,mu0,Sigma0)
w = rnorm(n); v = rnorm(n)
x = c(x0 + sQ*w[1]); y = c(x[1] + sR*v[1]) # initialize
for (t in 2:n){
x[t] = x[t-1] + sQ*w[t]
y[t] = x[t] + sR*v[t]
}
# run and plot the smoother
run = Ksmooth(y, A=1, mu0, Sigma0, Phi=1, sQ, sR)
tsplot(cbind(x, y, Xs=run$Xs), spaghetti=TRUE, type='o', col=c(3,4,6), pch=c(NA,1,NA),
addLegend=TRUE, location='bottomright', lwd=c(2,1,2), gg=TRUE, margins=1)
# NOTE: CRAN tests need extra white space :( so margins=1 not necessary nor desired
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.