knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.height=6, fig.width=6 )
Data education
is included in semMediation package. This data contains education duration, age, income of teachers, number of students in class.
We perform mean centering of independent and moderating variables.
library(semMediation) newData=meanCentering(education,colnames(education)[1:3]) str(newData)
You can make double moderation equation with modmedEquation() function.
moderator=list(name=c("number.c","age.c"),site=c("c","c")) equation=modmedEquation(X="duration.c",Y="income",moderator=moderator) cat(equation)
library(lavaan) fit=sem(model=equation,data=newData) estimatesTable2(fit)
You can draw conceptual diagram with conceptDiagram().
conceptDiagram(fit)
You can get path diagram of this moderation model.
semDiagram(fit)
You can use labels in diagrams.
labels <- list(duration.c = "교육기간", number.c = "학급인원수", age.c="나이", duration.cnumber.c = "교육기간X학급인원수", duration.cage.c="교육기간X나이",income = "월수입") if(Sys.info()[["sysname"]] == "Darwin") par(family="AppleGothic") conceptDiagram(fit,labels=labels) semDiagram(fit,labels=labels)
We are going to simulate a dataset that measured the following:
We are intentionally creating a moderated mediation effect here and we do so below by setting the relationships (the paths) between our causal chain variables and setting the relationships for our interaction terms. This example is adopted from "A Language, not a Letter: Learning Statistics in R" chapter 15 by Anthony N. Washburn(https://ademos.people.uic.edu/Chapter15.html)
set.seed(42) #This makes sure that everyone gets the same numbers generated through rnorm function a1 = -.59 #Set the path a1 strength (effect of X on M) a2 = -.17 #Set path a2 strength (effect of Z on M) a3 = .29 #Set path a3 strength (interaction between X and Z on M) b = .59 #Set path b strength (effect of M on Y) cdash1 = .27 #Set path c'1 strength (effect of X on Y) cdash2 = .01 #Set path c'2 strength (effect of Z on Y) cdash3 = -.01 #Set path c'3 strength (interaction betwee X and Z on Y)
n <- 200 #Set sample size X <- rnorm(n, 7, 1) #IV: Time spent in grad school (M = 7, SD = 1) Z <- rnorm(n, 5, 1) #Moderator: Time spent (hours per week) with Professor Demos in class or in office hours (M = 5, SD = 1) M <- a1*X + a2*Z + a3*X*Z + rnorm(n, 0, .1) #Mediator: Number of publications in grad school #The mediator variable is created as a function of the IV, moderator, and their interaction with some random noise thrown in the mix Y <- cdash1*X + cdash2*Z + cdash3*X*Z + b*M + rnorm(n, 0, .1) #DV: Number of job offers #Similar to the mediator, the DV is a function of the IV, moderator, their interaction, and the mediator with some random noise thrown in the mix
Now we put it all together and make our data frame.
Success <- data.frame(jobs = Y, time = X, pubs = M, alex = Z)
Success <- meanCentering(Success,c("time","alex")) str(Success)
require(lavaan) model=modmedEquation(X="time.c",Y="pubs",moderator=list(name="alex.c",site="c")) cat(model) fit=sem(model,data=Success)
You can draw cenceptual diagram using conceptDiagram() function.
require(semMediation) conceptDiagram(fit)
You can use labels in conceptDiagram() function.
labels=list(time.c="Time Spent in\nGrad School", alex.c="Time Spent\nwith Alex", pubs="# of\nPublications") conceptDiagram(fit,labels=labels)
You can get path diagram of this moderation model.
semDiagram(fit) semDiagram(fit,labels=labels) mediationPlot(fit)
You can make mediation model with this data.
model=makeEquation(X="time.c",M="pubs",Y="jobs") cat(model) fit=sem(model,data=Success)
You can draw conceptual model with conceptDiagram() function.
conceptDiagram(fit) labels=list(time.c="Time Spent in\nGrad School", pubs="# of\nPublications", jobs="# of Job Offers" ) conceptDiagram(fit,labels=labels)
You can get path diagram of this mediation model.
semDiagram(fit) semDiagram(fit,labels=labels) mediationPlot(fit)
X="time.c";M="pubs";Y="jobs" moderator=list(name=c("time.c"),site=list(c("b"))) model=modmedEquation(X=X,M=M,Y=Y,moderator=moderator) cat(model) fit=sem(model,data=Success) estimatesTable2(fit) conceptDiagram(fit) semDiagram(fit)
X="time.c";M="pubs";Y="jobs" moderator=list(name=c("alex.c"),site=list(c("a","c"))) model=modmedEquation(X=X,M=M,Y=Y,moderator=moderator,range=TRUE) cat(model) fit=sem(model,data=Success) estimatesTable2(fit) conceptDiagram(fit,labels=labels) semDiagram(fit,labels=labels)
X="time.c";M="pubs";Y="jobs" moderator=list(name=c("alex.c"),site=list(c("a","b"))) model=modmedEquation(X=X,M=M,Y=Y,moderator=moderator) cat(model) fit=sem(model,data=Success) estimatesTable2(fit) conceptDiagram(fit) semDiagram(fit,whatLabels="name") semDiagram(fit)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.