View source: R/multilevel.cfa.R
multilevel.cfa | R Documentation |
This function is a wrapper function for conducting multilevel confirmatory factor
analysis to investigate four types of constructs, i.e., within-cluster constructs,
shared cluster-level constructs, configural cluster constructs, and simultaneous
shared and configural cluster constructs by calling the cfa
function in
the R package lavaan.
multilevel.cfa(..., data = NULL, cluster, model = NULL, rescov = NULL,
model.w = NULL, model.b = NULL, rescov.w = NULL, rescov.b = NULL,
const = c("within", "shared", "config", "shareconf"),
fix.resid = NULL, ident = c("marker", "var", "effect"),
ls.fit = FALSE, estimator = c("ML", "MLR"),
optim.method = c("nlminb", "em"), missing = c("listwise", "fiml"),
print = c("all", "summary", "coverage", "descript", "fit", "est",
"modind", "resid"),
mod.minval = 6.63, resid.minval = 0.1, digits = 3, p.digits = 3,
as.na = NULL, write = NULL, append = TRUE, check = TRUE, output = TRUE)
... |
a matrix or data frame. If |
data |
a data frame when specifying one or more variables in the
argument |
cluster |
either a character string indicating the variable name of
the cluster variable in |
model |
a character vector for specifying the same factor structure
with one factor at the Within and Between Level, or a list
of character vectors for specifying the same measurement
model with more than one factor at the Within and Between
Level, e.g., |
rescov |
a character vector or a list of character vectors for specifying
residual covariances at the Within level, e.g. |
model.w |
a character vector specifying a measurement model with one factor at the Within level, or a list of character vectors for specifying a measurement model with more than one factor at the Within level. |
model.b |
a character vector specifying a measurement model with one factor at the Between level, or a list of character vectors for specifying a measurement model with more than one factor at the Between level. |
rescov.w |
a character vector or a list of character vectors for specifying residual covariances at the Within level. |
rescov.b |
a character vector or a list of character vectors for specifying residual covariances at the Between level. |
const |
a character string indicating the type of construct(s), i.e.,
|
fix.resid |
a character vector for specifying residual variances to be
fixed at 0 at the Between level, e.g., |
ident |
a character string indicating the method used for identifying
and scaling latent variables, i.e., |
ls.fit |
logical: if |
estimator |
a character string indicating the estimator to be used:
|
optim.method |
a character string indicating the optimizer, i.e., |
missing |
a character string indicating how to deal with missing data,
i.e., |
print |
a character string or character vector indicating which
results to show on the console, i.e. |
mod.minval |
numeric value to filter modification indices and only
show modifications with a modification index value equal
or higher than this minimum value. By default, modification
indices equal or higher 6.63 are printed. Note that a
modification index value of 6.63 is equivalent to a
significance level of |
resid.minval |
numeric value indicating the minimum absolute residual correlation coefficients and standardized means to highlight in boldface. By default, absolute residual correlation coefficients and standardized means equal or higher 0.1 are highlighted. Note that highlighting can be disabled by setting the minimum value to 1. |
digits |
an integer value indicating the number of decimal places
to be used for displaying results. Note that loglikelihood,
information criteria and chi-square test statistic is
printed with |
p.digits |
an integer value indicating the number of decimal places to be used for displaying the p-value. |
as.na |
a numeric vector indicating user-defined missing values,
i.e. these values are converted to |
write |
a character string naming a file for writing the output into
either a text file with file extension |
append |
logical: if |
check |
logical: if |
output |
logical: if |
Returns an object of class misty.object
, which is a list with following
entries:
call |
function call |
type |
type of analysis |
data |
data frame used for the current analysis |
args |
specification of function arguments |
model |
specified model |
model.fit |
fitted lavaan object ( |
check |
results of the convergence and model identification check |
result |
list with result tables, i.e., |
The function uses the functions cfa
, lavInspect
, lavTech
,
modindices
, parameterEstimates
, and standardizedsolution
provided in the R package lavaan by Yves Rosseel (2012).
Takuya Yanagida takuya.yanagida@univie.ac.at
Rosseel, Y. (2012). lavaan: An R Package for Structural Equation Modeling. Journal of Statistical Software, 48, 1-36. https://doi.org/10.18637/jss.v048.i02
item.cfa
, multilevel.fit
, multilevel.invar
,
multilevel.omega
, multilevel.cor
, multilevel.descript
## Not run:
# Load data set "Demo.twolevel" in the lavaan package
data("Demo.twolevel", package = "lavaan")
#----------------------------------------------------------------------------
# Model specification using 'x' for a one-factor model
# with the same factor structure with one factor at the Within and Between Level
#..........
# Cluster variable specification
# Example 1a: Cluster variable 'cluster' in 'x'
multilevel.cfa(Demo.twolevel[, c("y1", "y2", "y3", "y4", "cluster")], cluster = "cluster")
# Example 1b: Cluster variable 'cluster' not in 'x'
multilevel.cfa(Demo.twolevel[, c("y1", "y2", "y3", "y4")], cluster = Demo.twolevel$cluster)
# Example 1c: Alternative specification using the 'data' argument
multilevel.cfa(y1:y4, data = Demo.twolevel, cluster = "cluster")
#..........
# Type of construct
# Example 2a: Within-cluster constructs
multilevel.cfa(Demo.twolevel[, c("y1", "y2", "y3", "y4")], cluster = Demo.twolevel$cluster,
const = "within")
# Example 2b: Shared cluster-level construct
multilevel.cfa(Demo.twolevel[, c("y1", "y2", "y3", "y4")], cluster = Demo.twolevel$cluster,
const = "shared")
# Example 2c: Configural cluster construct (default)
multilevel.cfa(Demo.twolevel[, c("y1", "y2", "y3", "y4")], cluster = Demo.twolevel$cluster,
const = "config")
# Example 2d: Simultaneous shared and configural cluster construct
multilevel.cfa(Demo.twolevel[, c("y1", "y2", "y3", "y4")], cluster = Demo.twolevel$cluster,
const = "shareconf")
#..........
# Residual covariances at the Within level
# Example 3a: Residual covariance between 'y1' and 'y3'
multilevel.cfa(Demo.twolevel[, c("y1", "y2", "y3", "y4")], cluster = Demo.twolevel$cluster,
rescov = c("y1", "y3"))
# Example 3b: Residual covariance between 'y1' and 'y3', and 'y2' and 'y4'
multilevel.cfa(Demo.twolevel[, c("y1", "y2", "y3", "y4")], cluster = Demo.twolevel$cluster,
rescov = list(c("y1", "y3"), c("y2", "y4")))
#..........
# Residual variances at the Between level fixed at 0
# Example 4a: All residual variances fixed at 0
# i.e., strong factorial invariance across clusters
multilevel.cfa(Demo.twolevel[, c("y1", "y2", "y3", "y4")], cluster = Demo.twolevel$cluster,
fix.resid = "all")
# Example 4b: Fesidual variances of 'y1', 'y2', and 'y4' fixed at 0
# i.e., partial strong factorial invariance across clusters
multilevel.cfa(Demo.twolevel[, c("y1", "y2", "y3", "y4")], cluster = Demo.twolevel$cluster,
fix.resid = c("y1", "y2", "y4"))
#..........
# Print all results
# Example 5: Set minimum value for modification indices to 1
multilevel.cfa(Demo.twolevel[, c("y1", "y2", "y3", "y4")], cluster = Demo.twolevel$cluster,
print = "all", mod.minval = 1)
#..........
# Example 6: lavaan model and summary of the estimated model
mod <- multilevel.cfa(Demo.twolevel[, c("y1", "y2", "y3", "y4")], cluster = Demo.twolevel$cluster,
output = FALSE)
# lavaan model syntax
cat(mod$model)
# Fitted lavaan object
lavaan::summary(mod$model.fit, standardized = TRUE, fit.measures = TRUE)
#..........
# Write results
# Example 7a: Assign results into an object and write results into an Excel file
mod <- multilevel.cfa(Demo.twolevel[, c("y1", "y2", "y3", "y4")], cluster = Demo.twolevel$cluster,
print = "all", write = "Multilevel_CFA.txt", output = FALSE)
# Example 7b: Assign results into an object and write results into an Excel file
mod <- multilevel.cfa(Demo.twolevel[, c("y1", "y2", "y3", "y4")], cluster = Demo.twolevel$cluster,
print = "all", output = FALSE)
# Write results into an Excel file
write.result(mod, "Multilevel_CFA.xlsx")
# Estimate model and write results into an Excel file
multilevel.cfa(Demo.twolevel[, c("y1", "y2", "y3", "y4")], cluster = Demo.twolevel$cluster,
print = "all", write = "Multilevel_CFA.xlsx")
#----------------------------------------------------------------------------
# Model specification using 'model' for one or multiple factor model
# with the same factor structure at the Within and Between Level
# Example 8a: One-factor model
multilevel.cfa(Demo.twolevel, cluster = "cluster", model = c("y1", "y2", "y3", "y4"))
# Example 8b: Two-factor model
multilevel.cfa(Demo.twolevel, cluster = "cluster",
model = list(c("y1", "y2", "y3"), c("y4", "y5", "y6")))
# Example 8c: Two-factor model with user-specified labels for the factors
multilevel.cfa(Demo.twolevel, cluster = "cluster",
model = list(factor1 = c("y1", "y2", "y3"), factor2 = c("y4", "y5", "y6")))
#..........
# Type of construct
# Example 9a: Within-cluster constructs
multilevel.cfa(Demo.twolevel, cluster = "cluster", const = "within",
model = list(c("y1", "y2", "y3"), c("y4", "y5", "y6")))
# Example 9b: Shared cluster-level construct
multilevel.cfa(Demo.twolevel, cluster = "cluster", const = "shared",
model = list(c("y1", "y2", "y3"), c("y4", "y5", "y6")))
# Example 9c: Configural cluster construct (default)
multilevel.cfa(Demo.twolevel, cluster = "cluster", const = "config",
model = list(c("y1", "y2", "y3"), c("y4", "y5", "y6")))
# Example 9d: Simultaneous shared and configural cluster construct
multilevel.cfa(Demo.twolevel, cluster = "cluster", const = "shareconf",
model = list(c("y1", "y2", "y3"), c("y4", "y5", "y6")))
#..........
# Residual covariances at the Within level
# Example 10a: Residual covariance between 'y1' and 'y4' at the Within level
multilevel.cfa(Demo.twolevel, cluster = "cluster",
model = list(c("y1", "y2", "y3"), c("y4", "y5", "y6")),
rescov = c("y1", "y4"))
# Example 10b: Fix all residual variances at 0
# i.e., strong factorial invariance across clusters
multilevel.cfa(Demo.twolevel, cluster = "cluster",
model = list(c("y1", "y2", "y3"), c("y4", "y5", "y6")),
fix.resid = "all")
#----------------------------------------------------------------------------
# Model specification using 'model.w' and 'model.b' for one or multiple factor model
# with different factor structure at the Within and Between Level
# Example 11a: Two-factor model at the Within level and one-factor model at the Between level
multilevel.cfa(Demo.twolevel, cluster = "cluster",
model.w = list(c("y1", "y2", "y3"), c("y4", "y5", "y6")),
model.b = c("y1", "y2", "y3", "y4", "y5", "y6"))
# Example 11b: Residual covariance between 'y1' and 'y4' at the Within level
# Residual covariance between 'y5' and 'y6' at the Between level
multilevel.cfa(Demo.twolevel, cluster = "cluster",
model.w = list(c("y1", "y2", "y3"), c("y4", "y5", "y6")),
model.b = c("y1", "y2", "y3", "y4", "y5", "y6"),
rescov.w = c("y1", "y4"),
rescov.b = c("y5", "y6"))
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.