Package 'simStateSpace'

Title: Simulate Data from State Space Models
Description: Provides a streamlined and user-friendly framework for simulating data in state space models, particularly when the number of subjects/units (n) exceeds one, a scenario commonly encountered in social and behavioral sciences. For an introduction to state space models in social and behavioral sciences, refer to Chow, Ho, Hamaker, and Dolan (2010) <doi:10.1080/10705511003661553>.
Authors: Ivan Jacob Agaloos Pesigan [aut, cre, cph]
Maintainer: Ivan Jacob Agaloos Pesigan <[email protected]>
License: GPL (>= 3)
Version: 1.2.2.9000
Built: 2024-11-21 05:51:10 UTC
Source: https://github.com/jeksterslab/simStateSpace

Help Index


Coerce an Object of Class simstatespace to a Data Frame

Description

Coerce an Object of Class simstatespace to a Data Frame

Usage

## S3 method for class 'simstatespace'
as.data.frame(
  x,
  row.names = NULL,
  optional = FALSE,
  eta = FALSE,
  long = TRUE,
  ...
)

Arguments

x

Object of class simstatespace.

row.names

NULL or character vector giving the row names for the data frame. Missing values are not allowed.

optional

Logical. If TRUE, setting row names and converting column names is optional.

eta

Logical. If eta = TRUE, include eta. If eta = FALSE, exclude eta.

long

Logical. If long = TRUE, use long format. If long = FALSE, use wide format.

...

Additional arguments.

Author(s)

Ivan Jacob Agaloos Pesigan

Examples

# prepare parameters
set.seed(42)
## number of individuals
n <- 5
## time points
time <- 50
## dynamic structure
p <- 3
mu0 <- rep(x = 0, times = p)
sigma0 <- diag(p)
sigma0_l <- t(chol(sigma0))
alpha <- rep(x = 0, times = p)
beta <- 0.50 * diag(p)
psi <- diag(p)
psi_l <- t(chol(psi))
## measurement model
k <- 3
nu <- rep(x = 0, times = k)
lambda <- diag(k)
theta <- 0.50 * diag(k)
theta_l <- t(chol(theta))
## covariates
j <- 2
x <- lapply(
  X = seq_len(n),
  FUN = function(i) {
    matrix(
      data = stats::rnorm(n = time * j),
      nrow = j,
      ncol = time
    )
  }
)
gamma <- diag(x = 0.10, nrow = p, ncol = j)
kappa <- diag(x = 0.10, nrow = k, ncol = j)

# Type 0
ssm <- SimSSMFixed(
  n = n,
  time = time,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  alpha = alpha,
  beta = beta,
  psi_l = psi_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 0
)

head(as.data.frame(ssm))
head(as.data.frame(ssm, long = FALSE))

# Type 1
ssm <- SimSSMFixed(
  n = n,
  time = time,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  alpha = alpha,
  beta = beta,
  psi_l = psi_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 1,
  x = x,
  gamma = gamma
)

head(as.data.frame(ssm))
head(as.data.frame(ssm, long = FALSE))

# Type 2
ssm <- SimSSMFixed(
  n = n,
  time = time,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  alpha = alpha,
  beta = beta,
  psi_l = psi_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 2,
  x = x,
  gamma = gamma,
  kappa = kappa
)

head(as.data.frame(ssm))
head(as.data.frame(ssm, long = FALSE))

Coerce an Object of Class simstatespace to a Matrix

Description

Coerce an Object of Class simstatespace to a Matrix

Usage

## S3 method for class 'simstatespace'
as.matrix(x, eta = FALSE, long = TRUE, ...)

Arguments

x

Object of class simstatespace.

eta

Logical. If eta = TRUE, include eta. If eta = FALSE, exclude eta.

long

Logical. If long = TRUE, use long format. If long = FALSE, use wide format.

...

Additional arguments.

Author(s)

Ivan Jacob Agaloos Pesigan

Examples

# prepare parameters
set.seed(42)
## number of individuals
n <- 5
## time points
time <- 50
## dynamic structure
p <- 3
mu0 <- rep(x = 0, times = p)
sigma0 <- diag(p)
sigma0_l <- t(chol(sigma0))
alpha <- rep(x = 0, times = p)
beta <- 0.50 * diag(p)
psi <- diag(p)
psi_l <- t(chol(psi))
## measurement model
k <- 3
nu <- rep(x = 0, times = k)
lambda <- diag(k)
theta <- 0.50 * diag(k)
theta_l <- t(chol(theta))
## covariates
j <- 2
x <- lapply(
  X = seq_len(n),
  FUN = function(i) {
    matrix(
      data = stats::rnorm(n = time * j),
      nrow = j,
      ncol = time
    )
  }
)
gamma <- diag(x = 0.10, nrow = p, ncol = j)
kappa <- diag(x = 0.10, nrow = k, ncol = j)

# Type 0
ssm <- SimSSMFixed(
  n = n,
  time = time,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  alpha = alpha,
  beta = beta,
  psi_l = psi_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 0
)

head(as.matrix(ssm))
head(as.matrix(ssm, long = FALSE))

# Type 1
ssm <- SimSSMFixed(
  n = n,
  time = time,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  alpha = alpha,
  beta = beta,
  psi_l = psi_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 1,
  x = x,
  gamma = gamma
)

head(as.matrix(ssm))
head(as.matrix(ssm, long = FALSE))

# Type 2
ssm <- SimSSMFixed(
  n = n,
  time = time,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  alpha = alpha,
  beta = beta,
  psi_l = psi_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 2,
  x = x,
  gamma = gamma,
  kappa = kappa
)

head(as.matrix(ssm))
head(as.matrix(ssm, long = FALSE))

Convert Parameters from the Linear Stochastic Differential Equation Model to State Space Model Parameterization

Description

This function converts parameters from the linear stochastic differential equation model to state space model parameterization.

Usage

LinSDE2SSM(iota, phi, sigma_l, delta_t)

Arguments

iota

Numeric vector. An unobserved term that is constant over time (ι\boldsymbol{\iota}).

phi

Numeric matrix. The drift matrix which represents the rate of change of the solution in the absence of any random fluctuations (Φ\boldsymbol{\Phi}).

sigma_l

Numeric matrix. Cholesky factorization (t(chol(sigma))) of the covariance matrix of volatility or randomness in the process (Σ\boldsymbol{\Sigma}).

delta_t

Numeric. Time interval (Δt\Delta_t).

Details

Let the linear stochastic equation model be given by

dηi,t=(ι+Φηi,t)dt+Σ12dWi,t\mathrm{d} \boldsymbol{\eta}_{i, t} = \left( \boldsymbol{\iota} + \boldsymbol{\Phi} \boldsymbol{\eta}_{i, t} \right) \mathrm{d} t + \boldsymbol{\Sigma}^{\frac{1}{2}} \mathrm{d} \mathbf{W}_{i, t}

for individual ii and time tt. The discrete-time state space model given below represents the discrete-time solution for the linear stochastic differential equation.

ηi,tli=αΔtli+βΔtliηi,tli1+ζi,tli,withζi,tliN(0,ΨΔtli)\boldsymbol{\eta}_{i, t_{{l_{i}}}} = \boldsymbol{\alpha}_{\Delta t_{{l_{i}}}} + \boldsymbol{\beta}_{\Delta t_{{l_{i}}}} \boldsymbol{\eta}_{i, t_{l_{i} - 1}} + \boldsymbol{\zeta}_{i, t_{{l_{i}}}}, \quad \mathrm{with} \quad \boldsymbol{\zeta}_{i, t_{{l_{i}}}} \sim \mathcal{N} \left( \mathbf{0}, \boldsymbol{\Psi}_{\Delta t_{{l_{i}}}} \right)

with

βΔtli=exp(ΔtΦ),\boldsymbol{\beta}_{\Delta t_{{l_{i}}}} = \exp{ \left( \Delta t \boldsymbol{\Phi} \right) },

αΔtli=Φ1(βIp)ι,and\boldsymbol{\alpha}_{\Delta t_{{l_{i}}}} = \boldsymbol{\Phi}^{-1} \left( \boldsymbol{\beta} - \mathbf{I}_{p} \right) \boldsymbol{\iota}, \quad \mathrm{and}

vec(ΨΔtli)=[(ΦIp)+(IpΦ)][exp([(ΦIp)+(IpΦ)]Δt)Ip×p]vec(Σ)\mathrm{vec} \left( \boldsymbol{\Psi}_{\Delta t_{{l_{i}}}} \right) = \left[ \left( \boldsymbol{\Phi} \otimes \mathbf{I}_{p} \right) + \left( \mathbf{I}_{p} \otimes \boldsymbol{\Phi} \right) \right] \left[ \exp \left( \left[ \left( \boldsymbol{\Phi} \otimes \mathbf{I}_{p} \right) + \left( \mathbf{I}_{p} \otimes \boldsymbol{\Phi} \right) \right] \Delta t \right) - \mathbf{I}_{p \times p} \right] \mathrm{vec} \left( \boldsymbol{\Sigma} \right)

where tt denotes continuous-time processes that can be defined by any arbitrary time point, tlit_{l_{i}} the lthl^\mathrm{th} observed measurement occassion for individual ii, pp the number of latent variables and Δt\Delta t the time interval.

Value

Returns a list of state space parameters:

  • alpha: Numeric vector. Vector of constant values for the dynamic model (α\boldsymbol{\alpha}).

  • beta: Numeric matrix. Transition matrix relating the values of the latent variables from the previous time point to the current time point. (β\boldsymbol{\beta}).

  • psi_l: Numeric matrix. Cholesky factorization (t(chol(psi))) of the process noise covariance matrix Ψ\boldsymbol{\Psi}.

Author(s)

Ivan Jacob Agaloos Pesigan

References

Harvey, A. C. (1990). Forecasting, structural time series models and the Kalman filter. Cambridge University Press. doi:10.1017/cbo9781107049994

See Also

Other Simulation of State Space Models Data Functions: SimBetaN(), SimPhiN(), SimSSMFixed(), SimSSMIVary(), SimSSMLinGrowth(), SimSSMLinGrowthIVary(), SimSSMLinSDEFixed(), SimSSMLinSDEIVary(), SimSSMOUFixed(), SimSSMOUIVary(), SimSSMVARFixed(), SimSSMVARIVary(), TestPhi(), TestStability(), TestStationarity()

Examples

p <- 2
iota <- c(0.317, 0.230)
phi <- matrix(
  data = c(
   -0.10,
   0.05,
   0.05,
   -0.10
 ),
 nrow = p
)
sigma <- matrix(
  data = c(
    2.79,
    0.06,
    0.06,
    3.27
  ),
  nrow = p
)
sigma_l <- t(chol(sigma))
delta_t <- 0.10

LinSDE2SSM(
  iota = iota,
  phi = phi,
  sigma_l = sigma_l,
  delta_t = delta_t
)

Plot Method for an Object of Class simstatespace

Description

Plot Method for an Object of Class simstatespace

Usage

## S3 method for class 'simstatespace'
plot(x, id = NULL, time = NULL, eta = FALSE, type = "b", ...)

Arguments

x

Object of class simstatespace.

id

Numeric vector. Optional id numbers to plot. If id = NULL, plot all available data.

time

Numeric vector. Optional time points to plot. If time = NULL, plot all available data.

eta

Logical. If eta = TRUE, plot the latent variables. If eta = FALSE, plot the observed variables.

type

Character indicating the type of plotting; actually any of the types as in plot.default().

...

Additional arguments.

Author(s)

Ivan Jacob Agaloos Pesigan

Examples

# prepare parameters
set.seed(42)
## number of individuals
n <- 5
## time points
time <- 50
## dynamic structure
p <- 3
mu0 <- rep(x = 0, times = p)
sigma0 <- diag(p)
sigma0_l <- t(chol(sigma0))
alpha <- rep(x = 0, times = p)
beta <- 0.50 * diag(p)
psi <- diag(p)
psi_l <- t(chol(psi))
## measurement model
k <- 3
nu <- rep(x = 0, times = k)
lambda <- diag(k)
theta <- 0.50 * diag(k)
theta_l <- t(chol(theta))
## covariates
j <- 2
x <- lapply(
  X = seq_len(n),
  FUN = function(i) {
    matrix(
      data = stats::rnorm(n = time * j),
      nrow = j,
      ncol = time
    )
  }
)
gamma <- diag(x = 0.10, nrow = p, ncol = j)
kappa <- diag(x = 0.10, nrow = k, ncol = j)

# Type 0
ssm <- SimSSMFixed(
  n = n,
  time = time,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  alpha = alpha,
  beta = beta,
  psi_l = psi_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 0
)

plot(ssm)
plot(ssm, id = 1:3, time = 0:9)

# Type 1
ssm <- SimSSMFixed(
  n = n,
  time = time,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  alpha = alpha,
  beta = beta,
  psi_l = psi_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 1,
  x = x,
  gamma = gamma
)

plot(ssm)
plot(ssm, id = 1:3, time = 0:9)

# Type 2
ssm <- SimSSMFixed(
  n = n,
  time = time,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  alpha = alpha,
  beta = beta,
  psi_l = psi_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 2,
  x = x,
  gamma = gamma,
  kappa = kappa
)

plot(ssm)
plot(ssm, id = 1:3, time = 0:9)

Print Method for an Object of Class simstatespace

Description

Print Method for an Object of Class simstatespace

Usage

## S3 method for class 'simstatespace'
print(x, ...)

Arguments

x

Object of Class simstatespace.

...

Additional arguments.

Value

Prints simulated data in long format.

Author(s)

Ivan Jacob Agaloos Pesigan

Examples

# prepare parameters
set.seed(42)
## number of individuals
n <- 5
## time points
time <- 50
## dynamic structure
p <- 3
mu0 <- rep(x = 0, times = p)
sigma0 <- diag(p)
sigma0_l <- t(chol(sigma0))
alpha <- rep(x = 0, times = p)
beta <- 0.50 * diag(p)
psi <- diag(p)
psi_l <- t(chol(psi))
## measurement model
k <- 3
nu <- rep(x = 0, times = k)
lambda <- diag(k)
theta <- 0.50 * diag(k)
theta_l <- t(chol(theta))
## covariates
j <- 2
x <- lapply(
  X = seq_len(n),
  FUN = function(i) {
    matrix(
      data = stats::rnorm(n = time * j),
      nrow = j,
      ncol = time
    )
  }
)
gamma <- diag(x = 0.10, nrow = p, ncol = j)
kappa <- diag(x = 0.10, nrow = k, ncol = j)

# Type 0
ssm <- SimSSMFixed(
  n = n,
  time = time,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  alpha = alpha,
  beta = beta,
  psi_l = psi_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 0
)

print(ssm)

# Type 1
ssm <- SimSSMFixed(
  n = n,
  time = time,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  alpha = alpha,
  beta = beta,
  psi_l = psi_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 1,
  x = x,
  gamma = gamma
)

print(ssm)

# Type 2
ssm <- SimSSMFixed(
  n = n,
  time = time,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  alpha = alpha,
  beta = beta,
  psi_l = psi_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 2,
  x = x,
  gamma = gamma,
  kappa = kappa
)

print(ssm)

Simulate Transition Matrices from the Multivariate Normal Distribution

Description

This function simulates random transition matrices from the multivariate normal distribution. The function ensures that the generated transition matrices are stationary using TestStationarity().

Usage

SimBetaN(n, beta, vcov_beta_vec_l)

Arguments

n

Positive integer. Number of replications.

beta

Numeric matrix. The transition matrix (β\boldsymbol{\beta}).

vcov_beta_vec_l

Numeric matrix. Cholesky factorization (t(chol(vcov_beta_vec))) of the sampling variance-covariance matrix vec(β)\mathrm{vec} \left( \boldsymbol{\beta} \right).

Author(s)

Ivan Jacob Agaloos Pesigan

See Also

Other Simulation of State Space Models Data Functions: LinSDE2SSM(), SimPhiN(), SimSSMFixed(), SimSSMIVary(), SimSSMLinGrowth(), SimSSMLinGrowthIVary(), SimSSMLinSDEFixed(), SimSSMLinSDEIVary(), SimSSMOUFixed(), SimSSMOUIVary(), SimSSMVARFixed(), SimSSMVARIVary(), TestPhi(), TestStability(), TestStationarity()

Examples

n <- 10
beta <- matrix(
  data = c(
    0.7, 0.5, -0.1,
    0.0, 0.6, 0.4,
    0, 0, 0.5
  ),
  nrow = 3
)
vcov_beta_vec_l <- t(chol(0.001 * diag(9)))
SimBetaN(n = n, beta = beta, vcov_beta_vec_l = vcov_beta_vec_l)

Simulate Random Drift Matrices from the Multivariate Normal Distribution

Description

This function simulates random drift matrices from the multivariate normal distribution. The function ensures that the generated drift matrices are stable using TestPhi().

Usage

SimPhiN(n, phi, vcov_phi_vec_l)

Arguments

n

Positive integer. Number of replications.

phi

Numeric matrix. The drift matrix (Φ\boldsymbol{\Phi}).

vcov_phi_vec_l

Numeric matrix. Cholesky factorization (t(chol(vcov_phi_vec))) of the sampling variance-covariance matrix vec(Φ)\mathrm{vec} \left( \boldsymbol{\Phi} \right).

Author(s)

Ivan Jacob Agaloos Pesigan

See Also

Other Simulation of State Space Models Data Functions: LinSDE2SSM(), SimBetaN(), SimSSMFixed(), SimSSMIVary(), SimSSMLinGrowth(), SimSSMLinGrowthIVary(), SimSSMLinSDEFixed(), SimSSMLinSDEIVary(), SimSSMOUFixed(), SimSSMOUIVary(), SimSSMVARFixed(), SimSSMVARIVary(), TestPhi(), TestStability(), TestStationarity()

Examples

n <- 10
phi <- matrix(
  data = c(
    -0.357, 0.771, -0.450,
    0.0, -0.511, 0.729,
    0, 0, -0.693
  ),
  nrow = 3
)
vcov_phi_vec_l <- t(chol(0.001 * diag(9)))
SimPhiN(n = n, phi = phi, vcov_phi_vec_l = vcov_phi_vec_l)

Simulate Data from a State Space Model (Fixed Parameters)

Description

This function simulates data using a state space model. It assumes that the parameters remain constant across individuals and over time.

Usage

SimSSMFixed(
  n,
  time,
  delta_t = 1,
  mu0,
  sigma0_l,
  alpha,
  beta,
  psi_l,
  nu,
  lambda,
  theta_l,
  type = 0,
  x = NULL,
  gamma = NULL,
  kappa = NULL
)

Arguments

n

Positive integer. Number of individuals.

time

Positive integer. Number of time points.

delta_t

Numeric. Time interval. The default value is 1.0 with an option to use a numeric value for the discretized state space model parameterization of the linear stochastic differential equation model.

mu0

Numeric vector. Mean of initial latent variable values (μη0\boldsymbol{\mu}_{\boldsymbol{\eta} \mid 0}).

sigma0_l

Numeric matrix. Cholesky factorization (t(chol(sigma0))) of the covariance matrix of initial latent variable values (Ση0\boldsymbol{\Sigma}_{\boldsymbol{\eta} \mid 0}).

alpha

Numeric vector. Vector of constant values for the dynamic model (α\boldsymbol{\alpha}).

beta

Numeric matrix. Transition matrix relating the values of the latent variables at the previous to the current time point (β\boldsymbol{\beta}).

psi_l

Numeric matrix. Cholesky factorization (t(chol(psi))) of the covariance matrix of the process noise (Ψ\boldsymbol{\Psi}).

nu

Numeric vector. Vector of intercept values for the measurement model (ν\boldsymbol{\nu}).

lambda

Numeric matrix. Factor loading matrix linking the latent variables to the observed variables (Λ\boldsymbol{\Lambda}).

theta_l

Numeric matrix. Cholesky factorization (t(chol(theta))) of the covariance matrix of the measurement error (Θ\boldsymbol{\Theta}).

type

Integer. State space model type. See Details for more information.

x

List. Each element of the list is a matrix of covariates for each individual i in n. The number of columns in each matrix should be equal to time.

gamma

Numeric matrix. Matrix linking the covariates to the latent variables at current time point (Γ\boldsymbol{\Gamma}).

kappa

Numeric matrix. Matrix linking the covariates to the observed variables at current time point (κ\boldsymbol{\kappa}).

Details

Type 0

The measurement model is given by

yi,t=ν+Ληi,t+εi,t,withεi,tN(0,Θ)\mathbf{y}_{i, t} = \boldsymbol{\nu} + \boldsymbol{\Lambda} \boldsymbol{\eta}_{i, t} + \boldsymbol{\varepsilon}_{i, t}, \quad \mathrm{with} \quad \boldsymbol{\varepsilon}_{i, t} \sim \mathcal{N} \left( \mathbf{0}, \boldsymbol{\Theta} \right)

where yi,t\mathbf{y}_{i, t}, ηi,t\boldsymbol{\eta}_{i, t}, and εi,t\boldsymbol{\varepsilon}_{i, t} are random variables and ν\boldsymbol{\nu}, Λ\boldsymbol{\Lambda}, and Θ\boldsymbol{\Theta} are model parameters. yi,t\mathbf{y}_{i, t} represents a vector of observed random variables, ηi,t\boldsymbol{\eta}_{i, t} a vector of latent random variables, and εi,t\boldsymbol{\varepsilon}_{i, t} a vector of random measurement errors, at time tt and individual ii. ν\boldsymbol{\nu} denotes a vector of intercepts, Λ\boldsymbol{\Lambda} a matrix of factor loadings, and Θ\boldsymbol{\Theta} the covariance matrix of ε\boldsymbol{\varepsilon}.

An alternative representation of the measurement error is given by

εi,t=Θ12zi,t,withzi,tN(0,I)\boldsymbol{\varepsilon}_{i, t} = \boldsymbol{\Theta}^{\frac{1}{2}} \mathbf{z}_{i, t}, \quad \mathrm{with} \quad \mathbf{z}_{i, t} \sim \mathcal{N} \left( \mathbf{0}, \mathbf{I} \right)

where zi,t\mathbf{z}_{i, t} is a vector of independent standard normal random variables and (Θ12)(Θ12)=Θ.\left( \boldsymbol{\Theta}^{\frac{1}{2}} \right) \left( \boldsymbol{\Theta}^{\frac{1}{2}} \right)^{\prime} = \boldsymbol{\Theta} .

The dynamic structure is given by

ηi,t=α+βηi,t1+ζi,t,withζi,tN(0,Ψ)\boldsymbol{\eta}_{i, t} = \boldsymbol{\alpha} + \boldsymbol{\beta} \boldsymbol{\eta}_{i, t - 1} + \boldsymbol{\zeta}_{i, t}, \quad \mathrm{with} \quad \boldsymbol{\zeta}_{i, t} \sim \mathcal{N} \left( \mathbf{0}, \boldsymbol{\Psi} \right)

where ηi,t\boldsymbol{\eta}_{i, t}, ηi,t1\boldsymbol{\eta}_{i, t - 1}, and ζi,t\boldsymbol{\zeta}_{i, t} are random variables, and α\boldsymbol{\alpha}, β\boldsymbol{\beta}, and Ψ\boldsymbol{\Psi} are model parameters. Here, ηi,t\boldsymbol{\eta}_{i, t} is a vector of latent variables at time tt and individual ii, ηi,t1\boldsymbol{\eta}_{i, t - 1} represents a vector of latent variables at time t1t - 1 and individual ii, and ζi,t\boldsymbol{\zeta}_{i, t} represents a vector of dynamic noise at time tt and individual ii. α\boldsymbol{\alpha} denotes a vector of intercepts, β\boldsymbol{\beta} a matrix of autoregression and cross regression coefficients, and Ψ\boldsymbol{\Psi} the covariance matrix of ζi,t\boldsymbol{\zeta}_{i, t}.

An alternative representation of the dynamic noise is given by

ζi,t=Ψ12zi,t,withzi,tN(0,I)\boldsymbol{\zeta}_{i, t} = \boldsymbol{\Psi}^{\frac{1}{2}} \mathbf{z}_{i, t}, \quad \mathrm{with} \quad \mathbf{z}_{i, t} \sim \mathcal{N} \left( \mathbf{0}, \mathbf{I} \right)

where (Ψ12)(Ψ12)=Ψ.\left( \boldsymbol{\Psi}^{\frac{1}{2}} \right) \left( \boldsymbol{\Psi}^{\frac{1}{2}} \right)^{\prime} = \boldsymbol{\Psi} .

Type 1

The measurement model is given by

yi,t=ν+Ληi,t+εi,t,withεi,tN(0,Θ).\mathbf{y}_{i, t} = \boldsymbol{\nu} + \boldsymbol{\Lambda} \boldsymbol{\eta}_{i, t} + \boldsymbol{\varepsilon}_{i, t}, \quad \mathrm{with} \quad \boldsymbol{\varepsilon}_{i, t} \sim \mathcal{N} \left( \mathbf{0}, \boldsymbol{\Theta} \right) .

The dynamic structure is given by

ηi,t=α+βηi,t1+Γxi,t+ζi,t,withζi,tN(0,Ψ)\boldsymbol{\eta}_{i, t} = \boldsymbol{\alpha} + \boldsymbol{\beta} \boldsymbol{\eta}_{i, t - 1} + \boldsymbol{\Gamma} \mathbf{x}_{i, t} + \boldsymbol{\zeta}_{i, t}, \quad \mathrm{with} \quad \boldsymbol{\zeta}_{i, t} \sim \mathcal{N} \left( \mathbf{0}, \boldsymbol{\Psi} \right)

where xi,t\mathbf{x}_{i, t} represents a vector of covariates at time tt and individual ii, and Γ\boldsymbol{\Gamma} the coefficient matrix linking the covariates to the latent variables.

Type 2

The measurement model is given by

yi,t=ν+Ληi,t+κxi,t+εi,t,withεi,tN(0,Θ)\mathbf{y}_{i, t} = \boldsymbol{\nu} + \boldsymbol{\Lambda} \boldsymbol{\eta}_{i, t} + \boldsymbol{\kappa} \mathbf{x}_{i, t} + \boldsymbol{\varepsilon}_{i, t}, \quad \mathrm{with} \quad \boldsymbol{\varepsilon}_{i, t} \sim \mathcal{N} \left( \mathbf{0}, \boldsymbol{\Theta} \right)

where κ\boldsymbol{\kappa} represents the coefficient matrix linking the covariates to the observed variables.

The dynamic structure is given by

ηi,t=α+βηi,t1+Γxi,t+ζi,t,withζi,tN(0,Ψ).\boldsymbol{\eta}_{i, t} = \boldsymbol{\alpha} + \boldsymbol{\beta} \boldsymbol{\eta}_{i, t - 1} + \boldsymbol{\Gamma} \mathbf{x}_{i, t} + \boldsymbol{\zeta}_{i, t}, \quad \mathrm{with} \quad \boldsymbol{\zeta}_{i, t} \sim \mathcal{N} \left( \mathbf{0}, \boldsymbol{\Psi} \right) .

Value

Returns an object of class simstatespace which is a list with the following elements:

  • call: Function call.

  • args: Function arguments.

  • data: Generated data which is a list of length n. Each element of data is a list with the following elements:

    • id: A vector of ID numbers with length l, where l is the value of the function argument time.

    • time: A vector time points of length l.

    • y: A l by k matrix of values for the manifest variables.

    • eta: A l by p matrix of values for the latent variables.

    • x: A l by j matrix of values for the covariates (when covariates are included).

  • fun: Function used.

Author(s)

Ivan Jacob Agaloos Pesigan

References

Chow, S.-M., Ho, M. R., Hamaker, E. L., & Dolan, C. V. (2010). Equivalence and differences between structural equation modeling and state-space modeling techniques. Structural Equation Modeling: A Multidisciplinary Journal, 17(2), 303–332. doi:10.1080/10705511003661553

See Also

Other Simulation of State Space Models Data Functions: LinSDE2SSM(), SimBetaN(), SimPhiN(), SimSSMIVary(), SimSSMLinGrowth(), SimSSMLinGrowthIVary(), SimSSMLinSDEFixed(), SimSSMLinSDEIVary(), SimSSMOUFixed(), SimSSMOUIVary(), SimSSMVARFixed(), SimSSMVARIVary(), TestPhi(), TestStability(), TestStationarity()

Examples

# prepare parameters
set.seed(42)
## number of individuals
n <- 5
## time points
time <- 50
## dynamic structure
p <- 3
mu0 <- rep(x = 0, times = p)
sigma0 <- 0.001 * diag(p)
sigma0_l <- t(chol(sigma0))
alpha <- rep(x = 0, times = p)
beta <- 0.50 * diag(p)
psi <- 0.001 * diag(p)
psi_l <- t(chol(psi))
## measurement model
k <- 3
nu <- rep(x = 0, times = k)
lambda <- diag(k)
theta <- 0.001 * diag(k)
theta_l <- t(chol(theta))
## covariates
j <- 2
x <- lapply(
  X = seq_len(n),
  FUN = function(i) {
    matrix(
      data = stats::rnorm(n = time * j),
      nrow = j,
      ncol = time
    )
  }
)
gamma <- diag(x = 0.10, nrow = p, ncol = j)
kappa <- diag(x = 0.10, nrow = k, ncol = j)

# Type 0
ssm <- SimSSMFixed(
  n = n,
  time = time,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  alpha = alpha,
  beta = beta,
  psi_l = psi_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 0
)

plot(ssm)

# Type 1
ssm <- SimSSMFixed(
  n = n,
  time = time,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  alpha = alpha,
  beta = beta,
  psi_l = psi_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 1,
  x = x,
  gamma = gamma
)

plot(ssm)

# Type 2
ssm <- SimSSMFixed(
  n = n,
  time = time,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  alpha = alpha,
  beta = beta,
  psi_l = psi_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 2,
  x = x,
  gamma = gamma,
  kappa = kappa
)

plot(ssm)

Simulate Data from a State Space Model (Individual-Varying Parameters)

Description

This function simulates data using a state space model. It assumes that the parameters can vary across individuals.

Usage

SimSSMIVary(
  n,
  time,
  delta_t = 1,
  mu0,
  sigma0_l,
  alpha,
  beta,
  psi_l,
  nu,
  lambda,
  theta_l,
  type = 0,
  x = NULL,
  gamma = NULL,
  kappa = NULL
)

Arguments

n

Positive integer. Number of individuals.

time

Positive integer. Number of time points.

delta_t

Numeric. Time interval. The default value is 1.0 with an option to use a numeric value for the discretized state space model parameterization of the linear stochastic differential equation model.

mu0

List of numeric vectors. Each element of the list is the mean of initial latent variable values (μη0\boldsymbol{\mu}_{\boldsymbol{\eta} \mid 0}).

sigma0_l

List of numeric matrices. Each element of the list is the Cholesky factorization (t(chol(sigma0))) of the covariance matrix of initial latent variable values (Ση0\boldsymbol{\Sigma}_{\boldsymbol{\eta} \mid 0}).

alpha

List of numeric vectors. Each element of the list is the vector of constant values for the dynamic model (α\boldsymbol{\alpha}).

beta

List of numeric matrices. Each element of the list is the transition matrix relating the values of the latent variables at the previous to the current time point (β\boldsymbol{\beta}).

psi_l

List of numeric matrices. Each element of the list is the Cholesky factorization (t(chol(psi))) of the covariance matrix of the process noise (Ψ\boldsymbol{\Psi}).

nu

List of numeric vectors. Each element of the list is the vector of intercept values for the measurement model (ν\boldsymbol{\nu}).

lambda

List of numeric matrices. Each element of the list is the factor loading matrix linking the latent variables to the observed variables (Λ\boldsymbol{\Lambda}).

theta_l

List of numeric matrices. Each element of the list is the Cholesky factorization (t(chol(theta))) of the covariance matrix of the measurement error (Θ\boldsymbol{\Theta}).

type

Integer. State space model type. See Details in SimSSMFixed() for more information.

x

List. Each element of the list is a matrix of covariates for each individual i in n. The number of columns in each matrix should be equal to time.

gamma

List of numeric matrices. Each element of the list is the matrix linking the covariates to the latent variables at current time point (Γ\boldsymbol{\Gamma}).

kappa

List of numeric matrices. Each element of the list is the matrix linking the covariates to the observed variables at current time point (κ\boldsymbol{\kappa}).

Details

Parameters can vary across individuals by providing a list of parameter values. If the length of any of the parameters (mu0, sigma0_l, alpha, beta, psi_l, nu, lambda, theta_l, gamma, or kappa) is less the n, the function will cycle through the available values.

Value

Returns an object of class simstatespace which is a list with the following elements:

  • call: Function call.

  • args: Function arguments.

  • data: Generated data which is a list of length n. Each element of data is a list with the following elements:

    • id: A vector of ID numbers with length l, where l is the value of the function argument time.

    • time: A vector time points of length l.

    • y: A l by k matrix of values for the manifest variables.

    • eta: A l by p matrix of values for the latent variables.

    • x: A l by j matrix of values for the covariates (when covariates are included).

  • fun: Function used.

Author(s)

Ivan Jacob Agaloos Pesigan

References

Chow, S.-M., Ho, M. R., Hamaker, E. L., & Dolan, C. V. (2010). Equivalence and differences between structural equation modeling and state-space modeling techniques. Structural Equation Modeling: A Multidisciplinary Journal, 17(2), 303–332. doi:10.1080/10705511003661553

See Also

Other Simulation of State Space Models Data Functions: LinSDE2SSM(), SimBetaN(), SimPhiN(), SimSSMFixed(), SimSSMLinGrowth(), SimSSMLinGrowthIVary(), SimSSMLinSDEFixed(), SimSSMLinSDEIVary(), SimSSMOUFixed(), SimSSMOUIVary(), SimSSMVARFixed(), SimSSMVARIVary(), TestPhi(), TestStability(), TestStationarity()

Examples

# prepare parameters
# In this example, beta varies across individuals.
set.seed(42)
## number of individuals
n <- 5
## time points
time <- 50
## dynamic structure
p <- 3
mu0 <- list(
  rep(x = 0, times = p)
)
sigma0 <- 0.001 * diag(p)
sigma0_l <- list(
  t(chol(sigma0))
)
alpha <- list(
  rep(x = 0, times = p)
)
beta <- list(
  0.1 * diag(p),
  0.2 * diag(p),
  0.3 * diag(p),
  0.4 * diag(p),
  0.5 * diag(p)
)
psi <- 0.001 * diag(p)
psi_l <- list(
  t(chol(psi))
)
## measurement model
k <- 3
nu <- list(
  rep(x = 0, times = k)
)
lambda <- list(
  diag(k)
)
theta <- 0.001 * diag(k)
theta_l <- list(
  t(chol(theta))
)
## covariates
j <- 2
x <- lapply(
  X = seq_len(n),
  FUN = function(i) {
    matrix(
      data = stats::rnorm(n = time * j),
      nrow = j,
      ncol = time
    )
  }
)
gamma <- list(
  diag(x = 0.10, nrow = p, ncol = j)
)
kappa <- list(
  diag(x = 0.10, nrow = k, ncol = j)
)

# Type 0
ssm <- SimSSMIVary(
  n = n,
  time = time,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  alpha = alpha,
  beta = beta,
  psi_l = psi_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 0
)

plot(ssm)

# Type 1
ssm <- SimSSMIVary(
  n = n,
  time = time,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  alpha = alpha,
  beta = beta,
  psi_l = psi_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 1,
  x = x,
  gamma = gamma
)

plot(ssm)

# Type 2
ssm <- SimSSMIVary(
  n = n,
  time = time,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  alpha = alpha,
  beta = beta,
  psi_l = psi_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 2,
  x = x,
  gamma = gamma,
  kappa = kappa
)

plot(ssm)

Simulate Data from the Linear Growth Curve Model

Description

This function simulates data from the linear growth curve model.

Usage

SimSSMLinGrowth(
  n,
  time,
  mu0,
  sigma0_l,
  theta_l,
  type = 0,
  x = NULL,
  gamma = NULL,
  kappa = NULL
)

Arguments

n

Positive integer. Number of individuals.

time

Positive integer. Number of time points.

mu0

Numeric vector. A vector of length two. The first element is the mean of the intercept, and the second element is the mean of the slope.

sigma0_l

Numeric matrix. Cholesky factorization (t(chol(sigma0))) of the covariance matrix of the intercept and the slope.

theta_l

Numeric. Square root of the common measurement error variance.

type

Integer. State space model type. See Details for more information.

x

List. Each element of the list is a matrix of covariates for each individual i in n. The number of columns in each matrix should be equal to time.

gamma

Numeric matrix. Matrix linking the covariates to the latent variables at current time point (Γ\boldsymbol{\Gamma}).

kappa

Numeric matrix. Matrix linking the covariates to the observed variables at current time point (κ\boldsymbol{\kappa}).

Details

Type 0

The measurement model is given by

Yi,t=(10)(η0i,tη1i,t)+εi,t,withεi,tN(0,θ)Y_{i, t} = \left( \begin{array}{cc} 1 & 0 \\ \end{array} \right) \left( \begin{array}{c} \eta_{0_{i, t}} \\ \eta_{1_{i, t}} \\ \end{array} \right) + \boldsymbol{\varepsilon}_{i, t}, \quad \mathrm{with} \quad \boldsymbol{\varepsilon}_{i, t} \sim \mathcal{N} \left( 0, \theta \right)

where Yi,tY_{i, t}, η0i,t\eta_{0_{i, t}}, η1i,t\eta_{1_{i, t}}, and εi,t\boldsymbol{\varepsilon}_{i, t} are random variables and θ\theta is a model parameter. Yi,tY_{i, t} is the observed random variable at time tt and individual ii, η0i,t\eta_{0_{i, t}} (intercept) and η1i,t\eta_{1_{i, t}} (slope) form a vector of latent random variables at time tt and individual ii, and εi,t\boldsymbol{\varepsilon}_{i, t} a vector of random measurement errors at time tt and individual ii. θ\theta is the variance of ε\boldsymbol{\varepsilon}.

The dynamic structure is given by

(η0i,tη1i,t)=(1101)(η0i,t1η1i,t1).\left( \begin{array}{c} \eta_{0_{i, t}} \\ \eta_{1_{i, t}} \\ \end{array} \right) = \left( \begin{array}{cc} 1 & 1 \\ 0 & 1 \\ \end{array} \right) \left( \begin{array}{c} \eta_{0_{i, t - 1}} \\ \eta_{1_{i, t - 1}} \\ \end{array} \right) .

The mean vector and covariance matrix of the intercept and slope are captured in the mean vector and covariance matrix of the initial condition given by

μη0=(μη0μη1)and,\boldsymbol{\mu}_{\boldsymbol{\eta} \mid 0} = \left( \begin{array}{c} \mu_{\eta_{0}} \\ \mu_{\eta_{1}} \\ \end{array} \right) \quad \mathrm{and,}

Ση0=(ση02ση0,η1ση1,η0ση12).\boldsymbol{\Sigma}_{\boldsymbol{\eta} \mid 0} = \left( \begin{array}{cc} \sigma^{2}_{\eta_{0}} & \sigma_{\eta_{0}, \eta_{1}} \\ \sigma_{\eta_{1}, \eta_{0}} & \sigma^{2}_{\eta_{1}} \\ \end{array} \right) .

Type 1

The measurement model is given by

Yi,t=(10)(η0i,tη1i,t)+εi,t,withεi,tN(0,θ).Y_{i, t} = \left( \begin{array}{cc} 1 & 0 \\ \end{array} \right) \left( \begin{array}{c} \eta_{0_{i, t}} \\ \eta_{1_{i, t}} \\ \end{array} \right) + \boldsymbol{\varepsilon}_{i, t}, \quad \mathrm{with} \quad \boldsymbol{\varepsilon}_{i, t} \sim \mathcal{N} \left( 0, \theta \right) .

The dynamic structure is given by

(η0i,tη1i,t)=(1101)(η0i,t1η1i,t1)+Γxi,t\left( \begin{array}{c} \eta_{0_{i, t}} \\ \eta_{1_{i, t}} \\ \end{array} \right) = \left( \begin{array}{cc} 1 & 1 \\ 0 & 1 \\ \end{array} \right) \left( \begin{array}{c} \eta_{0_{i, t - 1}} \\ \eta_{1_{i, t - 1}} \\ \end{array} \right) + \boldsymbol{\Gamma} \mathbf{x}_{i, t}

where xi,t\mathbf{x}_{i, t} represents a vector of covariates at time tt and individual ii, and Γ\boldsymbol{\Gamma} the coefficient matrix linking the covariates to the latent variables.

Type 2

The measurement model is given by

Yi,t=(10)(η0i,tη1i,t)+κxi,t+εi,t,withεi,tN(0,θ)Y_{i, t} = \left( \begin{array}{cc} 1 & 0 \\ \end{array} \right) \left( \begin{array}{c} \eta_{0_{i, t}} \\ \eta_{1_{i, t}} \\ \end{array} \right) + \boldsymbol{\kappa} \mathbf{x}_{i, t} + \boldsymbol{\varepsilon}_{i, t}, \quad \mathrm{with} \quad \boldsymbol{\varepsilon}_{i, t} \sim \mathcal{N} \left( 0, \theta \right)

where κ\boldsymbol{\kappa} represents the coefficient matrix linking the covariates to the observed variables.

The dynamic structure is given by

(η0i,tη1i,t)=(1101)(η0i,t1η1i,t1)+Γxi,t.\left( \begin{array}{c} \eta_{0_{i, t}} \\ \eta_{1_{i, t}} \\ \end{array} \right) = \left( \begin{array}{cc} 1 & 1 \\ 0 & 1 \\ \end{array} \right) \left( \begin{array}{c} \eta_{0_{i, t - 1}} \\ \eta_{1_{i, t - 1}} \\ \end{array} \right) + \boldsymbol{\Gamma} \mathbf{x}_{i, t} .

Value

Returns an object of class simstatespace which is a list with the following elements:

  • call: Function call.

  • args: Function arguments.

  • data: Generated data which is a list of length n. Each element of data is a list with the following elements:

    • id: A vector of ID numbers with length l, where l is the value of the function argument time.

    • time: A vector time points of length l.

    • y: A l by k matrix of values for the manifest variables.

    • eta: A l by p matrix of values for the latent variables.

    • x: A l by j matrix of values for the covariates (when covariates are included).

  • fun: Function used.

Author(s)

Ivan Jacob Agaloos Pesigan

References

Chow, S.-M., Ho, M. R., Hamaker, E. L., & Dolan, C. V. (2010). Equivalence and differences between structural equation modeling and state-space modeling techniques. Structural Equation Modeling: A Multidisciplinary Journal, 17(2), 303–332. doi:10.1080/10705511003661553

See Also

Other Simulation of State Space Models Data Functions: LinSDE2SSM(), SimBetaN(), SimPhiN(), SimSSMFixed(), SimSSMIVary(), SimSSMLinGrowthIVary(), SimSSMLinSDEFixed(), SimSSMLinSDEIVary(), SimSSMOUFixed(), SimSSMOUIVary(), SimSSMVARFixed(), SimSSMVARIVary(), TestPhi(), TestStability(), TestStationarity()

Examples

# prepare parameters
set.seed(42)
## number of individuals
n <- 5
## time points
time <- 5
## dynamic structure
p <- 2
mu0 <- c(0.615, 1.006)
sigma0 <- matrix(
  data = c(
    1.932,
    0.618,
    0.618,
    0.587
  ),
  nrow = p
)
sigma0_l <- t(chol(sigma0))
## measurement model
k <- 1
theta <- 0.50
theta_l <- sqrt(theta)
## covariates
j <- 2
x <- lapply(
  X = seq_len(n),
  FUN = function(i) {
    return(
      matrix(
        data = rnorm(n = j * time),
        nrow = j
      )
    )
  }
)
gamma <- diag(x = 0.10, nrow = p, ncol = j)
kappa <- diag(x = 0.10, nrow = k, ncol = j)

# Type 0
ssm <- SimSSMLinGrowth(
  n = n,
  time = time,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  theta_l = theta_l,
  type = 0
)

plot(ssm)

# Type 1
ssm <- SimSSMLinGrowth(
  n = n,
  time = time,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  theta_l = theta_l,
  type = 1,
  x = x,
  gamma = gamma
)

plot(ssm)

# Type 2
ssm <- SimSSMLinGrowth(
  n = n,
  time = time,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  theta_l = theta_l,
  type = 2,
  x = x,
  gamma = gamma,
  kappa = kappa
)

plot(ssm)

Simulate Data from the Linear Growth Curve Model (Individual-Varying Parameters)

Description

This function simulates data from the linear growth curve model. It assumes that the parameters can vary across individuals.

Usage

SimSSMLinGrowthIVary(
  n,
  time,
  mu0,
  sigma0_l,
  theta_l,
  type = 0,
  x = NULL,
  gamma = NULL,
  kappa = NULL
)

Arguments

n

Positive integer. Number of individuals.

time

Positive integer. Number of time points.

mu0

A list of numeric vectors. Each element of the list is a vector of length two. The first element is the mean of the intercept, and the second element is the mean of the slope.

sigma0_l

A list of numeric matrices. Each element of the list is the Cholesky factorization (t(chol(sigma0))) of the covariance matrix of the intercept and the slope.

theta_l

A list numeric values. Each element of the list is the square root of the common measurement error variance.

type

Integer. State space model type. See Details in SimSSMLinGrowth() for more information.

x

List. Each element of the list is a matrix of covariates for each individual i in n. The number of columns in each matrix should be equal to time.

gamma

List of numeric matrices. Each element of the list is the matrix linking the covariates to the latent variables at current time point (Γ\boldsymbol{\Gamma}).

kappa

List of numeric matrices. Each element of the list is the matrix linking the covariates to the observed variables at current time point (κ\boldsymbol{\kappa}).

Details

Parameters can vary across individuals by providing a list of parameter values. If the length of any of the parameters (mu0, sigma0, mu, theta_l, gamma, or kappa) is less the n, the function will cycle through the available values.

Value

Returns an object of class simstatespace which is a list with the following elements:

  • call: Function call.

  • args: Function arguments.

  • data: Generated data which is a list of length n. Each element of data is a list with the following elements:

    • id: A vector of ID numbers with length l, where l is the value of the function argument time.

    • time: A vector time points of length l.

    • y: A l by k matrix of values for the manifest variables.

    • eta: A l by p matrix of values for the latent variables.

    • x: A l by j matrix of values for the covariates (when covariates are included).

  • fun: Function used.

Author(s)

Ivan Jacob Agaloos Pesigan

References

Chow, S.-M., Ho, M. R., Hamaker, E. L., & Dolan, C. V. (2010). Equivalence and differences between structural equation modeling and state-space modeling techniques. Structural Equation Modeling: A Multidisciplinary Journal, 17(2), 303–332. doi:10.1080/10705511003661553

See Also

Other Simulation of State Space Models Data Functions: LinSDE2SSM(), SimBetaN(), SimPhiN(), SimSSMFixed(), SimSSMIVary(), SimSSMLinGrowth(), SimSSMLinSDEFixed(), SimSSMLinSDEIVary(), SimSSMOUFixed(), SimSSMOUIVary(), SimSSMVARFixed(), SimSSMVARIVary(), TestPhi(), TestStability(), TestStationarity()

Other Simulation of State Space Models Data Functions: LinSDE2SSM(), SimBetaN(), SimPhiN(), SimSSMFixed(), SimSSMIVary(), SimSSMLinGrowth(), SimSSMLinSDEFixed(), SimSSMLinSDEIVary(), SimSSMOUFixed(), SimSSMOUIVary(), SimSSMVARFixed(), SimSSMVARIVary(), TestPhi(), TestStability(), TestStationarity()

Examples

# prepare parameters
# In this example, the mean vector of the intercept and slope vary.
# Specifically,
# there are two sets of values representing two latent classes.
set.seed(42)
## number of individuals
n <- 10
## time points
time <- 5
## dynamic structure
p <- 2
mu0_1 <- c(0.615, 1.006) # lower starting point, higher growth
mu0_2 <- c(1.000, 0.500) # higher starting point, lower growth
mu0 <- list(mu0_1, mu0_2)
sigma0 <- matrix(
  data = c(
    1.932,
    0.618,
    0.618,
    0.587
  ),
  nrow = p
)
sigma0_l <- list(t(chol(sigma0)))
## measurement model
k <- 1
theta <- 0.50
theta_l <- list(sqrt(theta))
## covariates
j <- 2
x <- lapply(
  X = seq_len(n),
  FUN = function(i) {
    matrix(
      data = stats::rnorm(n = time * j),
      nrow = j,
      ncol = time
    )
  }
)
gamma <- list(
  diag(x = 0.10, nrow = p, ncol = j)
)
kappa <- list(
  diag(x = 0.10, nrow = k, ncol = j)
)

# Type 0
ssm <- SimSSMLinGrowthIVary(
  n = n,
  time = time,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  theta_l = theta_l,
  type = 0
)

plot(ssm)

# Type 1
ssm <- SimSSMLinGrowthIVary(
  n = n,
  time = time,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  theta_l = theta_l,
  type = 1,
  x = x,
  gamma = gamma
)

plot(ssm)

# Type 2
ssm <- SimSSMLinGrowthIVary(
  n = n,
  time = time,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  theta_l = theta_l,
  type = 2,
  x = x,
  gamma = gamma,
  kappa = kappa
)

plot(ssm)

Simulate Data from the Linear Stochastic Differential Equation Model using a State Space Model Parameterization (Fixed Parameters)

Description

This function simulates data from the linear stochastic differential equation model using a state space model parameterization. It assumes that the parameters remain constant across individuals and over time.

Usage

SimSSMLinSDEFixed(
  n,
  time,
  delta_t = 1,
  mu0,
  sigma0_l,
  iota,
  phi,
  sigma_l,
  nu,
  lambda,
  theta_l,
  type = 0,
  x = NULL,
  gamma = NULL,
  kappa = NULL
)

Arguments

n

Positive integer. Number of individuals.

time

Positive integer. Number of time points.

delta_t

Numeric. Time interval (Δt\Delta_t).

mu0

Numeric vector. Mean of initial latent variable values (μη0\boldsymbol{\mu}_{\boldsymbol{\eta} \mid 0}).

sigma0_l

Numeric matrix. Cholesky factorization (t(chol(sigma0))) of the covariance matrix of initial latent variable values (Ση0\boldsymbol{\Sigma}_{\boldsymbol{\eta} \mid 0}).

iota

Numeric vector. An unobserved term that is constant over time (ι\boldsymbol{\iota}).

phi

Numeric matrix. The drift matrix which represents the rate of change of the solution in the absence of any random fluctuations (Φ\boldsymbol{\Phi}).

sigma_l

Numeric matrix. Cholesky factorization (t(chol(sigma))) of the covariance matrix of volatility or randomness in the process (Σ\boldsymbol{\Sigma}).

nu

Numeric vector. Vector of intercept values for the measurement model (ν\boldsymbol{\nu}).

lambda

Numeric matrix. Factor loading matrix linking the latent variables to the observed variables (Λ\boldsymbol{\Lambda}).

theta_l

Numeric matrix. Cholesky factorization (t(chol(theta))) of the covariance matrix of the measurement error (Θ\boldsymbol{\Theta}).

type

Integer. State space model type. See Details for more information.

x

List. Each element of the list is a matrix of covariates for each individual i in n. The number of columns in each matrix should be equal to time.

gamma

Numeric matrix. Matrix linking the covariates to the latent variables at current time point (Γ\boldsymbol{\Gamma}).

kappa

Numeric matrix. Matrix linking the covariates to the observed variables at current time point (κ\boldsymbol{\kappa}).

Details

Type 0

The measurement model is given by

yi,t=ν+Ληi,t+εi,t,withεi,tN(0,Θ)\mathbf{y}_{i, t} = \boldsymbol{\nu} + \boldsymbol{\Lambda} \boldsymbol{\eta}_{i, t} + \boldsymbol{\varepsilon}_{i, t}, \quad \mathrm{with} \quad \boldsymbol{\varepsilon}_{i, t} \sim \mathcal{N} \left( \mathbf{0}, \boldsymbol{\Theta} \right)

where yi,t\mathbf{y}_{i, t}, ηi,t\boldsymbol{\eta}_{i, t}, and εi,t\boldsymbol{\varepsilon}_{i, t} are random variables and ν\boldsymbol{\nu}, Λ\boldsymbol{\Lambda}, and Θ\boldsymbol{\Theta} are model parameters. yi,t\mathbf{y}_{i, t} represents a vector of observed random variables, ηi,t\boldsymbol{\eta}_{i, t} a vector of latent random variables, and εi,t\boldsymbol{\varepsilon}_{i, t} a vector of random measurement errors, at time tt and individual ii. ν\boldsymbol{\nu} denotes a vector of intercepts, Λ\boldsymbol{\Lambda} a matrix of factor loadings, and Θ\boldsymbol{\Theta} the covariance matrix of ε\boldsymbol{\varepsilon}.

An alternative representation of the measurement error is given by

εi,t=Θ12zi,t,withzi,tN(0,I)\boldsymbol{\varepsilon}_{i, t} = \boldsymbol{\Theta}^{\frac{1}{2}} \mathbf{z}_{i, t}, \quad \mathrm{with} \quad \mathbf{z}_{i, t} \sim \mathcal{N} \left( \mathbf{0}, \mathbf{I} \right)

where zi,t\mathbf{z}_{i, t} is a vector of independent standard normal random variables and (Θ12)(Θ12)=Θ.\left( \boldsymbol{\Theta}^{\frac{1}{2}} \right) \left( \boldsymbol{\Theta}^{\frac{1}{2}} \right)^{\prime} = \boldsymbol{\Theta} .

The dynamic structure is given by

dηi,t=(ι+Φηi,t)dt+Σ12dWi,t\mathrm{d} \boldsymbol{\eta}_{i, t} = \left( \boldsymbol{\iota} + \boldsymbol{\Phi} \boldsymbol{\eta}_{i, t} \right) \mathrm{d}t + \boldsymbol{\Sigma}^{\frac{1}{2}} \mathrm{d} \mathbf{W}_{i, t}

where ι\boldsymbol{\iota} is a term which is unobserved and constant over time, Φ\boldsymbol{\Phi} is the drift matrix which represents the rate of change of the solution in the absence of any random fluctuations, Σ\boldsymbol{\Sigma} is the matrix of volatility or randomness in the process, and dW\mathrm{d}\boldsymbol{W} is a Wiener process or Brownian motion, which represents random fluctuations.

Type 1

The measurement model is given by

yi,t=ν+Ληi,t+εi,t,withεi,tN(0,Θ).\mathbf{y}_{i, t} = \boldsymbol{\nu} + \boldsymbol{\Lambda} \boldsymbol{\eta}_{i, t} + \boldsymbol{\varepsilon}_{i, t}, \quad \mathrm{with} \quad \boldsymbol{\varepsilon}_{i, t} \sim \mathcal{N} \left( \mathbf{0}, \boldsymbol{\Theta} \right) .

The dynamic structure is given by

dηi,t=(ι+Φηi,t)dt+Γxi,t+Σ12dWi,t\mathrm{d} \boldsymbol{\eta}_{i, t} = \left( \boldsymbol{\iota} + \boldsymbol{\Phi} \boldsymbol{\eta}_{i, t} \right) \mathrm{d}t + \boldsymbol{\Gamma} \mathbf{x}_{i, t} + \boldsymbol{\Sigma}^{\frac{1}{2}} \mathrm{d} \mathbf{W}_{i, t}

where xi,t\mathbf{x}_{i, t} represents a vector of covariates at time tt and individual ii, and Γ\boldsymbol{\Gamma} the coefficient matrix linking the covariates to the latent variables.

Type 2

The measurement model is given by

yi,t=ν+Ληi,t+κxi,t+εi,t,withεi,tN(0,Θ)\mathbf{y}_{i, t} = \boldsymbol{\nu} + \boldsymbol{\Lambda} \boldsymbol{\eta}_{i, t} + \boldsymbol{\kappa} \mathbf{x}_{i, t} + \boldsymbol{\varepsilon}_{i, t}, \quad \mathrm{with} \quad \boldsymbol{\varepsilon}_{i, t} \sim \mathcal{N} \left( \mathbf{0}, \boldsymbol{\Theta} \right)

where κ\boldsymbol{\kappa} represents the coefficient matrix linking the covariates to the observed variables.

The dynamic structure is given by

dηi,t=(ι+Φηi,t)dt+Γxi,t+Σ12dWi,t.\mathrm{d} \boldsymbol{\eta}_{i, t} = \left( \boldsymbol{\iota} + \boldsymbol{\Phi} \boldsymbol{\eta}_{i, t} \right) \mathrm{d}t + \boldsymbol{\Gamma} \mathbf{x}_{i, t} + \boldsymbol{\Sigma}^{\frac{1}{2}} \mathrm{d} \mathbf{W}_{i, t} .

State Space Parameterization

The state space parameters as a function of the linear stochastic differential equation model parameters are given by

βΔtli=exp(ΔtΦ)\boldsymbol{\beta}_{\Delta t_{{l_{i}}}} = \exp{ \left( \Delta t \boldsymbol{\Phi} \right) }

αΔtli=Φ1(βIp)ι\boldsymbol{\alpha}_{\Delta t_{{l_{i}}}} = \boldsymbol{\Phi}^{-1} \left( \boldsymbol{\beta} - \mathbf{I}_{p} \right) \boldsymbol{\iota}

vec(ΨΔtli)=[(ΦIp)+(IpΦ)][exp([(ΦIp)+(IpΦ)]Δt)Ip×p]vec(Σ)\mathrm{vec} \left( \boldsymbol{\Psi}_{\Delta t_{{l_{i}}}} \right) = \left[ \left( \boldsymbol{\Phi} \otimes \mathbf{I}_{p} \right) + \left( \mathbf{I}_{p} \otimes \boldsymbol{\Phi} \right) \right] \left[ \exp \left( \left[ \left( \boldsymbol{\Phi} \otimes \mathbf{I}_{p} \right) + \left( \mathbf{I}_{p} \otimes \boldsymbol{\Phi} \right) \right] \Delta t \right) - \mathbf{I}_{p \times p} \right] \mathrm{vec} \left( \boldsymbol{\Sigma} \right)

where pp is the number of latent variables and Δt\Delta t is the time interval.

Value

Returns an object of class simstatespace which is a list with the following elements:

  • call: Function call.

  • args: Function arguments.

  • data: Generated data which is a list of length n. Each element of data is a list with the following elements:

    • id: A vector of ID numbers with length l, where l is the value of the function argument time.

    • time: A vector time points of length l.

    • y: A l by k matrix of values for the manifest variables.

    • eta: A l by p matrix of values for the latent variables.

    • x: A l by j matrix of values for the covariates (when covariates are included).

  • fun: Function used.

Author(s)

Ivan Jacob Agaloos Pesigan

References

Chow, S.-M., Ho, M. R., Hamaker, E. L., & Dolan, C. V. (2010). Equivalence and differences between structural equation modeling and state-space modeling techniques. Structural Equation Modeling: A Multidisciplinary Journal, 17(2), 303–332. doi:10.1080/10705511003661553

Chow, S.-M., Losardo, D., Park, J., & Molenaar, P. C. M. (2023). Continuous-time dynamic models: Connections to structural equation models and other discrete-time models. In R. H. Hoyle (Ed.), Handbook of structural equation modeling (2nd ed.). The Guilford Press.

Harvey, A. C. (1990). Forecasting, structural time series models and the Kalman filter. Cambridge University Press. doi:10.1017/cbo9781107049994

See Also

Other Simulation of State Space Models Data Functions: LinSDE2SSM(), SimBetaN(), SimPhiN(), SimSSMFixed(), SimSSMIVary(), SimSSMLinGrowth(), SimSSMLinGrowthIVary(), SimSSMLinSDEIVary(), SimSSMOUFixed(), SimSSMOUIVary(), SimSSMVARFixed(), SimSSMVARIVary(), TestPhi(), TestStability(), TestStationarity()

Examples

# prepare parameters
set.seed(42)
## number of individuals
n <- 5
## time points
time <- 50
delta_t <- 0.10
## dynamic structure
p <- 2
mu0 <- c(-3.0, 1.5)
sigma0 <- 0.001 * diag(p)
sigma0_l <- t(chol(sigma0))
iota <- c(0.317, 0.230)
phi <- matrix(
  data = c(
    -0.10,
    0.05,
    0.05,
    -0.10
  ),
  nrow = p
)
sigma <- matrix(
  data = c(
    2.79,
    0.06,
    0.06,
    3.27
  ),
  nrow = p
)
sigma_l <- t(chol(sigma))
## measurement model
k <- 2
nu <- rep(x = 0, times = k)
lambda <- diag(k)
theta <- 0.001 * diag(k)
theta_l <- t(chol(theta))
## covariates
j <- 2
x <- lapply(
  X = seq_len(n),
  FUN = function(i) {
    matrix(
      data = stats::rnorm(n = time * j),
      nrow = j,
      ncol = time
    )
  }
)
gamma <- diag(x = 0.10, nrow = p, ncol = j)
kappa <- diag(x = 0.10, nrow = k, ncol = j)

# Type 0
ssm <- SimSSMLinSDEFixed(
  n = n,
  time = time,
  delta_t = delta_t,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  iota = iota,
  phi = phi,
  sigma_l = sigma_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 0
)

plot(ssm)

# Type 1
ssm <- SimSSMLinSDEFixed(
  n = n,
  time = time,
  delta_t = delta_t,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  iota = iota,
  phi = phi,
  sigma_l = sigma_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 1,
  x = x,
  gamma = gamma
)

plot(ssm)

# Type 2
ssm <- SimSSMLinSDEFixed(
  n = n,
  time = time,
  delta_t = delta_t,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  iota = iota,
  phi = phi,
  sigma_l = sigma_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 2,
  x = x,
  gamma = gamma,
  kappa = kappa
)

plot(ssm)

Simulate Data from the Linear Stochastic Differential Equation Model using a State Space Model Parameterization (Individual-Varying Parameters)

Description

This function simulates data from the linear stochastic differential equation model using a state space model parameterization. It assumes that the parameters can vary across individuals.

Usage

SimSSMLinSDEIVary(
  n,
  time,
  delta_t = 1,
  mu0,
  sigma0_l,
  iota,
  phi,
  sigma_l,
  nu,
  lambda,
  theta_l,
  type = 0,
  x = NULL,
  gamma = NULL,
  kappa = NULL
)

Arguments

n

Positive integer. Number of individuals.

time

Positive integer. Number of time points.

delta_t

Numeric. Time interval. The default value is 1.0 with an option to use a numeric value for the discretized state space model parameterization of the linear stochastic differential equation model.

mu0

List of numeric vectors. Each element of the list is the mean of initial latent variable values (μη0\boldsymbol{\mu}_{\boldsymbol{\eta} \mid 0}).

sigma0_l

List of numeric matrices. Each element of the list is the Cholesky factorization (t(chol(sigma0))) of the covariance matrix of initial latent variable values (Ση0\boldsymbol{\Sigma}_{\boldsymbol{\eta} \mid 0}).

iota

List of numeric vectors. Each element of the list is an unobserved term that is constant over time (ι\boldsymbol{\iota}).

phi

List of numeric matrix. Each element of the list is the drift matrix which represents the rate of change of the solution in the absence of any random fluctuations (Φ\boldsymbol{\Phi}).

sigma_l

List of numeric matrix. Each element of the list is the Cholesky factorization (t(chol(sigma))) of the covariance matrix of volatility or randomness in the process Σ\boldsymbol{\Sigma}.

nu

List of numeric vectors. Each element of the list is the vector of intercept values for the measurement model (ν\boldsymbol{\nu}).

lambda

List of numeric matrices. Each element of the list is the factor loading matrix linking the latent variables to the observed variables (Λ\boldsymbol{\Lambda}).

theta_l

List of numeric matrices. Each element of the list is the Cholesky factorization (t(chol(theta))) of the covariance matrix of the measurement error (Θ\boldsymbol{\Theta}).

type

Integer. State space model type. See Details in SimSSMLinSDEFixed() for more information.

x

List. Each element of the list is a matrix of covariates for each individual i in n. The number of columns in each matrix should be equal to time.

gamma

List of numeric matrices. Each element of the list is the matrix linking the covariates to the latent variables at current time point (Γ\boldsymbol{\Gamma}).

kappa

List of numeric matrices. Each element of the list is the matrix linking the covariates to the observed variables at current time point (κ\boldsymbol{\kappa}).

Details

Parameters can vary across individuals by providing a list of parameter values. If the length of any of the parameters (mu0, sigma0_l, iota, phi, sigma_l, nu, lambda, theta_l, gamma, or kappa) is less the n, the function will cycle through the available values.

Value

Returns an object of class simstatespace which is a list with the following elements:

  • call: Function call.

  • args: Function arguments.

  • data: Generated data which is a list of length n. Each element of data is a list with the following elements:

    • id: A vector of ID numbers with length l, where l is the value of the function argument time.

    • time: A vector time points of length l.

    • y: A l by k matrix of values for the manifest variables.

    • eta: A l by p matrix of values for the latent variables.

    • x: A l by j matrix of values for the covariates (when covariates are included).

  • fun: Function used.

Author(s)

Ivan Jacob Agaloos Pesigan

References

Chow, S.-M., Ho, M. R., Hamaker, E. L., & Dolan, C. V. (2010). Equivalence and differences between structural equation modeling and state-space modeling techniques. Structural Equation Modeling: A Multidisciplinary Journal, 17(2), 303–332. doi:10.1080/10705511003661553

Chow, S.-M., Losardo, D., Park, J., & Molenaar, P. C. M. (2023). Continuous-time dynamic models: Connections to structural equation models and other discrete-time models. In R. H. Hoyle (Ed.), Handbook of structural equation modeling (2nd ed.). The Guilford Press.

Harvey, A. C. (1990). Forecasting, structural time series models and the Kalman filter. Cambridge University Press. doi:10.1017/cbo9781107049994

See Also

Other Simulation of State Space Models Data Functions: LinSDE2SSM(), SimBetaN(), SimPhiN(), SimSSMFixed(), SimSSMIVary(), SimSSMLinGrowth(), SimSSMLinGrowthIVary(), SimSSMLinSDEFixed(), SimSSMOUFixed(), SimSSMOUIVary(), SimSSMVARFixed(), SimSSMVARIVary(), TestPhi(), TestStability(), TestStationarity()

Examples

# prepare parameters
# In this example, phi varies across individuals.
set.seed(42)
## number of individuals
n <- 5
## time points
time <- 50
delta_t <- 0.10
## dynamic structure
p <- 2
mu0 <- list(
  c(-3.0, 1.5)
)
sigma0 <- 0.001 * diag(p)
sigma0_l <- list(
  t(chol(sigma0))
)
iota <- list(
  c(0.317, 0.230)
)
phi <- list(
  -0.1 * diag(p),
  -0.2 * diag(p),
  -0.3 * diag(p),
  -0.4 * diag(p),
  -0.5 * diag(p)
)
sigma <- matrix(
  data = c(
    2.79,
    0.06,
    0.06,
    3.27
  ),
  nrow = p
)
sigma_l <- list(
  t(chol(sigma))
)
## measurement model
k <- 2
nu <- list(
  rep(x = 0, times = k)
)
lambda <- list(
  diag(k)
)
theta <- 0.001 * diag(k)
theta_l <- list(
  t(chol(theta))
)
## covariates
j <- 2
x <- lapply(
  X = seq_len(n),
  FUN = function(i) {
    matrix(
      data = stats::rnorm(n = time * j),
      nrow = j,
      ncol = time
    )
  }
)
gamma <- list(
  diag(x = 0.10, nrow = p, ncol = j)
)
kappa <- list(
  diag(x = 0.10, nrow = k, ncol = j)
)

# Type 0
ssm <- SimSSMLinSDEIVary(
  n = n,
  time = time,
  delta_t = delta_t,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  iota = iota,
  phi = phi,
  sigma_l = sigma_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 0
)

plot(ssm)

# Type 1
ssm <- SimSSMLinSDEIVary(
  n = n,
  time = time,
  delta_t = delta_t,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  iota = iota,
  phi = phi,
  sigma_l = sigma_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 1,
  x = x,
  gamma = gamma
)

plot(ssm)

# Type 2
ssm <- SimSSMLinSDEIVary(
  n = n,
  time = time,
  delta_t = delta_t,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  iota = iota,
  phi = phi,
  sigma_l = sigma_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 2,
  x = x,
  gamma = gamma,
  kappa = kappa
)

plot(ssm)

Simulate Data from the Ornstein–Uhlenbeck Model using a State Space Model Parameterization (Fixed Parameters)

Description

This function simulates data from the Ornstein–Uhlenbeck (OU) model using a state space model parameterization. It assumes that the parameters remain constant across individuals and over time.

Usage

SimSSMOUFixed(
  n,
  time,
  delta_t = 1,
  mu0,
  sigma0_l,
  mu,
  phi,
  sigma_l,
  nu,
  lambda,
  theta_l,
  type = 0,
  x = NULL,
  gamma = NULL,
  kappa = NULL
)

Arguments

n

Positive integer. Number of individuals.

time

Positive integer. Number of time points.

delta_t

Numeric. Time interval (Δt\Delta_t).

mu0

Numeric vector. Mean of initial latent variable values (μη0\boldsymbol{\mu}_{\boldsymbol{\eta} \mid 0}).

sigma0_l

Numeric matrix. Cholesky factorization (t(chol(sigma0))) of the covariance matrix of initial latent variable values (Ση0\boldsymbol{\Sigma}_{\boldsymbol{\eta} \mid 0}).

mu

Numeric vector. The long-term mean or equilibrium level (μ\boldsymbol{\mu}).

phi

Numeric matrix. The drift matrix which represents the rate of change of the solution in the absence of any random fluctuations (Φ\boldsymbol{\Phi}). It also represents the rate of mean reversion, determining how quickly the variable returns to its mean.

sigma_l

Numeric matrix. Cholesky factorization (t(chol(sigma))) of the covariance matrix of volatility or randomness in the process (Σ\boldsymbol{\Sigma}).

nu

Numeric vector. Vector of intercept values for the measurement model (ν\boldsymbol{\nu}).

lambda

Numeric matrix. Factor loading matrix linking the latent variables to the observed variables (Λ\boldsymbol{\Lambda}).

theta_l

Numeric matrix. Cholesky factorization (t(chol(theta))) of the covariance matrix of the measurement error (Θ\boldsymbol{\Theta}).

type

Integer. State space model type. See Details for more information.

x

List. Each element of the list is a matrix of covariates for each individual i in n. The number of columns in each matrix should be equal to time.

gamma

Numeric matrix. Matrix linking the covariates to the latent variables at current time point (Γ\boldsymbol{\Gamma}).

kappa

Numeric matrix. Matrix linking the covariates to the observed variables at current time point (κ\boldsymbol{\kappa}).

Details

Type 0

The measurement model is given by

yi,t=ν+Ληi,t+εi,t,withεi,tN(0,Θ)\mathbf{y}_{i, t} = \boldsymbol{\nu} + \boldsymbol{\Lambda} \boldsymbol{\eta}_{i, t} + \boldsymbol{\varepsilon}_{i, t}, \quad \mathrm{with} \quad \boldsymbol{\varepsilon}_{i, t} \sim \mathcal{N} \left( \mathbf{0}, \boldsymbol{\Theta} \right)

where yi,t\mathbf{y}_{i, t}, ηi,t\boldsymbol{\eta}_{i, t}, and εi,t\boldsymbol{\varepsilon}_{i, t} are random variables and ν\boldsymbol{\nu}, Λ\boldsymbol{\Lambda}, and Θ\boldsymbol{\Theta} are model parameters. yi,t\mathbf{y}_{i, t} represents a vector of observed random variables, ηi,t\boldsymbol{\eta}_{i, t} a vector of latent random variables, and εi,t\boldsymbol{\varepsilon}_{i, t} a vector of random measurement errors, at time tt and individual ii. ν\boldsymbol{\nu} denotes a vector of intercepts, Λ\boldsymbol{\Lambda} a matrix of factor loadings, and Θ\boldsymbol{\Theta} the covariance matrix of ε\boldsymbol{\varepsilon}.

An alternative representation of the measurement error is given by

εi,t=Θ12zi,t,withzi,tN(0,I)\boldsymbol{\varepsilon}_{i, t} = \boldsymbol{\Theta}^{\frac{1}{2}} \mathbf{z}_{i, t}, \quad \mathrm{with} \quad \mathbf{z}_{i, t} \sim \mathcal{N} \left( \mathbf{0}, \mathbf{I} \right)

where zi,t\mathbf{z}_{i, t} is a vector of independent standard normal random variables and (Θ12)(Θ12)=Θ.\left( \boldsymbol{\Theta}^{\frac{1}{2}} \right) \left( \boldsymbol{\Theta}^{\frac{1}{2}} \right)^{\prime} = \boldsymbol{\Theta} .

The dynamic structure is given by

dηi,t=Φ(ηi,tμ)dt+Σ12dWi,t\mathrm{d} \boldsymbol{\eta}_{i, t} = \boldsymbol{\Phi} \left( \boldsymbol{\eta}_{i, t} - \boldsymbol{\mu} \right) \mathrm{d}t + \boldsymbol{\Sigma}^{\frac{1}{2}} \mathrm{d} \mathbf{W}_{i, t}

where μ\boldsymbol{\mu} is the long-term mean or equilibrium level, Φ\boldsymbol{\Phi} is the rate of mean reversion, determining how quickly the variable returns to its mean, Σ\boldsymbol{\Sigma} is the matrix of volatility or randomness in the process, and dW\mathrm{d}\boldsymbol{W} is a Wiener process or Brownian motion, which represents random fluctuations.

Type 1

The measurement model is given by

yi,t=ν+Ληi,t+εi,t,withεi,tN(0,Θ).\mathbf{y}_{i, t} = \boldsymbol{\nu} + \boldsymbol{\Lambda} \boldsymbol{\eta}_{i, t} + \boldsymbol{\varepsilon}_{i, t}, \quad \mathrm{with} \quad \boldsymbol{\varepsilon}_{i, t} \sim \mathcal{N} \left( \mathbf{0}, \boldsymbol{\Theta} \right) .

The dynamic structure is given by

dηi,t=Φ(ηi,tμ)dt+Γxi,t+Σ12dWi,t\mathrm{d} \boldsymbol{\eta}_{i, t} = \boldsymbol{\Phi} \left( \boldsymbol{\eta}_{i, t} - \boldsymbol{\mu} \right) \mathrm{d}t + \boldsymbol{\Gamma} \mathbf{x}_{i, t} + \boldsymbol{\Sigma}^{\frac{1}{2}} \mathrm{d} \mathbf{W}_{i, t}

where xi,t\mathbf{x}_{i, t} represents a vector of covariates at time tt and individual ii, and Γ\boldsymbol{\Gamma} the coefficient matrix linking the covariates to the latent variables.

Type 2

The measurement model is given by

yi,t=ν+Ληi,t+κxi,t+εi,t,withεi,tN(0,Θ)\mathbf{y}_{i, t} = \boldsymbol{\nu} + \boldsymbol{\Lambda} \boldsymbol{\eta}_{i, t} + \boldsymbol{\kappa} \mathbf{x}_{i, t} + \boldsymbol{\varepsilon}_{i, t}, \quad \mathrm{with} \quad \boldsymbol{\varepsilon}_{i, t} \sim \mathcal{N} \left( \mathbf{0}, \boldsymbol{\Theta} \right)

where κ\boldsymbol{\kappa} represents the coefficient matrix linking the covariates to the observed variables.

The dynamic structure is given by

dηi,t=Φ(ηi,tμ)dt+Γxi,t+Σ12dWi,t.\mathrm{d} \boldsymbol{\eta}_{i, t} = \boldsymbol{\Phi} \left( \boldsymbol{\eta}_{i, t} - \boldsymbol{\mu} \right) \mathrm{d}t + \boldsymbol{\Gamma} \mathbf{x}_{i, t} + \boldsymbol{\Sigma}^{\frac{1}{2}} \mathrm{d} \mathbf{W}_{i, t} .

The OU model as a linear stochastic differential equation model

The OU model is a first-order linear stochastic differential equation model in the form of

dηi,t=(ι+Φηi,t)dt+Σ12dWi,t\mathrm{d} \boldsymbol{\eta}_{i, t} = \left( \boldsymbol{\iota} + \boldsymbol{\Phi} \boldsymbol{\eta}_{i, t} \right) \mathrm{d}t + \boldsymbol{\Sigma}^{\frac{1}{2}} \mathrm{d} \mathbf{W}_{i, t}

where μ=Φ1ι\boldsymbol{\mu} = - \boldsymbol{\Phi}^{-1} \boldsymbol{\iota} and, equivalently ι=Φμ\boldsymbol{\iota} = - \boldsymbol{\Phi} \boldsymbol{\mu}.

Value

Returns an object of class simstatespace which is a list with the following elements:

  • call: Function call.

  • args: Function arguments.

  • data: Generated data which is a list of length n. Each element of data is a list with the following elements:

    • id: A vector of ID numbers with length l, where l is the value of the function argument time.

    • time: A vector time points of length l.

    • y: A l by k matrix of values for the manifest variables.

    • eta: A l by p matrix of values for the latent variables.

    • x: A l by j matrix of values for the covariates (when covariates are included).

  • fun: Function used.

Author(s)

Ivan Jacob Agaloos Pesigan

References

Chow, S.-M., Ho, M. R., Hamaker, E. L., & Dolan, C. V. (2010). Equivalence and differences between structural equation modeling and state-space modeling techniques. Structural Equation Modeling: A Multidisciplinary Journal, 17(2), 303–332. doi:10.1080/10705511003661553

Chow, S.-M., Losardo, D., Park, J., & Molenaar, P. C. M. (2023). Continuous-time dynamic models: Connections to structural equation models and other discrete-time models. In R. H. Hoyle (Ed.), Handbook of structural equation modeling (2nd ed.). The Guilford Press.

Harvey, A. C. (1990). Forecasting, structural time series models and the Kalman filter. Cambridge University Press. doi:10.1017/cbo9781107049994

Oravecz, Z., Tuerlinckx, F., & Vandekerckhove, J. (2011). A hierarchical latent stochastic differential equation model for affective dynamics. Psychological Methods, 16 (4), 468–490. doi:10.1037/a0024375

Uhlenbeck, G. E., & Ornstein, L. S. (1930). On the theory of the brownian motion. Physical Review, 36 (5), 823–841. doi:10.1103/physrev.36.823

See Also

Other Simulation of State Space Models Data Functions: LinSDE2SSM(), SimBetaN(), SimPhiN(), SimSSMFixed(), SimSSMIVary(), SimSSMLinGrowth(), SimSSMLinGrowthIVary(), SimSSMLinSDEFixed(), SimSSMLinSDEIVary(), SimSSMOUIVary(), SimSSMVARFixed(), SimSSMVARIVary(), TestPhi(), TestStability(), TestStationarity()

Examples

# prepare parameters
set.seed(42)
## number of individuals
n <- 5
## time points
time <- 50
delta_t <- 0.10
## dynamic structure
p <- 2
mu0 <- c(-3.0, 1.5)
sigma0 <- 0.001 * diag(p)
sigma0_l <- t(chol(sigma0))
mu <- c(5.76, 5.18)
phi <- matrix(
  data = c(
    -0.10,
    0.05,
    0.05,
    -0.10
  ),
  nrow = p
)
sigma <- matrix(
  data = c(
    2.79,
    0.06,
    0.06,
    3.27
  ),
  nrow = p
)
sigma_l <- t(chol(sigma))
## measurement model
k <- 2
nu <- rep(x = 0, times = k)
lambda <- diag(k)
theta <- 0.001 * diag(k)
theta_l <- t(chol(theta))
## covariates
j <- 2
x <- lapply(
  X = seq_len(n),
  FUN = function(i) {
    matrix(
      data = stats::rnorm(n = time * j),
      nrow = j,
      ncol = time
    )
  }
)
gamma <- diag(x = 0.10, nrow = p, ncol = j)
kappa <- diag(x = 0.10, nrow = k, ncol = j)

# Type 0
ssm <- SimSSMOUFixed(
  n = n,
  time = time,
  delta_t = delta_t,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  mu = mu,
  phi = phi,
  sigma_l = sigma_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 0
)

plot(ssm)

# Type 1
ssm <- SimSSMOUFixed(
  n = n,
  time = time,
  delta_t = delta_t,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  mu = mu,
  phi = phi,
  sigma_l = sigma_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 1,
  x = x,
  gamma = gamma
)

plot(ssm)

# Type 2
ssm <- SimSSMOUFixed(
  n = n,
  time = time,
  delta_t = delta_t,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  mu = mu,
  phi = phi,
  sigma_l = sigma_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 2,
  x = x,
  gamma = gamma,
  kappa = kappa
)

plot(ssm)

Simulate Data from the Ornstein–Uhlenbeck Model using a State Space Model Parameterization (Individual-Varying Parameters)

Description

This function simulates data from the Ornstein–Uhlenbeck model using a state space model parameterization. It assumes that the parameters can vary across individuals.

Usage

SimSSMOUIVary(
  n,
  time,
  delta_t = 1,
  mu0,
  sigma0_l,
  mu,
  phi,
  sigma_l,
  nu,
  lambda,
  theta_l,
  type = 0,
  x = NULL,
  gamma = NULL,
  kappa = NULL
)

Arguments

n

Positive integer. Number of individuals.

time

Positive integer. Number of time points.

delta_t

Numeric. Time interval. The default value is 1.0 with an option to use a numeric value for the discretized state space model parameterization of the linear stochastic differential equation model.

mu0

List of numeric vectors. Each element of the list is the mean of initial latent variable values (μη0\boldsymbol{\mu}_{\boldsymbol{\eta} \mid 0}).

sigma0_l

List of numeric matrices. Each element of the list is the Cholesky factorization (t(chol(sigma0))) of the covariance matrix of initial latent variable values (Ση0\boldsymbol{\Sigma}_{\boldsymbol{\eta} \mid 0}).

mu

List of numeric vectors. Each element of the list is the long-term mean or equilibrium level (μ\boldsymbol{\mu}).

phi

List of numeric matrix. Each element of the list is the drift matrix which represents the rate of change of the solution in the absence of any random fluctuations (Φ\boldsymbol{\Phi}). It also represents the rate of mean reversion, determining how quickly the variable returns to its mean.

sigma_l

List of numeric matrix. Each element of the list is the Cholesky factorization (t(chol(sigma))) of the covariance matrix of volatility or randomness in the process Σ\boldsymbol{\Sigma}.

nu

List of numeric vectors. Each element of the list is the vector of intercept values for the measurement model (ν\boldsymbol{\nu}).

lambda

List of numeric matrices. Each element of the list is the factor loading matrix linking the latent variables to the observed variables (Λ\boldsymbol{\Lambda}).

theta_l

List of numeric matrices. Each element of the list is the Cholesky factorization (t(chol(theta))) of the covariance matrix of the measurement error (Θ\boldsymbol{\Theta}).

type

Integer. State space model type. See Details in SimSSMOUFixed() for more information.

x

List. Each element of the list is a matrix of covariates for each individual i in n. The number of columns in each matrix should be equal to time.

gamma

List of numeric matrices. Each element of the list is the matrix linking the covariates to the latent variables at current time point (Γ\boldsymbol{\Gamma}).

kappa

List of numeric matrices. Each element of the list is the matrix linking the covariates to the observed variables at current time point (κ\boldsymbol{\kappa}).

Details

Parameters can vary across individuals by providing a list of parameter values. If the length of any of the parameters (mu0, sigma0_l, mu, phi, sigma_l, nu, lambda, theta_l, gamma, or kappa) is less the n, the function will cycle through the available values.

Value

Returns an object of class simstatespace which is a list with the following elements:

  • call: Function call.

  • args: Function arguments.

  • data: Generated data which is a list of length n. Each element of data is a list with the following elements:

    • id: A vector of ID numbers with length l, where l is the value of the function argument time.

    • time: A vector time points of length l.

    • y: A l by k matrix of values for the manifest variables.

    • eta: A l by p matrix of values for the latent variables.

    • x: A l by j matrix of values for the covariates (when covariates are included).

  • fun: Function used.

Author(s)

Ivan Jacob Agaloos Pesigan

References

Chow, S.-M., Ho, M. R., Hamaker, E. L., & Dolan, C. V. (2010). Equivalence and differences between structural equation modeling and state-space modeling techniques. Structural Equation Modeling: A Multidisciplinary Journal, 17(2), 303–332. doi:10.1080/10705511003661553

Chow, S.-M., Losardo, D., Park, J., & Molenaar, P. C. M. (2023). Continuous-time dynamic models: Connections to structural equation models and other discrete-time models. In R. H. Hoyle (Ed.), Handbook of structural equation modeling (2nd ed.). The Guilford Press.

Harvey, A. C. (1990). Forecasting, structural time series models and the Kalman filter. Cambridge University Press. doi:10.1017/cbo9781107049994

Oravecz, Z., Tuerlinckx, F., & Vandekerckhove, J. (2011). A hierarchical latent stochastic differential equation model for affective dynamics. Psychological Methods, 16 (4), 468–490. doi:10.1037/a0024375

Uhlenbeck, G. E., & Ornstein, L. S. (1930). On the theory of the brownian motion. Physical Review, 36 (5), 823–841. doi:10.1103/physrev.36.823

See Also

Other Simulation of State Space Models Data Functions: LinSDE2SSM(), SimBetaN(), SimPhiN(), SimSSMFixed(), SimSSMIVary(), SimSSMLinGrowth(), SimSSMLinGrowthIVary(), SimSSMLinSDEFixed(), SimSSMLinSDEIVary(), SimSSMOUFixed(), SimSSMVARFixed(), SimSSMVARIVary(), TestPhi(), TestStability(), TestStationarity()

Examples

# prepare parameters
# In this example, phi varies across individuals.
set.seed(42)
## number of individuals
n <- 5
## time points
time <- 50
delta_t <- 0.10
## dynamic structure
p <- 2
mu0 <- list(
  c(-3.0, 1.5)
)
sigma0 <- 0.001 * diag(p)
sigma0_l <- list(
  t(chol(sigma0))
)
mu <- list(
  c(5.76, 5.18)
)
phi <- list(
  -0.1 * diag(p),
  -0.2 * diag(p),
  -0.3 * diag(p),
  -0.4 * diag(p),
  -0.5 * diag(p)
)
sigma <- matrix(
  data = c(
    2.79,
    0.06,
    0.06,
    3.27
  ),
  nrow = p
)
sigma_l <- list(
  t(chol(sigma))
)
## measurement model
k <- 2
nu <- list(
  rep(x = 0, times = k)
)
lambda <- list(
  diag(k)
)
theta <- 0.001 * diag(k)
theta_l <- list(
  t(chol(theta))
)
## covariates
j <- 2
x <- lapply(
  X = seq_len(n),
  FUN = function(i) {
    matrix(
      data = stats::rnorm(n = time * j),
      nrow = j,
      ncol = time
    )
  }
)
gamma <- list(
  diag(x = 0.10, nrow = p, ncol = j)
)
kappa <- list(
  diag(x = 0.10, nrow = k, ncol = j)
)

# Type 0
ssm <- SimSSMOUIVary(
  n = n,
  time = time,
  delta_t = delta_t,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  mu = mu,
  phi = phi,
  sigma_l = sigma_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 0
)

plot(ssm)

# Type 1
ssm <- SimSSMOUIVary(
  n = n,
  time = time,
  delta_t = delta_t,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  mu = mu,
  phi = phi,
  sigma_l = sigma_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 1,
  x = x,
  gamma = gamma
)

plot(ssm)

# Type 2
ssm <- SimSSMOUIVary(
  n = n,
  time = time,
  delta_t = delta_t,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  mu = mu,
  phi = phi,
  sigma_l = sigma_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 2,
  x = x,
  gamma = gamma,
  kappa = kappa
)

plot(ssm)

Simulate Data from the Vector Autoregressive Model (Fixed Parameters)

Description

This function simulates data from the vector autoregressive model using a state space model parameterization. It assumes that the parameters remain constant across individuals and over time.

Usage

SimSSMVARFixed(
  n,
  time,
  mu0,
  sigma0_l,
  alpha,
  beta,
  psi_l,
  type = 0,
  x = NULL,
  gamma = NULL
)

Arguments

n

Positive integer. Number of individuals.

time

Positive integer. Number of time points.

mu0

Numeric vector. Mean of initial latent variable values (μη0\boldsymbol{\mu}_{\boldsymbol{\eta} \mid 0}).

sigma0_l

Numeric matrix. Cholesky factorization (t(chol(sigma0))) of the covariance matrix of initial latent variable values (Ση0\boldsymbol{\Sigma}_{\boldsymbol{\eta} \mid 0}).

alpha

Numeric vector. Vector of constant values for the dynamic model (α\boldsymbol{\alpha}).

beta

Numeric matrix. Transition matrix relating the values of the latent variables at the previous to the current time point (β\boldsymbol{\beta}).

psi_l

Numeric matrix. Cholesky factorization (t(chol(psi))) of the covariance matrix of the process noise (Ψ\boldsymbol{\Psi}).

type

Integer. State space model type. See Details for more information.

x

List. Each element of the list is a matrix of covariates for each individual i in n. The number of columns in each matrix should be equal to time.

gamma

Numeric matrix. Matrix linking the covariates to the latent variables at current time point (Γ\boldsymbol{\Gamma}).

Details

Type 0

The measurement model is given by

yi,t=ηi,t\mathbf{y}_{i, t} = \boldsymbol{\eta}_{i, t}

where yi,t\mathbf{y}_{i, t} represents a vector of observed variables and ηi,t\boldsymbol{\eta}_{i, t} a vector of latent variables for individual ii and time tt. Since the observed and latent variables are equal, we only generate data from the dynamic structure.

The dynamic structure is given by

ηi,t=α+βηi,t1+ζi,t,withζi,tN(0,Ψ)\boldsymbol{\eta}_{i, t} = \boldsymbol{\alpha} + \boldsymbol{\beta} \boldsymbol{\eta}_{i, t - 1} + \boldsymbol{\zeta}_{i, t}, \quad \mathrm{with} \quad \boldsymbol{\zeta}_{i, t} \sim \mathcal{N} \left( \mathbf{0}, \boldsymbol{\Psi} \right)

where ηi,t\boldsymbol{\eta}_{i, t}, ηi,t1\boldsymbol{\eta}_{i, t - 1}, and ζi,t\boldsymbol{\zeta}_{i, t} are random variables, and α\boldsymbol{\alpha}, β\boldsymbol{\beta}, and Ψ\boldsymbol{\Psi} are model parameters. Here, ηi,t\boldsymbol{\eta}_{i, t} is a vector of latent variables at time tt and individual ii, ηi,t1\boldsymbol{\eta}_{i, t - 1} represents a vector of latent variables at time t1t - 1 and individual ii, and ζi,t\boldsymbol{\zeta}_{i, t} represents a vector of dynamic noise at time tt and individual ii. α\boldsymbol{\alpha} denotes a vector of intercepts, β\boldsymbol{\beta} a matrix of autoregression and cross regression coefficients, and Ψ\boldsymbol{\Psi} the covariance matrix of ζi,t\boldsymbol{\zeta}_{i, t}.

An alternative representation of the dynamic noise is given by

ζi,t=Ψ12zi,t,withzi,tN(0,I)\boldsymbol{\zeta}_{i, t} = \boldsymbol{\Psi}^{\frac{1}{2}} \mathbf{z}_{i, t}, \quad \mathrm{with} \quad \mathbf{z}_{i, t} \sim \mathcal{N} \left( \mathbf{0}, \mathbf{I} \right)

where (Ψ12)(Ψ12)=Ψ.\left( \boldsymbol{\Psi}^{\frac{1}{2}} \right) \left( \boldsymbol{\Psi}^{\frac{1}{2}} \right)^{\prime} = \boldsymbol{\Psi} .

Type 1

The measurement model is given by

yi,t=ηi,t.\mathbf{y}_{i, t} = \boldsymbol{\eta}_{i, t} .

The dynamic structure is given by

ηi,t=α+βηi,t1+Γxi,t+ζi,t,withζi,tN(0,Ψ)\boldsymbol{\eta}_{i, t} = \boldsymbol{\alpha} + \boldsymbol{\beta} \boldsymbol{\eta}_{i, t - 1} + \boldsymbol{\Gamma} \mathbf{x}_{i, t} + \boldsymbol{\zeta}_{i, t}, \quad \mathrm{with} \quad \boldsymbol{\zeta}_{i, t} \sim \mathcal{N} \left( \mathbf{0}, \boldsymbol{\Psi} \right)

where xi,t\mathbf{x}_{i, t} represents a vector of covariates at time tt and individual ii, and Γ\boldsymbol{\Gamma} the coefficient matrix linking the covariates to the latent variables.

Value

Returns an object of class simstatespace which is a list with the following elements:

  • call: Function call.

  • args: Function arguments.

  • data: Generated data which is a list of length n. Each element of data is a list with the following elements:

    • id: A vector of ID numbers with length l, where l is the value of the function argument time.

    • time: A vector time points of length l.

    • y: A l by k matrix of values for the manifest variables.

    • eta: A l by p matrix of values for the latent variables.

    • x: A l by j matrix of values for the covariates (when covariates are included).

  • fun: Function used.

Author(s)

Ivan Jacob Agaloos Pesigan

References

Chow, S.-M., Ho, M. R., Hamaker, E. L., & Dolan, C. V. (2010). Equivalence and differences between structural equation modeling and state-space modeling techniques. Structural Equation Modeling: A Multidisciplinary Journal, 17(2), 303–332. doi:10.1080/10705511003661553

See Also

Other Simulation of State Space Models Data Functions: LinSDE2SSM(), SimBetaN(), SimPhiN(), SimSSMFixed(), SimSSMIVary(), SimSSMLinGrowth(), SimSSMLinGrowthIVary(), SimSSMLinSDEFixed(), SimSSMLinSDEIVary(), SimSSMOUFixed(), SimSSMOUIVary(), SimSSMVARIVary(), TestPhi(), TestStability(), TestStationarity()

Examples

# prepare parameters
set.seed(42)
## number of individuals
n <- 5
## time points
time <- 50
## dynamic structure
p <- 3
mu0 <- rep(x = 0, times = p)
sigma0 <- 0.001 * diag(p)
sigma0_l <- t(chol(sigma0))
alpha <- rep(x = 0, times = p)
beta <- 0.50 * diag(p)
psi <- 0.001 * diag(p)
psi_l <- t(chol(psi))
## covariates
j <- 2
x <- lapply(
  X = seq_len(n),
  FUN = function(i) {
    matrix(
      data = stats::rnorm(n = time * j),
      nrow = j,
      ncol = time
    )
  }
)
gamma <- diag(x = 0.10, nrow = p, ncol = j)

# Type 0
ssm <- SimSSMVARFixed(
  n = n,
  time = time,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  alpha = alpha,
  beta = beta,
  psi_l = psi_l,
  type = 0
)

plot(ssm)

# Type 1
ssm <- SimSSMVARFixed(
  n = n,
  time = time,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  alpha = alpha,
  beta = beta,
  psi_l = psi_l,
  type = 1,
  x = x,
  gamma = gamma
)

plot(ssm)

Simulate Data from the Vector Autoregressive Model (Individual-Varying Parameters)

Description

This function simulates data from the vector autoregressive model using a state space model parameterization. It assumes that the parameters can vary across individuals.

Usage

SimSSMVARIVary(
  n,
  time,
  mu0,
  sigma0_l,
  alpha,
  beta,
  psi_l,
  type = 0,
  x = NULL,
  gamma = NULL
)

Arguments

n

Positive integer. Number of individuals.

time

Positive integer. Number of time points.

mu0

List of numeric vectors. Each element of the list is the mean of initial latent variable values (μη0\boldsymbol{\mu}_{\boldsymbol{\eta} \mid 0}).

sigma0_l

List of numeric matrices. Each element of the list is the Cholesky factorization (t(chol(sigma0))) of the covariance matrix of initial latent variable values (Ση0\boldsymbol{\Sigma}_{\boldsymbol{\eta} \mid 0}).

alpha

List of numeric vectors. Each element of the list is the vector of constant values for the dynamic model (α\boldsymbol{\alpha}).

beta

List of numeric matrices. Each element of the list is the transition matrix relating the values of the latent variables at the previous to the current time point (β\boldsymbol{\beta}).

psi_l

List of numeric matrices. Each element of the list is the Cholesky factorization (t(chol(psi))) of the covariance matrix of the process noise (Ψ\boldsymbol{\Psi}).

type

Integer. State space model type. See Details in SimSSMVARFixed() for more information.

x

List. Each element of the list is a matrix of covariates for each individual i in n. The number of columns in each matrix should be equal to time.

gamma

List of numeric matrices. Each element of the list is the matrix linking the covariates to the latent variables at current time point (Γ\boldsymbol{\Gamma}).

Details

Parameters can vary across individuals by providing a list of parameter values. If the length of any of the parameters (mu0, sigma0_l, alpha, beta, psi_l, gamma, or kappa) is less the n, the function will cycle through the available values.

Value

Returns an object of class simstatespace which is a list with the following elements:

  • call: Function call.

  • args: Function arguments.

  • data: Generated data which is a list of length n. Each element of data is a list with the following elements:

    • id: A vector of ID numbers with length l, where l is the value of the function argument time.

    • time: A vector time points of length l.

    • y: A l by k matrix of values for the manifest variables.

    • eta: A l by p matrix of values for the latent variables.

    • x: A l by j matrix of values for the covariates (when covariates are included).

  • fun: Function used.

Author(s)

Ivan Jacob Agaloos Pesigan

References

Chow, S.-M., Ho, M. R., Hamaker, E. L., & Dolan, C. V. (2010). Equivalence and differences between structural equation modeling and state-space modeling techniques. Structural Equation Modeling: A Multidisciplinary Journal, 17(2), 303–332. doi:10.1080/10705511003661553

See Also

Other Simulation of State Space Models Data Functions: LinSDE2SSM(), SimBetaN(), SimPhiN(), SimSSMFixed(), SimSSMIVary(), SimSSMLinGrowth(), SimSSMLinGrowthIVary(), SimSSMLinSDEFixed(), SimSSMLinSDEIVary(), SimSSMOUFixed(), SimSSMOUIVary(), SimSSMVARFixed(), TestPhi(), TestStability(), TestStationarity()

Examples

# prepare parameters
# In this example, beta varies across individuals.
set.seed(42)
## number of individuals
n <- 5
## time points
time <- 50
## dynamic structure
p <- 3
mu0 <- list(
  rep(x = 0, times = p)
)
sigma0 <- 0.001 * diag(p)
sigma0_l <- list(
  t(chol(sigma0))
)
alpha <- list(
  rep(x = 0, times = p)
)
beta <- list(
  0.1 * diag(p),
  0.2 * diag(p),
  0.3 * diag(p),
  0.4 * diag(p),
  0.5 * diag(p)
)
psi <- 0.001 * diag(p)
psi_l <- list(
  t(chol(psi))
)
## covariates
j <- 2
x <- lapply(
  X = seq_len(n),
  FUN = function(i) {
    matrix(
      data = stats::rnorm(n = time * j),
      nrow = j,
      ncol = time
    )
  }
)
gamma <- list(
  diag(x = 0.10, nrow = p, ncol = j)
)

# Type 0
ssm <- SimSSMVARIVary(
  n = n,
  time = time,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  alpha = alpha,
  beta = beta,
  psi_l = psi_l,
  type = 0
)

plot(ssm)

# Type 1
ssm <- SimSSMVARIVary(
  n = n,
  time = time,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  alpha = alpha,
  beta = beta,
  psi_l = psi_l,
  type = 1,
  x = x,
  gamma = gamma
)

plot(ssm)

Test the Drift Matrix

Description

Both have to be true for the function to return TRUE.

  • Test that the real part of all eigenvalues of Φ\boldsymbol{\Phi} are less than zero.

  • Test that the diagonal values of Φ\boldsymbol{\Phi} are between 0 to negative inifinity.

Usage

TestPhi(phi)

Arguments

phi

Numeric matrix. The drift matrix (Φ\boldsymbol{\Phi}).

Author(s)

Ivan Jacob Agaloos Pesigan

See Also

Other Simulation of State Space Models Data Functions: LinSDE2SSM(), SimBetaN(), SimPhiN(), SimSSMFixed(), SimSSMIVary(), SimSSMLinGrowth(), SimSSMLinGrowthIVary(), SimSSMLinSDEFixed(), SimSSMLinSDEIVary(), SimSSMOUFixed(), SimSSMOUIVary(), SimSSMVARFixed(), SimSSMVARIVary(), TestStability(), TestStationarity()

Examples

phi <- matrix(
  data = c(
    -0.357, 0.771, -0.450,
    0.0, -0.511, 0.729,
    0, 0, -0.693
  ),
  nrow = 3
)
TestPhi(phi = phi)

Test Stability

Description

The function computes the eigenvalues of the input matrix x. It checks if the real part of all eigenvalues is negative. If all eigenvalues have negative real parts, the system is considered stable.

Usage

TestStability(x)

Arguments

x

Numeric matrix.

Author(s)

Ivan Jacob Agaloos Pesigan

See Also

Other Simulation of State Space Models Data Functions: LinSDE2SSM(), SimBetaN(), SimPhiN(), SimSSMFixed(), SimSSMIVary(), SimSSMLinGrowth(), SimSSMLinGrowthIVary(), SimSSMLinSDEFixed(), SimSSMLinSDEIVary(), SimSSMOUFixed(), SimSSMOUIVary(), SimSSMVARFixed(), SimSSMVARIVary(), TestPhi(), TestStationarity()

Examples

x <- matrix(
  data = c(
    -0.357, 0.771, -0.450,
    0.0, -0.511, 0.729,
    0, 0, -0.693
  ),
  nrow = 3
)
TestStability(x)

Test Stationarity

Description

The function computes the eigenvalues of the input matrix x. It checks if all eigenvalues have moduli less than 1. If all eigenvalues have moduli less than 1, the system is considered stationary.

Usage

TestStationarity(x)

Arguments

x

Numeric matrix.

Author(s)

Ivan Jacob Agaloos Pesigan

See Also

Other Simulation of State Space Models Data Functions: LinSDE2SSM(), SimBetaN(), SimPhiN(), SimSSMFixed(), SimSSMIVary(), SimSSMLinGrowth(), SimSSMLinGrowthIVary(), SimSSMLinSDEFixed(), SimSSMLinSDEIVary(), SimSSMOUFixed(), SimSSMOUIVary(), SimSSMVARFixed(), SimSSMVARIVary(), TestPhi(), TestStability()

Examples

x <- matrix(
  data = c(0.5, 0.3, 0.2, 0.4),
  nrow = 2
)
TestStationarity(x)

x <- matrix(
  data = c(0.9, -0.5, 0.8, 0.7),
  nrow = 2
)
TestStationarity(x)