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