knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

What is Mediation?

knitr::include_graphics("pictures/medmod/1.png")

Baron & Kenny (1986)

Baron & Kenny (1986)

Baron & Kenny (1986): Limitations

Sobel Test

Bootstrapping

Mediation: Data Screening

Mediation: Example

library(rio)
master <- import("data/mediation.sav")
str(master)

summary(master)
master <- na.omit(master)

Mediation: c Path

model1 <- lm(exam ~ previous, data = master)
summary(model1)

Mediation: a Path

model2 <- lm(facebook ~ previous, data = master)
summary(model2)

Mediation: b, c' Path

model3 <- lm(exam ~ previous + facebook, data = master)
summary(model3)

Mediation: Interpretation

Mediation: Sobel Test

#aroian sobel
a <- coef(model2)[2]
b <- coef(model3)[3]
SEa <- summary(model2)$coefficients[2,2]
SEb <- summary(model3)$coefficients[3,2]
zscore <- (a*b)/(sqrt((b^2*SEa^2)+(a^2*SEb^2)+(SEa^2*SEb^2)))
zscore
#two tailed test 
pnorm(abs(zscore), lower.tail = F)*2

Mediation: Sobel Test

Mediation: All Together + Bootstrapping

#devtools::install_github("doomlab/MeMoBootR")
library(MeMoBootR)

#no missing data allowed
med_results <- mediation1(y = "exam",
                          x = "previous", 
                          m = "facebook", 
                          df = master)

Mediation: MeMoBootR

head(med_results$datascreening$fulldata)
med_results$datascreening$correl
med_results$datascreening$linearity
med_results$datascreening$normality
med_results$datascreening$homogen

Mediation: MeMoBootR

summary(med_results$model1)
summary(med_results$model2)
summary(med_results$model3)

Mediation: MeMoBootR

med_results$indirect.effect
med_results$z.score
med_results$p.value

Mediation: MeMoBootR

med_results$boot.results
med_results$boot.ci
med_results$diagram

Moderation

Conceptual Moderation Model

knitr::include_graphics("pictures/medmod/4.png")

Conceptual Moderation Model

knitr::include_graphics("pictures/medmod/5.png")

Conceptual Moderation Model

knitr::include_graphics("pictures/medmod/6.png")

Statistical Moderation Model

knitr::include_graphics("pictures/medmod/7.png")

Centering variables

Centering variables

Moderation: Simple Slopes

Moderation: Simple Slopes

Moderation: Simple Slopes

master <- import("data/moderation.sav")
str(master)

Moderation: Centering

#center the X and M variable (NOT THE DV)
#when you create these use the final dataset 
master$zvid = scale(master$Vid_Games, scale = F) #mean center, not z score
master$zcal = scale(master$CaUnTs, scale = F)

Moderation: Running

#run the model to see if the moderation is significant
#use the z score variables!
#be sure to do X*M so the graph is right 
modmodel <- lm(Aggression ~ zvid*zcal, data = master)

Moderation: Interpretation

summary(modmodel)

Moderation: Simple Slopes

Moderation: Simple Slopes

#create the low and high z score variables 
master$zcallow <- master$zcal + sd(master$zcal) #bring them up
master$zcalhigh <- master$zcal - sd(master$zcal) #bring them down
summary(master)

Moderation: Simple Slopes

#run the models
#be sure to X*M
modmodellow <- lm(Aggression ~ zvid*zcallow, data = master)
modmodelhigh <- lm(Aggression ~ zvid*zcalhigh, data = master)

Moderation: Simple Slopes

summary(modmodellow) #low slope

Moderation: Simple Slopes

summary(modmodel) #average slope

Moderation: Simple Slopes

summary(modmodelhigh) #high slope

Moderation: Simple Slopes

Moderation: Graphing

library(ggplot2)
cleanup <- theme(panel.grid.major = element_blank(), 
                panel.grid.minor = element_blank(), 
                panel.background = element_blank(), 
                axis.line.x = element_line(color = "black"),
                axis.line.y = element_line(color = "black"),
                legend.key = element_rect(fill = "white"),
                text = element_text(size = 15))

modgraph <- ggplot(master, aes(zvid, Aggression))

##change Cal to the new moderator label
##change xlab for the new X label
modgraph + 
  xlab("Centered Video Games") + 
  geom_point(color = "gray") +
  geom_abline(aes(intercept = modmodellow$coefficients[1],
                  slope = modmodellow$coefficients[2], 
                  linetype = "-1SD Cal"), size = 1) +
  geom_abline(aes(intercept = modmodel$coefficients[1],
                  slope = modmodel$coefficients[2], 
                  linetype = "Average Cal"), size = 1) +
  geom_abline(aes(intercept = modmodelhigh$coefficients[1],
                  slope = modmodelhigh$coefficients[2], 
                  linetype = "+1SD Cal"), size = 1) +
  scale_linetype_manual(values = c("dotted", "dashed", "solid"),
                        breaks = c("-1SD Cal", "Average Cal", "+1SD Cal"),
                        name = "Simple Slope") +
  cleanup 

Moderation: MeMoBootR

mod_model <- moderation1(y = "Aggression",
                         x = "Vid_Games",
                         m = "CaUnTs",
                         df = master)

Moderation: MeMoBootR

#data screening
head(mod_model$datascreening$fulldata)
#mod_model$datascreening$correl
#mod_model$datascreening$linearity
#mod_model$datascreening$normality
#mod_model$datascreening$homogen

#models
summary(mod_model$model1)
#summary(mod_model$model1low)
#summary(mod_model$model1high)
mod_model$interpretation

#graphs
mod_model$graphslopes

Summary



doomlab/learnSTATS documentation built on June 9, 2022, 12:54 a.m.