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

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

Setup

Required Packages

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

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"))


Descriptives

# Prints basic descriptive statistics
sem_descriptives(data)

Correlation Matrix

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


EFA {.tabset .tabset-pills}

efa_data <- data

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

## Conduct EFA analysis with nfactors determined from methods above
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)

Full Output

efa_fit


CFA {.tabset .tabset-pills}

See the lavaan Tutorial{target="_blank"} for model syntax

# specify the model
model <- '
# latent factors

# correlated errors

# constraints

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

Summary Output

sem_tables(fit)

Diagram Output

factors <- fit@pta$vnames$lv[[1]]
size <- .65
semPaths(fit, latents = factors, whatLabels = "std", layout = "tree2", 
         rotation = 2, style = "lisrel", optimizeLatRes = TRUE, 
         structural = FALSE, layoutSplit = FALSE,
         intercepts = FALSE, residuals = FALSE, 
         curve = 1, curvature = 3, nCharNodes = 8, 
         sizeLat = 11 * size, sizeMan = 11 * size, sizeMan2 = 4 * size, 
         edge.label.cex = 1.2 * size, 
         edge.color = "#000000", edge.label.position = .40)

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}

See the lavaan Tutorial{target="_blank"} for model syntax

# specify the model
model <- '
# latent factors


# variances


# covariances


# regressions

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

Summary Output

sem_tables(fit)

Diagram Output

factors <- fit@pta$vnames$lv[[1]]
size <- .65
semPaths(fit, latents = factors, whatLabels = "std", layout = "tree2", 
         rotation = 2, style = "lisrel", optimizeLatRes = TRUE, 
         structural = FALSE, layoutSplit = TRUE,
         intercepts = FALSE, residuals = FALSE, 
         curve = 1, curvature = 3, nCharNodes = 8, 
         sizeLat = 11 * size, sizeMan = 11 * size, sizeMan2 = 4 * size, 
         edge.label.cex = 1.2 * size, 
         edge.color = "#000000", edge.label.position = .40)

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.