cross-lags in single times series of multiple variables

32 views
Skip to first unread message

martij...@hotmail.com

unread,
Jun 6, 2019, 9:29:37 AM6/6/19
to lavaan
Hi,

I have collected data on 3 variables X, Y, Z on the same single subject over time.The model I would like to fit in Lavaan includes both cross-and autolags.
The problem that I have is that I do not know how to construct such a model to fit on a single subject's timeseries of X,Y,Z.
I do know how to fit the model in Lavaan on data from multiple subjects with timeseries of X,Y,Z, I also know how to fit the SEM in RStan such that it can deal with both single subject and multiple subject timeseries. Can anyone provide a hint how to adapt my lavaan model structure so it can deal with single subject's timeseries?

In Rstan my model looks like:
  Y[2:N] ~ normal(beta[1] + beta[2] * X[2:N], sigma1);  
  X[2:N] ~ normal(beta[3] + beta[4] * X[1:(N - 1)] .* Y[1:(N - 1)] + beta[5] * X[1:(N - 1)] .* Z[1:(N - 1)], sigma2);    
  Z[2:N] ~ normal(beta[6], sigma3);    
where N is time-series length (and .* is just how RStan multiple two vectors)
So in this model Y~Xlag1, X~Xlag1*Ylag1+Xlag1*Zlag1 and Z~1.
.
I have tried to fit this model in lavaan using the growth function, which works fine as long as I analyze timeseries of multiple subjects simultaneously, but it does not work for single subjects :
My data I rearranged into wide format: e.g. variable Y is divided into N variables (y1, y2, etc., where y1 is the y-value at timestep 1).
subject  y1 y2 ... 
1     2.3  0.3 ...
2    1.4   4.9  ...
My model looks like
y1~beta2*x1
y2~beta2*x2
... series continues till N
x2~beta4*y1:x1 +beta5*z1:x1
x3~beta4*y2:x2 +beta5*z2:x2
... series continues till N
y1~~sigma1*y1
y2~~sigma1*y2
... series continues till N
x1~~sigma2*x1
x2~~sigma2*x2
... series continues till N
z1~~sigma3*z1
z1~~sigma3*z1
... series continues till N
If I fit his lavaan model to a dataset containing timeseries of mutliple subjects (output = growth(model, widedata)), the outcomes are as expected (I simulated data with known effect sizes). 
However, when I fit this model to a single timeseries (
output = growth(model, widedata[1,]), the growth function gives the following error:
Error in lav_data_full(data = data, group = group, cluster = cluster, : lavaan ERROR: some variables have only 1 observation or no finite variance
Apparently, and maybe not unsurprisingly, the growth function cannot handle datasets with only one observation per variable, even if regression parameters (beta2, etc.) are shared among equations.
I assume I should be able to code the model in Lavaan a different and smarter way, but am not sure how to do it? Is there a way to do it similar as in the above Stan model?
Any help is much appreciated,
cheers Martijn  


Mauricio Garnier-Villarreal

unread,
Jun 6, 2019, 1:48:11 PM6/6/19
to lavaan
Martijn

lavaan will not work with only 1 subject. For something like this I think the best option is the package ctsem (https://cran.r-project.org/web/packages/ctsem/vignettes/ctsem.pdf), you can see a cross-lagged effects model in page 8, and a single subject example in page 23

Here I am modifying one of their examples to make it a two variable model with a single subject. The DRIFT matrix is the one you are interested, the diagonal elements are the auto-effects, and the off diagonal elements are the cross-effects. The DRIFT matrix is in continuous time metric (lag time independent). You can modified it to show the effects at a certain time lag of interest expm(summary(example1fit_mx)$DRIFT * 2)

library(ctsem)

data("ctExample1")
head(ctExample1)

example1model <- ctModel(n.latent = 2, n.manifest = 2, Tpoints = 6,
                         manifestNames = c("LeisureTime", "Happiness"),
                         latentNames = c("LeisureTime", "Happiness"),
                         LAMBDA = diag(2) )

example1fit_mx <- ctFit(dat = t(ctExample1[1,]), ctmodelobj = example1model,
                        stationary = "T0VAR", retryattempts=400)
summary(example1fit_mx)

summary(example1fit_mx, verbose = TRUE)["discreteDRIFTstd"] ### parameteres at lag = 1
summary(example1fit_mx)$DRIFT ## continuous time parameters (lag independent)
expm(summary(example1fit_mx)$DRIFT * 2) ### parameteres at lag = 2
summary(example1fit_mx)$ctparameters
Reply all
Reply to author
Forward
0 new messages