Path analysis in R | SEM | Lavaan

Introduction

Structural equation modeling (SEM) provides a framework for evaluating systems of relationships specified from subject-matter theory. Path analysis is the observed-variable form of SEM; it can summarize direct and indirect associations under a proposed model. The analysis does not establish causality by itself, particularly when the data come from an observational or non-randomized study.

This tutorial uses the R package lavaan to specify and estimate a path model, review model-fit information, and draw a diagram. The goal is to explain the workflow and output. Interpretation must remain consistent with the study design, measurement quality, assumptions, and prior evidence.

We will use a path diagram to display hypothesized relationships among measured variables and composite constructs. Arrows represent relationships specified by the analyst; they should not be read as proof of cause and effect.

Data set

The example data include two design factors, a replication identifier, and measured traits related to enzyme activity, plant growth, and yield. Before modeling, confirm variable names, units, missing values, and the experimental layout against the data dictionary and study protocol.

Reset only objects that belong to the current analysis, if needed. Commands such as rm(list = ls()) remove workspace objects and cannot be undone; use them only when you are certain no work needs to be retained. Closing graphics devices is optional, and clearing the console does not change the data or analysis.

rm(list = ls(all = TRUE))
graphics.off()
shell("cls")

Importing data

Place the workbook in the project folder or provide its full path, then import it with readxl::read_excel(). Set col_names = TRUE only when the first row contains variable names. Inspect the imported data and confirm that the expected columns and observations are present.

Use head() to inspect initial rows and str() to check variable types. Convert treatment and blocking variables to factors when they represent categories. This example uses attach() for convenience; explicit references such as data$variable or a model data argument help prevent name conflicts.

library(readxl)
data = read_excel(
          path = 'data_path.xlsx',
          col_names = TRUE
)

head(data)

data$rep = as.factor(data$rep)
data$water = as.factor(data$water)
data$priming = as.factor(data$priming)

attach(data)
# # A tibble: 6 x 12
#     rep water priming   aba   apx   pod    ph   til    pl   grp   tgw    gy
#   <dbl> <chr> <chr>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1     1 FLOOD NP        4.6  0.9   3.78  90.6  11    13.7  75    17.3  20.9
# 2     2 FLOOD NP        5.7  0.77  4.4   81.6  11.0  16.9  84.5  13.6  24.0
# 3     3 FLOOD NP        4.1  0.81  5.33  86.5  10.4  14.4  70.2  15.6  19.1
# 4     1 FLOOD HP        7.1  0.97  6.73  94.2  15.4  18.4  94.1  20.9  28.8
# 5     2 FLOOD HP        5.6  0.91  6.31  86.9  13.9  18.9  88.8  19.7  25.9
# 6     3 FLOOD HP        6.3  0.87  5.4   82.0  12.7  15.4  81.1  17.4  30.3

Performing SEM

SEM involves three linked tasks: specify a model from theory, establish that its parameters are identified and estimate it, then evaluate and communicate the results. Fit indices cannot compensate for a scientifically unjustified model.

  1. Model specification
  2. Model estimation & identification
  3. Path diagram

Model specification

Specify the model using prior knowledge and the study objectives. Define constructs, indicators, and paths before inspecting results; avoid adding or deleting paths solely to improve fit without substantive justification.

The example groups selected measurements into two latent constructs and models their relationships with kernel yield. Retain a measured variable when its role is supported by the study rationale and measurement plan; do not exclude a variable only because earlier studies reported a small effect. Plant height may be relevant in some contexts.

The syntax defines latent construct EA using observed indicators. In lavaan, =~ denotes a measurement relationship between a latent variable and its indicators. Confirm that the indicators are suitable measures of the construct before interpreting loadings.

The model similarly defines YC from observed yield-related indicators. The indicators should reflect a coherent construct and be selected on scientific grounds. A significant loading alone does not establish that an indicator is appropriate.

The regression equations specify the hypothesized relationships of kernel yield with enzyme activity and the yield-related construct. In lavaan, ~ denotes regression. The direction of each equation should follow the scientific model and available temporal or experimental information.

mod.id = '
EA =~ aba + apx + pod
YC =~ til + pl + grp + tgw
gy ~ EA + YC
'

Model estimation and identification

Identification concerns whether the data and model constraints allow parameters to be uniquely estimated. Check identification before interpreting estimates; an underidentified or poorly specified model may fail to converge or yield non-unique estimates.

The sem() call estimates the model using the supplied data. Review warnings, convergence, and inadmissible estimates such as negative variances or implausible standardized coefficients. Successful execution alone does not confirm that a model is acceptable.

Use summary() to inspect estimates and, when requested, fit measures. Read the measurement model and structural regressions separately, and identify whether estimates are unstandardized or standardized.

library(lavaan)
mod.est = sem(
          model = mod.id,
          data = data
)

summary(mod.est)
# lavaan 0.6-8 ended normally after 85 iterations
# 
#   Estimator                                         ML
#   Optimization method                           NLMINB
#   Number of model parameters                        18
#                                                       
#   Number of observations                            18
#                                                       
# Model Test User Model:
#                                                       
#   Test statistic                                23.193
#   Degrees of freedom                                18
#   P-value (Chi-square)                           0.183
# 
# Parameter Estimates:
# 
#   Standard errors                             Standard
#   Information                                 Expected
#   Information saturated (h1) model          Structured
# 
# Latent Variables:
#                    Estimate  Std.Err  z-value  P(>|z|)
#   EA =~                                               
#     aba               1.000                           
#     apx               0.315    0.033    9.621    0.000
#     pod               1.102    0.117    9.385    0.000
#   YC =~                                               
#     til               1.000                           
#     pl                0.968    0.137    7.058    0.000
#     grp               6.103    0.742    8.230    0.000
#     tgw               1.060    0.151    7.006    0.000
# 
# Regressions:
#                    Estimate  Std.Err  z-value  P(>|z|)
#   gy ~                                                
#     EA                3.644    1.843    1.977    0.048
#     YC                0.082    1.034    0.080    0.937
# 
# Covariances:
#                    Estimate  Std.Err  z-value  P(>|z|)
#   EA ~~                                               
#     YC                6.341    2.359    2.689    0.007
# 
# Variances:
#                    Estimate  Std.Err  z-value  P(>|z|)
#    .aba               0.594    0.217    2.739    0.006
#    .apx               0.012    0.007    1.655    0.098
#    .pod               0.195    0.100    1.948    0.051
#    .til               2.748    0.974    2.822    0.005
#    .pl                1.287    0.492    2.619    0.009
#    .grp               9.552    8.392    1.138    0.255
#    .tgw               1.614    0.612    2.637    0.008
#    .gy               11.480    4.110    2.793    0.005
#     EA                3.767    1.443    2.611    0.009
#     YC               11.584    4.700    2.465    0.014

The chi-square test evaluates exact fit: under its assumptions, the null hypothesis is that the model-implied covariance structure matches the population covariance structure. A non-significant result is consistent with exact fit but does not prove the model is correct; the test is sensitive to sample size and model conditions. Do not rely on one threshold alone.

A marker loading may be fixed to 1 to set a latent variable’s scale. Because it is fixed for identification, it has no standard error or significance test. Interpret other loadings alongside their uncertainty and substantive meaning.

The regression section reports estimated relationships among constructs and the outcome. Consider coefficient direction, magnitude, uncertainty, and scale. Statistical significance alone does not establish practical importance or causality.

A covariance parameter describes modeled association between two variables or constructs. It is not directional and should not be described as a causal effect. Interpret its sign and magnitude in the context of the variables and model.

Set fit.measures = TRUE to request additional fit summaries. Evaluate these jointly with model specification, residuals, estimates, and substantive plausibility; no single index provides a complete assessment.

summary(mod.est,
        fit.measures = TRUE)
# lavaan 0.6-8 ended normally after 85 iterations
# 
#   Estimator                                         ML
#   Optimization method                           NLMINB
#   Number of model parameters                        18
#                                                       
#   Number of observations                            18
#                                                       
# Model Test User Model:
#                                                       
#   Test statistic                                23.193
#   Degrees of freedom                                18
#   P-value (Chi-square)                           0.183
# 
# Model Test Baseline Model:
# 
#   Test statistic                               278.306
#   Degrees of freedom                                28
#   P-value                                        0.000
# 
# User Model versus Baseline Model:
# 
#   Comparative Fit Index (CFI)                    0.979
#   Tucker-Lewis Index (TLI)                       0.968
# 
# Loglikelihood and Information Criteria:
# 
#   Loglikelihood user model (H0)               -258.507
#   Loglikelihood unrestricted model (H1)       -246.911
#                                                       
#   Akaike (AIC)                                 553.015
#   Bayesian (BIC)                               569.042
#   Sample-size adjusted Bayesian (BIC)          513.733
# 
# Root Mean Square Error of Approximation:
# 
#   RMSEA                                          0.127
#   90 Percent confidence interval - lower         0.000
#   90 Percent confidence interval - upper         0.259
#   P-value RMSEA <= 0.05                          0.223
# 
# Standardized Root Mean Square Residual:
# 
#   SRMR                                           0.024
# 
# Parameter Estimates:
# 
#   Standard errors                             Standard
#   Information                                 Expected
#   Information saturated (h1) model          Structured
# 
# Latent Variables:
#                    Estimate  Std.Err  z-value  P(>|z|)
#   EA =~                                               
#     aba               1.000                           
#     apx               0.315    0.033    9.621    0.000
#     pod               1.102    0.117    9.385    0.000
#   YC =~                                               
#     til               1.000                           
#     pl                0.968    0.137    7.058    0.000
#     grp               6.103    0.742    8.230    0.000
#     tgw               1.060    0.151    7.006    0.000
# 
# Regressions:
#                    Estimate  Std.Err  z-value  P(>|z|)
#   gy ~                                                
#     EA                3.644    1.843    1.977    0.048
#     YC                0.082    1.034    0.080    0.937
# 
# Covariances:
#                    Estimate  Std.Err  z-value  P(>|z|)
#   EA ~~                                               
#     YC                6.341    2.359    2.689    0.007
# 
# Variances:
#                    Estimate  Std.Err  z-value  P(>|z|)
#    .aba               0.594    0.217    2.739    0.006
#    .apx               0.012    0.007    1.655    0.098
#    .pod               0.195    0.100    1.948    0.051
#    .til               2.748    0.974    2.822    0.005
#    .pl                1.287    0.492    2.619    0.009
#    .grp               9.552    8.392    1.138    0.255
#    .tgw               1.614    0.612    2.637    0.008
#    .gy               11.480    4.110    2.793    0.005
#     EA                3.767    1.443    2.611    0.009
#     YC               11.584    4.700    2.465    0.014

The output may include RMSEA and SRMR. These indices summarize different aspects of model discrepancy, and their interpretation depends on model complexity, degrees of freedom, sample size, and estimation context.

RMSEA is an approximate-fit index that adjusts discrepancy for model complexity; values nearer zero generally indicate less estimated discrepancy. SRMR summarizes standardized residual differences, with lower values generally preferred. Published cutoffs are guidelines, not universal pass/fail rules; assess these indices alongside other evidence.

CFI compares the specified model with a baseline model. Larger values generally indicate better comparative fit, but interpretation depends on the model and data. Common reference values are heuristics, not proof of validity.

TLI is another comparative fit index that penalizes model complexity. Larger values are generally preferred, but no threshold replaces examination of the model, estimates, and residual evidence.

AIC and BIC support comparison among candidate models fitted to the same data under comparable estimation conditions. Lower values indicate a preferred trade-off under each criterion; neither is an absolute measure of fit or evidence that a model is true.

Path diagram

The semPlot package can display the fitted model with semPaths(). Choose labels that identify whether the diagram shows unstandardized estimates, standardized estimates, or another edge quantity. Check that all paths, constructs, indicators, and covariances match the fitted model.

library(semPlot)
semPaths(
          object = mod.est,
          what = "path",
          whatLabels = "par"
)

Refine layout, labels, and visual settings for readability. Keep the diagram consistent with the fitted model, distinguish observed from latent variables, and avoid styling that implies stronger evidence than the estimates support.

Rectangles conventionally denote observed variables and ellipses latent constructs. Single-headed arrows show directional regressions specified in the model; double-headed arrows show covariances. These conventions describe the model structure, not causal proof. Causal conclusions require an appropriate design and assumptions beyond a path diagram.

semPaths(
          object = mod.est,
          what = "path",
          whatLabels = "par",
          style = "ram",
          layout = "tree",
          rotation = 2,
          sizeMan = 7,
          sizeLat = 7,
          color = "lightgray",
          edge.label.cex = 1.2,
          label.cex = 1.3
)

Interpretation of the path diagram

In this path diagram we can see that observed variables enclosed in squares or rectangles and latent variables are in circles or ellipses. Single-headed arrows (paths) are used to define causal relationships in the model, with the variable at the tail of the arrow causing the variable at the head. The double headed arrows pointing the two variables shows the covariance between them. The double headed arrows pointed the same variable represents the variances associated with that variable but it is often omitted if the variable is standardized to unit variance.


Download data file — Click_here

Download Rscript — Click_here


Download R program — Click_here

Download R studio — Click_here


Comments