Skip to content

Instantly share code, notes, and snippets.

@SachaEpskamp
Created March 12, 2019 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SachaEpskamp/3493ca8cf79724c2c36373aa223aa2e4 to your computer and use it in GitHub Desktop.
Save SachaEpskamp/3493ca8cf79724c2c36373aa223aa2e4 to your computer and use it in GitHub Desktop.
library("foreign")
Data <- read.spss("vHB_SESP_Datafile_for_sharing 29.8.15.sav", to.data.frame = TRUE)
# Relabel data:
names(Data) <- gsub("\\.","",names(Data))
# Lavaan model:
mod <- '
f1 =~ SexDifG1 + GenVio1 + UAtt1 + MenUnf1 + SexHorm1 + EvoPsy1
f2 =~ SexDifG2 + GenVio2 + UAtt2 + MenUnf2 + SexHorm2 + EvoPsy2
f3 =~ SexDifG3 + GenVio3 + UAtt3 + MenUnf3 + SexHorm3 + EvoPsy3
SexDifG1 ~~ SexDifG2 + SexDifG3
SexDifG2 ~~ SexDifG3
GenVio1 ~~ GenVio2 + GenVio3
GenVio2 ~~ GenVio3
UAtt1 ~~ UAtt2 + UAtt3
UAtt2 ~~ UAtt3
MenUnf1 ~~ MenUnf2 + MenUnf3
MenUnf2 ~~ MenUnf3
SexHorm1 ~~ SexHorm2 + SexHorm3
SexHorm2 ~~ SexHorm3
EvoPsy1 ~~ EvoPsy2 + EvoPsy3
EvoPsy2 ~~ EvoPsy3
EP =~ f1 + f2 + f3
f1 ~~ f2
EP ~ Evo3 + EvoPrin3 + LibCons + ReligImp
'
# fit model:
lav <- lavaan::sem(mod, Data, estimator = "MLR")
# fit measures:
fitMeasures(lav, c("chisq","df","pvalue","rmsea","cfi","tli"))
# alternative model with prayer:
mod2 <- '
f1 =~ SexDifG1 + GenVio1 + UAtt1 + MenUnf1 + SexHorm1 + EvoPsy1
f2 =~ SexDifG2 + GenVio2 + UAtt2 + MenUnf2 + SexHorm2 + EvoPsy2
f3 =~ SexDifG3 + GenVio3 + UAtt3 + MenUnf3 + SexHorm3 + EvoPsy3
SexDifG1 ~~ SexDifG2 + SexDifG3
SexDifG2 ~~ SexDifG3
GenVio1 ~~ GenVio2 + GenVio3
GenVio2 ~~ GenVio3
UAtt1 ~~ UAtt2 + UAtt3
UAtt2 ~~ UAtt3
MenUnf1 ~~ MenUnf2 + MenUnf3
MenUnf2 ~~ MenUnf3
SexHorm1 ~~ SexHorm2 + SexHorm3
SexHorm2 ~~ SexHorm3
EvoPsy1 ~~ EvoPsy2 + EvoPsy3
EvoPsy2 ~~ EvoPsy3
EP =~ f1 + f2 + f3
f1 ~~ f2
EP ~ Evo3 + EvoPrin3 + ReligImp + Prayer
LibCons ~ Prayer
LibCons ~~ 0*EP
'
# fit model:
lav2 <- lavaan::sem(mod2, Data, estimator = "MLR")
# fit measures:
fitMeasures(lav2, c("chisq","df","pvalue","rmsea","cfi","tli"))
# Plot
pdf("evo.pdf",width=14,height=10)
layout(matrix(1:2))
semPaths(lav,"std","hide", style = "RAM", sizeMan = 8, layout = "tree3", mar = c(2,2,2,2),
rotation = 2, nCharNodes=10, sizeMan2 = 2)
box("figure")
semPaths(lav2,"std","hide", style = "RAM", sizeMan = 8, layout = "tree3", mar = c(2,2,2,2),
rotation = 2, nCharNodes=10, sizeMan2 = 2)
box("figure")
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment