Mediation SEM - error could not find function ":="

1,148 views
Skip to first unread message

Emma Mills

unread,
May 31, 2016, 3:37:57 AM5/31/16
to lavaan
Hi,

I am new to 'R' and to SEM.

I am trying to perform a mediation analysis of latent variables using Lavaan - I have copied my syntax below but I am getting an error when I try to use the ':=' command - does anyone know what I am doing wrong?


setwd("C:/Users/emma/Documents/Data for R")

SMBI_Alldata =read.csv("SMBI_Alldata.csv", header = TRUE)

View(SMBI_Alldata)

install.packages("nlme")

library(nlme)

library(lavaan)

#Setting the measurement model(latent variables) followed by the regressions

SCTModel<-'PF1=~pers2+pers4+pers3+pers8+pers7
BehF1=~BF1+BF2+BF3+BF4+BF6+BF7
EnvF1=~Env1+Env3+Env4+Env5
PF1~EnvF1
BehF1~EnvF1+PF1'


SCTfit<-sem(SCTModel, data = SMBI_Alldata, estimator="MLR")


summary(SCTfit, standardized = T, fit.measures = T, rsq = T)

SCTModel2 <-‘PF1=~pers2+pers4+pers3+pers8+pers7
BehF1=~BF1+BF2+BF3+BF4+BF6+BF7
EnvF1=~Env1+Env3+Env4+Env5
PF1 ~ a*EnvF1
BehF1 ~ b*EnvF1+c*PF1
Indirect := a*b
Direct := c
Total := c+ (a*b)’


But I get: Error: could not find function ":=" - Does anyone know why this is happening?


Also, in many other examples I have seen residual variances and covariances included in the syntax but I don’t know how you determine/ decide on these?

If anyone has any suggestions of where I can find help with interpreting the output that would be really appreciated :)

Thanks

Emma

Terrence Jorgensen

unread,
May 31, 2016, 5:12:36 AM5/31/16
to lavaan
But I get: Error: could not find function ":=" - Does anyone know why this is happening?

Hard to know without seeing what command was executed that led to the error.  Your R syntax stops after the character string with lavaan model syntax, and I don't think that would have produced the error, so I'm guessing you called sem() again after that?  What version of lavaan and R are you using?  You can copy/paste the output of sessionInfo() to show us.

Also, in many other examples I have seen residual variances and covariances included in the syntax but I don’t know how you determine/ decide on these?

Use the ~~ operator to specify a (residual) covariance between any pair of variables (or a variance, which is a covariance of a variable with itself). 


If anyone has any suggestions of where I can find help with interpreting the output that would be really appreciated :)

If the lavaan paper or tutorial pages don't have the information you need, this book teaches SEM using lavaan, and this book teaches latent variable models using lavaan for SEM and mirt for IRT.

Terrence D. Jorgensen
Postdoctoral Researcher, Methods and Statistics
Research Institute for Child Development and Education, the University of Amsterdam

Emma Mills

unread,
May 31, 2016, 4:02:09 PM5/31/16
to lavaan
Hi Thanks for your quick response- I think it was an issue with my syntax and not specifying all parts and it now works using the following commands:

SCTModel2 <- 'PF1=~pers2+pers4+pers3+pers8+pers7

BehF1=~BF1+BF2+BF3+BF4+BF6+BF7

EnvF1=~Env1+Env3+Env4+Env5

 # direct effect

             BehF1 ~ c*PF1

           # mediator

            EnvF1 ~ a*PF1

             BehF1 ~ b*EnvF1

           # indirect effect (a*b)

             ab := a*b

           # total effect

             total := c + (a*b)

         '

 

 

SCT2fit<-sem(SCTModel2, data = SMBI_Alldata, estimator="MLR", bootstrap = 5000)

 

summary(SCT2fit, standardized = T, fit.measures = T, rsq = T)



As you can see, I have used robust maximum likelihood estimation due to the lack of multivariate normality in my data (this is the approach that was recommended to me when I was doing confirmatory factor analysis using lavaan). However is this correct/ necessary in a mediation analysis, and should it be combined with the bootstrapping command like I've done above or not?


I used another command to perform bias corrected accelerated bootstrapping in order to get the confidence intervals for the indirect effect- as shown in my syntax below, but I'm not really sure what the difference is between the parameter estimates bootstrapping and the bootstrapping command I have used above? 


> boot.fit <- parameterEstimates(SCT2fit, boot.ci.type="bca.simple",level=0.95, ci=TRUE,standardized = FALSE)

> boot.fit


I'd be really grateful for any help in trying to understand the approach to bootstrapping a little better.


In addition, are there any resources to help interpret the output of this mediation analysis? I'm not sure what parts I should be focusing on/writing up??


Thanks


Emma

Terrence Jorgensen

unread,
Jun 1, 2016, 3:46:38 AM6/1/16
to lavaan

However is this correct/ necessary in a mediation analysis, and should it be combined with the bootstrapping command like I've done above or not?


MLR will correct SEs for the lack of multivariate normality in your data distribution.  The bootstrapping recommendation for mediation has nothing to do with the data distribution; rather, it accounts for the fact that the estimated indirect effect(s) will not be normally distributed around the true parameter(s) across repeated experiments, even if the direct effects are.

Because bootstrapping (a) doesn't assume your data are normally distributed and (b) doesn't assume that statistics are normally distributed, you could use the bootstrap CIs to test hypotheses about all your parameters and use the Bollen-Stine bootstrap to test exact model fit (although there is no bootstrap.  I'm not sure whether MLR or bootstrapping is more efficient because I haven't read a study comparing them, but either of them would free you of the multivariate normality assumption of your data, and bootstrap CIs also free you of the assumption that sample statistics are normally distributed across experiments.

I'm not really sure what the difference is between the parameter estimates bootstrapping and the bootstrapping command I have used above? 


Neither MLR nor bootstrapping affect the point estimates themselves, only the SEs (and by implication, the test statistics).

In addition, are there any resources to help interpret the output of this mediation analysis? I'm not sure what parts I should be focusing on/writing up??


You can interpret any and all of the parameters, but you might only be expected to interpret (and test null hypotheses about) the parameters that address your research questions, depending on your audience (report for an SEM class vs. thesis/dissertation vs. peer-reviewed journal).
Reply all
Reply to author
Forward
0 new messages