| sem.check | R Documentation |
sem.check produces outputs from a series of lavaan models. For each model, the code checks for previously saved model code, a hash of data, and important parameter inputs. If they exist and match values for the current code and data, the model is not run, the previous output is loaded instead. If either the code has changed, the hash of the data has changed, or important parameter inputs have change, or any of these do not exist, then the models are run as normal.
sem.check(
mods,
data,
keys_s = NULL,
keys_e = NULL,
fit_save = FALSE,
fit_measures = "all",
miss = "ML",
est = "default",
std.lv = FALSE,
std = TRUE,
orthogonal = FALSE,
target = NULL,
name = "sem",
check = FALSE,
save_out = FALSE
)
mods |
A named list of lavaan models to run. |
data |
A dataframe or object coercible to a dataframe. Data must include all observed variables used in any of the models. |
keys_s |
A named keys list matching the names and length of mod. |
keys_e |
A named keys list of the factors in an ESEM to be included. |
fit_save |
Logical.
|
fit_measures |
A vector of fit measures to save or 'all' to select all fit measures,
as per the |
miss |
A string.
Sets the |
est |
A string.
Sets the |
std.lv |
Logical.
Sets the |
std |
Logical.
|
orthogonal |
Logical.
Sets the |
target |
A matrix indicating a rotation target,
as used in the |
name |
A string indicating a subdirectory where model outputs will be saved when
|
check |
Logical.
|
save_out |
Logical.
|
The function is largely intended to be used as a helper function to upstream
functions,
including cfa.from.keys(), bifactor.from.keys(), efa.from.keys(), and
esem.from.mods().
Although it is recommended to use the appropriate upstream function whenever
possible,
there are not (currently) options to do so when customised lavaan models are
required;
for example, when allowing two items' residuals to correlate in a CFA.
sem.check() can be used in these cases (see example).
Matching the philosophy of the package, the function is designed to run for multiple models with a similar design. If you are using the function for a single model, transform inputs into lists as appropriate.
When save_out = TRUE, if a cache directory has been set,
the model will save various inputs and outputs from the function call.
When check = TRUE,
the model will look for any previously saved outputs from earlier model runs
in the same cache directory and only run again if nothing has changed.
In cases where something has changed for a subset of models
(e.g., a data cleaning mistake might affect only a subset of variables in a
subset of models), then the function will only re-run the models where
something has changed.
Changes to arguments that influence all lavaan model runs (e.g., miss or est)
will trigger all models to be re-run.
For either save_out = TRUE or check = TRUE,
the function will look for a cache directory set and created by the
cache.setup() function.
If a cache directory has not been set for the current session,
then the function will exit with an error suggesting that either
cache.setup() be run or save_out and check set to FALSE.
When the cache directory is found and output from previous runs are detected,
the comparisons performed are for:
model code;
hashes of the data (using openssl::md5());
values of the miss, est, std, std.lv, and orthogonalparameters;
the class of model objects (i.e., class lavaan);
and the class of parameter estimates (i.e., class lavaan.data.frame).
For most applications, the checking feature can be safely ignored by not
saving outputs (i.e., fit_save = FALSE)
and not checking for past saves (i.e., check = FALSE, both default).
The functionality is intended for a number of very slow models or a very
large number of faster models,
such that time spent rerunning code would be onerous.
However, the functionality can be safely used for faster runs too.
Returns a list of length 2 (if fit_save = FALSE) or
3 (if fit_save = TRUE).
The elements of the list are: a list of lavaan model output objects;
a list of parameter estimates from the models (standardized if std = TRUE);
and, if fit_save = TRUE, a matrix of fit measures for each model.
cfa.from.keys(), efa.from.keys(), bifactor.from.keys(), and
esem.from.mods()—all these function depend upon sem.check() to work;
lavaan::sem(), which is used to estimate the models;
lavaan::parameterEstimates(), which is used to estimate parameter values;
and lavaan::fitMeasures(), which is used to estimate fit statistics when
fit_save = TRUE.
# Create CFA keys
keys0 <- c("grit_c", "grit_p", "hope_a", "hope_p")
keys <- sapply(
keys0, function(x) names(BFIGritHope)[grep(x, names(BFIGritHope))]
)
# Create model code
mods <- mapply(
x = keys, y = names(keys), SIMPLIFY = FALSE,
FUN = function(x, y) paste(y, "=~", paste(x, collapse = " + "))
)
# Edit model code to add correlated residuals
mods[[1]] <- paste0(mods[[1]], "\ngrit_c_1 ~~ grit_c_2")
# Estimate the models with sem.check()
cfa_fit <- sem.check(mods, BFIGritHope, keys, name = "cfa", check = FALSE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.