knitr::opts_chunk$set(error = TRUE, message = FALSE, warning = TRUE)

Last updated: r format(Sys.Date(), "%B %d %Y")

See Package References{target="_blank"} for help documentation on semoutput functions

NOTE: We will use two data sets in this example output. They come from the lavaan tutorial data sets; the cfa data set isHolzingerSwineford1939 and the sem data set is PoliticalDemocracy

NOTE: You can download the RMarkdown file associated with this output by selecting the 'Code' button on the top-right of this document and selecting "Download Rmd". You can also display the code used to produce each output by selecting the 'Code' buttons along the right of the document.

NOTE: For each CFA or SEM model output block there are 4 tabs:

Once you install the package, you will be able to access an Rmarkdown template by going to:

File -> New File -> R Markdown... -> From Template -> CFA/SEM (lavaan)

Setup

Required Packages

library(readr)
library(here)
library(dplyr)
library(lavaan)
library(psych)
library(semoutput)
library(semPlot)
library(sjPlot)

Import Data

## Import Data
# data <- read_csv(here("relative file path", "file name"))
cfa_data <- dplyr::select(HolzingerSwineford1939, -id, -school)
sem_data <- PoliticalDemocracy



Descriptives

Typically there is only one descriptive table displayed but since we have two data sets for this one output two descriptive tables are displayed.

# Prints basic descriptive statistics
sem_descriptives(cfa_data)
sem_descriptives(sem_data)

Correlation Matrix

There are also two correlation matrices

This is a publication quality correlation matrix that can be inserted into a manuscript.

# Uses sjPlot to print a nice looking correlation table
tab_corr(cfa_data, na.deletion = "pairwise", digits = 2, triangle = "lower")
tab_corr(sem_data, na.deletion = "pairwise", digits = 2, triangle = "lower")


EFA {.tabset .tabset-pills}

efa_data <- dplyr::select(cfa_data, dplyr::starts_with("x"))

## Conduct EFA analysis with nfactors
efa_fit <- fa(efa_data, fm = "pa", nfactors = 3, rotate = "varimax")

Summary Output

efa_method(efa_fit)
efa_var(efa_fit)
efa_loadings(efa_fit)
efa_rotmatrix(efa_fit)

Diagram Output

fa.diagram(efa_fit)

## Determine the number of factors to extract
VSS.scree(efa_data)
fa.parallel(efa_data, fa = "fa")
VSS(efa_data, n = 4, rotate = "varimax")

Full Output

efa_fit


CFA {.tabset .tabset-pills}

# specify the model
model <- '
# latent factors
visual =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed =~ x7 + x8 + x9

# correlated errors

# constraints

'
# fit model
fit <- cfa(model = model, data = cfa_data, mimic = "lavaan", 
           estimator = "MLM", missing = "ML", 
           std.lv = TRUE, std.ov = FALSE, test = "standard", 
           se = "standard", bootstrap = 1000)

Summary Output

sem_sig(fit)
sem_fitmeasures(fit)
sem_factorloadings(fit, standardized = TRUE, ci = "standardized")
sem_factorcor(fit)

Diagram Output

semPaths(fit, latents = factors, whatLabels = "std", layout = "tree2", 
         rotation = 2, style = "lisrel", optimizeLatRes = TRUE, 
         intercepts = FALSE, residuals = TRUE, curve = 1, curvature = 3, 
         sizeLat = 10, nCharNodes = 8, sizeMan = 11, sizeMan2 = 4, 
         edge.label.cex = 1.2, edge.color = "#000000")

Residual Correlation Matrix

sem_residuals(fit)

Full Output

Summary

summary(fit, fit.measures = TRUE, standardized = TRUE)

Parameter Estimates

standardizedSolution(fit)

Modification Indices

modificationIndices(fit, sort. = TRUE, minimum.value = 3)


SEM {.tabset .tabset-pills}

# specify the model
model <- '
# measurement model
ind60 =~ x1 + x2 + x3
dem60 =~ y1 + y2 + y3 + y4
dem65 =~ y5 + y6 + y7 + y8

# regressions
dem60 ~ ind60
dem65 ~ ind60 + dem60

# covariances
y1 ~~ y5
y2 ~~ y4 + y6
y3 ~~ y7
y4 ~~ y8
y6 ~~ y8

# variances

'
# fit model
fit <- sem(model = model, data = sem_data, mimic = "lavaan", 
           estimator = "ML", missing = "ML", 
           std.lv = FALSE, std.ov = FALSE, test = "standard", 
           se = "standard", bootstrap = 1000)

Summary Output

sem_sig(fit)
sem_fitmeasures(fit)
sem_factorloadings(fit, standardized = TRUE, ci = "standardized")
sem_paths(fit, standardized = TRUE, ci = "standardized")
sem_factorcor(fit)
sem_factorvar(fit)
sem_rsquared(fit)

Diagram Output

Compared to the CFA figure above, I modified the r-code chunck option for figure width to fig.width = 10 to make the image wider. And in the 'Code' I also changed the paramter for edge labels to edge.label.cex = 8.

semPaths(fit, latents = factors, whatLabels = "std", layout = "tree2", 
         rotation = 2, style = "lisrel", optimizeLatRes = TRUE, 
         intercepts = FALSE, residuals = TRUE, curve = 1, curvature = 3, 
         sizeLat = 10, nCharNodes = 8, sizeMan = 11, sizeMan2 = 4, 
         edge.label.cex = .8, edge.color = "#000000")

Residual Correlation Matrix

sem_residuals(fit)

Full Output

Summary

summary(fit, fit.measures = TRUE, standardized = TRUE)

Parameter Estimates

standardizedSolution(fit)

Modification Indices

modificationIndices(fit, sort. = TRUE, minimum.value = 3)


Session Info

citation("lavaan")
citation()
sessionInfo()


dr-JT/semoutput documentation built on March 25, 2024, 9:18 p.m.