Package semMediation is an extension of package lavaan. The main functions of package semMediation are as follows:
You can install semMediation
package via github.
install.packages("devtools") devtools::install_github("cardiomoon/semMediation")
require(lavaan) require(semPlot) require(semMediation) require(ggiraphExtra) require(flextable) require(rrtable) HS.model <- ' visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 ' fit <- cfa(HS.model, data = HolzingerSwineford1939) semPaths(fit) mediationPlot(fit,width=8,residuals=TRUE)
Consider a classical mediation setup with four variables: Y1 is the dependent variable, X1 is the predictor and M1 and M2 are mediators. For illustration, we create a toy dataset containing these variables, and fit a path analysis model that includes the direct effect of X1 on Y1 and the indirect effect of X1 on Y1 via M1 or M2 and second indirect effect via M1 and M2.
set.seed(1234) X1 <- rnorm(100) X2 <- c(rnorm(50),rnorm(50)) M1 <- 0.5*X1 + 0.5*X2 +rnorm(100) M2 <-0.6*X1 +0.4*X2+rnorm(100) Y1 <- 0.3*M1 + 0.4*M2 + rnorm(100) Y2 <- 0.1*M1 + 0.5*M2 + rnorm(100) data <- data.frame(X1 = X1, X2 = X2, Y1 = Y1, Y2 = Y2, M1 = M1, M2 = M2) str(data)
To make a mediation equation, you can use makeEquation() function.
model= "X=~X1+X2 " model1=makeEquation(X="X",M=c("M1","M2"),Y="Y1") model=paste0(model,model1) fit=sem(model,data=data) summary(fit)
To visualize model, you can use semPaths() function from semPlot package. But, semPaths() function is inappropriate for models with two or more mediators.
#par(family="NanumGothic") semPaths(fit,rotation=2,whatLabels = "std")
mediationPlot() can visualize models with multiple mediators, multiple independent variables and/or multiple dependent variables.
mediationPlot(fit,width=8,height=4)
You can show standardized parameter estimates(default) or parameter estimates(est), or names(name) by settting the parameter "whatLabels".
mediationPlot(fit,whatLabels = "name") mediationPlot(fit,whatLabels = "est")
You can visualize indirect effects of this model.
mediationPlot(fit,regression=FALSE,residuals=FALSE,indirect=TRUE)
You can also visualize secondary indirect effect of this model.
mediationPlot(fit,regression=FALSE,residuals=FALSE,secondIndirect=TRUE)
You can extend model with two predictors, two mediators and one response variable.
model=makeEquation(X=c("X1","X2"),M=c("M1","M2"),Y="Y1") cat(model) fit=sem(model,data=data)
semPaths(fit) mediationPlot(fit)
You can extend model with two predictors, two mediators and one response variable.
model=makeEquation(X=c("X1","X2"),M="M1",Y="Y1") cat(model) fit=sem(model,data=data)
semPaths(fit) mediationPlot(fit)
You can extend model with two predictors, two mediators and two response variables.
model=makeEquation(X=c("X1","X2"),M=c("M1","M2"),Y=c("Y1","Y2")) cat(model) fit=sem(model,data=data)
semPaths(fit) mediationPlot(fit)
model <- ' # latent variables ind60 =~ x1 + x2 + x3 dem60 =~ y1 + y2 + y3 + y4 dem65 =~ y5 + y6 + y7 + y8 # regressions dem60 ~ ind60 dem65 ~ ind60 + dem60 ' fit <- sem(model, data=PoliticalDemocracy) semPaths(fit,rotation=2,whatLabels = "std",groups="manlat",pastel=TRUE) mediationPlot(fit)
Next example is about the teacher's intervention for students with ADHD. The ADHD data contained in semMediation package is a dataset contains measures about the teacher's knowlege, empathy and intervention about attention-deficit hyperactivity disorder(ADHD).(Source:Effects of teacher's knowledge and empathy on educational intervention for ADHD: Focused on the mediating effet of empathy. J Korean Acad Psychiatr Ment Health Nurs 2013:22;45-55.)
First of all, let's start from defining the latent variables. You can define latent variables with =~
operator.
model=' knowledge =~ general+symptoms+treatmt empathy =~ cognitiv+emotion+disposit+attitude intervention =~ classrm+instruct '
You can define mediation effects using the makeEquation() function. Here the knowledge
is an independent variable(X) and intervention
is a response variable(Y) and empathy
is a mediator(M).
mediationModel=makeEquation(X="knowledge",M="empathy",Y="intervention") cat(mediationModel)
You can add this mediation effects to the model. The final model is as follows.
model=paste0(model,mediationModel) cat(model)
You can fit this model by using the lavaan::sem() function.
fit=sem(model,data=ADHD) summary(fit,standardized= TRUE ,fit.measures= FALSE ,rsquare= FALSE ,modindices= FALSE )
You can draw figure using semPlot::semPaths() function. Alternatively, you can draw a plot with semMediation::mediationPlot().
semPaths(fit,rotation=2,whatLabels = "std") #mediationPlot(fit,whatLabels = "name",residuals=FALSE) mediationPlot(fit,width=8,height=4,whatLabels = "std",residuals = FALSE,base_size=4)
You can make a table shows a Cronbacha alpha and Guttmans lambda 6.
result=fit2alpha(fit) df2flextable(result)
You can draw correlation plot by CorPlot function
require(mycor) require(ggplot2) corPlot(fit) corTable2(fit)
You can make a table summarizing model fit measures.
modelFitTable2(fit)
estimatesTable2(fit,ci=TRUE)
estimatesTable2(fit,mediation=TRUE,ci=TRUE)
You can draw direct and indirect effects.
mediationPlot(fit,whatLabels = "std",residuals = FALSE,indirect=TRUE, regression=TRUE,mediationOnly = TRUE)
reliabilityTable2(fit) discriminantValidityTable2(fit)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.