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:
The "Summary Output" tab display nice looking tables summarizing the model results
The "Diagram Output" tab will display a model diagram
The "Residual Correlation Matrix" tab will display the residual correlation matrix
The "Full Output" tab will display the results from summary()
along with parameter estimates and modification indices. This way you can still get the full output from a lavaan model as it provides more information than the "Summary Output". You can also add additional output to this section if you need more info about the model.
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)
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
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)
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_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")
efa_method(efa_fit) efa_var(efa_fit) efa_loadings(efa_fit) efa_rotmatrix(efa_fit)
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")
efa_fit
# 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)
sem_sig(fit) sem_fitmeasures(fit) sem_factorloadings(fit, standardized = TRUE, ci = "standardized") sem_factorcor(fit)
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")
sem_residuals(fit)
summary(fit, fit.measures = TRUE, standardized = TRUE)
standardizedSolution(fit)
modificationIndices(fit, sort. = TRUE, minimum.value = 3)
# 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)
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)
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")
sem_residuals(fit)
summary(fit, fit.measures = TRUE, standardized = TRUE)
standardizedSolution(fit)
modificationIndices(fit, sort. = TRUE, minimum.value = 3)
citation("lavaan") citation() sessionInfo()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.