Continuous Time Structural Equation Modelling

Hierarchical continuous time state space modelling, for linear and nonlinear systems measured by continuous variables, with limited support for binary data. The subject specific dynamic system is modelled as a stochastic differential equation (SDE), measurement models are typically multivariate normal factor models. Linear mixed effects SDE's estimated via maximum likelihood and optimization are the default. Nonlinearities, (state dependent parameters) and random effects on all parameters are possible, using either max likelihood / max a posteriori optimization (with optional importance sampling) or Stan's Hamiltonian Monte Carlo sampling. See <> for details. Priors may be used. For the conceptual overview of the hierarchical Bayesian linear SDE approach, see <>. Exogenous inputs may also be included, for an overview of such possibilities see <> . Stan based functions are not available on 32 bit Windows systems at present. <> contains some tutorial blog posts.

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.


3.4.2 by Charles Driver, 2 months ago

Browse source code at

Authors: Charles Driver [aut, cre, cph] , Manuel Voelkle [aut, cph] , Han Oud [aut, cph] , Trustees of Columbia University [cph]

Documentation:   PDF Manual  

Task views:

GPL-3 license

Imports cOde, expm, data.table, datasets, Deriv, ggplot2, graphics, grDevices, MASS, Matrix, methods, mize, mvtnorm, parallel, pkgbuild, plyr, rstan, stats, tools, utils, RcppParallel, tibble

Depends on Rcpp

Suggests knitr, testthat, devtools, DEoptim, tinytex, lme4, shiny, gridExtra, arules

Linking to BH, Rcpp, RcppEigen, RcppParallel, rstan, StanHeaders, RcppParallel.1

System requirements: GNU make

Depended on by ctsemOMX.

See at CRAN