class: center, middle, inverse, title-slide .title[ # MT612 - Advanced Quant. Research Methods ] .subtitle[ ## Lecture 5: Path Analysis for Mediation Hypotheses ] .author[ ### Damien Dupré ] .date[ ### Dublin City University ] --- <style type="text/css"> .pre { white-space: pre !important; overflow-y: scroll !important; max-height: 100vh !important; } </style> # Correlation and Causality So far all the types of model tested are based on some sort of linear regression principles. However, linear regression is showing the correlation between 2 or more variables, it doesn't show causation. While identifying causality is possible, it an extremely complicated process. <img src="https://media.makeameme.org/created/always-remember-correlation.jpg" width="50%" style="display: block; margin: auto;" /> --- # Multivariate Models Models with more than one outcome variables are a closer step toward causal models while not testing causality directly. For example, a model using a 3 variables which demonstrate that an existing relationship may be explained by a third variable, is more explanatory than a classic linear model. This model is called Mediation > Mediation is a hypothesized causal model, whereby effect of a predictor to an outcome is transmitted through an intermediary variable M It is a useful tool for understanding the underlying mechanisms of how variables are related to each other.
--- # Mediation In this example, room temperature predicts the amount that people drink; specifically, we'd expect that higher temperatures would increase drinking.
However, it's possible that higher temperatures increase drinking indirectly: higher temperatures make people feel more thirsty, which in turn makes them drink more.
--- # Mediation Path Diagram .pull-left[
] .pull-right[ `\(c\)` is the **total effect** of the Predictor on the Outcome `$$c = c' + a \times b$$` ] .pull-left[
] .pull-right[ `\(a\)` - the estimate of the effect of the Predictor on the Mediator `\(b\)` - the estimate of the effect of the Mediator on the Outcome `\(c'\)` - the **direct effect** of the Predictor on the Outcome The path `\(ab\)` is the **indirect effect** of the Predictor on the Outcome ] --- # Paths `\(c\)` is the total effect of the Predictor on the Outcome and can be found using a simple regression: ```r lm(outcome ~ predictor, data = my_data) ``` `\(a\)` is the effect of the Predictor on the Mediator and can also be found using a simple regression: ```r lm(mediator ~ predictor, data = my_data) ``` `\(b\)` is the effect of the mediator on the outcome, controlling for the predictor. `\(c'\)` is the direct effect which checks whether the predictor predicts the outcome after controlling for the mediator. They both can be found using a multiple linear regression: ```r lm(outcome ~ predictor + mediator, data = my_data) ``` --- # Is there mediation? Now that we've fit all these models, how do we work out if there is mediation? A mediation is obtained if: - The estimate `\(c\)`, total effect, is significantly different to 0 (**requirement 1**) - The estimate `\(a\)` is significantly different to 0 (**requirement 2**) - The estimate `\(b\)` is significant while the value of the estimate `\(c'\)` has been reduced compared to `\(c\)` - The indirect effect `\(ab\)` is significant A mediation also assumes that: - The Outcome does not affect the Mediator - No 3rd variable explains the relationship between the Outcome and the Mediator - The Mediator is measured without error - The residuals of the Outcome and the Mediator are not correlated --- class: inverse, mline, center, middle # Application of a Mediation Analysis with Jamovi and R --- # Grades and Happiness Let's take the example from [University of Virginia](https://data.library.virginia.edu/introduction-to-mediation-analysis/): `\(self-esteem\)` mediates the effect of `\(grades\)` on `\(happiness\)` Data analysis is also presented in the [Jamovi Advanced Mediation Model website](https://jamovi-amm.github.io/glm_example1.html) and the data can be downloaded [here](https://jamovi-amm.github.io/glm_example1.html)
--- # Mediation Effect Imagine that previous studies have suggested that higher grades predict higher happiness. This is the **Total Effect**. <img src="https://library.virginia.edu/sites/default/files/inline-images/mediation_ex1_0.png" width="50%" style="display: block; margin: auto;" /> However, grades are not the real reason that happiness increases. Let's hypothesize that good grades boost one’s self-esteem and then high self-esteem boosts one’s happiness. This is the **Indirect Effect**. <img src="https://library.virginia.edu/sites/default/files/inline-images/mediation_ex2.png" width="50%" style="display: block; margin: auto;" /> Self-esteem is a mediator that explains the underlying mechanism of the relationship between grades (or `\(X\)`) and happiness (or `\(Y\)`). A mediation analysis is comprised of **three sets of regression**: `\(X\)` → `\(Y\)`, `\(X\)` → `\(M\)`, and `\(X\)` + `\(M\)` → `\(Y\)`. They are just three regression analyses! --- # Analyse Mediation Effects - Step 1 <img src="https://library.virginia.edu/sites/default/files/inline-images/mediation_step1.png" width="70%" style="display: block; margin: auto;" /> `$$Y = b_{0} + c\,X + e$$` Is `\(c\)` significant? We want `\(X\)` to affect `\(Y\)` (Direct Effect). If there is no relationship between `\(X\)` and `\(Y\)`, there is nothing to mediate. --- # Analyse Mediation Effects - Step 2 <img src="https://library.virginia.edu/sites/default/files/inline-images/mediation_step2.png" width="70%" style="display: block; margin: auto;" /> `$$M = b_{0} + a\,X + e$$` Is `\(a\)` significant? We want `\(X\)` to affect `\(M\)`. If `\(X\)` and `\(M\)` have no relationship, `\(M\)` is just a third variable that may or may not be associated with `\(Y\)`. A mediation makes sense only if `\(X\)` affects `\(M\)`. --- # Analyse Mediation Effects - Step 3 <img src="https://library.virginia.edu/sites/default/files/inline-images/mediation_step3.png" width="70%" style="display: block; margin: auto;" /> `$$Y = b_{0} + c'\,X + b\,M + e$$` Is `\(c'\)` non-significant or smaller than before? We want `\(M\)` to affect `\(Y\)`, but `\(X\)` to no longer affect `\(Y\)` (or `\(X\)` to still affect `\(Y\)` but in a smaller magnitude). - If the effect of `\(X\)` on `\(Y\)` completely disappears, `\(M\)` fully mediates between `\(X\)` and `\(Y\)`. - If the effect of X on Y still exists, but in a smaller magnitude, M partially mediates between `\(X\)` and `\(Y\)`. --- # Analyse Mediation Effects - Step 4 In any case, we have to check that the Indirect effect `\(ab\)` is significant. However, a regular regression cannot test the significance of product of two estimate, only a special test can: - Sobel Test - Bootstrap > The main difference between the two methods lies in the assumptions they make about the distribution of the data and the approach they use to estimate the standard error of the mediation effect. --- # Sobel vs. Bootstrap The Sobel test: - Parametric method that assumes that the sampling distribution of the mediation effect follows a normal distribution. - This test is easy to implement and interpret, but it may not be robust to violations of normality assumptions and may produce biased estimates when sample sizes are small. Bootstrap: - Non-parametric method that makes fewer assumptions about the distribution of the data. - It involves resampling the data to create multiple bootstrap samples, estimating the mediation effect for each sample, and then computing the standard error and confidence intervals of the effect based on the distribution of the bootstrap estimates. Bootstrap is generally considered more robust and reliable than Sobel when the sample size is small or the distribution of the data is non-normal. --- # Cautions: Indistinguishable Models <img src="img/med versus confounding.png" width="5926" style="display: block; margin: auto;" /> --- # Data ```r mediation_data <- "http://static.lib.virginia.edu/statlab/materials/data/mediationData.csv" |> read.csv() |> rename(grades = X, happiness = Y, self_esteem = M) ``` Quick look at the first 10 rows: | grades| self_esteem| happiness| |------:|-----------:|---------:| | 6| 5| 6| | 7| 5| 5| | 7| 7| 4| | 8| 4| 8| | 4| 3| 5| | 4| 4| 7| --- class: title-slide, middle ## Mediation with Jamovi --- # Data in Jamovi <img src="img/mediation_jamovi_data.gif" style="display: block; margin: auto;" /> --- # Check Requirements in JAMOVI **Is Grades → Happiness significant?** <img src="img/med_1.png" width="941" style="display: block; margin: auto;" /> `\(p < 0.001\)`, i.e. lower than 0.05, so `\(Grades\)` has a significant effect on `\(Happiness\)`. --- # Check Requirements in JAMOVI **Is Grades → Self-esteem significant?** <img src="img/med_2.png" width="1005" style="display: block; margin: auto;" /> `\(p < 0.001\)`, i.e. lower than 0.05, so `\(Grades\)` has a significant effect on `\(Self-esteem\)`. --- # Mediation Effect Test in JAMOVI **Is `\(c'\)` lower than `\(c\)`?** <img src="img/med_3.png" width="80%" style="display: block; margin: auto;" /> `\(p = 0.719\)` for `\(c'\)` while `\(c\)` was significant, therefore M (self-esteem) should mediate the relationship between X (grades) and Y (happiness). However, only the test of the indirect effect `\(ab\)` can confirm this assumption. --- # Available Modules To test of the indirect effect `\(ab\)` and instead of running 3 linear regressions, some modules are available in Jamovi Desktop to do them all at once and to provide additional information. - `medmod`: Is a very straight forward module designed for mediation and moderation analyses. - Only 1 mediator can be used - Only continuous variables - No advanced moderated mediation - Display percentage of mediation - See more information [here](https://blog.jamovi.org/2017/09/25/medmod.html) - `jAMM`: Jamovi Advanced Mediation Model - 1 mediator or more can be used - Continuous variables or Categorical Ordinal variables - Possibility of moderated mediation - No percentage of mediation - See more information [here](https://jamovi-amm.github.io/) --- # Mediation with medmod in JAMOVI Install the `medmod` module by clicking on the cross "Modules" at top right corner > JAMOVI library. <img src="img/mediation_jamovi_medmod.gif" style="display: block; margin: auto;" /> --- # Mediation with medmod in JAMOVI <img src="img/mediation_jamovi_medmod.png" width="1140" style="display: block; margin: auto;" /> --- # Mediation with jAMM in JAMOVI Install the `jAMM` module by clicking on the cross "Modules" at top right corner > JAMOVI library. Then follow the example described here: https://jamovi-amm.github.io/glm_example1.html <img src="https://jamovi-amm.github.io/pics/logosm.png" style="display: block; margin: auto;" /> --- # Mediation with jAMM in JAMOVI <img src="img/mediation_jamovi_jamm.gif" style="display: block; margin: auto;" /> --- class: title-slide, middle ## Mediation with R --- # mediate() from {psych} We can use the `mediate()` function from the {psych} package to add a mediating variable. ```r library(psych) ``` Importantly, we place `()` around the mediator. ```r med_model <- mediate( happiness ~ grades + (self_esteem), # note the name of the argument is y data = mediation_data ) ``` <img src="lecture_5_files/figure-html/unnamed-chunk-29-1.png" width="504" style="display: block; margin: auto;" /> --- # mediate() from {psych} ```r med_model # use summary() for longer output ``` Mediation/Moderation Analysis Call: mediate(y = happiness ~ grades + (self_esteem), data = mediation_data) The DV (Y) was happiness . The IV (X) was grades . The mediating variable(s) = self_esteem . Total effect(c) of grades on happiness = 0.4 S.E. = 0.11 t = 3.56 df= 98 with p = 0.00057 Direct effect (c') of grades on happiness removing self_esteem = 0.04 S.E. = 0.11 t = 0.36 df= 97 with p = 0.72 Indirect effect (ab) of grades on happiness through self_esteem = 0.36 Mean bootstrapped indirect effect = 0.36 with standard error = 0.08 Lower CI = 0.21 Upper CI = 0.53 R = 0.61 R2 = 0.37 F = 28.85 on 2 and 97 DF p-value: 0.000000000000202 To see the longer output, specify short = FALSE in the print statement or ask for the summary --- # med() from {medmod} As Jamovi is using R, all Jamovi modules are R packages. However `medmod` is not currently on CRAN and has to be installed from github. ```r install.packages("remotes") remotes::install_github("raviselker/medmod") ``` --- # med() from {medmod} The `medmod` package can handle simple models, and has some nice, readable output. ```r library(medmod) med_model <- med( data = mediation_data, dep = "happiness", pred = "grades", med = "self_esteem" ) med_model ``` ``` MEDIATION Mediation Estimates ────────────────────────────────────────────────────────────────── Effect Estimate SE Z p ────────────────────────────────────────────────────────────────── Indirect 0.35652220 0.08135460 4.3823238 0.0000117 Direct 0.03960392 0.10799119 0.3667329 0.7138183 Total 0.39612612 0.11004258 3.5997532 0.0003185 ────────────────────────────────────────────────────────────────── ``` --- # New Data In the **muller_mediation.csv** data, participants were primed with either “might” or “morality” primes and then engaged in a one-trial prisoner’s dilemma with a fictitious partner. Additionally, participants’ social value orientation (from pro-self and pro-social) was measured. Social value orientation differentiates people in their tendency to cooperate. Because cooperative behavior is known to be linked to expectations about the other cooperation, participants were asked to report their expectations about other’s cooperation. The aim of the study is to show whether prime has an effect on cooperation, if social value orientation moderates the effect of prime, and if expectations have a mediating role in the experimental effects. Variables There are four main variables: - **prime**: a two-group experimental condition - **EXP**: expectations about the other cooperation - **SVO**: continuous measure of social value orientation (higher levels mean more cooperative attitude) - **BEH**: behavior, the amount of experimental tokens given to the public good by the participant. --- class: title-slide, middle ## Exercise Use the **muller_mediation.csv** data located in the module's Loop page under the tile "Lecture Data" (or use the R code provided here below). Test the following mediation hypothesis: the variable *EXP* explains the relationship between the variable *BEH* (outcome) and *prime* (predictor). - Test this hypothesis in Jamovi with the `medmod` and `jAMM` modules - Test this hypothesis in R with the {psych} and {medmod} packages ```r muller_mediation <- haven::read_sav("https://github.com/mcfanda/jamm_docs/blob/master/data/muller_mediation.sav?raw=true") ```
−
+
10
:
00
--- class: inverse, mline, center, middle # Multiple Mediation Analysis with Jamovi and R --- class: title-slide, middle ## Exercise Use the **muller_mediation.csv** data to test the following mediation hypothesis: the variables *EXP* and *SVO* explain the relationship between the variable *BEH* (outcome) and *prime* (predictor). As `medmod` in Jamovi or in R is not suitable for more than 1 mediator variable: - Test this hypothesis in Jamovi with the `jAMM` module - Test this hypothesis in R with the {psych} packages
−
+
05
:
00
--- # Multiple Mediation with jAMM <img src="img/multiple_mediation.gif" style="display: block; margin: auto;" /> --- # Multiple Mediation with {psych} ```r multi_med <- mediate( BEH ~ prime + (EXP) + (SVO), data = muller_mediation ) ``` <img src="lecture_5_files/figure-html/unnamed-chunk-38-1.png" width="504" style="display: block; margin: auto;" /> --- # Multiple Mediation with {psych} It's also possible to have multiple mediators! Simply add additional predictors surrounded by brackets. In this one, all the mediation is via EXP. SVO doesn't influence BEH ```r multi_med ``` Mediation/Moderation Analysis Call: mediate(y = BEH ~ prime + (EXP) + (SVO), data = muller_mediation) The DV (Y) was BEH . The IV (X) was prime . The mediating variable(s) = EXP SVO . Total effect(c) of prime on BEH = 9.18 S.E. = 2.8 t = 3.27 df= 98 with p = 0.0015 Direct effect (c') of prime on BEH removing EXP SVO = 5.99 S.E. = 2.81 t = 2.13 df= 96 with p = 0.036 Indirect effect (ab) of prime on BEH through EXP SVO = 3.2 Mean bootstrapped indirect effect = 3.27 with standard error = 1.69 Lower CI = 0.45 Upper CI = 7.13 R = 0.46 R2 = 0.21 F = 8.74 on 3 and 96 DF p-value: 0.00000465 To see the longer output, specify short = FALSE in the print statement or ask for the summary --- class: inverse, mline, center, middle # Moderated Mediation Analysis with Jamovi and R --- # Moderated Mediation It's also possible to do moderated mediation. Simply include interaction terms for moderators. See: https://jamovi-amm.github.io/glm_example2.html <img src="https://jamovi-amm.github.io/examples/muller/moderator2.png" width="50%" style="display: block; margin: auto;" /> Also called conditional mediation, the Moderated Mediation includes an additional variable that has both a main effect and an interaction effect on the Outcome, the Mediator or both. --- # Moderated Mediation with Jamovi As `medmod` is limited to a total of 3 variables, `jAMM` can take as many as you want. - First, design your simple mediation model and observe from the figure that variables are correctly arranged. - Then, to add a moderator, you need to define it as Predictor (e.g., Covariates if it is a continuous variable, Factors if it is a categorical variable). Notice that jAMM automatically updates the diagram, but we still need to declare this Predictor as a moderator. To do that, we go to `Moderators` tab, and put variable in the moderator field. For simplicity, the diagram does not show the moderator main effects (cf. the Model diagram notes in your output), but they are correctly inserted in the model. You can check out the model in the `Full model` tab. By default, a moderator is interacting with all the variables. However, if your moderation is only about a specific variable, you can remove the interactions that are not relevant in the `Full model` tab. --- # Moderated Mediation with Jamovi <img src="img/moderated_mediation_jamovi.gif" style="display: block; margin: auto;" /> --- # Moderated Mediation with Jamovi In conditional mediation models, the first thing we want to check is whether the moderator actually has a moderating effect on the components of the mediated effect. We can check that in the results table Moderation effects (interactions). <img src="https://jamovi-amm.github.io/examples/muller/interactions.png" style="display: block; margin: auto;" /> In the table we see that `SVO` does not moderate the path from `PRIME` to `EXP` (at least not in a substantial way), because the interaction between PRIME and SVO in predicting `EXP` is not significant. However, SVO moderates the path from `EXP` to `BEH`, because the interaction between `EXP` and `SVO` in predicting `BEH` is clearly different from zero. This means that the conditional mediation is due to the fact that when `EXP` affects `BEH`, its effect depends on SVO. --- # Moderated Mediation with Jamovi After finding a significant interaction, we know that the mediated effect depends on the levels of the moderator. Thus, we should see how the mediated effects look when estimated at different levels of the moderators. We can call these simple mediated effects. Simple mediated effects are in the Conditional Mediation results section. The results show the mediatated (indirect), direct, and total effects at different levels of the moderators. By default, the levels of the moderators are `SVO=mean-SD`, `SVO=mean`, and `SVO=mean+SD`. The levels of the moderator used to computed the conditional mediation parameters can be changed in the `Covariates Scaling` tab. <img src="https://jamovi-amm.github.io/examples/muller/results0.png" width="60%" style="display: block; margin: auto;" /> --- # Moderated Mediation Analysis with R ```r mod_medi <- mediate( BEH ~ prime*SVO + (EXP), data = muller_mediation ) ``` <img src="lecture_5_files/figure-html/unnamed-chunk-44-1.png" width="504" style="display: block; margin: auto;" /> --- # Moderated Mediation Analysis with R ```r mod_medi ``` Mediation/Moderation Analysis Call: mediate(y = BEH ~ prime * SVO + (EXP), data = muller_mediation) The DV (Y) was BEH . The IV (X) was prime SVO prime*SVO . The mediating variable(s) = EXP . Total effect(c) of prime on BEH = 9.16 S.E. = 2.69 t = 3.4 df= 96 with p = 0.00099 Direct effect (c') of prime on BEH removing EXP = 6.02 S.E. = 2.72 t = 2.21 df= 95 with p = 0.029 Indirect effect (ab) of prime on BEH through EXP = 3.14 Mean bootstrapped indirect effect = 3.13 with standard error = 1.57 Lower CI = 0.52 Upper CI = 6.73 Total effect(c) of SVO on BEH = 2.04 S.E. = 0.98 t = 2.09 df= 96 with p = 0.039 Direct effect (c') of SVO on BEH removing EXP = 2.09 S.E. = 0.93 t = 2.25 df= 95 with p = 0.026 Indirect effect (ab) of SVO on BEH through EXP = -0.05 Mean bootstrapped indirect effect = -0.08 with standard error = 0.36 Lower CI = -0.9 Upper CI = 0.62 Total effect(c) of prime*SVO on BEH = 5.15 S.E. = 1.95 t = 2.64 df= 96 with p = 0.0098 Direct effect (c') of prime*SVO on BEH removing EXP = 5.05 S.E. = 1.86 t = 2.72 df= 95 with p = 0.0078 Indirect effect (ab) of prime*SVO on BEH through EXP = 0.1 Mean bootstrapped indirect effect = 0.17 with standard error = 0.71 Lower CI = -1.16 Upper CI = 1.82 R = 0.52 R2 = 0.27 F = 8.84 on 4 and 95 DF p-value: 0.000000646 To see the longer output, specify short = FALSE in the print statement or ask for the summary --- class: inverse, mline, center, middle # Introduction to Advanced Path Analyses for Mediation Hypotheses --- # Path Analyses for Mediations Structural Equation Model (SEM) is a flexible method that allows researchers to incorporate both observed and latent variables, and to test complex models with many variables and paths. Mediations are path analyses with specific constrain to analyse simultaneously the Total, Direct, and Indirect effects. Therefore, mediations analyses can be done with SEM tools. However, while it is possible to specify Indirect Effects in SEM, it is not possible to assess the Total Effect simultaneously. --- # Terminology Broadly, variables can be categorised as either exogenous or endogenous. - **Exogenous:** are essentially predictor variables. - Only have directed arrows going out. - **Endogenous:** are outcome variables in at least one part of the model. - They have directed arrows going in. - In a linear model there is only one endogenous variable, but in a path model we can have multiple. Most commonly used for "explanation" why a relation between an exogenous and an endogenous construct exists - e.g. one observes a relation between two constructs, but is unsure "why" this relation exist or if the relation is the only possible relation between the constructs --- # Path Analysis in Jamovi In Jamovi, two modules allow SEM analyses: `SEMLj` and `pathj`. - The `SEMLj` module in Jamovi is split into 2 components. - Syntax allow the SEM analyses using a coding syntax for its formula in the same way that it would be done in R - Interactive is made of a GUI with drag and drop variables in addition to clickable options give the possibility to either use a R code or to use a GUI. - The `pathj` module in Jamovi is only using a GUI. However, contrary to `SEMLj`, only observed variables can be used in `pathj`. --- # Path Analysis in Jamovi <img src="img/jamovi_sem_med_1.gif" style="display: block; margin: auto;" /> The Indirect Effect is added by ticking a box but the Total Effect is still missing. --- # Path Analysis in Jamovi You need to add the Total Effect manually in addition to ticking the box for Indirect Effects (see previous slide): <img src="img/jamovi_sem_med_2.png" width="80%" style="display: block; margin: auto;" /> Note: you could speficy the Indirect effect manually as well. --- # Path Analysis in R - The main package used for path analyses is called {lavaan}. This package is from far the most used but also the most complicated because it involves a specific syntax to define the model. - An alternative package called {seminr} is offering a less complicated approach. The [lavaan](https://lavaan.ugent.be/index.html) package for Structural Equation Modelling can be used to fit all sort of complicated models. ```r #install.packages("lavaan") library(lavaan) ``` For clarity reason, it is better to specify the model used in an object first and then to run the `sem()` function which runs the model. Lavaan model can be very complicated but first, note the following: - To specify a regression path, we use `~` - To specify a covariance, we use `~~` --- # General Linear Models with {lavaan} ``` lm_1 <- 'happiness ~ grades' lm_2 <- 'happiness ~ self_esteem' lm_3 <- 'happiness ~ grades + self_esteem' lm_4 <- 'happiness ~ grades happiness ~ self_esteem' lm_3_cov <- 'happiness ~ grades + self_esteem grades ~~ self_esteem' lm_4_cov <- 'happiness ~ grades happiness ~ self_esteem grades ~~ self_esteem' ```
<i class="fas fa-exclamation-triangle faa-flash animated faa-slow " style=" color:red;"></i>
Note: lm_3, lm_3_cov, lm_4 and lm_4_cov are the exact same models - Lavaan automatically merge regressions using the same outcome - Specifying covariance is not meaningfull when only 2 predictors are included --- # Running a {lavaan} model Once we have our model statement, we then need to run our model. There are a number of functions to do this, we will only use `sem()` ```r m1 <- sem( model, # your model statement orderd = c(), # if variables are ordered categories list them estimator = "ml", # name of the estimation method you wish to use missing = "" , # name of the missing data method you wish to use data = tbl ) # your data set ``` - {lavaan} has sensible defaults, meaning most of the time you will only need to state you model and data. ```r m1 <- sem(model, data = tbl) ``` - There is **lots** of information on using lavaan with lots of examples [online](https://lavaan.ugent.be/) --- # Viewing the results Lastly, we need to use a `summary()` function (like in `lm` and `glm`) to see results. ```r summary(m1) ``` Or for even more information in your result output: ```r summary( m1, # name given to our results object fit.measures = TRUE, # model fit information standardized = TRUE # provides standardized coefficients ) ``` --- # Mediation Example Specify the model: ```r med_model <- ' happiness ~ grades happiness ~ self_esteem self_esteem ~ grades ' ``` Estimate the model: ```r results_lavaan <- sem(med_model, data = mediation_data) ``` Observe the results: ```r summary(results_lavaan) ``` ``` lavaan 0.6.15 ended normally after 1 iteration Estimator ML Optimization method NLMINB Number of model parameters 5 Number of observations 100 Model Test User Model: Test statistic 0.000 Degrees of freedom 0 Parameter Estimates: Standard errors Standard Information Expected Information saturated (h1) model Structured Regressions: Estimate Std.Err z-value P(>|z|) happiness ~ grades 0.040 0.108 0.367 0.714 self_esteem 0.635 0.099 6.418 0.000 self_esteem ~ grades 0.561 0.094 5.998 0.000 Variances: Estimate Std.Err z-value P(>|z|) .happiness 2.581 0.365 7.071 0.000 .self_esteem 2.633 0.372 7.071 0.000 ``` However, once again we haven't specified the Indirect and the Total Effects. It has to be done manually. --- # The model output .pre[ ```r summary(results_lavaan) ``` ``` lavaan 0.6.15 ended normally after 1 iteration Estimator ML Optimization method NLMINB Number of model parameters 5 Number of observations 100 Model Test User Model: Test statistic 0.000 Degrees of freedom 0 Parameter Estimates: Standard errors Standard Information Expected Information saturated (h1) model Structured Regressions: Estimate Std.Err z-value P(>|z|) happiness ~ grades 0.040 0.108 0.367 0.714 self_esteem 0.635 0.099 6.418 0.000 self_esteem ~ grades 0.561 0.094 5.998 0.000 Variances: Estimate Std.Err z-value P(>|z|) .happiness 2.581 0.365 7.071 0.000 .self_esteem 2.633 0.372 7.071 0.000 ``` ] --- # Visualising the model We can use `semPaths()` from the {semPlot} package to help us visualise the model It shows the parameter estimates within an SEM diagram ```r library(semPlot) semPaths(results_lavaan, what = "std") ``` <img src="lecture_5_files/figure-html/unnamed-chunk-58-1.png" width="504" style="display: block; margin: auto;" /> --- # Calculating the indirect effects To calculate the indirect effect of X on Y in path mediation, we need a new parameter `\(a*b\)` made of: - `\(a\)` = the regression coefficient for M~X - `\(b\)` = the regression coefficient for Y~M Then, use the `:=` operator to create a new parameter for example called `ind` or `indirect`, or `ab` which represents our indirect effect ```r med_model <- ' happiness ~ grades happiness ~ b * self_esteem self_esteem ~ a * grades ind := a * b ' ``` --- # Significance of the indirect effects Default method of assessing the statistical significance of indirect effects assume normal sampling distribution May not hold for indirect effects which are the product of regression coefficients Instead we can use **bootstrapping** - Allows 95% confidence intervals (CIs) to be computed - If 95% CI includes 0, the indirect effect is not significant at alpha = 0.05 ```r med_model <- ' happiness ~ grades happiness ~ b * self_esteem self_esteem ~ a * grades ind := a * b ' results_lavaan <- sem( med_model, data = mediation_data, se = "bootstrap" ) ``` --- # Output for bootstrapped CIs .pre[ ```r summary(med_model, ci = TRUE) # we add the argument ci=TRUE to see the confidence intervals in the output ``` ``` Length Class Mode 1 character character ``` ] --- # Total effects in path mediation It is a requirement to know if the **total** effect of X on Y is significant `$$Total = Indirect + Direct$$` or `$$Total = a*b + c$$` Which in lavaan is: ```r med_model <- ' happiness ~ c * grades happiness ~ b * self_esteem self_esteem ~ a * grades ind := a * b total := a * b + c ' results_lavaan <- sem( med_model, data = mediation_data, se = "bootstrap" ) ``` --- # Total effect in lavaan output .pre[ ```r summary(results_lavaan, ci = TRUE) ``` ``` lavaan 0.6.15 ended normally after 1 iteration Estimator ML Optimization method NLMINB Number of model parameters 5 Number of observations 100 Model Test User Model: Test statistic 0.000 Degrees of freedom 0 Parameter Estimates: Standard errors Bootstrap Number of requested bootstrap draws 1000 Number of successful bootstrap draws 1000 Regressions: Estimate Std.Err z-value P(>|z|) ci.lower ci.upper happiness ~ grades (c) 0.040 0.124 0.319 0.749 -0.197 0.292 self_estem (b) 0.635 0.107 5.956 0.000 0.418 0.845 self_esteem ~ grades (a) 0.561 0.097 5.762 0.000 0.382 0.778 Variances: Estimate Std.Err z-value P(>|z|) ci.lower ci.upper .happiness 2.581 0.327 7.883 0.000 1.875 3.149 .self_esteem 2.633 0.365 7.215 0.000 1.921 3.337 Defined Parameters: Estimate Std.Err z-value P(>|z|) ci.lower ci.upper ind 0.357 0.080 4.472 0.000 0.211 0.522 total 0.396 0.124 3.182 0.001 0.171 0.654 ``` ] --- # Total effects in path mediation ```r med_model <- ' BEH ~ c * prime BEH ~ b * EXP EXP ~ a * prime ind := a * b total := a * b + c ' results_lavaan <- sem( med_model, data = muller_mediation, se = "bootstrap" ) summary(results_lavaan, ci = TRUE) ``` ``` lavaan 0.6.15 ended normally after 1 iteration Estimator ML Optimization method NLMINB Number of model parameters 5 Number of observations 100 Model Test User Model: Test statistic 0.000 Degrees of freedom 0 Parameter Estimates: Standard errors Bootstrap Number of requested bootstrap draws 1000 Number of successful bootstrap draws 932 Regressions: Estimate Std.Err z-value P(>|z|) ci.lower ci.upper BEH ~ prime (c) 6.037 2.654 2.275 0.023 0.889 11.855 EXP (b) 0.584 0.247 2.362 0.018 0.123 1.089 EXP ~ prime (a) 5.384 1.494 3.604 0.000 2.506 8.534 Variances: Estimate Std.Err z-value P(>|z|) ci.lower ci.upper .BEH 173.922 28.885 6.021 0.000 114.861 225.717 .EXP 54.742 7.706 7.104 0.000 39.737 69.356 Defined Parameters: Estimate Std.Err z-value P(>|z|) ci.lower ci.upper ind 3.144 NA 0.591 6.774 total 9.181 NA 3.999 14.973 ``` --- # Why code the total effect in lavaan? We could have just added up the coefficients for the direct and indirect effects By coding it in lavaan, however, we can assess the statistical significance of the total effect Useful because the total effect has to be significant to have a mediation. Sometimes the indirect effect is significant but the total effect isn't. --- # Standardised parameters Standardised parameters can be obtained in the summary using `std = TRUE`: ```r summary(model1.est, ci = TRUE, std = TRUE) ``` standardized estimates (also known as standardized coefficients or beta coefficients) are the estimates of the regression coefficients after the predictor variables have been standardized. Standardizing the variables involves converting them to a common scale, typically by subtracting the mean and dividing by the standard deviation. Standardized estimates have several advantages over unstandardized estimates. - First, they allow for direct comparison of the magnitude of the effects of different predictors, even when the predictors are measured on different scales. - Second, they can help identify which predictors are most important in explaining the outcome variable, as the estimates reflect the size of the effect of each predictor after controlling for the other predictors in the model. --- # Generic mediation with lavaan Here is how to do a mediation with lavaan: ```r model <- ' # direct effect Y ~ c*X # mediator M ~ a*X Y ~ b*M # indirect effect (a*b) ab := a*b # total effect total := c + (a*b) ' fit <- sem(model, data = Data) ``` --- # Reporting path mediation models #### 1. Methods/ Analysis Strategy - The model being tested (e.g. 'Y was regressed on both X and M and M was regressed on X') - The estimator used (e.g., maximum likelihood estimation) - The method used to test the significance of indirect effects ('bootstrapped 95% confidence intervals') #### 2. Results - Model fit (for over-identified models) - Can be useful to present these in a SEM diagram - The parameter estimates for the path mediation and their statistical significance - The coefficient for the indirect effect and the bootstrapped 95% confidence intervals - Common to also report **proportion mediation**: `$$proportion\,mediation= \frac{indirect\,effect}{total\,effect}$$` --- # Reporting path mediation models
<i class="fas fa-exclamation-triangle faa-flash animated faa-slow " style=" color:red;"></i>
Important to be aware of limitations: - Big proportion mediation possible when total effect is small - makes effect seem more impressive - Small proportion mediation even when total effect is big - can underplay importance of effect - Should be interpreted in context of total effect
<i class="fas fa-exclamation-triangle faa-flash animated faa-slow " style=" color:red;"></i>
Tricky interpretation if there are a mix of negative and positive effects involved --- # Extensions of path mediation models We can extend our path mediation model in various ways: - Several mediators in sequence or parallel - Multiple outcomes - Multiple predictors - Multiple groups (e.g., comparing direct and indirect effects across males and females) - Add covariates to adjust for potential confounders Example: Multiple mediation model ```r model <- ' # direct effect BEH ~ c*prime # mediator EXP ~ a1*prime BEH ~ b1*EXP SVO ~ a2*prime BEH ~ b2*SVO # indirect effect (a*b) ind1 := a1*b1 ind2 := a2*b2 # total effect total := c + a1*b1 + a2*b2 ' ``` --- # Other path analysis models Path mediation models are a common application of path models but they are just one example Anything that can be expressed in terms of regressions between observed variables can be tested as a path model: - Can include ordinal or binary variables - Can include moderation Other common path analysis models include: - Autoregressive models for longitudinal data - Cross-lagged panel models for longitudinal data
<i class="fas fa-exclamation-triangle faa-flash animated faa-slow " style=" color:red;"></i>
Cautions regarding path analysis models: **Assumption** that the paths represent causal effects is only an assumption Mediation models should ideally be estimated on longitudinal data (i.e., X time 1, M time 2, Y time 3). --- class: inverse, mline, left, middle <img class="circle" src="https://github.com/damien-dupre.png" width="250px"/> # Thanks for your attention and don't hesitate if you have any questions! [
@damien_dupre](http://twitter.com/damien_dupre) [
@damien-dupre](http://github.com/damien-dupre) [
damien-datasci-blog.netlify.app](https://damien-datasci-blog.netlify.app) [
damien.dupre@dcu.ie](mailto:damien.dupre@dcu.ie)