I’m trying to get the indirect effects of the mediation variables m1, m2, and m3 in my dependent variable “y”. But also considering the effect of the independent variable “iv” on each of these mediation variables and on the dependent variable.
The models I’m considering are:
m1 = a*iv
m2 = d*iv
Where a and d are the coefficient estimates for iv in model 1 and model 2.
My last model is:
y = civ + bm1 + e*m2
Since I have a panel data set, I would like to include unit (represented by the id variable) and time (represented by the year variable) fixed effects. However, I haven’t been able to include them. I looked at many references, but I’m still not able to do it. Here are some of the references I used:
Reference1
https://www.researchgate.net/publication/355162153_A_Closer_Look_at_Random_and_Fixed_Effects_Panel_Regression_in_Structural_Equation_Modeling_Using_Lavaan
Reference2
Fixed effect and random intercept models using “lavaan” in R: advice on coding
Here is my code (excluding unsuccessful attempts to incorporate time and unit fixed effects):
library(lavaan)
library(semPlot)
set.seed(123)
n_obs <- 5
n_ids <- 10
dat <- tibble(
id = rep(1:n_ids, each = n_obs),
year = rep(2010:2014, times = n_ids),
y = rnorm(n_obs * n_ids, mean = 100, sd = 10),
m1 = rnorm(n_obs * n_ids, mean = 50, sd = 5),
m2 = rnorm(n_obs * n_ids, mean = 30, sd = 3),
m3 = rnorm(n_obs * n_ids, mean = 20, sd = 2),
iv = rnorm(n_obs * n_ids, mean = 1000, sd = 53),
weight = runif(n_obs * n_ids, min = 0.5, max = 2)
)
mediation_model <- '
# Direct effects
m1 ~ a * iv
m2 ~ d * iv
y ~ c * iv + b * m1 + e * m2
# Indirect effect (a * b) + (d * e)
indirect1 := a * b
indirect2 :=d * e
# Total effect (c + indirect1 + indirect2)
total := c + indirect1 + indirect2
#Direct effect
direct := c
'
mediation_results <- sem(mediation_model, data = dat)
Any help is super appreciated!!!