Created
August 4, 2022 06:12
-
-
Save TomTrebs/15b84bb7d09dcf7f4a25201b9ca66629 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Centering IV and Mediator | |
X.C <- scale(X, center = TRUE, scale = FALSE)[,] | |
M.C <- scale(M, center = TRUE, scale = FALSE)[,] | |
#Centering Moderators | |
W.C <- scale(W, center = TRUE, scale = FALSE)[,] | |
#Identifying values for simple slopes of moderator | |
quantile(W, probs = c(0.16, 0.5, 0.84)) | |
summary(W) | |
#Set Moderator interaction terms | |
XcxWc <- X.C*W.C #Interaction term, IV and moderator | |
McxWc <- M.C*W.C #Interaction term, Mediator and moderator | |
ModMed_Data <- data.frame (X.C, Group, M.C, W.C, XcxWc) #dataframe for model | |
lapply(ModMed_Data[,c("Group")], ordered) #ensure that lavaan recognises Group as an endogenous ordinal variable | |
ModeratedMediationModel_Defined <- | |
' #Regressions | |
Group ~ b1*M.C + b2*McxWc + cdash1*X.C +cdash2*W.C + cdash3*XcxWc | |
M.C ~ a1*X.C + a2*W.C +a3*XcxWc | |
#Simple slope of M on X (a1+a3*W). Values in brackets derived from values the moderator obtained with quantile/summary etc functions above | |
aSDbelow := a1+a3*(W) | |
aLow := a1+a3*(W) | |
aMedian := a1+a3*(W) | |
aMean := a1+a3*(0) | |
aSDabove := a1+a3*(W) | |
aHigh := a1+a3*(W) | |
#Simple slope of Y on M (b1+b2*W). | |
bSDbelow := b1+b2*(W) | |
bLow := b1+b2*(W) | |
bMedian := b1+b2*(W) | |
bMean := b1+b2*(0) | |
bSDabove := b1+b2*(W) | |
bHigh := b1+b2*(W) | |
#Indirect effects conditional on moderator (a1 + a3)*(b1+ b2) | |
abSDbelow := bSDbelow*aSDbelow | |
abLow: = bLow*aLow | |
abMedian: = bMedian*aMedian | |
abMean: = bMean*aMean | |
abSDabove := bSDabove*aSDabove | |
abHigh: = bHigh*aHigh | |
#Direct effects conditional on moderator (cdash1 + cdash3)*W | |
cSDbelow := cdash1+cdash3*(W) | |
cLow := cdash1+cdash3*(W) | |
cMedian := cdash1+cdash3*(W) | |
cMean := cdash1+cdash3*(0) | |
cSDabove := cdash1+cdash3*(W) | |
cHigh := cdash1+cdash3*(W) | |
#Total effects conditional on moderator | |
total.SDbelow := cSDbelow + abSDbelow | |
total.Low := clow + ablow | |
total.Median := cMedian + abMedian | |
total.Mean := cMean + abMean | |
total.SDabove := cSDabove + abSDabove | |
total.High := cHigh + abHigh | |
#Proportion mediated conditional on moderator | |
#To match the output of "mediate" package | |
prop.mediated.SDbelow := abSDbelow / total.SDbelow | |
prop.mediated.Low := abLow / total.Low | |
prop.mediated.Median := abMedian / total.Median | |
prop.mediated.Mean := abMean / total.Mean | |
prop.mediated.SDabove := abSDabove / total.SDabove | |
prop.mediated.High := abHigh / total.High | |
#Index of moderated mediation | |
index.mod.med1 := a3*b2 | |
index.mod.med2 := a1*b2+a3*b1 | |
' | |
ModeratedMediationModel <- sem(model = ModeratedMediationModel_Defined, data = ModMed_Data, estimator = "WLSMV", bootstrap = 10000) | |
Summary(ModeratedMediationModel, fit.measures = TRUE, standardize = TRUE, rsquare = TRUE) | |
paramaterEstimates(ModeratedMediationModel, boot.ci.type = "bca.simple", level =.95, ci = TRUE, standardized = FALSE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment