Welcome State space notation Functions > simulate.ssm
Last update Jul 10, 2004

DESCRIPTION:

Simulates observations based on the specification in a ssm object.

USAGE:

      simulate.ssm(ssm)

REQUIRED ARGUMENTS:

ssm:
Object of class ssm (state space model).

OPTIONAL ARGUMENTS:

None

VALUE:

Returns an object of class ssm with updated attributes:
Yt:
Vector or matrix of simulated observations.
simulated:
List containing the simulated states ($theta) and signals ($lambda).

SIDE EFFECTS:

The function simulate.ssm causes creation of the dataset .Random.seed if it does not already exist, otherwise its value is updated.

DETAILS:

None.

REFERENCES:

Klein, (2003), State Space Models for Exponential Family Data, Ph.D. Thesis, Department of Statistics, University of Southern Denmark.

EXAMPLES:

# local level state space model
ss <- ssm(
          Ft = function(i, x, phi) {matrix(1,1,1)},
          Gt = function(i, x, phi) {matrix(1,1,1)},
          Vt = function(i, x, phi) {matrix(10,1,1)},
          Wt = function(i, x, phi) {matrix(80,1,1)},
          m0 = c(20),
          C0 = matrix(50,1,1))

# simulate latent process and observations
ss <- simulate.ssm(ss)

# plot like Figure 1.2, upper panel
par(mfrow=c(2,1))
plot(ss$simulated$theta, type="l", lty=3, ylim=range(ss$Yt))
points(ss$Yt)

# Exchange evolution and observation variances
ss$Vt <- function(i, x, phi) matrix(80,1,1)
ss$Wt <- function(i, x, phi) matrix(10,1,1)

# Remove observations
ss$Yt <- NA

# simulate latent process and observations
ss <- simulate.ssm(ss)

# plot like Figure 1.2, lower panel
plot(ss$simulated$theta, type="l", lty=3, ylim=range(ss$Yt))
points(ss$Yt)



# Dynamic proportional odds model
# note: that the number of trials varies, and
#       how phi is used to handle the variances
ss <- ssm(
      Ft = function(i,xt,phi) t(cbind(diag(5),-0.8)),
      Gt = function(i,xt,phi) diag(6),
      Wt = function(i,xt,phi) diag(c(rep(phi[1],5),phi[2])),
      phi = c(0.001,0.1),
      m0 = c(-2,-1,0,1,2,0),
      C0 = diag(6),
      nt = floor(runif(50,min=20,max=50)),
      fam= "multinomial", 
      link="pom")

ss <- simulate.ssm(ss)

# Produce plots like on Figure 5.3
plot(ss$simulated$theta[,6], type="l", 
     ylim=range(ss$simulated$theta))
for(i in 1:5)   lines(ss$simulated$theta[,i])

# Cumulate observations
tmp <- t(apply(ss$Yt,1,cumsum))/ss$nt
plot(tmp[,1], type="l", ylim=c(0,1))
for(i in 2:6) lines(tmp[,i])