I have a simple mediation model:
IV: Experience (binary – yes1/no0)
Mediator: Trust (5 point likert scale measure)
DV: Offer (continuous, values can be anything between 5 and 15)
Each respondent (ID) was exposed to a scenario without experience and with experience. For each scenario they got a value for trust and for offer
My data looks something like this:
ID | Experience | Trust | Offer |
---|---|---|---|
A1000 | 0 | 2 | 10 |
A1000 | 1 | 4 | 12.5 |
A1001 | 0 | 4 | 6 |
A1001 | 1 | 5 | 11 |
I want to conduct a mediation analysis in R studio, but I need to account for within subjects repeated measures (per ID).
WHAT I HAVE SO FAR:
install.packages("readxl")
install.packages("lavaan")
install.packages("sandwich")
install.packages("lmtest")
install.packages("dplyr")
install.packages("car")
library(readxl)
library(lavaan)
library(sandwich)
library(lmtest)
library(dplyr)
library(car)
# Load your data using the specific path
data <- read_excel("/Users/reka/Documents/RSM 2022-2023/THESIS/Main Data/Data.xlsx")
# Display the first few rows and column names to verify
head(data)
str(data)
# Convert variables to numeric
data$Time <- as.numeric(as.factor(data$Time)) # IV
data$Matrix <- as.numeric(as.factor(data$Matrix)) # Mediator
data$Offer <- as.numeric(data$Offer) # DV
data$ID <- as.factor(data$ID) # Cluster ID
# Model specification
model <- '
# Direct effect of Time on Offer
Offer ~ Time_to_Offer*Time
# Effect of Time on Matrix (Mediator)
Matrix ~ Time_to_Matrix*Time
# Effect of Matrix on Offer
Offer ~ Matrix_to_Offer*Matrix
# Indirect effect
indirect := Time_to_Matrix * Matrix_to_Offer
# Direct effect
direct := Time_to_Offer
# Total effect
total := direct + indirect
'
# Fit the mediation model using lavaan
fit <- sem(model, data = data)
summary(fit, standardized = TRUE)
After this I don’t know how to proceed to make sure i cluster standard errors by ID.
Is my mediation model even good?