| plot.fkf {FKF} | R Documentation |
Plotting method for objects of class fkf. This function
provides tools for graphical analysis of the Kalman filter output:
Visualization of the state vector, QQ-plot of the individual
residuals, QQ-plot of the Mahalanobis distance, auto- as well as
crosscorrelation function of the residuals.
## S3 method for class 'fkf':
plot(x, type = c("state", "resid.qq", "qqchisq", "acf"),
CI = 0.95, at.idx = 1:nrow(x$at), att.idx = 1:nrow(x$att), ...)
x |
The output of fkf. |
type |
A string stating what shall be plotted (see Details). |
CI |
The confidence interval in case type == "state". Set
CI to NA if no confidence interval shall be plotted. |
at.idx |
An vector giving the indexes of the predicted state variables
which shall be plotted if type == "state". |
att.idx |
An vector giving the indexes of the filtered state variables
which shall be plotted if type == "state". |
... |
Arguments passed to either plot,
qqnorm, qqplot or acf. |
The argument type states what shall be plotted. type
must partially match one of the following:
stateat.idx and att.idx, the user can specify
which of the predicted (at) and filtered
(att) state variables will be drawn.resid.qqvt.qqchisqacfacf) on the diagonal panels and the
crosscorrelation function (ccf) of the residuals on the
off-diagnoal panels.Invisibly returns an list with components:
distance | The Mahalanobis distance of the residuals as a vector of length n. |
std.resid | The standardized residuals as an d * n-matrix. It should hold that std.resid[i,j] iid N_d(0, 1), |
David Luethi, Philipp Erb, Simon Otziger
## This example shows how to filter a series by an AR(2) filter
ar1 <- -0.44
ar2 <- 0.4
sigma <- .4
## Sample from an AR(2) process and put it into the Kalman filter
a <- arima.sim(model = list(ar = c(ar1, ar2)),
n = 60, innov = rnorm(60) * sigma)
## Set up the state space representation for an AR(2) model:
## Transition equation:
## alpha[, t + 1] = matrix(ar1, 1, ar2, 0, ncol = 2) * alpha[,t] + e[t]
## Measurement equation:
## y[,t] = c(1, 0) * a[,t] */
## Initial values:
## a0 = c(0, 0), P0 = diag(c(10, 10))
ans <- fkf(a0 = c(0, 0), P0 = diag(c(10, 10)),
dt = rbind(0, 0), ct = matrix(0),
Tt = matrix(c(ar1, 1, ar2, 0), ncol = 2),
Zt = cbind(1, 0),
HHt = matrix(c(sigma^2, 0, 0, 0), ncol = 2),
GGt = matrix(0), yt = rbind(a))
## Compared the filtered series with the simulated AR(2) series:
plot(ans, att.idx = 1, at.idx = NA)
lines(a, lty = "dotted")
## Compared the predicted series with the simulated AR(2) series.
## Additionally, a 99
plot(ans, att.idx = NA, at.idx = 1, CI = .99)
lines(a, lty = "dotted")
## Check the residuals for normality
plot(ans, type = "resid.qq")
## Test for autocorrelation
plot(ans, type = "acf")