#his script contains example of basic lavaan modeling #install packages install.packages("lavaan") install.packages("semPlot") #load packages library(lavaan) library(semPlot) ## PoliticalDemocracy example PoliticalDemocracy.model <- " # measurement model ind60 =~ x1 + x2 + x3 dem60 =~ y1 + y2 + y3 + y4 dem65 =~ y5 + y6 + y7 + y8 # structural model dem60 ~ ind60 dem65 ~ ind60 + dem60 # covariances y1 ~~ y5 y2 ~~ y4 + y6 y3 ~~ y7 y4 ~~ y8 y6 ~~ y8 " #fit the model PoliticalDemocracy.fit <- sem(PoliticalDemocracy.model, data=PoliticalDemocracy) #look at model parameters summary(PoliticalDemocracy.fit, standardized=TRUE, fit.measures=TRUE) fitMeasures(PoliticalDemocracy.fit, fit.measures = c("chisq", "df", "pvalue","cfi","tli","rmsea","srmr","aic", "bic" )) semPaths(object = PoliticalDemocracy.fit, layout = "tree", rotation = 1, whatLabels = "std", edge.label.cex = 1, what = "std", edge.color = "navy") #load data data = read.delim(file.choose(),stringsAsFactors = F, header = T,sep = "\t", quote = "\"", dec = ".") #look at the data str(data) # define the model npas_NEO.model=" #measurement model NPAS =~ Q1+Q2+Q3+Q4+Q5+Q6+Q7+Q8+Q9+Q10+Q11+Q12+Q13+Q14+Q15+Q16+Q17+Q18+Q19+Q20+Q21+Q22+Q23+Q24+Q25+Q26 Extraversion =~ TIPI1 + TIPI6 Neuroticism =~ TIPI4 + TIPI9 Openess =~ TIPI5 + TIPI10 #structural model NPAS ~ Extraversion + Neuroticism + Openess" #fit the model npas_NEO.fit = sem(model = npas_NEO.model, data = data,orthogonal = F) # inspect model parameters summary(npas_NEO.fit, standardized=TRUE) # inspect fit indices fitMeasures(npas_NEO.fit, fit.measures = c("chisq", "df", "pvalue","cfi","tli","rmsea","srmr","aic", "bic" )) # plot model semPaths(object = npas_NEO.fit, layout = "tree", rotation = 1, whatLabels = "std", edge.label.cex = 1, what = "std", edge.color = "navy") #look at suggested modifications modindices(npas_NEO.fit, sort=T) # redefine the model npas_NEO_cov.model=" #measurement model NPAS =~ Q1+Q2+Q3+Q4+Q5+Q6+Q7+Q8+Q9+Q10+Q11+Q12+Q13+Q14+Q15+Q16+Q17+Q18+Q19+Q20+Q21+Q22+Q23+Q24+Q25+Q26 Extraversion =~ TIPI1 + TIPI6 Neuroticism =~ TIPI4 + TIPI9 Openess =~ TIPI5 + TIPI10 #structural model NPAS ~ Extraversion + Neuroticism + Openess #covariances Q11 ~~ Q19 Q1 ~~ Q4 Q7 ~~ Q12 + Q25 Q16 ~~ Q17 + Q8 Q12 ~~ Q25 Q2 ~~ Q15 + Q7 + Q25 + Q12 + Q8 Q3 ~~ Q16 + Q17 + Q8 Q17 ~~ Q8 + Q9 + Q13 Q26 ~~ Q9 + Q5 + Q24 Q8 ~~ Q15 Q5 ~~ Q24 Q9 ~~ Q5 + Q24 Q23 ~~ Q21 + Q22 + Q6 " #fit the model npas_NEO_cov.fit = lavaan::sem(model = npas_NEO_cov.model, data = data,orthogonal = F,estimator = "ML") # inspect model parameters summary(npas_NEO_cov.fit, standardized=TRUE) # inspect fit indices fitMeasures(npas_NEO_cov.fit, fit.measures = c("chisq", "df", "pvalue","cfi","tli","rmsea","srmr","aic", "bic" )) # plot model semPaths(object = npas_NEO_cov.fit, layout = "tree", rotation = 1, whatLabels = "std", edge.label.cex = 1, what = "std", edge.color = "navy")