Skip to content

Instantly share code, notes, and snippets.

@mikbuch
Last active April 19, 2019 21:31
Show Gist options
  • Save mikbuch/af44b570aa85421d6b4a5876a5965b81 to your computer and use it in GitHub Desktop.
Save mikbuch/af44b570aa85421d6b4a5876a5965b81 to your computer and use it in GitHub Desktop.
WTAD task
message("\n")
message("############################################")
message("# Mediation analysis example #")
message("############################################")
message("\n")
message(" + Import packages\n")
library('mediation')
# For Sobel filter:
library('multilevel')
# Preliminaries
message("\n + Read the data")
data = read.csv('ExamAnxiety.csv')
# Get the results of the Sobel test
message("\n + Calculate the results")
outcome = with(data, sobel(pred=Revise, med=Anxiety, out=Exam))
message("\n + Overview of the analysis")
outcome
message("\np-value from the from CDF of the normal distribution")
round(2*(1-pnorm(outcome$z.value)),5)
# Sobel test adjustments
message("\n + Get coefficients")
a <- outcome$`Mod3: M~X`[2,1]; b <- outcome$`Mod2: Y~X+M`[3,1]
sa <- outcome$`Mod3: M~X`[2,2]; sb <- outcome$`Mod2: Y~X+M`[3,2]
message("\n + Z Aroian")
(z.aroian <- (a*b)/sqrt(b^2*sa^2 + a^2*sb^2 + sa^2 * sb^2))
message("\n + Aroian p-value")
round(2*(1-pnorm(z.aroian)),5)
message("\n + Mediation percentage")
(VAF <- outcome$Indirect.Effect/outcome$`Mod1: Y~X`[2,1])
message("\n")
message("############################################")
message("# End of Mediation analysis example #")
message("############################################")
message("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment