ctsem v3.0.4


Monthly downloads



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. Using the original ctsem formulation based on OpenMx, described in the JSS paper "Continuous Time Structural Equation Modeling with R Package ctsem", with updated version as CRAN vignette <https://cran.r-project.org/web/packages/ctsem/vignettes/ctsem.pdf> , linear mixed effects SDE's estimated via maximum likelihood and optimization are possible. Using the Stan based formulation, described in <https://github.com/cdriveraus/ctsem/raw/master/vignettes/hierarchicalmanual.pdf> , nonlinearity (state dependent parameters) and random effects on all parameters are possible, using either optimization (with optional importance sampling) or Stan's Hamiltonian Monte Carlo sampling. Priors may be used. For the conceptual overview of the hierarchical Bayesian linear SDE approach, see <https://www.researchgate.net/publication/324093594_Hierarchical_Bayesian_Continuous_Time_Dynamic_Modeling>. Exogenous inputs may also be included, for an overview of such possibilities see <https://www.researchgate.net/publication/328221807_Understanding_the_Time_Course_of_Interventions_with_Continuous_Time_Dynamic_Models> . Stan based functions are not available on 32 bit Windows systems at present.



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 is documented in a JSS publication (Driver, Voelkle, Oud, 2017), and in R vignette form at https://cran.r-project.org/package=ctsem/vignettes/ctsem.pdf . The Bayesian approach is outlined in Introduction to Hierarchical Continuous Time Dynamic Modelling with ctsem, at https://github.com/cdriveraus/ctsem/raw/master/vignettes/hierarchicalmanual.pdf . To cite ctsem please use the citation(“ctsem”) command in R.

To install the github version, use:

remotes::install_github('cdriveraus/ctsem', INSTALL_opts = "--no-multiarch", dependencies = c("Depends", "Imports"))

Troubleshooting Rstan / Rtools install for Windows:

Ensure recent version of R and Rtools is installed.

try including these lines in home/.R/makevars. :

CXX14 = g++ -std=c++1y
CXX14FLAGS = -O3 -Wno-unused-variable -Wno-unused-function

If makevars does not exist, run this code within R:

dotR <- file.path(Sys.getenv("HOME"), ".R")
if (!file.exists(dotR)) dir.create(dotR)
M <- file.path(dotR, ifelse(.Platform$OS.type == "windows", "Makevars.win", "Makevars"))
if (!file.exists(M)) file.create(M)
cat("\nCXX14FLAGS=-O3 -march=native -mtune=native",
    if( grepl("^darwin", R.version$os)) "CXX14FLAGS += -arch x86_64 -ftemplate-depth-256" else
    if (.Platform$OS.type == "windows") "CXX11FLAGS=-O3 -march=native -mtune=native" else
    "CXX14FLAGS += -fPIC",
    file = M, sep = "\n", append = TRUE)

In case of compile errors like g++ not found, ensure the devtools package is installed:


and include the following in your .Rprofile

Sys.setenv(PATH = paste("C:/Rtools/bin", Sys.getenv("PATH"), sep=";"))
Sys.setenv(PATH = paste("C:\\Rtools\\mingw_64\\bin", Sys.getenv("PATH"), sep=";"))
Sys.setenv(BINPREF = "C:/Rtools/mingw_$(WIN)/bin/")

Functions in ctsem

Name Description
ctDiscretePars ctDiscretePars
ctIntervalise Converts absolute times to intervals for wide format ctsem panel data
ctPlotArray Plots three dimensional y values for quantile plots
ctMultigroupFit Fits a multiple group continuous time model.
ctDocs Get documentation pdf for ctsem
ctDiscretiseData Discretise long format continuous time (ctsem) data to specific timestep.
ctPlot ctPlot
Kalman Kalman
ctPoly Plots uncertainty bands with shading
ctGenerate ctGenerate
ctFit Fit a ctsem object
ctExample2 ctExample2
ctExample2level ctExample2level
ctKalman ctKalman
ctStanKalman Get Kalman filter estimates from a ctStanFit object
ctStanParnames ctStanParnames
ctStanGenerateFromFit Add a $generated object to ctstanfit object, with random data generated from posterior of ctstanfit object
ctExample4 ctExample4
ctExample1 ctExample1
ctGenerateFromFit Generates data according to the model estimated in a ctsemFit object.
ctStanUpdModel Update an already compiled and fit ctStanFit object
ctStanDiscretePars ctStanDiscretePars
ctStanTIpredeffects Get time independent predictor effect estimates
ctStanContinuousPars ctStanContinuousPars
plot.ctKalman Plots Kalman filter output from ctKalman.
msquare Right multiply a matrix by its transpose.
ctIndplot ctIndplot
ctWideNames ctWideNames sets default column names for wide ctsem datasets. Primarily intended for internal ctsem usage.
ctStanPlotPost ctStanPlotPost
ctModelLatex Generate and optionally compile latex equation of subject level ctsem model.
ctPostPredict Posterior predictive type check for ctsemFit.
ctExample1TIpred ctExample1TIpred
ctModelFromFit Extract a ctsem model structure with parameter values from a ctsem fit object.
ctStanTIpredMarginal Plot marginal relationships between covariates and parameters for a ctStanFit object.
ctstantestfit ctstantestfit
ctRefineTo ctRefineTo
ctStanPostPredict Compares model implied density and values to observed, for a ctStanFit object.
standatact_specificsubjects Adjust standata from ctsem to only use specific subjects
ctWideToLong ctWideToLong Convert ctsem wide to long format
ctStanDiscreteParsPlot ctStanDiscreteParsPlot
ctModel Define a ctsem model
ctLongToWide ctLongToWide Restructures time series / panel data from long format to wide format for ctsem analysis
datastructure datastructure
ctStanFit ctStanFit
plot.ctsemFit Plotting function for object class ctsemFit
ctsem ctsem
plot.ctsemFitMeasure Misspecification plot using ctCheckFit output
extract Extract samples from a ctStanFit object
inv_logit Inverse logit
stanoptimis Optimize / importance sample a stan or ctStan model.
ctStanModel Convert a frequentist (omx) ctsem model specification to Bayesian (Stan).
ctStanParMatrices Returns population system matrices from a ctStanFit object, and vector of values for free parameters.
longexample longexample
isdiag Diagnostics for ctsem importance sampling
stan_confidenceRegion Extract functions of multiple variables from a stanfit object
stan_postcalc Compute functions of matrices from samples of a stanfit object
sdpcor2cov sdcor2cov
plot.ctsemMultigroupFit Plot function for ctsemMultigroupFit object
summary.ctsemMultigroupFit Summary function for ctsemMultigroupFit object
summary.ctStanFit summary.ctStanFit
plot.ctStanFit plot.ctStanFit
plot.ctStanModel Prior plotting
stan_unconstrainsamples Convert samples from a stanfit object to the unconstrained scale
ctstantestdat ctstantestdat
stanWplot Runs stan, and plots sampling information while sampling.
stan_checkdivergences Analyse divergences in a stanfit object
stan_reinitsf Quickly initialise stanfit object from model and data
summary.ctsemFit Summary function for ctsemFit object
ctCheckFit Check absolute fit of ctFit or ctStanFit object.
ctCompareExpected ctCompareExpected Compares model implied to observed means and covariances for panel data fit with ctsem.
AnomAuth AnomAuth
Oscillating Oscillating
ctCollapse ctCollapse Easily collapse an array margin using a specified function.
ctDensity ctDensity
ctDeintervalise ctDeintervalise
ctExample3 ctExample3
ctCI ctCI Computes confidence intervals on specified parameters / matrices for already fitted ctsem fit object.
No Results!

Vignettes of ctsem

No Results!

Last month downloads


Type Package
Date 2019-9-12
License GPL-3
URL https://github.com/cdriveraus/ctsem
Encoding UTF-8
LazyData true
ByteCompile true
LinkingTo StanHeaders (>= 2.18.0), rstan (>= 2.18.1), BH (>= 1.66.0-1), Rcpp (>= 0.12.16), RcppEigen (>=
SystemRequirements GNU make
NeedsCompilation yes
VignetteBuilder knitr
RoxygenNote 6.1.1
Packaged 2019-09-12 15:31:52 UTC; Driver
Repository CRAN
Date/Publication 2019-09-12 22:40:15 UTC

Include our badge in your README