Rose Maier, 4/28/14
I'm using lavaan. Here's an example of mediation in lavaan: http://lavaan.ugent.be/tutorial/mediation.html
# Make sure to set working directory to wherever the data file is saved.
data <- read.delim("optimismclose.txt", header = F, sep = "\t")
# assign variable names, since they're not included in the data file
colnames(data) <- c("mlot", "mmq", "mcss", "flot", "fmq", "fcss")
# checking for missing values. turns out there are none, so no action
# needed.
check <- apply(data, 2, is.nan)
summary(data[, 4:6]) # double checking the ranges on the variables, to see if they look sensible (e.g. no -12345 values)
## flot fmq fcss
## Min. :16.0 Min. :15.3 Min. :1.38
## 1st Qu.:25.0 1st Qu.:26.6 1st Qu.:3.97
## Median :29.5 Median :29.1 Median :4.38
## Mean :29.4 Mean :28.7 Mean :4.29
## 3rd Qu.:34.0 3rd Qu.:31.3 3rd Qu.:4.75
## Max. :40.0 Max. :35.0 Max. :5.00
library(lavaan)
## This is lavaan 0.5-16
## lavaan is BETA software! Please report any bugs.
model1 <- "\n # regressions\n fcss ~ fmq\n fmq ~ flot\n"
fit1 <- sem(model1, data = data)
# This second model is equivalent, but it asks R to calculate the indirect
# effect for me.
model1 <- "\n # regressions\n fcss ~ b*fmq\n fmq ~ a*flot\n # indirect effect\n ab := a*b\n"
fit1 <- sem(model1, data = data)
summary(fit1, standardized = TRUE)
## lavaan (0.5-16) converged normally after 15 iterations
##
## Number of observations 108
##
## Estimator ML
## Minimum Function Test Statistic 2.479
## Degrees of freedom 1
## P-value (Chi-square) 0.115
##
## Parameter estimates:
##
## Information Expected
## Standard Errors Standard
##
## Estimate Std.err Z-value P(>|z|) Std.lv Std.all
## Regressions:
## fcss ~
## fmq (b) 0.105 0.013 8.137 0.000 0.105 0.617
## fmq ~
## flot (a) 0.173 0.063 2.770 0.006 0.173 0.258
##
## Variances:
## fcss 0.263 0.036 0.263 0.620
## fmq 13.632 1.855 13.632 0.934
##
## Defined parameters:
## ab 0.018 0.007 2.622 0.009 0.018 0.159
# The argument standardized=TRUE augments the output with standardized
# parameter values. Two extra columns of standardized parameter values are
# printed. In the first column (labeled Std.lv), only the latent variables
# are standardized. In the second column (labeled Std.all), both latent and
# observed variables are standardized. The latter is often called the
# ‘completely standardized solution’.
The unstandardized effect of FLOT on FCSS via FMQ is 0.018. The standardized estimate is 0.159.
Yep! The Z-test of the indirect effect is significant (Z = 2.622, p = .009), but of course this is based on the assumption that the direct effect is zero.
In this case, the model specifies that the direct effect of FLOT on FCSS is 0, so the total effect is equal to the indirect effect (a*b) = 0.018.
model2 <- "\n # regressions\n fcss ~ fmq\n fmq ~ flot\n fcss ~ flot\n"
fit2 <- sem(model2, data = data)
# This second model is equivalent, but it asks R to calculate the indirect
# and total effects for me.
model2 <- "\n # regressions\n fcss ~ c*flot\n fcss ~ b*fmq\n fmq ~ a*flot\n # indirect effect\n ab := a*b\n # total effect\n total := c + (a*b)\n"
fit2 <- sem(model2, data = data)
summary(fit2, standardized = TRUE)
## lavaan (0.5-16) converged normally after 18 iterations
##
## Number of observations 108
##
## Estimator ML
## Minimum Function Test Statistic 0.000
## Degrees of freedom 0
## P-value (Chi-square) 0.000
##
## Parameter estimates:
##
## Information Expected
## Standard Errors Standard
##
## Estimate Std.err Z-value P(>|z|) Std.lv Std.all
## Regressions:
## fcss ~
## flot (c) 0.014 0.009 1.583 0.113 0.014 0.123
## fmq (b) 0.100 0.013 7.546 0.000 0.100 0.585
## fmq ~
## flot (a) 0.173 0.063 2.770 0.006 0.173 0.258
##
## Variances:
## fcss 0.257 0.035 0.257 0.606
## fmq 13.632 1.855 13.632 0.934
##
## Defined parameters:
## ab 0.017 0.007 2.600 0.009 0.017 0.151
## total 0.031 0.011 2.954 0.003 0.031 0.273
The unstandardized effect of FLOT on FCSS via FMQ is 0.017. The standardized estimate is 0.151.
Yep! The Z-test of the indirect effect is significant (Z = 2.600, p = .009).
The total effect of FLOT on FCSS is the direct effect plus the indirect effect = 0.031 (unstandardized) or 0.273 (standardized).