Multilevel Mediation in R (nested dataframe)

34 Views Asked by At

I am struggling to find a R package that can deal with my dataframe structure to conduct mediation analysis.

My Dataframe has 3 Groups, each with 3 Products and resulting recorded reactions. My goal is to mediate and summarise 1) between groups, and 2) within the groups between the products.

Here is a dataframe for reproducibility:

set.seed(1994)

# Generate 90 unique IDs
n_IDs <- 90
ID <- paste0("ID", 1:n_IDs)

# Define groups and products
Groups <- rep(c("A", "B", "C"), each = n_IDs / 3)
Products <- rep(c("Fruit", "Dairy", "Meat"), times = n_IDs)

# Repeat each ID 3 times to match each product
ID_rep <- rep(ID, each = 3)

# Ensure the lengths match for all vectors
n_total <- length(ID_rep)

# Generate mediator, covariate, and DV values
Mediator1 <- round(runif(n_total, -100, 100))
Mediator2 <- round(runif(n_total, -100, 100))
Mediator3 <- round(runif(n_total, -100, 100))
Mediator4 <- round(runif(n_total, -100, 100))
Covariate1 <- round(runif(n_total, -100, 100))
Covariate2 <- round(runif(n_total, -100, 100))
IV <- round(runif(n_total, -100, 100))
DV <- round(runif(n_total, -100, 100))

# Create the data frame
DF <- data.frame(ID = ID_rep, Group = rep(Groups, each = 3), Product = Products, Mediator1, Mediator2, Mediator3, Mediator4, Covariate1, Covariate2, IV, DV)

As the data is quite nested within Group & multiple reactions per user ID (3 rows per ID), I am struggling to find appropriate R packages and code to implement mediation analysis.

A good role model (unfortunately not in R) is the macro: MEMORE (Mediation and Moderation for Repeated Measures) by Amanda Kay Montoya

0

There are 0 best solutions below