0% found this document useful (0 votes)
176 views3 pages

Script CFA Model

This document demonstrates different confirmatory factor analysis (CFA) models using R. It first fits a simple CFA with three factors (familiarity, attitudes, and aversion) to questionnaire data. It then fits a one-factor model to the same data. Next, it fits several models including first-order, second-order, and bifactor models to dataset measuring depression, anxiety, and stress. It produces path diagrams and compares fit indices for each model.

Uploaded by

macrisio2002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
176 views3 pages

Script CFA Model

This document demonstrates different confirmatory factor analysis (CFA) models using R. It first fits a simple CFA with three factors (familiarity, attitudes, and aversion) to questionnaire data. It then fits a one-factor model to the same data. Next, it fits several models including first-order, second-order, and bifactor models to dataset measuring depression, anxiety, and stress. It produces path diagrams and compares fit indices for each model.

Uploaded by

macrisio2002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

# simple CFA script

library(lavaan)
library(semPlot)

##load the data


data <- read.csv("~/e_files/TEACHING/751 SEM/class assignments/R/f cfa
basics.csv")

##create the models


three.model = '
familiar =~ q3 + q13 + q14 + q16 + q20 + q21 + q22 + q23 + q27 + q30
attitudes =~ q1 + q2 + q4 + q5 + q8 + q11 + q18 + q19 + q28 + q29
aversion =~ q6 + q7 + q9 + q10 + q12 + q15 + q17 + q24 + q25 + q26
'

one.model = '
computer =~ q3 + q13 + q14 + q16 + q20 + q21 + q22 + q23 + q27 + q30 + q1 +
q2 + q4 + q5 + q8 + q11 + q18 + q19 + q28 + q29 + q6 + q7 + q9 + q10 + q12 +
q15 + q17 + q24 + q25 + q26
'

##run the models


three.fit = cfa(three.model, data = data)
one.fit = cfa(one.model, data = data)

##create pictures
semPaths(three.fit, whatLabels="std", layout="tree")
semPaths(one.fit, whatLabels = "std", layout = "tree")

##summaries
summary(three.fit, standardized=TRUE, rsquare=TRUE)
modindices(three.fit, sort. = TRUE, minimum.value = 30.00)
summary(one.fit, standardized=TRUE, rsquare=TRUE)
fitMeasures(three.fit)
fitMeasures(one.fit)

##residual correlations
correl = residuals(three.fit, type="cor")
View(correl$cor)
zcorrel = residuals(three.fit, type = "standardized")
View(zcorrel$cov)
#complex CFA

library(lavaan)
library(semPlot)

##import the dataset


data <- read.csv("~/e_files/TEACHING/751 SEM/class assignments/R/g 2nd
order.csv")

##create the model - first order model


first.model = '
depression =~ Q3+Q5+Q10+Q13+Q16+Q17+Q21
anxiety =~ Q2+Q4+Q7+Q9+Q15+Q19+Q20
stress =~ Q1+Q6+Q8+Q11+Q12+Q14+Q18
'

##second order
second.model = '
depression =~ Q3+Q5+Q10+Q13+Q16+Q17+Q21
anxiety =~ Q2+Q4+Q7+Q9+Q15+Q19+Q20
stress =~ Q1+Q6+Q8+Q11+Q12+Q14+Q18
global =~ depression + anxiety + stress
'
alternative.model = '
depression =~ Q3+Q5+Q10+Q13+Q16+Q17+Q21
anxiety =~ Q2+Q4+Q7+Q9+Q15+Q19+Q20
stress =~ Q1+Q6+Q8+Q11+Q12+Q14+Q18
global =~ NA*depression + anxiety + stress
global ~~ 1*global
'
##bifactor model
bifactor.model = '
depression =~ Q3+Q5+Q10+Q13+Q16+Q17+Q21
anxiety =~ Q2+Q4+Q7+Q9+Q15+Q19+Q20
stress =~ Q1+Q6+Q8+Q11+Q12+Q14+Q18
global =~
Q3+Q5+Q10+Q13+Q16+Q17+Q21+Q2+Q4+Q7+Q9+Q15+Q19+Q20+Q1+Q6+Q8+Q11+Q12+Q14+Q18
'

##run the models


first.fit = cfa(first.model, data=data)
second.fit = cfa(second.model, data=data)
bifactor.fit = cfa(bifactor.model, data=data, orthogonal = TRUE, std.lv =
TRUE)

##pictures
semPaths(first.fit, whatLabels = "std", layout="tree")
semPaths(second.fit, whatLabels = "std", layout="tree")
semPaths(bifactor.fit, whatLabels = "std", layout="tree")

##fit indicees
fitMeasures(first.fit)
fitMeasures(second.fit)
fitmeasures(bifactor.fit)

#summary
summary(first.fit, standardized = TRUE, rsquare=TRUE)
summary(second.fit, standardized = TRUE, rsquare=TRUE)
summary(bifactor.fit, standardized = TRUE, rsquare=TRUE)

You might also like