BhadraNivedita / Causal-Mediation-Analyses-in-R-

Mediation test in statistics with an example code in R

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Overview of mediation analyses

Mediation analysis is a statistical method used to examine the relationship between an independent variable (the predictor) and a dependent variable (the outcome) by exploring the underlying mechanism or pathway through which the predictor influences the outcome. In other words, mediation analysis tests whether the effect of the predictor on the outcome is mediated (explained) by one or more intermediate variables (mediators).

There are different types of mediation analyses, but the most common one is known as "indirect effect" or "mediated effect" analysis. This approach examines the relationship between the predictor and the outcome, both directly and indirectly through the mediator. The indirect effect represents the amount of the total effect of the predictor on the outcome that is mediated by the mediator, while the direct effect represents the remaining effect of the predictor on the outcome that is not explained by the mediator.

In this project a simple example is provided to understand the mediation test using R package "mediation".

Installation command:

install.packages("mediation")

Let's try an example. We would work on an example dataset downloaded from the website: http://static.lib.virginia.edu/statlab/materials/data/.

data<- read.csv('http://static.lib.virginia.edu/statlab/materials/data/mediationData.csv') ##loading the dataset
model.M <- lm(M ~ X, data) ##linear model showing the relation between the variable M and X
summary(model.M) ## summary from the linear model 
model.Y <- lm(Y ~ X + M, data) ## We have a model showing the relation between Y and X and M as independent variables.
summary(model.Y) ## Summary from the model Y.
library(mediation). ## loading the library to conduct mediation test.
res <- mediate(model.M, model.Y, treat='X', mediator='M',
                   boot=TRUE, sims=500). ## Conducting the mediation test assuming M as the mediator variable.
summary(results)
'''


## Other packages available in R for mediation test: https://www.youtube.com/watch?v=X6bdxTukNWI
https://library.virginia.edu/data/articles/introduction-to-mediation-analysis
https://ademos.people.uic.edu/Chapter14.html

About

Mediation test in statistics with an example code in R


Languages

Language:R 100.0%