R: Extracting the Partial Signal Of a State Space Model
signal
R Documentation
Extracting the Partial Signal Of a State Space Model
Description
Function signal returns the signal of a state space model using only
subset of states.
Usage
signal(object, states = "all", filtered = FALSE)
Arguments
object
Object of class KFS.
states
Which states are combined? Either a numeric vector containing
the indices of the corresponding states, or a character vector defining the
types of the corresponding states. Possible choices are
"all", "level", "slope",
"trend", "regression", "arima", "custom",
"cycle" or "seasonal", where "trend" extracts states relating to trend.
These can be combined. Default is "all".
filtered
If TRUE, filtered signal is used. Otherwise smoothed signal is
used.
Value
signal
Time series object of filtered signal Z[t]a[t] or
smoothed signal Z[t]α[t] using only the defined states.
variance
Cov(Z[t]a[t]) or Cov(Z[t]α[t]) using only the defined states.
For the covariance matrices of the filtered signal, only the non-diffuse part of P is used.
Examples
model <- SSModel(log(drivers) ~ SSMtrend(1, NA) +
SSMseasonal(12, sea.type = 'trigonometric', Q = NA) +
log(PetrolPrice) + law,data = Seatbelts, H = NA)
ownupdatefn <- function(pars,model,...){
model$H[] <- exp(pars[1])
diag(model$Q[,,1]) <- exp(c(pars[2], rep(pars[3], 11)))
model
}
fit <- fitSSM(inits = log(c(var(log(Seatbelts[,'drivers'])), 0.001, 0.0001)),
model = model, updatefn = ownupdatefn, method = 'BFGS')
out <- KFS(fit$model, smoothing = c('state', 'mean'))
ts.plot(cbind(out$model$y, fitted(out)),lty = 1:2, col = 1:2,
main = 'Observations and smoothed signal with and without seasonal component')
lines(signal(out, states = c('regression', 'trend'))$signal, col = 4, lty = 1)
legend('bottomleft',
legend = c('Observations', 'Smoothed signal','Smoothed level'),
col = c(1, 2, 4), lty = c(1, 2, 1))
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(KFAS)
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/KFAS/signal.Rd_%03d_medium.png", width=480, height=480)
> ### Name: signal
> ### Title: Extracting the Partial Signal Of a State Space Model
> ### Aliases: signal
>
> ### ** Examples
>
> model <- SSModel(log(drivers) ~ SSMtrend(1, NA) +
+ SSMseasonal(12, sea.type = 'trigonometric', Q = NA) +
+ log(PetrolPrice) + law,data = Seatbelts, H = NA)
>
> ownupdatefn <- function(pars,model,...){
+ model$H[] <- exp(pars[1])
+ diag(model$Q[,,1]) <- exp(c(pars[2], rep(pars[3], 11)))
+ model
+ }
>
> fit <- fitSSM(inits = log(c(var(log(Seatbelts[,'drivers'])), 0.001, 0.0001)),
+ model = model, updatefn = ownupdatefn, method = 'BFGS')
>
> out <- KFS(fit$model, smoothing = c('state', 'mean'))
> ts.plot(cbind(out$model$y, fitted(out)),lty = 1:2, col = 1:2,
+ main = 'Observations and smoothed signal with and without seasonal component')
> lines(signal(out, states = c('regression', 'trend'))$signal, col = 4, lty = 1)
> legend('bottomleft',
+ legend = c('Observations', 'Smoothed signal','Smoothed level'),
+ col = c(1, 2, 4), lty = c(1, 2, 1))
>
>
>
>
>
>
> dev.off()
null device
1
>