r start.time <- Sys.time()
An R package which utilizes metaheuristic algorithms to simultaneously find the best-fitting model for various factor structures and select which factor structure has the highest fit. Currently, simulated annealing is the only metaheuristic algorithm in use, but ant colony optimization may be incorporated in the future.
To install this package, you must have devtools
installed! It is only found on
GitHub at the moment.
require(devtools) devtools::install_github("AnthonyRaborn/mecfa") # automatically installs stable branch
The goal of mecfa
is to find the best-fitting confirmatory factor analysis
from a range of possible latent factors.
(If you are unsure of what this means, here is an article which talks about the basics).
It fits these CFA models using the lavaan
software.
To start, you need a dataset, some initial models, and the mecfa
package.
sessionInfo()
library(mecfa) # sample data to use testData <- lavaan::HolzingerSwineford1939
The lavaan
Holzinger-Swineford dataset is a 9-item test of mental ability for
7th and 8th-grade children.
It also includes other variables which may be of interest for structural equation
modeling, but we will ignore these for now.
The test items are labeled x1
thru x9
, so we can use these as our item names
when we create the different initial models.
While the generally-accepted model has 3 factors, we will investigate 2, 3, and
4 factors for this example.
factorsNames <- c("visual", "textual", "speed", "accuracy") itemNames <- paste0("x", 1:9) initialModels <- createInitialModels( maxFactors = factorsNames, items = itemNames) cat( paste( initialModels, collapse = "\n\n" ) )
maxSteps <- 750 fitStatistic <- "rmsea" maximize <- FALSE lavaan.model.specs <- list( model.type = "cfa", auto.var = TRUE, estimator = "default", ordered = NULL, int.ov.free = TRUE, int.lv.free = FALSE, std.lv = TRUE, auto.fix.first = FALSE, auto.fix.single = TRUE, auto.cov.lv.x = TRUE, auto.th = TRUE, auto.delta = TRUE, auto.cov.y = TRUE, auto.efa = TRUE, missing = "listwise" ) maxChanges <- 2 temperatureFunction <- "quadratic"
example <- exploratorySA( initialModels = initialModels, originalData = testData, maxSteps = maxSteps, fitStatistic = fitStatistic, maximize = maximize, lavaan.model.specs = lavaan.model.specs, temperature = temperatureFunction )
# show/print method example # summary method summary(example) # plot method plot(example)
It took a total of r round(as.difftime(Sys.time() - start.time, units = "mins"),2)
r attr(round(as.difftime(Sys.time() - start.time, units = "mins"),2), 'units')
to compile this document.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.