Skip to content

Data Science Tutorials

For Data Science Learners

  • How To Become a Business Intelligence Analyst
    How To Become a Business Intelligence Analyst Course
  • Multiple Plots to PDF in R R
  • Separate a data frame column into multiple columns
    Separate a data frame column into multiple columns-tidyr Part3 R
  • Top 5 Books to Learn Data Engineering R
  • Top 10 online data science programmes
    Top 10 online data science programs Course
  • Beginner's Guide to Data Science
    Beginner’s Guide to Data Science Machine Learning
  • Best Data Visualization Books Course
  • Run a specific code block in R R
Determine the significance of a mediation effect in R

Determine the significance of a mediation effect in R

Posted on September 15September 14 By Admin No Comments on Determine the significance of a mediation effect in R

Determine the significance of a mediation effect in R, the Sobel test is a statistical test to determine the significance of a mediation effect.

A mediation effect occurs when the relationship between two variables (X and Y) is explained by the presence of a third variable (M).

The Sobel test is used to determine whether the mediation effect is statistically significant or not.

In R, there are several packages that can be used to conduct a Sobel test. One of the most commonly used packages is ‘mediation’.

In this article, we will demonstrate how to conduct a Sobel test using the ‘bda’ package in R.

Computational Gastronomy for Data Science » Data Science Tutorials

Example 1: Simple Mediation

In this example, we will use a dataset named ‘mtcars’ that is included in the R base package. We will test whether the relationship between mpg and hp is mediated by the variable wt.

First, we need to load the ‘bda’ package:

library(bda)

Next, we need to define the variables:

mtcars_data <- mtcars
X <- mtcars_data$hp[1:10]
M <- mtcars_data$wt[1:10]
Y <- mtcars_data$mpg[1:10]

We can create a simple mediation model using the ‘mediate.test’ function:

mediation.test(X,M,Y)
            Sobel Aroian Goodman
z.value -1.5071302 -1.4743966 -1.5421454
p.value 0.1317773 0.1403748 0.1230383

The output will display the estimated mediation effect, the standard error, the z-value, and the p-value of the Sobel test.

Example 2: Multiple Mediators

In this example, we will use a hypothetical dataset with multiple mediators to test the indirect effect of X on Y.

We define the variables:

X <- rnorm(100)
M <- rnorm(100)
Y <- rnorm(100)

We can create a mediation model with the two mediators using the ‘mediate.test’ function:

mediation.test(X,M,Y)
Sobel Aroian Goodman
z.value 0.09745783 0.09081421 0.1058115
p.value 0.92236283 0.92764022 0.9157319

The output will display the estimated mediation effect, bias-corrected confidence intervals, and the p-value of the Sobel test.

Business Analyst with R&D »

R

Post navigation

Previous Post: How to Display Percentages on Histogram in R
Next Post: One proportion Z Test in R

Related Posts

  • Arrange Data by Month in R
    Arrange Data by Month in R with example R
  • Best GGPlot Themes
    Best GGPlot Themes You Should Know R
  • How to perform MANOVA test in R
    How to perform the MANOVA test in R? R
  • Add new calculated variables to a data frame and drop all existing variables
    Add new calculated variables to a data frame and drop all existing variables R
  • Error: Can't rename columns that don't exist
    Can’t rename columns that don’t exist R
  • glm function in R
    glm function in r-Generalized Linear Models R

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Best Prompt Engineering Books
  • Understanding Machine Learning and Data Science
  • Best Git Books
  • Top 5 Books to Learn Data Engineering
  • Mastering R Programming for Data Science: Tips and Tricks
  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy

https://www.r-bloggers.com

  • YouTube
  • Twitter
  • Facebook
  • Course
  • Excel
  • Machine Learning
  • Opensesame
  • R
  • Statistics

Check your inbox or spam folder to confirm your subscription.

  • Descriptive statistics vs Inferential statistics
    Descriptive statistics vs Inferential statistics: Guide Statistics
  • How to test the significance of a mediation effect
    How to test the significance of a mediation effect R
  • Cross product of transpose of matrix in R R
  • Hypothesis Testing in R Programming
    Hypothesis Testing in R Programming R
  • Two-Way ANOVA Example in R
    Two-Way ANOVA Example in R-Quick Guide R
  • How Do Online Criminals Acquire Sensitive Data
    How Do Online Criminals Acquire Sensitive Data Machine Learning
  • gganatogram Plot in R
    How to create Anatogram plot in R R
  • How to put margins on tables or arrays in R?
    How to put margins on tables or arrays in R? R

Privacy Policy

Copyright © 2025 Data Science Tutorials.

Powered by PressBook News WordPress theme