lav_mice: Fit SEM Model to Each Dataset in a MIDS Object Without...

View source: R/lav_mice.R

lav_miceR Documentation

Fit SEM Model to Each Dataset in a MIDS Object Without Pooling

Description

Fits a SEM model to each dataset in a mids object without pooling the results. This function is an extension for the lavaan::sem() function to handle mice::mids objects from the mice package. It allows for both a SEM model syntax as a character string or a pre-fitted lavaan model object.

Usage

lav_mice(model, mids, ...)

Arguments

model

Either a character string representing the SEM model to be fitted or a pre-fitted lavaan model object.

mids

A mids object from the mice package.

...

Additional arguments to be passed to lavaan::sem().

Value

A list of lavaan model fits, one for each imputed dataset.

Author(s)

Davood Tofighi dtofighi@gmail.com

Examples

## Not run: 
# library(mice)
# library(lavaan)
# Load Holzinger and Swineford (1939) dataset
data("HolzingerSwineford1939", package = "lavaan")
# Introduce missing data
df_complete <- na.omit(HolzingerSwineford1939)
amp <- mice::ampute(df_complete, prop = 0.2, mech = "MAR")
data_with_missing <- amp$amp

# Perform multiple imputation
imputed_data <- mice::mice(data_with_missing, m = 3, maxit = 5, seed = 12345, printFlag = FALSE)

# fit the Holzinger and Swineford (1939) example model
HS_model <- " visual  =~ x1 + x2 + x3
             textual =~ x4 + x5 + x6
             speed   =~ x7 + x8 + x9 "
# Fit the SEM model without running
fit_HS <- lavaan::sem(HS_model, data = data_with_missing, do.fit = FALSE)
# Fit the SEM model without pooling to each imputed dataset
fit_list1 <- lav_mice(HS_model, imputed_data)
# 'fit_list1' now contains a list of lavaan objects, one for each imputed dataset
# Fit the SEM model without pooling to each imputed dataset using a pre-fitted model object
fit_list2 <- lav_mice(fit_HS, imputed_data)
# 'fit_list2' now contains a list of lavaan objects, one for each imputed dataset

## End(Not run)

quantPsych/RMediation documentation built on March 4, 2024, 6 p.m.