Fkfs: Fast Kalman filtering and smoothing of state space models...

Description Usage Arguments Details Value Author(s) See Also Examples

Description

Does Kalman filtering followed by smoothing using KFS for gaussian state space models otherwise by use of logLik.SSModel.

Usage

1
Fkfs(ss, tvar, offset = 1)

Arguments

ss

object of class ssm.

tvar

vector with four elements indicating the dynamic nature of matrices in the object ssm. The first element corresponds to ss$Fmat, the second to ss$Gmat, the third to ss$Vmat and the fourth to ss$Wmat. If tvar[i]==1 the corresponding matrix is constant, otherwise tvar[i] is set to ss$n.

offset

vector of length equal to length of observations.

Details

The Kalman filter and smoother in KFAS provides faster computations than kfilter and smoother, however requires alternative specification of the state space model. This function transform the object SSon which Kalman filtering followed by smoothing using KFS may be performed.

Value

List with two objects. The first object, kfas, is the output from KFS. The second object, ss, of class SS with updated m, C and loglik.

Author(s)

Anette Luther Christensen and Claus Dethlefsen.

See Also

Fkfilter

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
## Gaussian model to simulate observations from
m1 <- SS( Fmat = function(tt, x, phi) {
            Fmat      <- matrix(NA, nrow=3, ncol=1)
            Fmat[1,1] <- 1
            Fmat[2,1] <- cos(2*pi*tt/12)
            Fmat[3,1] <- sin(2*pi*tt/12)
            return(Fmat)
          },
          Gmat = function(tt, x, phi) {
            matrix(c(1,0,0,0,1,0,0,0,1), nrow=3)
          },
          Wmat = function(tt, x, phi) {
                    matrix(c(0.01,0,0,0,0.1,0,0,0,0.1), nrow=3)},
          Vmat = function(tt, x, phi) {matrix(1)},
          m0   = matrix(c(0,0,0), nrow=1),
          C0   = matrix(c(1,0,0,0,0.001,0,0,0,0.001), nrow=3, ncol=3)
        )

## Simulates 100 observation from m1
m1 <- recursion(m1, 100)

## Formulates model og class ssm
ssm1 <- ssm(m1$y ~ -1 + tvar(polytime(1:m1$n),1)
                      + tvar(polytrig(1:m1$n, 1)), 
                        family="gaussian", fit=FALSE)

#smoothed <- Fkfs(ssm1$ss, tvar=c(m1$n, 1, 1, 1))
smoothed <- kfs(ssm1$ss, tvar=c(m1$n, 1, 1, 1))


## Non Gaussian model
phi.start <- StructTS(log10(UKgas),type="BSM")$coef[c(4,1,2,3)]
gasmodel <- ssm( log10(UKgas) ~ -1+ tvar(polytime(time,1))+
tvar(sumseason(time,4)), phi=phi.start, fit=FALSE)

#smoothed <- Fkfs(gasmodel$ss,tvar=c(1,1,1,1))
smoothed <- kfs(gasmodel$ss,tvar=c(1,1,1,1))
## Trend plot
## Trend plot
#ts.plot( smoothed$kfas$alphahat[1,] )
## Season plot
#ts.plot( smoothed$kfas$alphahat[3,] )

ClausDethlefsen/sspir documentation built on May 6, 2019, 7 p.m.