Skip to contents

Get Kalman filter estimates from a ctStanFit object. ctStanKalman is maintained as a backward-compatible alias for this function. Its pre-3.11 partner ctKalman is an alias for ctPredict, not for this function, and returns a long data frame rather than a list of arrays.

Usage

ctKalmanArray(
  fit,
  nsamples = NA,
  pointest = TRUE,
  collapsefunc = NA,
  cores = getOption("mc.cores", 2L),
  subjects = "all",
  timestep = "asdata",
  maxtime = "asdata",
  standardisederrors = FALSE,
  subjectpars = TRUE,
  indvarstates = FALSE,
  removeObs = F,
  realid = TRUE,
  newdata = NULL,
  ...
)

Arguments

fit

fit object from ctFit, from any backend.

nsamples

either NA (to extract all) or a positive integer from 1 to maximum samples in the fit.

pointest

If TRUE, uses the posterior mode as the single sample.

collapsefunc

function to apply over samples, such as mean

cores

Integer number of cpu cores to use. Only needed if savescores was set to FALSE when fitting. backend='stan' only – the julia path runs this loop serially in the engine and refuses cores by name rather than accepting it and ignoring it.

subjects

'all' (the default), or the subjects to compute for, as ids in the data (see realid). The output numbers them in the order given, sorted.

timestep

Either a positive numeric value, 'asdata' to use the times in the dataset, or 'auto' to select a timestep automatically (resulting in some interpolation but not excessive computation).

maxtime

only relevant if timestep is not 'asdata'. Positive numeric denoting max time for computations.

standardisederrors

If TRUE, computes standardised errors for prior, upd, smooth conditions.

subjectpars

if TRUE, state estimates are not returned, instead, predictions of each subjects parameters are returned, for parameters that had random effects specified.

indvarstates

if TRUE, do not remove indvarying states from output

removeObs

Logical or integer. If TRUE, observations (but not covariates) are set to NA, so only expectations based on parameters and covariates are returned. If a positive integer N, every N observations are retained while others are set NA for computing model expectations – useful for observing prediction performance forward further in time than one observation.

realid

If TRUE (the default), subjects are read as the ids in the data, and integers that are not ids there are read as ctsem's numbering of the subjects, 1 to N, with a message. If FALSE, they are read as that numbering.

newdata

Optional long format data, laid out as the data the model was fitted to, to compute for instead of the fit's own: new subjects, the fit's subjects with other covariate values, or a record cut short. The fit's estimates (or, with pointest=FALSE, its draws) are used as they stand, under the model the fit ran; nothing is refitted. See ctPredict.

...

additional arguments to collpsefunc.

Value

list containing Kalman filter elements, each element in array of iterations, data row, variables. llrow is the log likelihood for each row of data.

For a binary or ordinal manifest variable the returned manifest quantities are the *linear predictor* \(\nu = \Lambda \eta + \tau\), on the logit scale, not a probability and not a category. Turn one into a probability with plogis(nu) for binary, or into cumulative category probabilities with plogis(tau_k - nu) for ordinal, taking the thresholds from summary(fit)$popmeans. The manifest covariance for such a variable is likewise the covariance of the linear predictor.

Examples

k=ctKalmanArray(ctstantestfit,subjectpars=TRUE,collapsefunc=mean)