## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  message = FALSE,
  fig.width = 7,
  fig.align = "center"
)

## -----------------------------------------------------------------------------
library(EFAtools)

## -----------------------------------------------------------------------------
# only use a subset to make analyses faster
DOSPERT_sub <- DOSPERT_raw[1:500, ]

## ----warning = FALSE----------------------------------------------------------
efa_screen(DOSPERT_sub, seed = 2)

## -----------------------------------------------------------------------------
# Bartlett's test of sphericity
efa_bartlett(DOSPERT_sub)

# KMO criterion
efa_kmo(DOSPERT_sub)

## -----------------------------------------------------------------------------
# parallel analysis based on SMC eigenvalues
pa <- efa_parallel(DOSPERT_sub, eigen_type = "smc")
pa

## -----------------------------------------------------------------------------
plot(pa)

## -----------------------------------------------------------------------------
# empirical Kaiser criterion
efa_ekc(DOSPERT_sub)

## -----------------------------------------------------------------------------
ret <- efa_retain(DOSPERT_sub,
                  criteria = c("parallel", "ekc", "kgc", "smt", "map"))
ret

## -----------------------------------------------------------------------------
plot(ret)

## -----------------------------------------------------------------------------
efa_retain(test_models$baseline$cormat, N = 500, estimator = "uls",
           criteria = c("parallel", "ekc", "smt"),
           eigen_type_other = c("smc", "pca"))

## -----------------------------------------------------------------------------
efa_fit(DOSPERT_sub, n_factors = 6)

## -----------------------------------------------------------------------------
efa_dospert <- efa_fit(DOSPERT_sub, n_factors = 6, rotation = "promax")
efa_dospert

## -----------------------------------------------------------------------------
efa_uls <- efa_fit(DOSPERT_sub, n_factors = 6, rotation = "oblimin", estimator = "uls")
efa_uls

## -----------------------------------------------------------------------------
efa_compare(efa_dospert$rot_loadings, efa_uls$rot_loadings,
            x_labels = c("PAF and promax", "ULS and oblimin"))

## -----------------------------------------------------------------------------
avg_dospert <- efa_average(DOSPERT_sub, n_factors = 6,
                           estimator = c("paf", "uls", "ml"),
                           type = "EFAtools", start_method = "psych")
avg_dospert

## -----------------------------------------------------------------------------
fac_scores <- efa_scores(DOSPERT_sub, f = efa_dospert)
fac_scores

summary(fac_scores)

## -----------------------------------------------------------------------------
sl_dospert <- efa_schmid_leiman(efa_dospert)
sl_dospert

## -----------------------------------------------------------------------------
efa_reliability(sl_dospert)

## -----------------------------------------------------------------------------
efa_reliability(sl_dospert,
  factor_map = matrix(c(rep(1, 6), rep(0, 24),                  # F1: ethical
                        rep(0, 18), rep(1, 6), rep(0, 6),       # F2: recreational
                        rep(0, 6), 1, 0, 1, 0, 1, rep(0, 19),   # F3: financial (1, 3, 5)
                        rep(0, 12), rep(1, 6), rep(0, 12),      # F4: health
                        rep(0, 24), rep(1, 6),                  # F5: social
                        rep(0, 7), 1, 0, 1, 0, 1, rep(0, 18)),  # F6: financial (2, 4, 6)
                      ncol = 6, byrow = FALSE))

