Last active
January 28, 2026 17:53
-
-
Save mingjiphd/d96f849ddb4ae569779c57bcd788f4fa to your computer and use it in GitHub Desktop.
Causal Analysis using R Path Analysis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| This R script provides a step-by-step demonstration and explanation of how to perform a path analysis using R. Path analysis is presented as a special case of structural equation modeling (SEM), utilizing R packages such as lavaan, sem, and semPlot. | |
| A step by step video demonstration can be found at: https://youtu.be/0eO54Bzmtmc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ### How to Perform a Path Analysis using R | |
| # Path analysis is an extension of regression that models both direct and indirect relationships | |
| # among a set of observed variables within a system of structured equations. | |
| # It allows variables to act as both predictors and outcomes, thus examining complex causal chains, | |
| # not just single dependencies. | |
| # The method breaks down effects into direct effects (immediate influence) and indirect effects | |
| # (mediated through other variables), clarifying how causal influence travels through the network. | |
| # Path analysis is commonly visualized using a path diagram, where arrows represent hypothesized | |
| # causal connections, and it serves as a subset of structural equation modeling with only observed | |
| # (not latent) variables. | |
| # Applications span psychology, social sciences, healthcare, marketing, and more, making it | |
| # valuable for understanding real-world, multi-step causal processes. | |
| set.seed(8312025) | |
| n <- 200 | |
| attitude <- rnorm(n) | |
| norms <- rnorm(n) | |
| control <- rnorm(n) | |
| # Assume 'attitude' is predicted by 'norms' | |
| attitude <- 0.5*norms + rnorm(n, sd=0.7) | |
| # 'intention' is predicted by all three | |
| intention <- 0.3*attitude + 0.3*norms + 0.2*control + rnorm(n, sd=0.6) | |
| df <- data.frame(attitude, norms, control, intention) | |
| head(df) | |
| ##install.packages("lavaan") | |
| library(lavaan) | |
| model <- ' | |
| attitude ~ norms # norms affects attitude | |
| intention ~ attitude + norms + control # intention affected by attitude, norms, control | |
| ' | |
| fit <- sem(model, data = df) | |
| summary(fit, fit.measures=TRUE) # check fit indices and estimates | |
| #Chi-square Test: Test statistic = 0.007, p-value = 0.934. This high p-value indicates that the model fits the data very well (no significant difference between the model and the observed data). | |
| #CFI (Comparative Fit Index) = 1.000, TLI = 1.029: Values above 0.95 indicate excellent fit. | |
| #RMSEA (Root Mean Square Error of Approximation) = 0.000, 90% CI [0.000, 0.052]: Values below 0.06 are regarded as a good fit; your result is near zero (excellent). | |
| #SRMR (Standardized Root Mean Square Residual) = 0.002: Extremely low, indicating minimal difference between observed and predicted co | |
| #All predictors are statistically significant (p < .001); | |
| #their z-values (>3) reinforce the strength of effects. | |
| #Variances | |
| #.attitude variance = 0.421, .intention variance = 0.437: These are error variances (residuals) for each endogenous variable | |
| #—the unexplained part by predictors. | |
| #Both are significant, meaning not all of the variation | |
| #in attitude or intention is explained by the model. | |
| ###Path Diagram | |
| #install.packages("semPlot") | |
| #install.packages("sem") | |
| library(sem) | |
| library(semPlot) | |
| semPaths(fit, whatLabels="std", layout="tree", edge.label.cex=0.9, title=FALSE) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| A step by step video demonstration can be found at: https://youtu.be/0eO54Bzmtmc |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A step by step video demonstration can be found at: https://youtu.be/0eO54Bzmtmc