Continuous Time Structural Equation Modelling

A hierarchical, multivariate, continuous (and discrete) time dynamic modelling package for panel and time series data, using stochastic differential equations. Contains a faster frequentist set of functions using OpenMx for single subject and mixed-effects (random intercepts only) structural equation models, or a hierarchical Bayesian implementation using Stan that allows for random effects and non-linearity over all model parameters. Allows for modelling of multiple noisy measurements of multiple stochastic processes, time varying input / event covariates, and time invariant covariates used to predict the parameters. Bayesian formulation not available on 32 bit Windows systems.

Build Status

See the NEWS file for recent updates!

ctsem allows for easy specification and fitting of a range of continuous and discrete time dynamic models, including multiple indicators (dynamic factor analysis), multiple, potentially higher order processes, and time dependent (varying within subject) and time independent (not varying within subject) covariates. Classic longitudinal models like latent growth curves and latent change score models are also possible. Version 1 of ctsem provided SEM based functionality by linking to the OpenMx software, allowing mixed effects models (random means but fixed regression and variance parameters) for multiple subjects. For version 2 of the R package ctsem, we include a Bayesian specification and fitting routine that uses the Stan probabilistic programming language, via the rstan package in R. This allows for all parameters of the dynamic model to individually vary, using an estimated population mean and variance, and any time independent covariate effects, as a prior. ctsem version 1 is documented in a JSS publication (Driver, Voelkle, Oud, 2017), and in R vignette form at . The Bayesian approach is outlined in Introduction to Hierarchical Continuous Time Dynamic Modelling with ctsem, at . To cite ctsem please use the citation("ctsem") command in R.


ctsem news:



  • Updated for rstan 2.18.1 compatibility
  • Non-linear dynamics now handled using mixture of extended and unscented filters for improved speed.
  • Priors for hierarchical variance modified so prior for total variance has consistent shape regardless of dimension.
  • Optimization / importance sampling works well for many cases, see arguments using ?ctStanFit.
  • ctStanPostPredict produces a range of posterior predictive plots.
  • Various small non-critical bug fixes / updates. (see github for details)



  • Fixed bug in ctStanFit introduced in previous release leading to errors in handling of missing data.
  • ctStanTIpredMarginal function for plotting marginal relationships between predictors and parameters.



  • Removed need for compilation of standard models.
  • Unscented Kalman filter for ctStanFit:
    • Non-linear / time-varying / state dependent specifications now possible.
    • Optimization followed by importance sampling can be used instead of sampling via Stan.
    • Most plotting functions still not working correctly for such models.
  • ctCheckFit function for plotting covariance of data generated from posterior against original.
  • Time independent predictors can now be used independent of random effects.
  • Fix bug in summary preventing display when binary variables were used in fit.
  • Allow data sets to contain both binary and continuous variables.
  • More robust data import, character string id's and jumbled order of rows now manageable.
  • stanWplot no longer requires shiny to be explicitly loaded.
  • Fix bug in ctKalman plotting function preventing interpolation.
  • Fix bug in additional summary matrices introduced in 2.5.0 -- some were transposed.
  • Summary no longer returns errors when partial stationarity is set.




  • stanWplot function for trace plots while sampling with stan was not working on non windows platforms.
  • ctStan summary reports population standard deviations more accurately -- improved delta approach.
  • various minor plotting improvements

Additions / Changes:

  • ctStanFit now handles correlation matrices differently -- little substantive impact.
  • ctKalman can now be used to plot individual trajectories from ctFit objects and ctStanFit objects (ctStanKalman function no longer exists).
  • ctStanFit now handles missing data on covariate effects -- time dependent predictors are set to zero, time independent predictors are imputed with a normal(0,10) prior (can adjust via the $tipredsimputedprior subobject of the ctStanModel).
  • ctStanFit default population standard deviation prior now changed to a regularised independence Jeffreys -- previous truncated normal approach still possible because...
  • ctStanFit now accepts custom specifications for the population standard deviation -- see the $rawhypersd , $rawhypersdlowerbound, and $hypersdtransform subobjects of the ctStanModel object.
  • ctStan: Plotting covariate effects via ctStanTIpredeffects function now easier to use and more versatile -- can plot effects on discrete time matrices, for instance.
  • ctFit and ctMultigroupFit data argument changed to 'dat' instead of 'datawide', and now dataform="long" argument can be used to use long format data (as per ctStan) directly.
  • additional parameter matrices shown for summary of ctStanFit objects.
  • ctStanParMatrices function to compute continuous time matrices for a given model and vector of free population means.




  • Time dependent predictors generated errors with the frequentist Kalman filter form since 2.2.0
  • With stationary set to NULL (not the default but offered in help file) for ctFit, t0 matrices were mistakenly set to stationary.
  • Duplicated parameter names now allowed in a ctStanFit model.


  • ctGenerateFromFit generates data based on a model fitted with ctFit.
  • ctPostPredict generates distributions from data based on a model fitted with ctFit and plots this against the original data.




  • summary: Standard errors were not reported in some cases
  • ctStanFit: 2.3.0 hierarchical correlation changes were applied too broadly
  • ctFit: discreteTime switch no longer gives errors when traits included
  • ctFit: transformedParams=FALSE argument no longer throwing errors.
  • ctStanKalman: correct handling of missing data for plotting.




  • TRAITVAR in frequentist ctsem was incorrectly accounting for differing time intervals since v2.0.0. TRAITVAR is now (again) reported as total between subjects variance.
  • Default quantiles on ctStanDiscretePars adjusted to 95%.
  • Hierarchical correlation probabilities adjusted in ctStanFit for more consistent behaviour with high dimensional processes.


  • Default to unstandardised cross effects plots.




  • Time dependent predictors now have instantaneous effect in both frequentist and Bayesian approaches, and the documentation is updated to reflect this. Previously, no TDpreds affecting first time point in frequentist. Accordingly, wide data structure is changed, with an extra column per predictor and predictors now sorted by time point as for indicators. See vignette for example.
  • Default to 0 covariance between time dependent predictors and initial (T0) latents / traits / time independent predictors. Specify matrix as 'free' in ctModel to estimate instead.
  • Default carefulFit = TRUE for multiple groups frequentist models (ctMultigroupFit)
  • Improve optimization approach for ctStanFit - but still not reliable for random effects.


  • Multiple time dependent predictors with multiple processes resulted in inaccurate estimates for TDPREDEFFECT in frequentist approach of previous versions.
  • Prevent ctGenerate from auto-filling matrices to 0 variance.
  • Correct oscillating example for change in tolerance in OpenMx.




  • improved fitting of frequentist models with ctFit and ctRefineTo, due to changes to carefulFit penalisation and refining approach.


  • Removed package 'PSM' from suggests field and vignette as requested by CRAN


  • rstan 2.14 caused problems with data import for ctStanFit
  • eliminated spurious warnings for ctStanFit




  • Empirical Bayes, experimental but can now optimize with hierarchical model (when using the Kalman filter, as per defaults)
  • Easy extraction and plotting of time independent predictor (covariate) effects, see ctStanTIpredEffects for example.
  • Added stationary argument to ctStanFit - much more efficient than setting priors on stationarity.

Bugs fixed:

  • incorrect number of cores spawned for parallel sessions.
  • optimize and variational bayes switches for ctStanFit did not work.
  • ctKalman would break if only 1 row of data passed in.





  • Defaults change: Fix CINT to 0 and free MANIFESTMEANS
  • Reintroduce variable effect of TRAITVAR at T0 (more flexible but more fitting problems - try MANIFESTTRAITVAR instead if problematic, or use step-wise fitting approach, automated with ctRefineTo)

ctsem 1.1.6

Features added:

  • now with a change log!
  • ctCompareExpectation plots expected means and covariances against model implied.
  • remove log transform of drift matrix diagonal, positive drift diagonals again possible.
  • ctRefineTo allows easy step wise fitting from simple to complex - faster and more robust fitting in many cases.
  • ctPlot is a new function that allows more customization of plots.
  • ctModel now allows time varying means to be specified.

Bugs fixed:

  • corrected handling of Cholesky inputs for ctGenerate

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.


2.8.2 by Charles Driver, a month ago

Browse source code at

Authors: Charles Driver [aut, cre, cph] , Manuel Voelkle [aut, cph] , Han Oud [aut, cph]

Documentation:   PDF Manual  

Task views: Psychometric Models and Methods

GPL-3 license

Imports rstan, rstantools, plyr, tools, data.table, Matrix, datasets, stats, graphics, grDevices, parallel, shiny, MASS, methods, utils, corrplot, mvtnorm, DEoptim, KernSmooth

Depends on Rcpp, OpenMx

Suggests knitr, testthat

Linking to StanHeaders, rstan, BH, Rcpp, RcppEigen

System requirements: GNU make

See at CRAN