Contents

Overview

There are two basic functions that allow you to run path analysis in Lavaan: the ‘sem’ and the ‘lavaan’ functions.This video will demonstrate how to specify a path model involving only manifest variables and how to estimate model parameters using the ‘lavaan’ function.

Reference | المرجع

Firstly, Model specification in ‘lavaan’ function | بناء النموذج النظري وتوصيفه

Step 1: Use lavaan model syntax to specify path model and have it stored in an R object. In our model, we will treat ses, mastery goals, and performance goals as predictors of student achievement. The effect of mastery on achievement will be both direct and indirect (via interest and anxiety. The effects of ses and performance goals will be treated as being fully mediated through anxiety and interest.

When specifying predictive relationships in the model, we use the tilde sign (‘~’), which separates thedependent variable in each equation from its predictors. Predictors are separated in each equation by ‘+’ sign. In our model, we will also allow the residuals for anxiety and interest to correlate (see ‘~~’ in syntax below).

Model specification

model<-'
#equation where interest is predicted by ses & mastery and 
#performance goals
interest~mastery+perfgoal+ses
#equation where achieve is predicted by interest and anxiety
achieve~anxiety+interest+mastery
#equation where anxiety is predicted by mastery and performance goals
anxiety~perfgoal+mastery
#estimtating the variances of the exogenous variables (ses, mastery,performance)
mastery~~mastery
perfgoal~~perfgoal
ses~~ses
#estimtating the covariances of the exogenous variables (ses, mastery,performance)
mastery~~perfgoal+ses
perfgoal~~ses
#estimating the residual variances for endogenous variables (interest, anxiety, achieve)
interest~~interest
anxiety~~anxiety
achieve~~achieve
#estimating the covariance of residuals for interest and anxiety
interest~~anxiety'

Secondly, Model Identification | تحديد النموذج وتعيينه

\[ \begin{array}{l}\text { Parameters being estimated } \\ \text { Variances of three exogenous variables } \\ \text { Residual variances associated with three endogenous variables } \\ 3 \text { covariances among exogenous variables } \\ 1 \text { residual covariance between endogenous variables } \\ 8 \text { path coefficients } \\ \qquad \text { Total }\end{array} \]

\[ M=\frac{V(V+1)}{2}=\frac{6(6+1)}{2}=21 \] \[ d f=M-P=21-18=3 \]

Thirdly, Model Estimation | تقدير معالم النموذج

Step 2: Use ‘lavaan’ function to run analysis. Here, I will be saving the results in an R object called ‘fit’ (arbitrarily named). Inside the parenthesis are arguments separated by commas. The first argument contains the name of the object containing the model syntax (see above). The object is named ‘model’ (again, arbitrarily named above). Next, we have the ‘data’ argument. This identifies the object (i.e., data frame) containing the raw data.

fit<-lavaan(model,data=Data_OAlwusaidi)
summary(fit,fit.measures=TRUE)
## lavaan 0.6-7 ended normally after 34 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of free parameters                         18
##                                                       
##   Number of observations                           140
##                                                       
## Model Test User Model:
##                                                       
##   Test statistic                                29.231
##   Degrees of freedom                                 3
##   P-value (Chi-square)                           0.000
## 
## Model Test Baseline Model:
## 
##   Test statistic                               202.409
##   Degrees of freedom                                15
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.860
##   Tucker-Lewis Index (TLI)                       0.300
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -1391.274
##   Loglikelihood unrestricted model (H1)      -1376.659
##                                                       
##   Akaike (AIC)                                2818.548
##   Bayesian (BIC)                              2871.498
##   Sample-size adjusted Bayesian (BIC)         2814.548
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.250
##   90 Percent confidence interval - lower         0.172
##   90 Percent confidence interval - upper         0.336
##   P-value RMSEA <= 0.05                          0.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.074
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   interest ~                                          
##     mastery           0.708    0.088    8.066    0.000
##     perfgoal         -0.035    0.040   -0.879    0.380
##     ses               0.520    0.242    2.154    0.031
##   achieve ~                                           
##     anxiety          -0.040    0.054   -0.747    0.455
##     interest          0.211    0.060    3.527    0.000
##     mastery           0.345    0.079    4.358    0.000
##   anxiety ~                                           
##     perfgoal          0.025    0.045    0.556    0.578
##     mastery          -0.387    0.097   -4.009    0.000
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   mastery ~~                                          
##     perfgoal         -0.935    0.361   -2.590    0.010
##     ses               0.170    0.061    2.805    0.005
##   perfgoal ~~                                         
##     ses              -0.226    0.128   -1.768    0.077
##  .interest ~~                                         
##    .anxiety           0.059    0.181    0.329    0.742
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     mastery           1.944    0.232    8.367    0.000
##     perfgoal          8.936    1.068    8.367    0.000
##     ses               0.249    0.030    8.367    0.000
##    .interest          1.895    0.227    8.367    0.000
##    .anxiety           2.410    0.288    8.367    0.000
##    .achieve           0.988    0.118    8.367    0.000
summary(fit,fit.measures=TRUE,standardized=TRUE,rsquare=TRUE)
## lavaan 0.6-7 ended normally after 34 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of free parameters                         18
##                                                       
##   Number of observations                           140
##                                                       
## Model Test User Model:
##                                                       
##   Test statistic                                29.231
##   Degrees of freedom                                 3
##   P-value (Chi-square)                           0.000
## 
## Model Test Baseline Model:
## 
##   Test statistic                               202.409
##   Degrees of freedom                                15
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.860
##   Tucker-Lewis Index (TLI)                       0.300
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -1391.274
##   Loglikelihood unrestricted model (H1)      -1376.659
##                                                       
##   Akaike (AIC)                                2818.548
##   Bayesian (BIC)                              2871.498
##   Sample-size adjusted Bayesian (BIC)         2814.548
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.250
##   90 Percent confidence interval - lower         0.172
##   90 Percent confidence interval - upper         0.336
##   P-value RMSEA <= 0.05                          0.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.074
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   interest ~                                                            
##     mastery           0.708    0.088    8.066    0.000    0.708    0.558
##     perfgoal         -0.035    0.040   -0.879    0.380   -0.035   -0.060
##     ses               0.520    0.242    2.154    0.031    0.520    0.147
##   achieve ~                                                             
##     anxiety          -0.040    0.054   -0.747    0.455   -0.040   -0.053
##     interest          0.211    0.060    3.527    0.000    0.211    0.294
##     mastery           0.345    0.079    4.358    0.000    0.345    0.379
##   anxiety ~                                                             
##     perfgoal          0.025    0.045    0.556    0.578    0.025    0.045
##     mastery          -0.387    0.097   -4.009    0.000   -0.387   -0.327
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   mastery ~~                                                            
##     perfgoal         -0.935    0.361   -2.590    0.010   -0.935   -0.224
##     ses               0.170    0.061    2.805    0.005    0.170    0.244
##   perfgoal ~~                                                           
##     ses              -0.226    0.128   -1.768    0.077   -0.226   -0.151
##  .interest ~~                                                           
##    .anxiety           0.059    0.181    0.329    0.742    0.059    0.028
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##     mastery           1.944    0.232    8.367    0.000    1.944    1.000
##     perfgoal          8.936    1.068    8.367    0.000    8.936    1.000
##     ses               0.249    0.030    8.367    0.000    0.249    1.000
##    .interest          1.895    0.227    8.367    0.000    1.895    0.606
##    .anxiety           2.410    0.288    8.367    0.000    2.410    0.884
##    .achieve           0.988    0.118    8.367    0.000    0.988    0.613
## 
## R-Square:
##                    Estimate
##     interest          0.394
##     anxiety           0.116
##     achieve           0.387
parameterEstimates(fit)
##         lhs op      rhs    est    se      z pvalue ci.lower ci.upper
## 1  interest  ~  mastery  0.708 0.088  8.066  0.000    0.536    0.880
## 2  interest  ~ perfgoal -0.035 0.040 -0.879  0.380   -0.114    0.043
## 3  interest  ~      ses  0.520 0.242  2.154  0.031    0.047    0.994
## 4   achieve  ~  anxiety -0.040 0.054 -0.747  0.455   -0.146    0.066
## 5   achieve  ~ interest  0.211 0.060  3.527  0.000    0.094    0.328
## 6   achieve  ~  mastery  0.345 0.079  4.358  0.000    0.190    0.500
## 7   anxiety  ~ perfgoal  0.025 0.045  0.556  0.578   -0.063    0.113
## 8   anxiety  ~  mastery -0.387 0.097 -4.009  0.000   -0.576   -0.198
## 9   mastery ~~  mastery  1.944 0.232  8.367  0.000    1.488    2.399
## 10 perfgoal ~~ perfgoal  8.936 1.068  8.367  0.000    6.842   11.029
## 11      ses ~~      ses  0.249 0.030  8.367  0.000    0.191    0.308
## 12  mastery ~~ perfgoal -0.935 0.361 -2.590  0.010   -1.642   -0.227
## 13  mastery ~~      ses  0.170 0.061  2.805  0.005    0.051    0.288
## 14 perfgoal ~~      ses -0.226 0.128 -1.768  0.077   -0.476    0.024
## 15 interest ~~ interest  1.895 0.227  8.367  0.000    1.451    2.339
## 16  anxiety ~~  anxiety  2.410 0.288  8.367  0.000    1.845    2.974
## 17  achieve ~~  achieve  0.988 0.118  8.367  0.000    0.757    1.220
## 18 interest ~~  anxiety  0.059 0.181  0.329  0.742   -0.295    0.414
fitMeasures(fit)
##                npar                fmin               chisq                  df 
##              18.000               0.104              29.231               3.000 
##              pvalue      baseline.chisq         baseline.df     baseline.pvalue 
##               0.000             202.409              15.000               0.000 
##                 cfi                 tli                nnfi                 rfi 
##               0.860               0.300               0.300               0.278 
##                 nfi                pnfi                 ifi                 rni 
##               0.856               0.171               0.868               0.860 
##                logl   unrestricted.logl                 aic                 bic 
##           -1391.274           -1376.659            2818.548            2871.498 
##              ntotal                bic2               rmsea      rmsea.ci.lower 
##             140.000            2814.548               0.250               0.172 
##      rmsea.ci.upper        rmsea.pvalue                 rmr          rmr_nomean 
##               0.336               0.000               0.122               0.122 
##                srmr        srmr_bentler srmr_bentler_nomean                crmr 
##               0.074               0.074               0.074               0.088 
##         crmr_nomean          srmr_mplus   srmr_mplus_nomean               cn_05 
##               0.088               0.074               0.074              38.428 
##               cn_01                 gfi                agfi                pgfi 
##              55.335               0.941               0.587               0.134 
##                 mfi                ecvi 
##               0.911               0.466

Fourthly, Model Modification | تعديل النموذج المفترض في ضوء مؤشرات التعديل

modificationIndices(fit)
##         lhs op      rhs     mi     epc sepc.lv sepc.all sepc.nox
## 19 interest ~~  achieve 25.396  -2.899  -2.899   -2.118   -2.118
## 23  achieve ~~  anxiety  6.668   6.803   6.803    4.408    4.408
## 24  achieve ~~  mastery 22.476  -1.743  -1.743   -1.257   -1.257
## 25  achieve ~~ perfgoal  2.762  -0.406  -0.406   -0.137   -0.137
## 26  achieve ~~      ses 20.541   0.186   0.186    0.376    0.376
## 27  anxiety ~~  mastery  0.921   0.765   0.765    0.354    0.354
## 28  anxiety ~~ perfgoal  0.921  -3.576  -3.576   -0.771   -0.771
## 29  anxiety ~~      ses  0.921  -0.061  -0.061   -0.078   -0.078
## 30 interest  ~  achieve 25.396  -2.933  -2.933   -2.106   -2.106
## 32  achieve  ~ perfgoal  4.551  -0.062  -0.062   -0.146   -0.146
## 33  achieve  ~      ses 22.431   0.837   0.837    0.329    0.329
## 34  anxiety  ~ interest  0.921  -0.502  -0.502   -0.538   -0.538
## 35  anxiety  ~  achieve  1.119   1.922   1.922    1.478    1.478
## 36  anxiety  ~      ses  0.921  -0.261  -0.261   -0.079   -0.079
## 37  mastery  ~ interest  0.921  12.878  12.878   16.335   16.335
## 38  mastery  ~  achieve 22.801  -1.774  -1.774   -1.616   -1.616
## 39  mastery  ~  anxiety  0.921   0.318   0.318    0.376    0.376
## 42 perfgoal  ~ interest  0.920 -60.126 -60.126  -35.570  -35.570
## 43 perfgoal  ~  achieve  2.744  -0.410  -0.410   -0.174   -0.174
## 44 perfgoal  ~  anxiety  0.921  -1.484  -1.484   -0.819   -0.819
## 47      ses  ~ interest  0.921  -1.020  -1.020   -3.614   -3.614
## 48      ses  ~  achieve 20.964   0.190   0.190    0.484    0.484
## 49      ses  ~  anxiety  0.921  -0.025  -0.025   -0.083   -0.083
# Specification of model using auto.var argument...
model<-'
#equation where interest is predicted by ses & mastery and 
#performance goals
interest~mastery+perfgoal+ses
#equation where achieve is predicted by interest and anxiety
achieve~anxiety+interest+mastery
#equation where anxiety is predicted by mastery and performance goals
anxiety~perfgoal+mastery
#estimtating the variances of the exogenous variables (ses, mastery,performance)
mastery~~mastery
perfgoal~~perfgoal
ses~~ses
#estimtating the covariances of the exogenous variables (ses, mastery,performance)
mastery~~perfgoal+ses
perfgoal~~ses
#The auto.var argument when fitting the model can be used so that 
#you do not have to directly request estimation of residual variances
#Estimating the covariance of residuals for interest and anxiety
interest~~anxiety'

fit<-lavaan(model,data=Data_OAlwusaidi,auto.var=TRUE)
summary(fit,fit.measures=TRUE,standardized=TRUE,rsquare=TRUE)
## lavaan 0.6-7 ended normally after 34 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of free parameters                         18
##                                                       
##   Number of observations                           140
##                                                       
## Model Test User Model:
##                                                       
##   Test statistic                                29.231
##   Degrees of freedom                                 3
##   P-value (Chi-square)                           0.000
## 
## Model Test Baseline Model:
## 
##   Test statistic                               202.409
##   Degrees of freedom                                15
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.860
##   Tucker-Lewis Index (TLI)                       0.300
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -1391.274
##   Loglikelihood unrestricted model (H1)      -1376.659
##                                                       
##   Akaike (AIC)                                2818.548
##   Bayesian (BIC)                              2871.498
##   Sample-size adjusted Bayesian (BIC)         2814.548
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.250
##   90 Percent confidence interval - lower         0.172
##   90 Percent confidence interval - upper         0.336
##   P-value RMSEA <= 0.05                          0.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.074
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   interest ~                                                            
##     mastery           0.708    0.088    8.066    0.000    0.708    0.558
##     perfgoal         -0.035    0.040   -0.879    0.380   -0.035   -0.060
##     ses               0.520    0.242    2.154    0.031    0.520    0.147
##   achieve ~                                                             
##     anxiety          -0.040    0.054   -0.747    0.455   -0.040   -0.053
##     interest          0.211    0.060    3.527    0.000    0.211    0.294
##     mastery           0.345    0.079    4.358    0.000    0.345    0.379
##   anxiety ~                                                             
##     perfgoal          0.025    0.045    0.556    0.578    0.025    0.045
##     mastery          -0.387    0.097   -4.009    0.000   -0.387   -0.327
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   mastery ~~                                                            
##     perfgoal         -0.935    0.361   -2.590    0.010   -0.935   -0.224
##     ses               0.170    0.061    2.805    0.005    0.170    0.244
##   perfgoal ~~                                                           
##     ses              -0.226    0.128   -1.768    0.077   -0.226   -0.151
##  .interest ~~                                                           
##    .anxiety           0.059    0.181    0.329    0.742    0.059    0.028
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##     mastery           1.944    0.232    8.367    0.000    1.944    1.000
##     perfgoal          8.936    1.068    8.367    0.000    8.936    1.000
##     ses               0.249    0.030    8.367    0.000    0.249    1.000
##    .interest          1.895    0.227    8.367    0.000    1.895    0.606
##    .achieve           0.988    0.118    8.367    0.000    0.988    0.613
##    .anxiety           2.410    0.288    8.367    0.000    2.410    0.884
## 
## R-Square:
##                    Estimate
##     interest          0.394
##     achieve           0.387
##     anxiety           0.116

Fifthly, Path diagrams | الرسم التخطيطي للنموذج المقترح

# There are a couple of ways you can obtain path diagrams (although they can be somewhat tricky to implement.
# One approach is to use the 'semPaths' function 

library("semPlot")
## Registered S3 methods overwritten by 'huge':
##   method    from   
##   plot.sim  BDgraph
##   print.sim BDgraph
semPaths(fit,what="paths",whatLabels="par",style="lisrel",layout="tree",
         rotation=2)

# A second approach is to use the 'lavaanPlot" function

library(lavaanPlot)

lavaanPlot(model = fit, node_options = list(shape = "box", fontname = 
                                              "Helvetica"), edge_options = list(color = "grey"), coefs = TRUE,covs=
             TRUE,stars = c("regress"))