knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
knitr::opts_chunk$set(echo = TRUE) library(lavaan) library(semPlot)
knitr::include_graphics("pictures/diagram_sem.png")
# a famous example, build the model HS.model <- ' visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 ' # fit the model HS.fit <- cfa(HS.model, data = HolzingerSwineford1939) # diagram the model semPaths(HS.fit, whatLabels = "std", layout = "tree", edge.label.cex = 1)
# a famous example, build the model HS.model <- ' visual <~ x1 + x2 + x3' # fit the model HS.fit <- cfa(HS.model, data = HolzingerSwineford1939) # diagram the model semPaths(HS.fit, whatLabels = "std", layout = "tree", edge.label.cex = 1)
factor loadings
or coefficientsstd.lv
in the the standardized output std.all
as part of the "completely standardized solution?information
, similarities
, word reasoning
, matrix reasoning
, and picture concepts
wisc4.cor <- lav_matrix_lower2full(c(1, 0.72,1, 0.64,0.63,1, 0.51,0.48,0.37,1, 0.37,0.38,0.38,0.38,1)) # enter the SDs wisc4.sd <- c(3.01 , 3.03 , 2.99 , 2.89 , 2.98) # give everything names colnames(wisc4.cor) <- rownames(wisc4.cor) <- names(wisc4.sd) <- c("Information", "Similarities", "Word.Reasoning", "Matrix.Reasoning", "Picture.Concepts") # convert wisc4.cov <- cor2cov(wisc4.cor, wisc4.sd)
=~
is used to define a reflexive latent variable ~
can be interpreted as Y is predicted by X=~
can be interpreted as X is indicated by Yswisc4.model <- ' g =~ Information + Similarities + Word.Reasoning + Matrix.Reasoning + Picture.Concepts '
cfa()
functionstd.lv
option can be used to only see the standardized solution on the latent variable, usually you want to set this to FALSE
wisc4.fit <- cfa(model = wisc4.model, sample.cov = wisc4.cov, sample.nobs = 550, std.lv = FALSE)
summary(wisc4.fit, standardized=TRUE, rsquare = TRUE, fit.measures=TRUE)
std.nox
: the standardized estimates are based on both the variances of both (continuous) observed and latent variables, but not the variances of exogenous covariatesparameterestimates(wisc4.fit, standardized=TRUE)
fitted(wisc4.fit) ## estimated covariances wisc4.cov ## actual covariances
fitmeasures(wisc4.fit)
modificationindices(wisc4.fit, sort = T)
semPaths(wisc4.fit, whatLabels="std", what = "std", layout ="tree", edge.color = "blue", edge.label.cex = 1)
wisc4.model2 <- ' V =~ Information + Similarities + Word.Reasoning F =~ Matrix.Reasoning + Picture.Concepts ' # wisc4.model2 <- ' # V =~ Information + Similarities + Word.Reasoning # F =~ a*Matrix.Reasoning + a*Picture.Concepts # '
wisc4.fit2 <- cfa(wisc4.model2, sample.cov=wisc4.cov, sample.nobs=550, std.lv = F)
summary(wisc4.fit2, standardized=TRUE, rsquare = TRUE, fit.measures=TRUE)
semPaths(wisc4.fit2, whatLabels="std", what = "std", edge.color = "pink", edge.label.cex = 1, layout="tree")
anova(wisc4.fit, wisc4.fit2) fitmeasures(wisc4.fit, c("aic", "ecvi")) fitmeasures(wisc4.fit2, c("aic", "ecvi"))
#install.packages("parameters") library(parameters) model_parameters(wisc4.fit, standardize = TRUE)
library(broom) tidy(wisc4.fit) glance(wisc4.fit)
In this lecture you've learned:
How to create a simple confirmatory factor analysis (measurement model)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.