#test set from Harman Table 7.1 P 116
har5 <- structure(c(1, 0.485, 0.4, 0.397, 0.295, 0.485, 1, 0.397, 0.397,
0.247, 0.4, 0.397, 1, 0.335, 0.275, 0.397, 0.397, 0.335, 1, 0.195,
0.295, 0.247, 0.275, 0.195, 1), dim = c(5L, 5L), dimnames = list(
c("V1", "V2", "V3", "V4", "V5"), c("V1", "V2", "V3", "V4",
"V5")))
CFA(har5) #The Harman example. Note that the model not necessary for the 1 factor case.
CFA(Harman_5) #the Harman example of a Heywood case
v9 <- sim.hierarchical() #Create a 3 correlated factor model using default values
model <- 'F1=~ V1 + V2 + V3
F2=~ V4 + V5 + V6
F3 =~ V7 +V8 + V9'
CFA(model,v9)
model9 <- 'F1 =~ .9*V1 + .8*V2 + .7*V3
F2 =~ .8*V4 + .7*V5 +.6*V6
F3 =~ .7*V7 + .6*V8 +.5*V9
F1 ~ .6*F2 + .5*F3
F2 ~ .4*F3'
#An alternative way to create 3 correlated factors
#note that CFA drops the coefficients, the model is for generating the data
#lavaan does not drop coefficients
v9s <- sim(model9,n=500)
test <- CFA(model,v9s$observed ) #do a cfa using Lavaan syntax
test.bi <- CFA.bifactor(model9,v9)
test.hi <- CFA.bifactor(model9,v9,g=TRUE)
#graphic displays make the output more understandable.
diagram(test) #show three correlated factors
diagram(test.bi) #show the bifactor solution
diagram(test.hi) #show the hierarchical/higher order solution
#this next example requires psychTools not run
#for a four factor model using keys
#CFA(psychTools::ability.keys[-1],psychTools::ability, cor="tet")
CFA(bfi.keys,bfi) # a five factor model of the bfi items
colnames(Thurstone) <- rownames(Thurstone) <- paste0("x",1:9 ) #to match lavaan syntax
model <- HS.model <- ' visual =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed =~ x7 + x8 + x9 '
c3 <- CFA(model,Thurstone,n.obs=213) #compare with the lavaan solution which has a smaller chi^2
c3 #show the result
diagram(c3) #graphically display the result
c3.hi <- CFA.bifactor(model,Thurstone,n.obs=213)
#do not run the next examples, they require lavaan
#They compare lavaan cfa solutions to CFA
if(FALSE) {
#
#The next examples require lavaan and are thus not run
library(lavaan)
#The basic lavaan example
fit <- cfa(model,sample.cov=Thurstone,sample.nobs=213,std.lv=TRUE, estimator="ML")
factor.congruence(fit,c3) #identical loadings to 2 decimals
round(fit@Model@GLIST$lambda-c3$loadings,4)
#add the g factor
HS.model <- ' general =~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9
visual =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed =~ x7 + x8 + x9 '
g.fit <- cfa(HS.model,sample.cov=Thurstone,sample.nobs=213,std.lv=TRUE,orthogonal=TRUE)
fa.congruence(g.fit,c3.hi) #identical congruence to 2 decimals
round(g.fit@Model@GLIST$lambda-c3.hi$loadings,2) #loadings with ULS are identicla
#All 24 variables from Harman
harman24 <- psychTools::holzinger.raw[157:301,8:31]
colnames(harman24) <- paste0("v",1:24)
mod.24<-'g=~v1+v2+v3+v4+v5+v6+v7+v8+v9+v10+v11+v12+v13+v14+v15+v16+v17+v18+v19+v20+v21+v22+v23+v24
spatial =~ v1 + v2 + v3 + v4
verbal=~ v5 + v6 + v7 + v8 + v9
perceptual =~ v10 + v11 + v12 + v13
recognition =~ v14+v15 + v16 + v17
memory =~ v18 + v19 + v20
'
lav.har.uls <- cfa(mod.24, data=harman24,std.lv=TRUE,std.ov=TRUE, orthogonal=TRUE, estimator="ULS")
lav.har.ml <-cfa(mod.24, data=harman24,std.lv=TRUE,std.ov=TRUE,orthogonal=TRUE)
model.har24.5 <- 'spatial =~ v1 + v2 + v3 + v4
verbal=~ v5 + v6 + v7 + v8 + v9
perceptual =~ v10 + v11 + v12 + v13
recognition =~ v14+v15 + v16 + v17
memory =~ v18 + v19 + v20'
cfa.har24 <- CFA(model.har24.5,harman24)
cfa.har.bi <- CFA.bifactor(model.har24.5,harman24)
factor.congruence(list(lav.har.uls,lav.har.ml,cfa.har.bi)) #g is very good f1-4 very good
round(lav.har@Model@GLIST$lambda-cfa.har.bi$loadings,2) #not the same
}
Run the code above in your browser using DataLab