lavPredict: Predict the values of latent variables (and their...

View source: R/lav_predict.R

lavPredictR Documentation

Predict the values of latent variables (and their indicators).

Description

The main purpose of the lavPredict() function is to compute (or ‘predict’) estimated values for the latent variables in the model (‘factor scores’). NOTE: the goal of this function is NOT to predict future values of dependent variables as in the regression framework! (For models with only continuous observed variables, the function lavPredictY() supports this.)

Usage

lavPredict(object, newdata = NULL, type = "lv", method = "EBM",
           transform = FALSE, se = "none", acov = "none", 
           label = TRUE, fsm = FALSE, mdist = FALSE, rel = FALSE,
           append.data = FALSE, assemble = FALSE,
           level = 1L, optim.method = "bfgs", ETA = NULL,
           drop.list.single.group = TRUE)

Arguments

object

An object of class lavaan.

newdata

An optional data.frame, containing the same variables as the data.frame used when fitting the model in object.

type

A character string. If "lv", estimated values for the latent variables in the model are computed. If "ov", model predicted values for the indicators of the latent variables in the model are computed. If "yhat", the estimated value for the observed indicators, given user-specified values for the latent variables provided by de ETA argument. If "fy", densities (or probabilities) for each observed indicator, given user-specified values for the latent variables provided by de ETA argument.

method

A character string. In the linear case (when the indicators are continuous), the possible options are "regression" or "Bartlett". In the categorical case, the two options are "EBM" for the Empirical Bayes Modal approach, and "ML" for the maximum likelihood approach.

transform

Logical. If TRUE, transform the factor scores (per group) so that their mean and variance-covariance matrix matches the model-implied mean and variance-covariance matrix. This may be useful if the individual factor scores will be used in a follow-up (regression) analysis. Note: the standard errors (if requested) and the factor score matrix (if requested) are not transformed (yet).

se

Character. If "none", no standard errors are computed. If "standard", naive standard errors are computed (assuming the parameters of the measurement model are known). The standard errors are returned as an attribute. Currently only available for complete continuous data.

acov

Similar to the "se" argument, but optionally returns the full sampling covariance matrix of factor scores as an attribute. Currently only available for complete continuous data.

label

Logical. If TRUE, the columns in the output are labeled.

fsm

Logical. If TRUE, return the factor score matrix as an attribute. Only for numeric data.

mdist

Logical. If TRUE, the (squared) Mahalanobis distances of the factor scores (if type = "lv") or the casewise residuals (if type = "resid") are returned as an attribute.

rel

Logical. Only used if type = "lv". If TRUE, the factor reliabilities are returned as an attribute. (The squared values are often called the factor determinacies.)

append.data

Logical. Only used when type = "lv". If TRUE, the original data (or the data provided in the newdata argument) is appended to the factor scores.

assemble

Logical. If TRUE, the separate multiple groups are reassembled again to form a single data.frame with a group column, having the same dimensions are the original (or newdata) dataset.

level

Integer. Only used in a multilevel SEM. If level = 1, only factor scores for latent variable defined at the first (within) level are computed; if level = 2, only factor scores for latent variables defined at the second (between) level are computed.

optim.method

Character string. Only used in the categorical case. If "nlminb" (the default in 0.5), the "nlminb()" function is used for the optimization. If "bfgs" or "BFGS" (the default in 0.6), the "optim()" function is used with the BFGS method.

ETA

An optional matrix or list, containing latent variable values for each observation. Used for computations when type = "ov".

drop.list.single.group

Logical. If FALSE, the results are returned as a list, where each element corresponds to a group (even if there is only a single group). If TRUE, the list will be unlisted if there is only a single group.

Details

The predict() function calls the lavPredict() function with its default options.

If there are no latent variables in the model, type = "ov" will simply return the values of the observed variables. Note that this function can not be used to ‘predict’ values of dependent variables, given the values of independent values (in the regression sense). In other words, the structural component is completely ignored (for now).

See Also

lavPredictY to predict y-variables given x-variables.

Examples

data(HolzingerSwineford1939)

## fit model
HS.model <- ' visual  =~ x1 + x2 + x3
              textual =~ x4 + x5 + x6
              speed   =~ x7 + x8 + x9 '

fit <- cfa(HS.model, data = HolzingerSwineford1939)
head(lavPredict(fit))
head(lavPredict(fit, type = "ov"))


## ------------------------------------------
## merge factor scores to original data.frame
## ------------------------------------------

idx <- lavInspect(fit, "case.idx")
fscores <- lavPredict(fit)
## loop over factors
for (fs in colnames(fscores)) {
  HolzingerSwineford1939[idx, fs] <- fscores[ , fs]
}
head(HolzingerSwineford1939)


## multigroup models return a list of factor scores (one per group)
data(HolzingerSwineford1939)
mgfit <- update(fit, group = "school", group.equal = c("loadings","intercepts"))

idx <- lavInspect(mgfit, "case.idx") # list: 1 vector per group
fscores <- lavPredict(mgfit)         # list: 1 matrix per group
## loop over groups and factors
for (g in seq_along(fscores)) {
  for (fs in colnames(fscores[[g]])) {
    HolzingerSwineford1939[ idx[[g]], fs] <- fscores[[g]][ , fs]
  }
}
head(HolzingerSwineford1939)

## -------------------------------------
## Use factor scores in susequent models
## -------------------------------------

## see Examples in semTools package: ?plausibleValues

lavaan documentation built on Sept. 27, 2024, 9:07 a.m.