if (FALSE) # because of long execution time
# A simple confirmatory factor-analysis model using polychoric correlations.
# The polycor package is required for the hetcor function.
if (require(polycor)){
# The following function returns correlations computed by hetcor,
# and is used for the bootstrapping.
hcor <- function(data) hetcor(data, std.err=FALSE)$correlations
model.cnes <- specifyModel(text="
F -> MBSA2, lam1
F -> MBSA7, lam2
F -> MBSA8, lam3
F -> MBSA9, lam4
F <-> F, NA, 1
MBSA2 <-> MBSA2, the1
MBSA7 <-> MBSA7, the2
MBSA8 <-> MBSA8, the3
MBSA9 <-> MBSA9, the4
")
R.cnes <- hcor(CNES)
sem.cnes <- sem(model.cnes, R.cnes, N=1529)
summary(sem.cnes)
}
# Note: this can take a minute:
set.seed(12345) # for reproducibility
system.time(boot.cnes <- bootSem(sem.cnes, R=100, Cov=hcor, data=CNES))
summary(boot.cnes, type="norm")
# cf., standard errors to those computed by summary(sem.cnes)
if (FALSE) # because of long execution time
# An example bootstrapping a multi-group model
mod.hs <- cfa(text="
spatial: visual, cubes, paper, flags
verbal: general, paragrap, sentence, wordc, wordm
memory: wordr, numberr, figurer, object, numberf, figurew
math: deduct, numeric, problemr, series, arithmet
")
mod.mg <- multigroupModel(mod.hs, groups=c("Female", "Male"))
sem.mg <- sem(mod.mg, data=HS.data, group="Gender",
formula = ~ visual + cubes + paper + flags +
general + paragrap + sentence + wordc + wordm +
wordr + numberr + figurer + object + numberf + figurew +
deduct + numeric + problemr + series + arithmet
)
# Note: this example can take several minutes or more;
# you can decrease R if you just want to see how it works:
set.seed(12345) # for reproducibility
system.time(boot.mg <- bootSem(sem.mg, R=100))
summary(boot.mg, type="norm")
# cf., standard errors to those computed by summary(sem.mg)
Run the code above in your browser using DataLab