Skip to content

About

simStateSpace: Simulate Data from State Space Models (Pesigan, Russell, & Chow, 2025: https://doi.org/10.1037/met0000779).

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Repository files navigation

simStateSpace

Ivan Jacob Agaloos Pesigan

DOI CRAN Status R-Universe Status pages-build-deployment codecov Make Project (make all) Make Project R-CMD-check-full R-CMD-check-oldrel R-CMD-check-standard Compile LaTeX Lint R Package Non-ASCII Check Package Website (GitHub Pages) rhub 1 (essential) rhub 2 (memory) rhub 3 (numerics), review only rhub 4 (compilers), review only Shell Check Source Code R Package Test Coverage

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. This package was designed to generate data for the simulations performed in Pesigan, Russell, and Chow (2025: https://doi.org/10.1037/met0000779).

Installation

You can install the CRAN release of simStateSpace with:

install.packages("simStateSpace")

You can install the development version of simStateSpace from GitHub with:

if (!require("pak")) install.packages("pak")
pak::pkg_install("jeksterslab/simStateSpace")

Example

In this example, data are simulated from a first-order vector autoregressive model with two variables for five individuals measured at fifty time points, using the SimSSMVARFixed() function. The first variable predicts the second at the next time point, but not the other way around.

library(package = "simStateSpace")

Set the model parameters.

alpha <- c(0, 0)
beta <- matrix(
  data = c(
    0.5, 0.2,
    0.0, 0.4
  ),
  nrow = 2
)
psi <- diag(x = 0.1, nrow = 2)

The initial condition is set to the steady-state mean vector and covariance matrix of the process.

mu0 <- SSMMeanEta(beta = beta, alpha = alpha)
sigma0 <- SSMCovEta(beta = beta, psi = psi)

Simulate the data.

set.seed(42)
sim <- SimSSMVARFixed(
  n = 5,
  time = 50,
  mu0 = mu0,
  sigma0_l = t(chol(sigma0)),
  alpha = alpha,
  beta = beta,
  psi_l = t(chol(psi))
)

Convert the data to a data frame (long format).

data <- as.data.frame(x = sim)
head(data)
#>   id time         y1          y2
#> 1  1    0 -0.1940770  0.26782025
#> 2  1    1  0.6041807  0.16278091
#> 3  1    2  0.4301179 -0.01174823
#> 4  1    3  0.5275478  0.48950009
#> 5  1    4  0.1561974  0.85910238
#> 6  1    5  0.1056374  0.35938559

Plot the data.

plot(x = sim)

Screening and Realized Moments

Person-specific transition matrices drawn with SimBetaN() are redrawn until each one is stationary. The screening changes the distribution of the draws, so the realized moments can differ from the nominal values. The attribute draws records the number of draws made for each accepted matrix, and RealizedMoments() returns the realized means, their Monte Carlo standard errors, the covariance matrix, and the rejection rate. SimBetaN2() and SimPhiN2() project the draws instead, and record the projected draws in the attribute projected.

set.seed(42)
beta_i <- SimBetaN(
  n = 1000,
  beta = matrix(data = c(0.7, 0.0, 0.0, 0.6), nrow = 2),
  vcov_beta_vec_l = t(chol(0.04 * diag(4)))
)
RealizedMoments(x = beta_i)
#> Realized moments of 1000 matrices
#> 
#> Mean:
#>        [,1]   [,2]
#> [1,] 0.6655 0.0130
#> [2,] 0.0011 0.5868
#> 
#> Monte Carlo standard error of the mean:
#>        [,1]   [,2]
#> [1,] 0.0055 0.0061
#> [2,] 0.0063 0.0058
#> 
#> Variances of the elements (column-major order):
#> [1] 0.0303 0.0401 0.0375 0.0339
#> 
#> Draws: 1121 
#> Rejection rate: 0.1079

To report the true values of a simulation study on the realized scale, draw a large number of matrices (for example, 1e5) once, with the settings of the study.

Other Features

The package provides the following functions, grouped as in the reference index.

Function Purpose
SimSSMFixed(), SimSSMIVary() State space model, with fixed or individual-varying parameters
SimSSMLinGrowth(), SimSSMLinGrowthIVary() Linear growth curve model
SimSSMVARFixed(), SimSSMVARIVary() Vector autoregressive model
SimSSMLinSDEFixed(), SimSSMLinSDEIVary() Linear stochastic differential equation model
SimSSMOUFixed(), SimSSMOUIVary() Ornstein-Uhlenbeck model
SimBetaN(), SimPhiN(), SimAlphaN(), SimIotaN(), SimCovN(), SimMVN(), … Random parameters that vary across individuals
SSMMeanEta(), SSMCovEta(), LinSDEMeanEta(), LinSDECovEta(), … Steady-state means, covariances, and intercepts
LinSDE2SSM(), SSM2LinSDE(), CTVAR2SSM(), ExpMat(), LogMat() Transformations between continuous-time and discrete-time parameters
TestStability(), TestStationarity(), TestPhi(), ProjectToStability(), ProjectToHurwitz() Stability and stationarity tests and projections
RealizedMoments() Realized moments and rejection or projection rates of screened person-specific matrices

The as.data.frame(), as.matrix(), plot(), and print() methods are available for the simulated data.

Documentation

See GitHub Pages for package documentation, including examples for each model and for panel models (random intercept cross-lagged panel model, STARTS model, latent change score model, and latent curve model with structured residuals).

Citation

To cite simStateSpace in publications, please cite Pesigan et al. (2025).

References

Pesigan, I. J. A., Russell, M. A., & Chow, S.-M. (2025). Inferences and effect sizes for direct, indirect, and total effects in continuous-time mediation models. Psychological Methods. https://doi.org/10.1037/met0000779

R Core Team. (2026). R: A language and environment for statistical computing. R Foundation for Statistical Computing. https://www.R-project.org/

About

simStateSpace: Simulate Data from State Space Models (Pesigan, Russell, & Chow, 2025: https://doi.org/10.1037/met0000779).

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages