Skip to contents

For each latent variable in a structural model, add an estimated factor score to observed data.

Usage

add_factor_scores(
  d,
  m,
  mu = 0,
  sigma = 1,
  CI = FALSE,
  p = 0.95,
  names_suffix = "_FS",
  keep_observed_scores = TRUE,
  ...
)

Arguments

d

A data.frame with observed data in standardized form (i.e, z-scores)

m

A character string with lavaan model

mu

Population mean of the observed scores. Factor scores will also have this mean. Defaults to 0.

sigma

Population standard deviation of the observed scores. Factor scores will also have this standard deviation. Defaults to 1.

CI

Add confidence intervals? Defaults to `FALSE`. If `TRUE`, for each factor score, a lower and upper bound of the confidence interval is created. For example, the lower bound of factor score `X` is `X_LB`, and the upper bound is `X_UB`.

p

confidence interval proportion. Defaults to 0.95

names_suffix

A character string added to each factor score name

keep_observed_scores

The observed scores are returned along with the factor scores.

...

parameters passed to simstandardized_matrices

Value

data.frame with observed data and estimated factor scores

Examples

library(simstandard)
# lavaan model
m = "
X =~ 0.9 * X1 + 0.8 * X2 + 0.7 * X3
"

# Make data.frame for two cases
d <- data.frame(
  X1 = c(1.2, -1.2),
  X2 = c(1.5, -1.8),
  X3 = c(1.8, -1.1))

# Compute factor scores for two cases
add_factor_scores(d, m)
#>     X1   X2   X3      X_FS
#> 1  1.2  1.5  1.8  1.435708
#> 2 -1.2 -1.8 -1.1 -1.398951