SlideShare a Scribd company logo
Path Analysis and Mediation in lavaan
George Mount - george@georgejmount.com - @gjmount
LAtent VAriable ANalysis
lavaan is available as a beta package for structural equation modeling.
In this example we will examine the mediating effects of self-esteem on the relationship
between grades and happiness.
#call lavaan
library(lavaan)
## This is lavaan 0.5-23.1097
## lavaan is BETA software! Please report any bugs.
#read file, name columns
myData <- read.csv('http://static.lib.virginia.edu/statlab/materials/data/mediat
colnames(myData) <- c("grades","selfesteem","happiness")
In classical statistics, a mediation model is one in which the dependent variable is
influenced by the mediator variable (B), which is influenced by the independent variable
(A). Thus the direct effect from the independent to dependent variable is insignificant
(C).
First we must specify the model. In lavaan the model is put in quotation marks. The
usual ~ mark is used for a regression and parameters are labeled for model specification.
set.seed(1234)
med.model <- '#direct effect
happiness ~ c * grades
#mediators
happiness ~ b * selfesteem
selfesteem ~ a * grades
#indirect effects
indirect := a*b
#direct effects
direct := c
#total effects
total := c + (a*b)'
To run the model, we use the sem function.
fit <- sem(med.model, data = myData)
Then we look at the results. We will choose to look at the standardized results and
model fit statistics.
summary(fit, standardized=T, fit.measures = TRUE, rsquare = TRUE)
## lavaan (0.5-23.1097) converged normally after 14 iterations
##
## Number of observations 100
##
## Estimator ML
## Minimum Function Test Statistic 0.000
## Degrees of freedom 0
## Minimum Function Value 0.0000000000000
##
## Model test baseline model:
##
## Minimum Function Test Statistic 77.413
## Degrees of freedom 3
## P-value 0.000
##
## User model versus baseline model:
##
## Comparative Fit Index (CFI) 1.000
## Tucker-Lewis Index (TLI) 1.000
##
## Loglikelihood and Information Criteria:
##
## Loglikelihood user model (H0) -576.601
## Loglikelihood unrestricted model (H1) -576.601
##
## Number of free parameters 5
## Akaike (AIC) 1163.203
## Bayesian (BIC) 1176.229
## Sample-size adjusted Bayesian (BIC) 1160.438
##
## Root Mean Square Error of Approximation:
##
## RMSEA 0.000
## 90 Percent Confidence Interval 0.000 0.000
## P-value RMSEA <= 0.05 NA
##
## Standardized Root Mean Square Residual:
##
## SRMR 0.000
##
## Parameter Estimates:
##
## Information Expected
## Standard Errors Standard
##
## Regressions:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## happiness ~
## grades (c) 0.040 0.108 0.367 0.714 0.040 0.034
## selfesteem (b) 0.635 0.099 6.418 0.000 0.635 0.593
## selfesteem ~
## grades (a) 0.561 0.094 5.998 0.000 0.561 0.514
##
## Variances:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## .happiness 2.581 0.365 7.071 0.000 2.581 0.627
## .selfesteem 2.633 0.372 7.071 0.000 2.633 0.735
##
## R-Square:
## Estimate
## happiness 0.373
## selfesteem 0.265
##
## Defined Parameters:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## indirect 0.357 0.081 4.382 0.000 0.357 0.305
## direct 0.040 0.108 0.367 0.714 0.040 0.034
## total 0.396 0.110 3.600 0.000 0.396 0.339
Compare this to the results of regression happiness on grades.
set.seed(1234)
med.model.b <- '#direct effect
happiness ~ c * grades
#direct effects
direct := c'
fit.b <- sem(med.model.b, data = myData)
set.seed(1234)
med.model.b <- '#direct effect
happiness ~ c * grades
#direct effects
direct := c'
fit.b <- sem(med.model.b, data = myData)
summary(fit.b, standardized=T, fit.measures = TRUE, rsquare = TRUE)
## lavaan (0.5-23.1097) converged normally after 11 iterations
##
## Number of observations 100
##
## Estimator ML
## Minimum Function Test Statistic 0.000
## Degrees of freedom 0
## Minimum Function Value 0.0000000000000
##
## Model test baseline model:
##
## Minimum Function Test Statistic 12.185
## Degrees of freedom 1
## P-value 0.000
##
## User model versus baseline model:
##
## Comparative Fit Index (CFI) 1.000
## Tucker-Lewis Index (TLI) 1.000
##
## Loglikelihood and Information Criteria:
##
## Loglikelihood user model (H0) -403.548
## Loglikelihood unrestricted model (H1) -403.548
##
## Number of free parameters 2
## Akaike (AIC) 811.096
## Bayesian (BIC) 816.307
## Sample-size adjusted Bayesian (BIC) 809.990
##
## Root Mean Square Error of Approximation:
##
## RMSEA 0.000
## 90 Percent Confidence Interval 0.000 0.000
## P-value RMSEA <= 0.05 NA
##
## Standardized Root Mean Square Residual:
##
## SRMR 0.000
##
## Parameter Estimates:
##
## Information Expected
## Standard Errors Standard
##
## Regressions:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## happiness ~
## grades (c) 0.396 0.110 3.600 0.000 0.396 0.339
##
## Variances:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## .happiness 3.645 0.515 7.071 0.000 3.645 0.885
##
## R-Square:
## Estimate
## happiness 0.115
##
## Defined Parameters:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## direct 0.396 0.110 3.600 0.000 0.396 0.339
Last but not least, let’s plot our mediated model.
library(semPlot)
semPaths(fit, "model", "est", layout = 'spring',
edge.label.cex=1.25, fade=FALSE)
0.04 0.64
0.56
2.58
2.633.01
hpp
slfgrd
THANK YOU
Contact me:
george@georgejmount.com
@gjmount

More Related Content

What's hot

7 classical assumptions of ordinary least squares
7 classical assumptions of ordinary least squares7 classical assumptions of ordinary least squares
7 classical assumptions of ordinary least squares
Yugesh Dutt Panday
 
Introduction to Structural Equation Modeling Partial Least Sqaures (SEM-PLS)
Introduction to Structural Equation Modeling Partial Least Sqaures (SEM-PLS)Introduction to Structural Equation Modeling Partial Least Sqaures (SEM-PLS)
Introduction to Structural Equation Modeling Partial Least Sqaures (SEM-PLS)
Ali Asgari
 
판별 모델을 통한 대체어 추출
판별 모델을 통한 대체어 추출판별 모델을 통한 대체어 추출
판별 모델을 통한 대체어 추출
ssuserf03c031
 
Methods of data collection
Methods of data collectionMethods of data collection
Methods of data collection
Jithin Thomas
 
Evaluating Published Research
Evaluating Published ResearchEvaluating Published Research
Evaluating Published Research
Dissertation Success, LLC
 
Key ideas, terms and concepts in SEM
Key ideas, terms and concepts in SEMKey ideas, terms and concepts in SEM
Key ideas, terms and concepts in SEM
University of Southampton
 
Statistical test in spss
Statistical test in spssStatistical test in spss
Statistical test in spss
Bipin Neupane
 
Data extraction/coding and database structure in meta-analysis
Data extraction/coding and database structure in meta-analysisData extraction/coding and database structure in meta-analysis
Data extraction/coding and database structure in meta-analysis
Rizwan S A
 
SURVIVAL ANALYSIS 1.pptx
SURVIVAL ANALYSIS 1.pptxSURVIVAL ANALYSIS 1.pptx
SURVIVAL ANALYSIS 1.pptx
DrVikasKaushik1
 
Sampling design
Sampling designSampling design
Sampling design
Jayaprakash CR
 
Structural Equation Modelling (SEM) Part 3
Structural Equation Modelling (SEM) Part 3Structural Equation Modelling (SEM) Part 3
Structural Equation Modelling (SEM) Part 3
COSTARCH Analytical Consulting (P) Ltd.
 
Stata Uygulamalı Panel Eşbütünleşme Testleri ve Model Tahmini
Stata Uygulamalı Panel Eşbütünleşme Testleri ve Model TahminiStata Uygulamalı Panel Eşbütünleşme Testleri ve Model Tahmini
Stata Uygulamalı Panel Eşbütünleşme Testleri ve Model Tahmini
yigitcanozmeral
 
Basics of Structural Equation Modeling
Basics of Structural Equation ModelingBasics of Structural Equation Modeling
Basics of Structural Equation Modeling
smackinnon
 
Structural Equation Modelling (SEM) Part 2
Structural Equation Modelling (SEM) Part 2Structural Equation Modelling (SEM) Part 2
Structural Equation Modelling (SEM) Part 2
COSTARCH Analytical Consulting (P) Ltd.
 
Meta-analysis and systematic reviews
Meta-analysis and systematic reviews Meta-analysis and systematic reviews
Meta-analysis and systematic reviews coolboy101pk
 

What's hot (16)

7 classical assumptions of ordinary least squares
7 classical assumptions of ordinary least squares7 classical assumptions of ordinary least squares
7 classical assumptions of ordinary least squares
 
Introduction to Structural Equation Modeling Partial Least Sqaures (SEM-PLS)
Introduction to Structural Equation Modeling Partial Least Sqaures (SEM-PLS)Introduction to Structural Equation Modeling Partial Least Sqaures (SEM-PLS)
Introduction to Structural Equation Modeling Partial Least Sqaures (SEM-PLS)
 
판별 모델을 통한 대체어 추출
판별 모델을 통한 대체어 추출판별 모델을 통한 대체어 추출
판별 모델을 통한 대체어 추출
 
Methods of data collection
Methods of data collectionMethods of data collection
Methods of data collection
 
Evaluating Published Research
Evaluating Published ResearchEvaluating Published Research
Evaluating Published Research
 
Key ideas, terms and concepts in SEM
Key ideas, terms and concepts in SEMKey ideas, terms and concepts in SEM
Key ideas, terms and concepts in SEM
 
Statistical test in spss
Statistical test in spssStatistical test in spss
Statistical test in spss
 
Data extraction/coding and database structure in meta-analysis
Data extraction/coding and database structure in meta-analysisData extraction/coding and database structure in meta-analysis
Data extraction/coding and database structure in meta-analysis
 
SURVIVAL ANALYSIS 1.pptx
SURVIVAL ANALYSIS 1.pptxSURVIVAL ANALYSIS 1.pptx
SURVIVAL ANALYSIS 1.pptx
 
Sampling design
Sampling designSampling design
Sampling design
 
Sem+Essentials
Sem+EssentialsSem+Essentials
Sem+Essentials
 
Structural Equation Modelling (SEM) Part 3
Structural Equation Modelling (SEM) Part 3Structural Equation Modelling (SEM) Part 3
Structural Equation Modelling (SEM) Part 3
 
Stata Uygulamalı Panel Eşbütünleşme Testleri ve Model Tahmini
Stata Uygulamalı Panel Eşbütünleşme Testleri ve Model TahminiStata Uygulamalı Panel Eşbütünleşme Testleri ve Model Tahmini
Stata Uygulamalı Panel Eşbütünleşme Testleri ve Model Tahmini
 
Basics of Structural Equation Modeling
Basics of Structural Equation ModelingBasics of Structural Equation Modeling
Basics of Structural Equation Modeling
 
Structural Equation Modelling (SEM) Part 2
Structural Equation Modelling (SEM) Part 2Structural Equation Modelling (SEM) Part 2
Structural Equation Modelling (SEM) Part 2
 
Meta-analysis and systematic reviews
Meta-analysis and systematic reviews Meta-analysis and systematic reviews
Meta-analysis and systematic reviews
 

Similar to Mediation in R's lavaan package

Course Project for Coursera Practical Machine Learning
Course Project for Coursera Practical Machine LearningCourse Project for Coursera Practical Machine Learning
Course Project for Coursera Practical Machine LearningJohn Edward Slough II
 
Predicting Employee Attrition
Predicting Employee AttritionPredicting Employee Attrition
Predicting Employee Attrition
Shruti Mohan
 
wk5ppt2_Iris
wk5ppt2_Iriswk5ppt2_Iris
wk5ppt2_Iris
AliciaWei1
 
Customer analytics for e commerce
Customer analytics for e commerceCustomer analytics for e commerce
Customer analytics for e commerce
Alok Tayal (PMP, PMI-ACP, TOGAF)
 
Data analysis on bank data
Data analysis on bank dataData analysis on bank data
Data analysis on bank data
ANISH BHANUSHALI
 
Predicting Employee Churn: A Data-Driven Approach Project Presentation
Predicting Employee Churn: A Data-Driven Approach Project PresentationPredicting Employee Churn: A Data-Driven Approach Project Presentation
Predicting Employee Churn: A Data-Driven Approach Project Presentation
Boston Institute of Analytics
 
Phase 2 of Predicting Payment default on Vehicle Loan EMI
Phase 2 of Predicting Payment default on Vehicle Loan EMIPhase 2 of Predicting Payment default on Vehicle Loan EMI
Phase 2 of Predicting Payment default on Vehicle Loan EMI
Vikas Virani
 
Telecom Churn Analysis
Telecom Churn AnalysisTelecom Churn Analysis
Telecom Churn Analysis
Vasudev pendyala
 
Higgs Boson Challenge
Higgs Boson ChallengeHiggs Boson Challenge
Higgs Boson Challenge
Raouf KESKES
 
Random forest algorithm for regression a beginner's guide
Random forest algorithm for regression   a beginner's guideRandom forest algorithm for regression   a beginner's guide
Random forest algorithm for regression a beginner's guide
prateek kumar
 
Factor analysis in R by Aman Chauhan
Factor analysis in R by Aman ChauhanFactor analysis in R by Aman Chauhan
Factor analysis in R by Aman Chauhan
Aman Chauhan
 
Software Analytics In Action: A Hands-on Tutorial on Mining, Analyzing, Model...
Software Analytics In Action: A Hands-on Tutorial on Mining, Analyzing, Model...Software Analytics In Action: A Hands-on Tutorial on Mining, Analyzing, Model...
Software Analytics In Action: A Hands-on Tutorial on Mining, Analyzing, Model...
Chakkrit (Kla) Tantithamthavorn
 
R Programming For Beginners | R Language Tutorial | R Tutorial For Beginners ...
R Programming For Beginners | R Language Tutorial | R Tutorial For Beginners ...R Programming For Beginners | R Language Tutorial | R Tutorial For Beginners ...
R Programming For Beginners | R Language Tutorial | R Tutorial For Beginners ...
Edureka!
 
Logistic Regression using Mahout
Logistic Regression using MahoutLogistic Regression using Mahout
Logistic Regression using Mahout
tanuvir
 
Six sigma statistics
Six sigma statisticsSix sigma statistics
Six sigma statisticsShankaran Rd
 
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
Yao Yao
 
Stat-weight Improving the Estimator of Interleaved Methods Outcomes with Stat...
Stat-weight Improving the Estimator of Interleaved Methods Outcomes with Stat...Stat-weight Improving the Estimator of Interleaved Methods Outcomes with Stat...
Stat-weight Improving the Estimator of Interleaved Methods Outcomes with Stat...
Sease
 
Artificial Intelligence.pptx
Artificial Intelligence.pptxArtificial Intelligence.pptx
Artificial Intelligence.pptx
Mubashir Hashmi
 
Data science with R - Clustering and Classification
Data science with R - Clustering and ClassificationData science with R - Clustering and Classification
Data science with R - Clustering and Classification
Brigitte Mueller
 
Essay on-data-analysis
Essay on-data-analysisEssay on-data-analysis
Essay on-data-analysis
Raman Kannan
 

Similar to Mediation in R's lavaan package (20)

Course Project for Coursera Practical Machine Learning
Course Project for Coursera Practical Machine LearningCourse Project for Coursera Practical Machine Learning
Course Project for Coursera Practical Machine Learning
 
Predicting Employee Attrition
Predicting Employee AttritionPredicting Employee Attrition
Predicting Employee Attrition
 
wk5ppt2_Iris
wk5ppt2_Iriswk5ppt2_Iris
wk5ppt2_Iris
 
Customer analytics for e commerce
Customer analytics for e commerceCustomer analytics for e commerce
Customer analytics for e commerce
 
Data analysis on bank data
Data analysis on bank dataData analysis on bank data
Data analysis on bank data
 
Predicting Employee Churn: A Data-Driven Approach Project Presentation
Predicting Employee Churn: A Data-Driven Approach Project PresentationPredicting Employee Churn: A Data-Driven Approach Project Presentation
Predicting Employee Churn: A Data-Driven Approach Project Presentation
 
Phase 2 of Predicting Payment default on Vehicle Loan EMI
Phase 2 of Predicting Payment default on Vehicle Loan EMIPhase 2 of Predicting Payment default on Vehicle Loan EMI
Phase 2 of Predicting Payment default on Vehicle Loan EMI
 
Telecom Churn Analysis
Telecom Churn AnalysisTelecom Churn Analysis
Telecom Churn Analysis
 
Higgs Boson Challenge
Higgs Boson ChallengeHiggs Boson Challenge
Higgs Boson Challenge
 
Random forest algorithm for regression a beginner's guide
Random forest algorithm for regression   a beginner's guideRandom forest algorithm for regression   a beginner's guide
Random forest algorithm for regression a beginner's guide
 
Factor analysis in R by Aman Chauhan
Factor analysis in R by Aman ChauhanFactor analysis in R by Aman Chauhan
Factor analysis in R by Aman Chauhan
 
Software Analytics In Action: A Hands-on Tutorial on Mining, Analyzing, Model...
Software Analytics In Action: A Hands-on Tutorial on Mining, Analyzing, Model...Software Analytics In Action: A Hands-on Tutorial on Mining, Analyzing, Model...
Software Analytics In Action: A Hands-on Tutorial on Mining, Analyzing, Model...
 
R Programming For Beginners | R Language Tutorial | R Tutorial For Beginners ...
R Programming For Beginners | R Language Tutorial | R Tutorial For Beginners ...R Programming For Beginners | R Language Tutorial | R Tutorial For Beginners ...
R Programming For Beginners | R Language Tutorial | R Tutorial For Beginners ...
 
Logistic Regression using Mahout
Logistic Regression using MahoutLogistic Regression using Mahout
Logistic Regression using Mahout
 
Six sigma statistics
Six sigma statisticsSix sigma statistics
Six sigma statistics
 
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
 
Stat-weight Improving the Estimator of Interleaved Methods Outcomes with Stat...
Stat-weight Improving the Estimator of Interleaved Methods Outcomes with Stat...Stat-weight Improving the Estimator of Interleaved Methods Outcomes with Stat...
Stat-weight Improving the Estimator of Interleaved Methods Outcomes with Stat...
 
Artificial Intelligence.pptx
Artificial Intelligence.pptxArtificial Intelligence.pptx
Artificial Intelligence.pptx
 
Data science with R - Clustering and Classification
Data science with R - Clustering and ClassificationData science with R - Clustering and Classification
Data science with R - Clustering and Classification
 
Essay on-data-analysis
Essay on-data-analysisEssay on-data-analysis
Essay on-data-analysis
 

More from George Mount

Building a Data Academy: Presentation to Pittsburgh Chapter, Association for ...
Building a Data Academy: Presentation to Pittsburgh Chapter, Association for ...Building a Data Academy: Presentation to Pittsburgh Chapter, Association for ...
Building a Data Academy: Presentation to Pittsburgh Chapter, Association for ...
George Mount
 
Building the Data Academy (Pluralsight LIVE presentation)
Building the Data Academy (Pluralsight LIVE presentation)Building the Data Academy (Pluralsight LIVE presentation)
Building the Data Academy (Pluralsight LIVE presentation)
George Mount
 
Blogging Effectively about Coding (WordCamp Denver 2020)
Blogging Effectively about Coding (WordCamp Denver 2020)Blogging Effectively about Coding (WordCamp Denver 2020)
Blogging Effectively about Coding (WordCamp Denver 2020)
George Mount
 
Demo guide: The central limit theorem, visualized in Excel
Demo guide: The central limit theorem, visualized in ExcelDemo guide: The central limit theorem, visualized in Excel
Demo guide: The central limit theorem, visualized in Excel
George Mount
 
What is the data analytics stack?
What is the data analytics stack? What is the data analytics stack?
What is the data analytics stack?
George Mount
 
Blended learning for data education
Blended learning for data educationBlended learning for data education
Blended learning for data education
George Mount
 
Teaching coding: What is pair programming?
Teaching coding: What is pair programming?Teaching coding: What is pair programming?
Teaching coding: What is pair programming?
George Mount
 
Hiring data scientists doesn't make a data culture
Hiring data scientists doesn't make a data cultureHiring data scientists doesn't make a data culture
Hiring data scientists doesn't make a data culture
George Mount
 
It’s time to open-source your data tools and processes
It’s time to open-source your data tools and processesIt’s time to open-source your data tools and processes
It’s time to open-source your data tools and processes
George Mount
 
Building the data academy: Measuring ROI
Building the data academy: Measuring ROIBuilding the data academy: Measuring ROI
Building the data academy: Measuring ROI
George Mount
 
Building your data academy: Assessing candidate skills
Building your data academy: Assessing candidate skillsBuilding your data academy: Assessing candidate skills
Building your data academy: Assessing candidate skills
George Mount
 
Teaching coding: What is a faded example?
Teaching coding: What is a faded example?Teaching coding: What is a faded example?
Teaching coding: What is a faded example?
George Mount
 
YouTube is not a data upskilling strategy
YouTube is not a data upskilling strategyYouTube is not a data upskilling strategy
YouTube is not a data upskilling strategy
George Mount
 
Five myths about learning data analytics
Five myths about learning data analyticsFive myths about learning data analytics
Five myths about learning data analytics
George Mount
 
Building your data academy
Building your data academyBuilding your data academy
Building your data academy
George Mount
 
Survey and Measure Development in R
Survey and Measure Development in RSurvey and Measure Development in R
Survey and Measure Development in R
George Mount
 
It's not what you know, it's how you show.
It's not what you know, it's how you show.It's not what you know, it's how you show.
It's not what you know, it's how you show.
George Mount
 
Qualitative Research Study Exercise: Reading Choices and Habits
Qualitative Research Study Exercise: Reading Choices and HabitsQualitative Research Study Exercise: Reading Choices and Habits
Qualitative Research Study Exercise: Reading Choices and Habits
George Mount
 
Hey, Analyst! Learn Some Content Marketing
Hey, Analyst! Learn Some Content MarketingHey, Analyst! Learn Some Content Marketing
Hey, Analyst! Learn Some Content Marketing
George Mount
 
Why Be a Social Analyst?
Why Be a Social Analyst? Why Be a Social Analyst?
Why Be a Social Analyst?
George Mount
 

More from George Mount (20)

Building a Data Academy: Presentation to Pittsburgh Chapter, Association for ...
Building a Data Academy: Presentation to Pittsburgh Chapter, Association for ...Building a Data Academy: Presentation to Pittsburgh Chapter, Association for ...
Building a Data Academy: Presentation to Pittsburgh Chapter, Association for ...
 
Building the Data Academy (Pluralsight LIVE presentation)
Building the Data Academy (Pluralsight LIVE presentation)Building the Data Academy (Pluralsight LIVE presentation)
Building the Data Academy (Pluralsight LIVE presentation)
 
Blogging Effectively about Coding (WordCamp Denver 2020)
Blogging Effectively about Coding (WordCamp Denver 2020)Blogging Effectively about Coding (WordCamp Denver 2020)
Blogging Effectively about Coding (WordCamp Denver 2020)
 
Demo guide: The central limit theorem, visualized in Excel
Demo guide: The central limit theorem, visualized in ExcelDemo guide: The central limit theorem, visualized in Excel
Demo guide: The central limit theorem, visualized in Excel
 
What is the data analytics stack?
What is the data analytics stack? What is the data analytics stack?
What is the data analytics stack?
 
Blended learning for data education
Blended learning for data educationBlended learning for data education
Blended learning for data education
 
Teaching coding: What is pair programming?
Teaching coding: What is pair programming?Teaching coding: What is pair programming?
Teaching coding: What is pair programming?
 
Hiring data scientists doesn't make a data culture
Hiring data scientists doesn't make a data cultureHiring data scientists doesn't make a data culture
Hiring data scientists doesn't make a data culture
 
It’s time to open-source your data tools and processes
It’s time to open-source your data tools and processesIt’s time to open-source your data tools and processes
It’s time to open-source your data tools and processes
 
Building the data academy: Measuring ROI
Building the data academy: Measuring ROIBuilding the data academy: Measuring ROI
Building the data academy: Measuring ROI
 
Building your data academy: Assessing candidate skills
Building your data academy: Assessing candidate skillsBuilding your data academy: Assessing candidate skills
Building your data academy: Assessing candidate skills
 
Teaching coding: What is a faded example?
Teaching coding: What is a faded example?Teaching coding: What is a faded example?
Teaching coding: What is a faded example?
 
YouTube is not a data upskilling strategy
YouTube is not a data upskilling strategyYouTube is not a data upskilling strategy
YouTube is not a data upskilling strategy
 
Five myths about learning data analytics
Five myths about learning data analyticsFive myths about learning data analytics
Five myths about learning data analytics
 
Building your data academy
Building your data academyBuilding your data academy
Building your data academy
 
Survey and Measure Development in R
Survey and Measure Development in RSurvey and Measure Development in R
Survey and Measure Development in R
 
It's not what you know, it's how you show.
It's not what you know, it's how you show.It's not what you know, it's how you show.
It's not what you know, it's how you show.
 
Qualitative Research Study Exercise: Reading Choices and Habits
Qualitative Research Study Exercise: Reading Choices and HabitsQualitative Research Study Exercise: Reading Choices and Habits
Qualitative Research Study Exercise: Reading Choices and Habits
 
Hey, Analyst! Learn Some Content Marketing
Hey, Analyst! Learn Some Content MarketingHey, Analyst! Learn Some Content Marketing
Hey, Analyst! Learn Some Content Marketing
 
Why Be a Social Analyst?
Why Be a Social Analyst? Why Be a Social Analyst?
Why Be a Social Analyst?
 

Recently uploaded

一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
ukgaet
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
ewymefz
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
Subhajit Sahu
 
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
vcaxypu
 
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
dwreak4tg
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Boston Institute of Analytics
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
nscud
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
yhkoc
 
Quantitative Data AnalysisReliability Analysis (Cronbach Alpha) Common Method...
Quantitative Data AnalysisReliability Analysis (Cronbach Alpha) Common Method...Quantitative Data AnalysisReliability Analysis (Cronbach Alpha) Common Method...
Quantitative Data AnalysisReliability Analysis (Cronbach Alpha) Common Method...
2023240532
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
slg6lamcq
 
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
ewymefz
 
My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.
rwarrenll
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
slg6lamcq
 
Machine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptxMachine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptx
balafet
 
FP Growth Algorithm and its Applications
FP Growth Algorithm and its ApplicationsFP Growth Algorithm and its Applications
FP Growth Algorithm and its Applications
MaleehaSheikh2
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
Timothy Spann
 
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
vcaxypu
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
Opendatabay
 
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
mbawufebxi
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
nscud
 

Recently uploaded (20)

一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
 
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
 
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
 
Quantitative Data AnalysisReliability Analysis (Cronbach Alpha) Common Method...
Quantitative Data AnalysisReliability Analysis (Cronbach Alpha) Common Method...Quantitative Data AnalysisReliability Analysis (Cronbach Alpha) Common Method...
Quantitative Data AnalysisReliability Analysis (Cronbach Alpha) Common Method...
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
 
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
 
My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
 
Machine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptxMachine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptx
 
FP Growth Algorithm and its Applications
FP Growth Algorithm and its ApplicationsFP Growth Algorithm and its Applications
FP Growth Algorithm and its Applications
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
 
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
 
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
 

Mediation in R's lavaan package

  • 1. Path Analysis and Mediation in lavaan George Mount - george@georgejmount.com - @gjmount
  • 2. LAtent VAriable ANalysis lavaan is available as a beta package for structural equation modeling. In this example we will examine the mediating effects of self-esteem on the relationship between grades and happiness. #call lavaan library(lavaan) ## This is lavaan 0.5-23.1097 ## lavaan is BETA software! Please report any bugs. #read file, name columns myData <- read.csv('http://static.lib.virginia.edu/statlab/materials/data/mediat colnames(myData) <- c("grades","selfesteem","happiness")
  • 3. In classical statistics, a mediation model is one in which the dependent variable is influenced by the mediator variable (B), which is influenced by the independent variable (A). Thus the direct effect from the independent to dependent variable is insignificant (C).
  • 4. First we must specify the model. In lavaan the model is put in quotation marks. The usual ~ mark is used for a regression and parameters are labeled for model specification. set.seed(1234) med.model <- '#direct effect happiness ~ c * grades #mediators happiness ~ b * selfesteem selfesteem ~ a * grades #indirect effects indirect := a*b #direct effects direct := c #total effects total := c + (a*b)'
  • 5. To run the model, we use the sem function. fit <- sem(med.model, data = myData) Then we look at the results. We will choose to look at the standardized results and model fit statistics.
  • 6. summary(fit, standardized=T, fit.measures = TRUE, rsquare = TRUE) ## lavaan (0.5-23.1097) converged normally after 14 iterations ## ## Number of observations 100 ## ## Estimator ML ## Minimum Function Test Statistic 0.000 ## Degrees of freedom 0 ## Minimum Function Value 0.0000000000000 ## ## Model test baseline model: ## ## Minimum Function Test Statistic 77.413 ## Degrees of freedom 3 ## P-value 0.000 ## ## User model versus baseline model: ## ## Comparative Fit Index (CFI) 1.000 ## Tucker-Lewis Index (TLI) 1.000 ## ## Loglikelihood and Information Criteria: ## ## Loglikelihood user model (H0) -576.601 ## Loglikelihood unrestricted model (H1) -576.601 ## ## Number of free parameters 5 ## Akaike (AIC) 1163.203 ## Bayesian (BIC) 1176.229 ## Sample-size adjusted Bayesian (BIC) 1160.438
  • 7. ## ## Root Mean Square Error of Approximation: ## ## RMSEA 0.000 ## 90 Percent Confidence Interval 0.000 0.000 ## P-value RMSEA <= 0.05 NA ## ## Standardized Root Mean Square Residual: ## ## SRMR 0.000 ## ## Parameter Estimates: ## ## Information Expected ## Standard Errors Standard ## ## Regressions: ## Estimate Std.Err z-value P(>|z|) Std.lv Std.all ## happiness ~ ## grades (c) 0.040 0.108 0.367 0.714 0.040 0.034 ## selfesteem (b) 0.635 0.099 6.418 0.000 0.635 0.593 ## selfesteem ~ ## grades (a) 0.561 0.094 5.998 0.000 0.561 0.514 ## ## Variances: ## Estimate Std.Err z-value P(>|z|) Std.lv Std.all ## .happiness 2.581 0.365 7.071 0.000 2.581 0.627 ## .selfesteem 2.633 0.372 7.071 0.000 2.633 0.735 ## ## R-Square:
  • 8. ## Estimate ## happiness 0.373 ## selfesteem 0.265 ## ## Defined Parameters: ## Estimate Std.Err z-value P(>|z|) Std.lv Std.all ## indirect 0.357 0.081 4.382 0.000 0.357 0.305 ## direct 0.040 0.108 0.367 0.714 0.040 0.034 ## total 0.396 0.110 3.600 0.000 0.396 0.339
  • 9. Compare this to the results of regression happiness on grades. set.seed(1234) med.model.b <- '#direct effect happiness ~ c * grades #direct effects direct := c' fit.b <- sem(med.model.b, data = myData)
  • 10. set.seed(1234) med.model.b <- '#direct effect happiness ~ c * grades #direct effects direct := c' fit.b <- sem(med.model.b, data = myData) summary(fit.b, standardized=T, fit.measures = TRUE, rsquare = TRUE) ## lavaan (0.5-23.1097) converged normally after 11 iterations ## ## Number of observations 100 ## ## Estimator ML ## Minimum Function Test Statistic 0.000 ## Degrees of freedom 0 ## Minimum Function Value 0.0000000000000 ## ## Model test baseline model: ## ## Minimum Function Test Statistic 12.185 ## Degrees of freedom 1 ## P-value 0.000 ## ## User model versus baseline model: ## ## Comparative Fit Index (CFI) 1.000
  • 11. ## Tucker-Lewis Index (TLI) 1.000 ## ## Loglikelihood and Information Criteria: ## ## Loglikelihood user model (H0) -403.548 ## Loglikelihood unrestricted model (H1) -403.548 ## ## Number of free parameters 2 ## Akaike (AIC) 811.096 ## Bayesian (BIC) 816.307 ## Sample-size adjusted Bayesian (BIC) 809.990 ## ## Root Mean Square Error of Approximation: ## ## RMSEA 0.000 ## 90 Percent Confidence Interval 0.000 0.000 ## P-value RMSEA <= 0.05 NA ## ## Standardized Root Mean Square Residual: ## ## SRMR 0.000 ## ## Parameter Estimates: ## ## Information Expected ## Standard Errors Standard ## ## Regressions: ## Estimate Std.Err z-value P(>|z|) Std.lv Std.all ## happiness ~
  • 12. ## grades (c) 0.396 0.110 3.600 0.000 0.396 0.339 ## ## Variances: ## Estimate Std.Err z-value P(>|z|) Std.lv Std.all ## .happiness 3.645 0.515 7.071 0.000 3.645 0.885 ## ## R-Square: ## Estimate ## happiness 0.115 ## ## Defined Parameters: ## Estimate Std.Err z-value P(>|z|) Std.lv Std.all ## direct 0.396 0.110 3.600 0.000 0.396 0.339
  • 13. Last but not least, let’s plot our mediated model. library(semPlot) semPaths(fit, "model", "est", layout = 'spring', edge.label.cex=1.25, fade=FALSE) 0.04 0.64 0.56 2.58 2.633.01 hpp slfgrd