Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Talks/SPSP_lavaan/SPSP_lavaan.R
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
58 lines (47 sloc)
1.45 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################### | |
############################### | |
## R code to accompany: ## | |
## Schoemann (2015) ## | |
## SEM with lavaan ## | |
## Presented at SPSP, 2015 ## | |
############################### | |
############################### | |
library(lavaan) | |
library(psych) | |
library(semTools) | |
library(semPlot) | |
## CFA model | |
mod <- ' | |
energetic =~ active + vigorous + wakeful + lively | |
negative =~ jittery + nervous + scared | |
' | |
#Default is a marker variable method of scale setting | |
fit <- cfa(mod, data = msq) | |
summary(fit) | |
#Get path diagram | |
semPaths(fit, "est", nCharNodes = 0) | |
#sd.lv changes to a fixex factor method of scale setting | |
fit1 <- cfa(mod, data = msq, std.lv = TRUE) | |
summary(fit1) | |
semPaths(fit1, "est", nCharNodes = 0) | |
## Multiple group CFA | |
fitg <- cfa(mod, data = msq, group = "scale") | |
summary(fitg) | |
#Set loadings equal across groups | |
fitgW <- cfa(mod, data = msq, group = "scale", | |
group.equal = "loadings") | |
#Compare models with anova | |
anova(fitg, fitgW) | |
#Test for invariance across groups | |
measurementInvariance(mod, data = msq, group = "scale") | |
## Cateogrical indicators | |
ability <- data.frame(ability) | |
modCat <- ' | |
reason =~ reason.4 + reason.16 + reason.17 + reason.19 | |
letter =~ letter.7 + letter.33 + letter.34 + letter.58 | |
matrix =~ matrix.45 + matrix.46 + matrix.47 + matrix.55 | |
rotate =~ rotate.3 + rotate.4 + rotate.6 + rotate.8 | |
' | |
fitCat <- cfa(modCat, data = ability, std.lv = TRUE, | |
ordered = names(ability)) | |
summary(fitCat) |