A ctsem model has two halves. The dynamic half says how the latent processes evolve between observations; the measurement half says how an observation at a time point relates to the latent state then. This vignette is entirely about the second half: how many indicators load on a process, what kind of variable each indicator is, what happens when kinds are mixed, and what to do when a loading or a residual is not a constant.
Everything here uses backend = 'julia'. The non-Gaussian
indicator types (ordinal, count, censored) exist only on that backend,
and binary is integrated there rather than linearised, so a
vignette that switched backends between sections would not be comparing
like with like.
The measurement equation
At each observed time point the filter has a predicted latent state . For every manifest variable observed at that row, ctsem forms a linear predictor
and then attaches an observation model to it, chosen by that
variable’s manifesttype:
manifesttype |
Variable | Observation model | Residual |
|---|---|---|---|
0 |
continuous | free | |
1 |
binary, coded 0/1 | none | |
2 |
ordinal, coded | none | |
3 |
count, non-negative integers | , | free |
4 |
censored | , observed only inside | free |
Four things follow from that table and are worth stating before any code.
Binary and ordinal indicators have no residual
variance. Their randomness is entirely in the link, and a term
added to the linear predictor would not be identified if you wanted one:
under a probit link
exactly, so such a term is absorbed into the loadings and thresholds,
and the logistic link differs only in that the absorption is
approximate. Their rows and columns of MANIFESTVAR are
fixed to zero, and ctFit() fixes any that were left
free.
The other three do have one, and it means something different
in each. For a continuous indicator it is the usual measurement
error variance. For a censored one it is the variance of the Gaussian
that is observed only inside the limits. For a count it is a
dispersion: a Gaussian term added to the log rate, making the
observation Poisson-lognormal rather than Poisson. It is identified
where the binary and ordinal versions of it are not, because a Poisson’s
variance is locked to its mean — the dispersion shifts the mean by
,
which MANIFESTMEANS absorbs, and multiplies the variance by
a factor nothing else in the model can produce. It is the only parameter
in a ctsem model that moves a count’s variance-to-mean ratio, and
leaving it out does not make the data equidispersed: it makes
DIFFUSION absorb the difference, which is usually the
parameter the model was for. Only the diagonal counts — a count row is
scored on its own, so it has no correlated measurement error, and
ctFit() fixes a free off-diagonal to zero.
The scale of the latent process has to come from
somewhere. With a Gaussian indicator you can anchor it by
fixing a loading, or by fixing DIFFUSION. With only binary
or ordinal indicators there is no free residual to trade against, so a
loading and the process scale are two names for one quantity: fix the
loadings and let DIFFUSION carry the scale.
Location works the same way. CINT and
T0MEANS set where the process sits, and each indicator’s
MANIFESTMEANS sets where that indicator sits relative to
it. ctsem’s default fixes CINT and T0MEANS to
zero and frees MANIFESTMEANS, which is what the examples
below use — ordinal indicators included. An ordinal indicator’s
thresholds are also locations, so one among them is redundant:
adding a constant to
and to every
leaves the cumulative logit unchanged. ctsem resolves that by pinning
at zero, so the location of an ordinal indicator lives in its
MANIFESTMEANS like everyone else’s, and what
THRESHOLDS estimates is the K-2 gaps between successive
thresholds. The point of putting it there rather than in
is that MANIFESTMEANS can be indvarying: one
random effect then shifts all of an indicator’s category boundaries
together, which is what “this person answers higher” means, where a
random effect on the thresholds would need one per boundary.
One indicator or many
A univariate Ornstein-Uhlenbeck process, measured three times over at each occasion with loadings 1, 0.8 and 1.2:
set.seed(3)
genmulti <- ctModel(type = 'ct', n.latent = 1, n.manifest = 3,
manifestNames = c('y1', 'y2', 'y3'), latentNames = 'eta1',
LAMBDA = matrix(c(1, 0.8, 1.2), 3, 1),
DRIFT = matrix(-0.4), DIFFUSION = matrix(1.5), T0VAR = matrix(1.7),
T0MEANS = matrix(0), CINT = matrix(0),
MANIFESTMEANS = matrix(c(0, 0.5, -0.5), 3, 1),
MANIFESTVAR = diag(0.8, 3), Tpoints = 10)
dmulti <- data.frame(ctGenerate(genmulti, n = 50, Tpoints = 10,
backend = 'julia'))
head(dmulti, 3)
#> id time y1 y2 y3
#> 1 1 0 -3.347169 -1.8190194 -2.395708
#> 2 1 1 -1.309557 -0.5163224 -2.078168
#> 3 1 2 -1.175804 0.1784173 -1.663167A single indicator
With one indicator per process the loading is not estimable and is
fixed, by convention to 1. That does not mean measurement error
is inestimable: in a continuous time model the measurement error and the
process innovation have different signatures across the time interval,
so MANIFESTVAR and DIFFUSION remain separately
identified.
msingle <- ctModel(type = 'ct', n.latent = 1, n.manifest = 1,
manifestNames = 'y1', latentNames = 'eta1', LAMBDA = matrix(1),
CINT = matrix(0), T0MEANS = matrix(0))
msingle$pars$indvarying <- FALSE
fsingle <- ctFit(dmulti, msingle, backend = 'julia', cores = 2)
summary(fsingle)$popmeans
#> mean sd 2.5% 50% 97.5%
#> drift_eta1 -0.328 0.092 -0.533 -0.316 -0.186
#> diff_eta1 1.347 0.214 0.968 1.325 1.798
#> mvary1 0.972 0.132 0.738 0.964 1.258
#> mm_y1 0.053 0.160 -0.252 0.061 0.368
#> T0var_eta1 1.670 0.239 1.245 1.656 2.187Three indicators
Freeing the two remaining loadings and keeping the first at 1 makes
eta1 a common factor. The first indicator’s scale is now
the latent’s scale, which is what the fixed 1 buys:
mmulti <- ctModel(type = 'ct', n.latent = 1, n.manifest = 3,
manifestNames = c('y1', 'y2', 'y3'), latentNames = 'eta1',
LAMBDA = matrix(c(1, 'lambda_y2', 'lambda_y3'), 3, 1),
CINT = matrix(0), T0MEANS = matrix(0))
mmulti$pars$indvarying <- FALSE
fmulti <- ctFit(dmulti, mmulti, backend = 'julia', cores = 2)
summary(fmulti)$popmeans
#> mean sd 2.5% 50% 97.5%
#> lambda_y2 0.794 0.027 0.742 0.793 0.846
#> lambda_y3 1.192 0.034 1.127 1.192 1.257
#> drift_eta1 -0.414 0.058 -0.539 -0.412 -0.310
#> diff_eta1 1.589 0.082 1.439 1.589 1.750
#> mvary1 0.798 0.037 0.729 0.798 0.874
#> mvary2 0.784 0.032 0.722 0.784 0.846
#> mvary3 0.756 0.047 0.667 0.756 0.851
#> mm_y1 0.071 0.150 -0.222 0.069 0.366
#> mm_y2 0.615 0.123 0.376 0.611 0.856
#> mm_y3 -0.416 0.180 -0.761 -0.416 -0.060
#> T0var_eta1 1.751 0.191 1.419 1.740 2.155The loadings come back near their generating values, and the dynamic parameters are estimated more sharply than from one indicator alone:
s1 <- summary(fsingle)$popmeans
s3 <- summary(fmulti)$popmeans
pars <- c('drift_eta1', 'diff_eta1')
data.frame(
parameter = pars,
truth = c(-0.4, 1.5),
single = s1[pars, 'mean'], single_se = s1[pars, 'sd'],
three = s3[pars, 'mean'], three_se = s3[pars, 'sd'],
row.names = NULL)
#> parameter truth single single_se three three_se
#> 1 drift_eta1 -0.4 -0.328 0.092 -0.414 0.058
#> 2 diff_eta1 1.5 1.347 0.214 1.589 0.082Anchoring, and where the optimiser starts
Fixing
LAMBDA[1,1]is not the only way to set the scale — fixingDIFFUSIONto 1 and freeing all three loadings fits the same model, and reports loadings on the process’s own scale instead of ony1’s.The two are equivalent in principle but not always in practice, because where the optimiser starts matters.
ctFitreads the starting values for the variance diagonals off the data by default, throughoptimcontrol$datastart. Before that it began every fit from a fixed point that putDIFFUSIONnear 7 whatever the data were measured in, and on data at a much smaller scale a loading-anchored model would collapse the latent to zero variance and explain the whole signal as measurement error — a local optimum, with the loadings running off to implausible magnitudes to compensate. On the data above rescaled by 1/100 that failed on four starting seeds out of four, and converges on four out of four now.It is not a guarantee. A model can sit near a basin boundary at a given scale, where the small random jitter ctsem still adds to the start is enough to decide which optimum the fit reaches — on the same data rescaled by 100, two seeds in four land 328 log units apart, with or without the derived start. So a fit reporting non-convergence, or loadings nowhere near anything plausible, is worth re-running, and worth checking against
DIFFUSION-anchored scaling or explicitinits.
The five indicator types
To see each type on its own, generate one latent trajectory and then measure it five different ways. Generating the process with a near-zero measurement variance makes the single manifest variable a direct readout of the latent state, which the indicators below are built from:
set.seed(5)
nsub <- 80; nobs <- 10
process <- ctModel(type = 'ct', n.latent = 1, n.manifest = 1,
manifestNames = 'eta', latentNames = 'eta1', LAMBDA = matrix(1),
DRIFT = matrix(-0.4), DIFFUSION = matrix(1), T0VAR = matrix(1.12),
T0MEANS = matrix(0), CINT = matrix(0), MANIFESTMEANS = matrix(0),
MANIFESTVAR = matrix(1e-6), Tpoints = nobs)
d <- data.frame(ctGenerate(process, n = nsub, Tpoints = nobs,
backend = 'r'))
eta <- d$etaTwo indicators of each kind, built by applying each row of the table above by hand, so that the link in the model and the link in the simulation are visibly the same thing:
invlogit <- function(x) 1 / (1 + exp(-x))
ordinaldraw <- function(mu, tau) { # cumulative logit
cum <- sapply(tau, function(k) invlogit(k - mu))
p <- cbind(cum, 1)
p <- cbind(p[, 1, drop = FALSE], t(apply(p, 1, diff)))
apply(p, 1, function(pr) sample.int(length(pr), 1, prob = pmax(pr, 0)))
}
tau <- c(-1, 0, 1) # four categories
d$y1 <- 0.0 + eta + rnorm(nrow(d), 0, 0.6) # 0 continuous
d$y2 <- 0.5 + eta + rnorm(nrow(d), 0, 0.6)
d$b1 <- rbinom(nrow(d), 1, invlogit(-0.4 + eta)) # 1 binary
d$b2 <- rbinom(nrow(d), 1, invlogit( 0.4 + eta))
d$o1 <- ordinaldraw(eta, tau) # 2 ordinal
d$o2 <- ordinaldraw(eta, tau)
disp <- 0.3 # count dispersion, on the log rate
d$c1 <- rpois(nrow(d), # 3 count
exp(1.4 + 0.5 * eta + rnorm(nrow(d), 0, disp)))
d$c2 <- rpois(nrow(d), exp(1.2 + 0.5 * eta + rnorm(nrow(d), 0, disp)))
d$x1 <- pmin(pmax(2.0 + eta + rnorm(nrow(d), 0, 0.5), 0), 4) # 4 censored
d$x2 <- pmin(pmax(1.8 + eta + rnorm(nrow(d), 0, 0.5), 0), 4)
d$eta <- NULL
head(d, 3)
#> id time y1 y2 b1 b2 o1 o2 c1 c2 x1 x2
#> 1 1 0 -1.426062341 0.59163773 0 0 2 3 4 1 0.6966535 0.5905002
#> 2 1 1 1.752632464 0.03085567 1 1 2 4 5 3 2.3802010 1.5137950
#> 3 1 2 0.006634949 -0.84232851 1 0 2 4 1 5 0.9411608 1.0590349One subject, seen through all five instruments at once:
shown <- c('y1', 'b1', 'o1', 'c1', 'x1')
long <- do.call(rbind, lapply(shown, function(v)
data.frame(time = d$time[d$id == 2], value = d[[v]][d$id == 2],
indicator = v)))
long$indicator <- factor(long$indicator, levels = shown,
labels = c('y1 continuous', 'b1 binary', 'o1 ordinal', 'c1 count',
'x1 censored [0,4]'))
ggplot(long, aes(time, value)) +
geom_line(colour = 'grey60') + geom_point(size = 2) +
facet_wrap(~indicator, scales = 'free_y', ncol = 2) +
theme_bw() + labs(x = 'time', y = NULL)
Specifying each type
manifesttype is a vector with one entry per manifest
variable. Two of the types need something extra alongside it, and both
are needed because ctModel() never sees the data:
-
ordinal needs
ncategories, so that the right number of thresholds can be created.ctGenerate()needs it for the same reason. -
censored needs
censorminandcensormax. These are known constants rather than parameters: an instrument’s floor and ceiling are properties of the instrument, and the data cannot inform them. A variable censored on one side only leaves the other infinite.
A helper, since the five models below differ only in which columns they take:
buildmodel <- function(names, type, lambda = 1, ncat = 0,
cmin = -Inf, cmax = Inf) {
n <- length(names)
# binary and ordinal are the two types with no residual of any kind
mvar <- diag(0, n)
if (type %in% c(0, 3, 4)) diag(mvar) <- paste0('mvar_', names)
m <- ctModel(type = 'ct', n.latent = 1, n.manifest = n,
manifestNames = names, latentNames = 'eta1',
manifesttype = rep(type, n), ncategories = rep(ncat, n),
censormin = rep(cmin, n), censormax = rep(cmax, n),
LAMBDA = matrix(lambda, n, 1),
MANIFESTMEANS = matrix(paste0('mm_', names), n, 1), MANIFESTVAR = mvar,
CINT = matrix(0), T0MEANS = matrix(0))
m$pars$indvarying <- FALSE
m
}
models <- list(
continuous = buildmodel(c('y1', 'y2'), 0),
binary = buildmodel(c('b1', 'b2'), 1),
ordinal = buildmodel(c('o1', 'o2'), 2, ncat = 4),
count = buildmodel(c('c1', 'c2'), 3, lambda = 0.5),
censored = buildmodel(c('x1', 'x2'), 4, cmin = 0, cmax = 4))Every one of these fixes its loadings rather than freeing them, so that the latent scale is anchored the same way in all five and the dynamic estimates below can be read side by side.
The count pair is fixed at 0.5 rather than 1, and that is a choice
worth making deliberately. A count’s loading sets how far its
log rate travels as the process moves, and the exponential
turns a wide latent range into a very skewed observation. The filter
carries a Gaussian summary of the state, and a Poisson update is well
summarised that way only while the rate stays in a moderate range.
Measured on this design at 200 subjects and 12 occasions: a count read
directly off a process with a standard deviation of 1.12 – log rates
spanning about seven units, rates from 0.05 to 50 – recovers
DRIFT at -0.20 and -0.32 on two seeds against a generating
-0.4, and DIFFUSION at 0.69 and 0.74 against 1. The same
process at a standard deviation of 0.45 recovers -0.407 and -0.418, and
its DIFFUSION to within 0.012. Halving the loading halves
the range the link sees, which is why the count column below is as
accurate as the others. If your counts genuinely do swing over orders of
magnitude, treat the dynamic estimates from them with the same
suspicion.
The count models here also estimate a MANIFESTVAR
diagonal, which for a count is the dispersion rather than a measurement
error: a Gaussian term on the log rate, so the observation is
Poisson-lognormal. The counts above were generated with one, of 0.3.
Freeing it is worth doing on real counts whatever you expect, because
the alternative is not a Poisson assumption quietly satisfied — it is
DIFFUSION absorbing whatever overdispersion there is, and
DIFFUSION is usually the parameter the model was for.
The same process, through five instruments
fits <- lapply(models, function(m) ctFit(d, m, backend = 'julia', cores = 2))
pull <- function(f, p, what) summary(f)$popmeans[p, what]
data.frame(
type = names(fits),
drift = sapply(fits, pull, 'drift_eta1', 'mean'),
drift_se = sapply(fits, pull, 'drift_eta1', 'sd'),
diffusion = sapply(fits, pull, 'diff_eta1', 'mean'),
diffusion_se = sapply(fits, pull, 'diff_eta1', 'sd'),
row.names = NULL)
#> type drift drift_se diffusion diffusion_se
#> 1 continuous -0.442 0.052 1.052 0.049
#> 2 binary -0.289 0.119 0.698 0.230
#> 3 ordinal -0.346 0.113 0.888 0.188
#> 4 count -0.356 0.061 0.966 0.081
#> 5 censored -0.436 0.050 1.046 0.045Every row of that table estimates the same two generating values,
and
,
from the same latent trajectory, and every row is within about one
standard error of them. What differs is how much of the trajectory each
instrument transmits, which is visible in the standard errors rather
than in the estimates: two binary items per occasion carry a few bits
about a continuous state and are the least precise by a factor of two,
while the continuous, censored and count pairs each recover
DRIFT to about 0.05.
The parameters specific to a type appear in the same summary. An
ordinal indicator’s location is in MANIFESTMEANS;
THRESHOLDS holds the first threshold, pinned at zero and
not reported, and then the gap to each subsequent one,
which is what keeps them ordered. So a four category variable
contributes one mean and two gaps:
thr <- summary(fits$ordinal)$popmeans
gaps <- grep('threshold_o1', rownames(thr), value = TRUE)
thr[c('mm_o1', gaps), c('mean', 'sd')]
#> mean sd
#> mm_o1 0.940 0.118
#> threshold_o1_2 0.999 0.079
#> threshold_o1_3 0.945 0.079
# cumulate the gaps from the pinned zero, then subtract the mean, to read the
# thresholds on the latent scale
cumsum(c(0, thr[gaps, 'mean'])) - thr['mm_o1', 'mean'] # against c(-1, 0, 1)
#> [1] -0.940 0.059 1.004A censored indicator’s residual standard deviation is the one that means what it usually means. The model recovers it from the uncensored part of the scale, while treating a value sitting at a limit as the probability of being there rather than as a density:
Combining types
Several types on one latent
Nothing stops indicators of different types from loading on the same process. Each contributes its own likelihood term against the same predicted state, and the filter’s update at that row takes all of them:
mixednames <- c('y1', 'b1', 'o1', 'c1', 'x1')
mvar <- diag(0, 5)
mvar[1, 1] <- 'mvar_y1' # measurement error
mvar[4, 4] <- 'mvar_c1' # the count's dispersion
mvar[5, 5] <- 'mvar_x1' # the scale inside the censoring limits
mmixed <- ctModel(type = 'ct', n.latent = 1, n.manifest = 5,
manifestNames = mixednames, latentNames = 'eta1',
manifesttype = c(0, 1, 2, 3, 4),
ncategories = c(0, 0, 4, 0, 0),
censormin = c(-Inf, -Inf, -Inf, -Inf, 0),
censormax = c( Inf, Inf, Inf, Inf, 4),
LAMBDA = matrix(c(1, 1, 1, 0.5, 1), 5, 1), # the count as generated
MANIFESTMEANS = matrix(paste0('mm_', mixednames), 5, 1),
MANIFESTVAR = mvar, CINT = matrix(0), T0MEANS = matrix(0))
mmixed$pars$indvarying <- FALSE
fmixed <- ctFit(d, mmixed, backend = 'julia', cores = 2)
summary(fmixed)$popmeans
#> mean sd 2.5% 50% 97.5%
#> drift_eta1 -0.450 0.049 -0.551 -0.449 -0.360
#> diff_eta1 1.036 0.044 0.949 1.036 1.119
#> mvar_y1 0.621 0.022 0.580 0.622 0.663
#> mvar_c1 0.319 0.027 0.269 0.317 0.373
#> mvar_x1 0.496 0.024 0.449 0.495 0.541
#> mm_y1 0.131 0.078 -0.024 0.133 0.277
#> mm_b1 -0.222 0.111 -0.444 -0.219 -0.013
#> mm_o1 0.946 0.110 0.726 0.949 1.152
#> mm_c1 1.463 0.042 1.378 1.464 1.539
#> mm_x1 2.083 0.077 1.927 2.084 2.228
#> T0var_eta1 1.176 0.103 0.991 1.174 1.387
#> threshold_o1_2 1.004 0.073 0.871 1.005 1.148
#> threshold_o1_3 0.954 0.072 0.831 0.951 1.101One indicator of each kind recovers the process about as precisely as
the best same-kind pair above, and roughly twice as precisely as the
binary or ordinal pairs – with the thresholds, the three
MANIFESTVAR entries and five intercepts all estimated
alongside. That is the practical argument for using indicators of every
kind you have, rather than keeping only whichever are convenient to
model.
Several types on different latents
More usually the types are not interchangeable measures of one thing.
An ordinal questionnaire item and a binary event indicator measure one
construct; a count, a censored scale score and a plain continuous
variable measure another; and the question is how the two constructs
drive each other. That is a two-process model with a block-structured
LAMBDA.
set.seed(11)
twonames <- c('o1', 'b1', 'c1', 'x1', 'y1')
lambda2 <- matrix(0, 5, 2)
lambda2[1:2, 1] <- 1 # ordinal and binary measure eta1
lambda2[3:5, 2] <- 1 # count, censored and continuous measure eta2
genvar <- diag(0, 5)
genvar[3, 3] <- 0.3 # the count's dispersion, on its log rate
genvar[4, 4] <- 0.4
genvar[5, 5] <- 0.6
gen2 <- ctModel(type = 'ct', n.latent = 2, n.manifest = 5,
manifestNames = twonames, latentNames = c('eta1', 'eta2'),
manifesttype = c(2, 1, 3, 4, 0), ncategories = c(4, 0, 0, 0, 0),
censormin = c(-Inf, -Inf, -Inf, 0, -Inf),
censormax = c(Inf, Inf, Inf, 4, Inf),
LAMBDA = lambda2,
DRIFT = matrix(c(-0.4, 0.25, -0.15, -0.7), 2, 2, byrow = TRUE),
DIFFUSION = diag(0.8, 2), T0VAR = diag(1, 2), T0MEANS = matrix(0, 2, 1),
CINT = matrix(0, 2, 1),
MANIFESTMEANS = matrix(c(1, -0.4, 0.5, 2.0, 0.3), 5, 1),
MANIFESTVAR = genvar, Tpoints = 10)
# The first threshold is pinned at zero and the rest are gaps, so thresholds of
# -1, 0 and 1 are a manifest mean of 1 with both gaps at 1.
gen2$pars$value[gen2$pars$matrix %in% 'THRESHOLDS' & gen2$pars$col > 1] <- 1
d2 <- data.frame(ctGenerate(gen2, n = 60, Tpoints = 10,
backend = 'julia'))
head(d2, 3)
#> id time o1 b1 c1 x1 y1
#> 1 1 0 1 0 7 2.183019 0.5605175
#> 2 1 1 1 0 1 1.688552 -1.3622011
#> 3 1 2 2 0 1 1.867846 0.3068517ctGenerate(backend = 'julia') simulates the latent
process and draws each observation from its measurement model given the
state, so it applies the links above rather than producing continuous
values for a variable the model calls binary. The R generator integrates
a linear Gaussian system and has no notion of a link; it warns if asked
to generate from a model with non-Gaussian indicators.
The estimation model has the same block structure, with the measurement parameters freed:
estvar <- diag(0, 5)
estvar[3, 3] <- 'mvar_c1'
estvar[4, 4] <- 'mvar_x1'
estvar[5, 5] <- 'mvar_y1'
m2 <- ctModel(type = 'ct', n.latent = 2, n.manifest = 5,
manifestNames = twonames, latentNames = c('eta1', 'eta2'),
manifesttype = c(2, 1, 3, 4, 0), ncategories = c(4, 0, 0, 0, 0),
censormin = c(-Inf, -Inf, -Inf, 0, -Inf),
censormax = c(Inf, Inf, Inf, 4, Inf),
LAMBDA = lambda2,
MANIFESTMEANS = matrix(paste0('mm_', twonames), 5, 1),
MANIFESTVAR = estvar, CINT = matrix(0, 2, 1), T0MEANS = matrix(0, 2, 1))
m2$pars$indvarying <- FALSE
f2 <- ctFit(d2, m2, backend = 'julia', cores = 2)
summary(f2)$popmeans
#> mean sd 2.5% 50% 97.5%
#> drift_eta1 -0.369 0.135 -0.664 -0.346 -0.167
#> drift_eta1_eta2 0.441 0.147 0.145 0.440 0.732
#> drift_eta2_eta1 -0.178 0.064 -0.303 -0.178 -0.058
#> drift_eta2 -0.644 0.083 -0.817 -0.641 -0.498
#> diff_eta1 0.890 0.219 0.523 0.872 1.335
#> diff_eta2_eta1 -0.022 0.170 -0.362 -0.026 0.313
#> diff_eta2 0.779 0.045 0.699 0.778 0.873
#> mvar_c1 0.336 0.053 0.245 0.332 0.450
#> mvar_x1 0.405 0.023 0.360 0.405 0.449
#> mvar_y1 0.597 0.022 0.555 0.597 0.640
#> mm_o1 0.924 0.134 0.668 0.928 1.183
#> mm_b1 -0.737 0.134 -0.998 -0.741 -0.456
#> mm_c1 0.526 0.056 0.413 0.524 0.631
#> mm_x1 2.093 0.046 2.003 2.093 2.181
#> mm_y1 0.380 0.048 0.283 0.380 0.467
#> T0var_eta1 1.290 0.303 0.769 1.255 1.976
#> T0var_eta2_eta1 -0.341 0.210 -0.757 -0.336 0.059
#> T0var_eta2 0.809 0.084 0.662 0.808 0.979
#> threshold_o1_2 1.054 0.092 0.885 1.049 1.238
#> threshold_o1_3 0.986 0.093 0.815 0.979 1.184The cross-effects are the point of such a model, and they are estimated from indicators of four different kinds at once:
p2 <- summary(f2)$popmeans
drifts <- c('drift_eta1', 'drift_eta1_eta2', 'drift_eta2_eta1', 'drift_eta2')
data.frame(
effect = c('eta1 on itself', 'eta2 on eta1', 'eta1 on eta2', 'eta2 on itself'),
truth = c(-0.4, 0.25, -0.15, -0.7),
estimate = p2[drifts, 'mean'],
se = p2[drifts, 'sd'],
row.names = NULL)
#> effect truth estimate se
#> 1 eta1 on itself -0.40 -0.369 0.135
#> 2 eta2 on eta1 0.25 0.441 0.147
#> 3 eta1 on eta2 -0.15 -0.178 0.064
#> 4 eta2 on itself -0.70 -0.644 0.083ctKalman() reconstructs the latent trajectories from the
mixed indicators as it would from any others:
State-dependent measurement
A LAMBDA or MANIFESTVAR cell need not be a
constant. Writing a latent process name into the cell makes it an
expression evaluated at the current state, so an instrument can become
more or less sensitive, or noisier, as the process moves.
Free parameters used inside such an expression have to live in the
PARS matrix: a cell containing an expression is the
specification itself rather than a labelled parameter, so ctsem resolves
names appearing in it against PARS. PARS
entries are identity-transformed and unbounded, which is why the
residual below is written as exp(...) — that is what keeps
a standard deviation positive.
Data where the second indicator’s loading rises with the process, and the first indicator’s residual standard deviation rises with it too:
set.seed(9)
gensd <- ctModel(type = 'ct', n.latent = 1, n.manifest = 2,
manifestNames = c('y1', 'y2'), latentNames = 'eta1',
LAMBDA = matrix(c(1, '0.9 + 0.35 * eta1'), 2, 1),
MANIFESTVAR = matrix(c('exp(-0.7 + 0.3 * eta1)', 0,
0, 0.8), 2, 2),
DRIFT = matrix(-0.4), DIFFUSION = matrix(1.5), T0VAR = matrix(1.7),
T0MEANS = matrix(0), CINT = matrix(0),
MANIFESTMEANS = matrix(c(0, 0.5), 2, 1), Tpoints = 10)
dsd <- data.frame(ctGenerate(gensd, n = 60, Tpoints = 10,
backend = 'julia'))
head(dsd, 3)
#> id time y1 y2
#> 1 1 0 -1.617416 0.1401635
#> 2 1 1 -1.797942 -0.4742866
#> 3 1 2 -1.316891 -0.6378433The estimation model writes the same two expressions, with parameter names in place of the generating constants:
msd <- ctModel(type = 'ct', n.latent = 1, n.manifest = 2,
manifestNames = c('y1', 'y2'), latentNames = 'eta1',
LAMBDA = matrix(c(1, 'lam0 + lamslope * eta1'), 2, 1),
MANIFESTVAR = matrix(c('exp(logsd0 + logsdslope * eta1)', 0,
0, 'mvar_y2'), 2, 2),
MANIFESTMEANS = matrix(c(0, 'mm_y2'), 2, 1),
CINT = matrix(0), T0MEANS = matrix(0),
PARS = matrix(c('lam0', 'lamslope', 'logsd0', 'logsdslope'), 4, 1))
msd$pars$indvarying <- FALSE
fsd <- ctFit(dsd, msd, backend = 'julia', cores = 2)
summary(fsd)$popmeans
#> mean sd 2.5% 50% 97.5%
#> drift_eta1 -0.449 0.060 -0.574 -0.448 -0.347
#> diff_eta1 1.538 0.086 1.373 1.535 1.714
#> mvar_y2 1.189 0.042 1.106 1.188 1.270
#> mm_y2 1.140 0.054 1.036 1.139 1.246
#> T0var_eta1 1.992 0.190 1.650 1.987 2.384
#> lam0 0.803 0.040 0.723 0.803 0.879
#> lamslope 0.234 0.026 0.183 0.234 0.286
#> logsd0 -0.761 0.177 -1.110 -0.762 -0.426
#> logsdslope 0.207 0.105 0.020 0.205 0.403Against the generating values:
psd <- summary(fsd)$popmeans
sdpars <- c('lam0', 'lamslope', 'logsd0', 'logsdslope')
data.frame(
parameter = sdpars,
meaning = c('loading on y2 at eta1 = 0',
'change in that loading per unit of eta1',
'log residual sd of y1 at eta1 = 0',
'change in that log sd per unit of eta1'),
truth = c(0.9, 0.35, -0.7, 0.3),
estimate = psd[sdpars, 'mean'],
row.names = NULL)
#> parameter meaning truth estimate
#> 1 lam0 loading on y2 at eta1 = 0 0.90 0.803
#> 2 lamslope change in that loading per unit of eta1 0.35 0.234
#> 3 logsd0 log residual sd of y1 at eta1 = 0 -0.70 -0.761
#> 4 logsdslope change in that log sd per unit of eta1 0.30 0.207ctModelIsNonlinear() reports that such a model has cells
with no single value, so every summary of it is a reading at one
particular state:
ctModelIsNonlinear(fsd)
#> [1] TRUEWhich is why the honest picture of a state-dependent model is a curve
rather than a number. ctStateDependencePlot() finds the
cells that actually move — by materialising every model matrix across a
grid of states and keeping the ones whose value changes, so dependence
arriving indirectly, through a PARS entry or through a
covariance built from a dependent cell, is caught too — and draws each
of them against the process:
ctStateDependencePlot(fsd, along = 'eta1', nsamples = 30)
One panel is the loading on y2, rising with
eta1. The other is the residual variance of
y1 — MANIFESTcov rather than
MANIFESTVAR, since that is the covariance the filter
actually uses — rising with it as well. A summary() of this
model reports one point from each of those curves.
Only LAMBDA and MANIFESTVAR are
state-dependent here, but the same mechanism applies to
MANIFESTMEANS, and on the dynamic side to
DRIFT, DIFFUSION and CINT.
Summary
-
manifesttypepicks the observation model per indicator:0continuous,1binary,2ordinal (withncategories),3count,4censored (withcensorminandcensormax). Everything but0needsbackend = 'julia'. - Continuous, count and censored indicators each have a free
MANIFESTVARdiagonal, meaning a measurement error variance, a log-scale dispersion, and the variance inside the limits respectively. Binary and ordinal have none; fix theirs to zero. - An ordinal indicator’s location is its
MANIFESTMEANS, like every other indicator’s.THRESHOLDSpins the first threshold at zero and estimates the K-2 gaps, so a person-level shift of the whole category scale is one random effect on the mean rather than one per boundary. - Anchor the latent scale exactly once: a fixed loading, or a fixed
DIFFUSION. With no Gaussian indicator on a process it has to be the loadings. - A count indicator’s loading sets how far its log rate travels. Keep that range moderate – the filter’s Gaussian summary of a Poisson update degrades once the rate swings over orders of magnitude, and the dynamic parameters come back attenuated.
- Types can be mixed freely, on one process or across several.
- A
LAMBDAorMANIFESTVARcell written as an expression in a latent process name is evaluated at the current state; free parameters inside it go inPARS, andctStateDependencePlot()shows what the result claims.