Everything ctsem does in continuous time it can also do in discrete time, where the model describes change from one occasion to the next: vector autoregressions, cross-lagged panel models with random intercepts, factor models for the processes, and checks of the fit. A discrete time model treats the occasions as equally spaced, so it suits data collected at a fixed interval, and its parameters refer to that interval. Why continuous time says more about the choice between the two.
Data
The example follows 100 workers through their first ten weeks in a
new job. Stress is measured by two questionnaire items,
Stress and Stress2, and the quality of each
worker’s output by Quality. The questions are how stress
and quality change after starting the job, whether one affects the
other, and how people differ in where they start and where they
settle.
The data are simulated from a discrete time model in which stress lowers the next week’s quality, workers start stressed and settle at their own long run levels, and those with a higher long run level of stress have a lower long run level of quality:
set.seed(2)
gm <- ctModel(type = 'dt',
manifestNames = c('Stress', 'Stress2', 'Quality'),
latentNames = c('stress', 'quality'),
LAMBDA = matrix(c(
1, 0,
0.8, 0,
0, 1), 3, 2, byrow = TRUE),
DRIFT = matrix(c(
0.6, 0,
-0.3, 0.7), 2, 2, byrow = TRUE),
DIFFUSION = diag(c(0.8, 0.5)),
CINT = c('cint_stress', 'cint_quality'),
T0MEANS = c('t0_stress', 't0_quality'),
MANIFESTMEANS = c(0, 1, 0),
MANIFESTVAR = diag(0.5, 3))
dat <- data.frame(ctGenerate(gm, n = 100, Tpoints = 10, backend = 'julia',
values = list(
popmeans = c(cint_stress = 1, cint_quality = 1.5,
t0_stress = 4, t0_quality = 0),
popsd = c(cint_stress = 0.4, cint_quality = 0.4,
t0_stress = 1, t0_quality = 1),
rawpopcorr = c(cint_stress__cint_quality = -0.6))))
head(dat)
#> id time Stress Stress2 Quality
#> 1 1 0 2.484161 3.730844 0.7584292
#> 2 1 1 2.547169 2.484342 -0.4371261
#> 3 1 2 3.319269 4.923508 -0.2389048
#> 4 1 3 3.704649 4.188055 -1.9908365
#> 5 1 4 5.209853 5.103995 -1.3112271
#> 6 1 5 4.885877 4.798512 -2.3627164A bivariate model
The first model uses one indicator per process, leaving out
Stress2. In discrete time the DRIFT matrix
holds the autoregressions on its diagonal and the cross-lagged effects
off it. DRIFT[2, 1] is the effect of this week’s stress on
next week’s quality. The measurement intercepts
(MANIFESTMEANS) are each person’s long run levels, since
the processes return to zero, and they vary over people, as do the
starting values (T0MEANS). Most of this is ctsem’s default,
but the model is written out in full here to show each part:
model <- ctModel(type = 'dt',
manifestNames = c('Stress', 'Quality'),
latentNames = c('stress', 'quality'),
LAMBDA = diag(2),
T0MEANS = c('t0_stress', 't0_quality'),
MANIFESTMEANS = c('mi_stress, indvarying = TRUE',
'mi_quality, indvarying = TRUE'),
MANIFESTVAR = c(
'merr_stress', 0,
0, 'merr_quality'),
DRIFT = c(
'a11', 'a12',
'a21', 'a22'),
DIFFUSION = c(
'diff11', 0,
'diff21', 'diff22'),
CINT = 0)A vector given for a matrix fills it row by row.
DIFFUSION and MANIFESTVAR hold standard
deviations on their diagonals and correlation parameters below them.
fit <- ctFit(dat, model, cores = 2)
s <- summary(fit)
s$popmeans
#> mean sd 2.5% 50% 97.5% mode
#> t0_stress 1.180 0.155 0.882 1.186 1.487 1.173
#> t0_quality -1.878 0.262 -2.357 -1.880 -1.391 -1.855
#> a11 0.656 0.060 0.522 0.659 0.761 0.657
#> a12 0.078 0.038 0.001 0.078 0.148 0.077
#> a21 -0.300 0.036 -0.370 -0.301 -0.228 -0.301
#> a22 0.684 0.028 0.630 0.683 0.737 0.683
#> diff11 0.762 0.070 0.642 0.758 0.908 0.762
#> diff21 0.019 0.075 -0.126 0.016 0.168 0.022
#> diff22 0.503 0.047 0.420 0.500 0.602 0.501
#> merr_stress 0.532 0.073 0.399 0.530 0.692 0.525
#> merr_quality 0.503 0.036 0.435 0.504 0.574 0.502
#> mi_stress 3.015 0.134 2.750 3.011 3.270 3.019
#> mi_quality 1.873 0.238 1.416 1.874 2.324 1.854a21, the effect of stress on next week’s quality, is
-0.30, as generated, with an interval well away from zero.
a12, the effect of quality on stress, was generated as 0
and is estimated at 0.08, with an interval that only just excludes zero.
With a 95% interval for each of several parameters, one now and then
lands just beside its true value. rawpopcorr holds the
correlations of the random effects:
s$rawpopcorr
#> mean sd 2.5% 50% 97.5% mode z
#> t0_quality__t0_stress -0.583 0.092 -0.740 -0.592 -0.391 -0.598 -6.362
#> mi_stress__t0_stress -0.615 0.090 -0.770 -0.623 -0.426 -0.627 -6.826
#> mi_quality__t0_stress 0.615 0.083 0.442 0.619 0.763 0.628 7.394
#> mi_stress__t0_quality 0.773 0.070 0.614 0.782 0.881 0.787 10.974
#> mi_quality__t0_quality -0.880 0.029 -0.927 -0.883 -0.818 -0.884 -30.722
#> mi_quality__mi_stress -0.847 0.052 -0.925 -0.856 -0.717 -0.858 -16.144Those with a higher long run level of stress (mi_stress)
have a lower long run level of quality (mi_quality). The
generating values imply a correlation of about -0.86 between the
two.
The starting values t0_stress and
t0_quality are deviations from each person’s long run
level, because the processes return to zero and the measurement
intercepts hold the long run levels. The data were generated with
starting levels unrelated to the long run levels, so someone with a
higher long run level starts further below it, and each starting value
correlates negatively with its own long run level.
Predictions and effects over time
ctPredict() gives the predicted values for each person
from their earlier observations (yprior), and the smoothed
values from all of them (ysmooth):
With removeObs = TRUE the predictions use no
observations at all, only the population parameters. This is the average
course of a new worker:
ctDiscretePars() shows the effect of a change in one
process on each process, one or more weeks later. It is still a first
order model. A change this week affects next week directly, and later
weeks through the weeks in between.
ctDiscretePars(fit, plot = TRUE)
Checking the model
ctFitCheckCov() compares the correlations in the data,
within and across weeks, with those the model implies:
ctFitAdequacy() tests the one-step-ahead predictions:
whether the prediction errors have the mean, variance and correlations
over time that the model says they should:
adequacy <- ctFitAdequacy(fit)
adequacy
#> Adequacy: no family detects misfit at alpha = 0.05
#> (estimation removed by: derivative; 100 generated datasets)
#>
#> family statistics worst worst_z p p_adj
#> residuals 16 Quality~Stress gap 1-2 1.910 0.5680 1
#> latent 16 stress~quality gap <=1 1.890 0.6120 1
#> volatility 8 Quality^2 gap >3 -2.430 0.0918 1
#> pairs 1 Stress:Quality -0.568 0.6950 1
#> periodic 12 Quality period 2.25 1.860 0.5320 1
#> distribution 8 skew(Stress) -1.980 0.9360 1
#> fit 2 spread of unit fit -1.140 0.9150 1
#> effects 19 P(u2 < 1.5) 1.230 0.9860 1
#> dependence 4 dcor(Quality^2, past) 1.640 0.2530 1
#> subjects 8 sd(lag1_Quality_Quality) 1.050 0.9170 1
#> nonlinear 10 cor(dQuality,Quality*Quality) 1.690 1.0000 1
#>
#> Size of what remains (root mean square, bias-corrected; p_close tests that it is at least close):
#> measure estimate lower upper close
#> residual correlation, all gaps 0.0110 0 0.03170 0.05
#> residual correlation, shortest gaps 0.0000 0 0.02640 0.05
#> latent residual correlation, all gaps 0.0000 0 0.02680 0.05
#> latent residual correlation, shortest gaps 0.0186 0 0.04290 0.05
#> within-row correlation 0.0000 0 0.00688 0.05
#> p_close
#> 6.07e-08
#> 1.72e-11
#> 4.83e-10
#> 2.31e-03
#> 0.00e+00A factor model for stress
Adding the second stress item gives stress two indicators. The
loading of Stress2 is free, and the loading of
Stress is fixed to 1, which sets the scale of the stress
process. Stable differences between people now belong to the processes
rather than to each item, so they go in the continuous time intercepts
(CINT, the intercepts of the processes, in discrete time
added at each step). The measurement intercepts are fixed to zero except
that of Stress2, which is the same for everyone:
factorfit <- ctFit(dat, factormodel, cores = 2)
summary(factorfit)$popmeans
#> mean sd 2.5% 50% 97.5% mode
#> T0m_stress 4.181 0.104 3.968 4.181 4.373 4.181
#> T0m_quality -0.004 0.122 -0.230 -0.008 0.240 -0.004
#> stressLoading 0.772 0.016 0.740 0.772 0.804 0.771
#> drift_stress 0.631 0.043 0.547 0.632 0.710 0.632
#> drift_stress_quality 0.062 0.030 0.004 0.062 0.120 0.061
#> drift_quality_stress -0.283 0.028 -0.337 -0.283 -0.226 -0.284
#> drift_quality 0.693 0.024 0.645 0.693 0.738 0.691
#> diff_stress 0.820 0.029 0.767 0.819 0.877 0.821
#> diff_quality_stress 0.015 0.061 -0.109 0.015 0.130 0.014
#> diff_quality 0.505 0.045 0.424 0.503 0.596 0.503
#> mvarStress 0.461 0.028 0.409 0.460 0.517 0.460
#> mvarStress2 0.519 0.018 0.485 0.518 0.553 0.519
#> mvarQuality 0.505 0.035 0.439 0.504 0.577 0.504
#> mi_stress2 1.096 0.056 0.990 1.095 1.203 1.099
#> cint_stress 0.987 0.165 0.674 0.983 1.317 0.990
#> cint_quality 1.427 0.115 1.196 1.429 1.645 1.432The loading and intercept of Stress2 come out near the
generating 0.8 and 1, and the drift near its generating values.
ctScoreTest() checks, without refitting, whether a
parameter the model holds fixed or the same for everyone should be freed
or allowed to vary:
ctScoreTest(factorfit)
#> Score tests at the fitted model (laplace route, 100 subjects)
#>
#> Unmodelled heterogeneity (LM: the variance with its covariances, against an
#> equal mixture of chi-square on df - 1 and df; z: the variance alone, one sided;
#> p: the stronger of the two; sd: the one-step estimate, raw scale):
#> parameter LM df z p p_adj sd sd_natural class
#> drift_stress_quality 12.40 5 0.59800 0.0400 0.349 0.03690 0.03690 <NA>
#> drift_stress 11.30 5 -2.59000 0.0685 0.349 NA NA <NA>
#> mi_stress2 9.86 5 -1.50000 0.0920 0.349 NA NA A
#> MANIFESTMEANS[Stress] 9.57 5 -0.46300 0.1080 0.349 NA NA A
#> mvarStress 7.67 5 0.15400 0.2640 0.688 0.07040 0.06190 <NA>
#> diff_quality 5.37 5 0.63300 0.4540 0.851 0.17200 0.16900 <NA>
#> drift_quality 3.67 5 0.48000 0.5220 0.851 0.15200 0.06480 <NA>
#> drift_quality_stress 1.28 5 0.04240 0.7180 0.851 0.00557 0.00557 <NA>
#> diff_quality_stress 2.99 5 -0.06440 0.7400 0.851 NA NA <NA>
#> stressLoading 2.33 5 -0.00212 0.7510 0.851 0.00238 0.01190 <NA>
#> Covariances with: T0m_stress, T0m_quality, cint_stress, cint_quality
#>
#> A new dimension of variation: largest eigenvalue 3.6, p = 0.922; direction diff_quality_stress 1, drift_quality -0.73, mvarStress -0.57, diff_quality -0.53, diff_stress 0.21
#>
#> State dependence of the drift:
#> equation LM df p
#> stress 5.830 3 0.132
#> quality 0.196 3 0.976
#> all 7.520 6 0.374
#>
#> term equation LM p p_adj estimate se
#> stress*stress stress 4.290000 0.0384 0.231 -0.027500 0.01330
#> stress*quality stress 0.845000 0.3580 0.983 0.011300 0.01230
#> stress*quality quality 0.097700 0.7550 0.983 0.003180 0.01020
#> quality*quality stress 0.024800 0.8750 0.983 -0.001030 0.00654
#> quality*quality quality 0.001380 0.9700 0.983 -0.000239 0.00644
#> stress*stress quality 0.000477 0.9830 0.983 NA NA
#> reads
#> DRIFT[stress,stress] varies with stress
#> DRIFT[stress,stress] varies with quality, or DRIFT[stress,quality] with stress
#> DRIFT[quality,stress] varies with quality, or DRIFT[quality,quality] with stress
#> DRIFT[stress,quality] varies with quality
#> DRIFT[quality,quality] varies with quality
#> DRIFT[quality,stress] varies with stress
#>
#> State dependence of the innovation sds:
#> equation LM df p
#> stress 0.0472 2 0.976
#> quality 2.9500 2 0.289
#> all 3.0100 4 0.598
#>
#> term LM p p_adj estimate se
#> sd(quality) x exp(v * quality) 0.397000 0.529 0.989 -0.026600 0.0422
#> sd(quality) x exp(v * stress) 0.252000 0.616 0.989 -0.025400 0.0505
#> sd(stress) x exp(v * stress) 0.029000 0.865 0.989 0.003980 0.0233
#> sd(stress) x exp(v * quality) 0.000198 0.989 0.989 0.000232 0.0165
#>
#> Fixed cells, freed: joint LM = 1.84, df = 3, p = 0.604
#> cell LM p p_adj estimate se
#> MANIFESTVAR[Stress2,Stress] 1.220 0.269 0.492 1.1300 1.0200
#> MANIFESTVAR[Quality,Stress] 0.648 0.421 0.492 -0.0634 0.0788
#> MANIFESTVAR[Quality,Stress2] 0.472 0.492 0.492 0.0389 0.0565
#>
#> Parameters depending on a subject's level (a random intercept):
#> level LM df p
#> cint_stress 3.84 10 0.976
#> cint_quality 1.88 10 0.996
#> all 8.28 20 0.998
#>
#> term LM p p_adj estimate se
#> drift_quality ~ cint_quality 1.7400 0.187 0.995 -0.0300 0.0227
#> drift_quality ~ cint_stress 1.5000 0.221 0.995 0.0358 0.0293
#> diff_quality_stress ~ cint_stress 1.4300 0.232 0.995 -0.1540 0.1290
#> drift_stress ~ cint_stress 1.2300 0.267 0.995 -0.1060 0.0950
#> mvarStress2 ~ cint_stress 0.8380 0.360 0.995 -0.0299 0.0326
#> drift_stress_quality ~ cint_stress 0.1130 0.737 0.995 NA NA
#> mvarStress ~ cint_stress 0.0919 0.762 0.995 NA NA
#> mvarQuality ~ cint_quality 0.0497 0.824 0.995 NA NA
#> diff_quality ~ cint_quality 0.0443 0.833 0.995 NA NA
#> mvarStress2 ~ cint_quality 0.0321 0.858 0.995 NA NANone of the tests is significant after adjusting for the number of
tests, which is what should happen when the model is the one that
generated the data. vignette('score-tests') explains the
tests and their output.