Skip to content

Instantly share code, notes, and snippets.

@bwiernik
Created April 24, 2020 15:53
Show Gist options
  • Select an option

  • Save bwiernik/403382436c37faeb80ea1c1ac358fd2d to your computer and use it in GitHub Desktop.

Select an option

Save bwiernik/403382436c37faeb80ea1c1ac358fd2d to your computer and use it in GitHub Desktop.
Script to meta-analyze results of lavaan sem models
# Script to meta-analyze results of lavaan sem models
#
# Author: Brenton M. Wiernik
# Date: 2020-04-24
# License: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)
#
# This script will meta-analyze the results of lavaan sem models. It assumes
# that the exact same lavaan model is specified for each sample and is
# not robust to differences in model specfications.#
library(dplyr)
library(purrr)
library(data.table)
library(metafor)
# make a list of your fit lavaan models like this:
fit_models <- list(sample1 = fit_sample1,
sample2 = fit_sample2,
sample3 = fit_sample3)
coef_data <- fit_models %>%
map(~ c(coef(.x)) %>%
data.frame(param = names(.),
coef = .)
) %>%
data.table::rbindlist(idcol = "sample")
vcov_data <- fit_models %>%
map(~ vcov(.x) %>%
as.data.frame() %>%
bind_cols(param = names(.), .) %>%
rename_at(-1, ~ paste0("v_", .x))
) %>%
data.table::rbindlist(idcol = "sample")
V_mat <- fit_models %>%
map(~ vcov(.x)) %>%
bldiag()
ma_data <- full_join(coef_data, vcov_data, by = c("sample", "param"))
ma_mod <- ma_data %>%
rma.mv(yi = coef,
V = V_mat,
mods = ~ param - 1,
random = ~ param | sample,
struct = "UN",
data = .
)
ma_mod
predict(ma_mod)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment