piecewiseSEM: Piecewise Structural Equation Modeling in R

repository·main·Indexed 41 days ago

https://github.com/jslefche/piecewisesem

An R package for Piecewise Structural Equation Modeling that combines various regression models—including lm, glm, gam, gls, lme, and glmmTMB—into a single structural equation model framework. It provides the psem() function for model construction and tools via summary() and update() to resolve structural conflicts and correlated errors.

Tokens
978
Snippets
4
Records
4
Agent score
2%

What's inside piecewiseSEM

  1. Install piecewiseSEM

    main

    You can install the stable version of piecewiseSEM from CRAN or the development version from GitHub.

    CRAN (Stable): Use install.packages("piecewiseSEM").

    GitHub (Development): Use devtools::install_github("jslefche/piecewiseSEM@devel"). Note that the development version may be unstable.

    # Install from CRAN
    install.packages("piecewiseSEM")
    
    # Install development version from GitHub
    devtools::install_github("jslefche/piecewiseSEM@devel")
  2. Complete piecewiseSEM usage example

    main

    This example demonstrates the full workflow: loading the library, generating synthetic data, constructing a model with mixed distributions (linear and Poisson), and summarizing the results.

    # Load library
    library(piecewiseSEM)
    
    # Create fake data
    set.seed(1)
    data <- data.frame(
      x = runif(100),
      y1 = runif(100),
      y2 = rpois(100, 1),
      y3 = runif(100)
    )
    
    # Create SEM using `psem`
    modelList <- psem(
      lm(y1 ~ x, data),
      glm(y2 ~ x, "poisson", data),
      lm(y3 ~ y1 + y2, data),
      data
    )
    
    # Run summary
    summary(modelList)
    
    # Address conflict using conserve = T
    summary(modelList, conserve = T)
    
    # Address conflict using direction = c()
    summary(modelList, direction = c("y2 <- y1"))
    
    # Address conflict using correlated errors
    modelList2 <- update(modelList, y2 %~~% y1)
    
    summary(modelList2)
  3. Create a Piecewise SEM using `psem()`

    main

    The psem() function is used to construct a Piecewise Structural Equation Model by combining multiple model objects (e.g., lm, glm, gam). You must provide the individual models and the dataset used.

    Supported model classes include:

    • lm, glm, gls
    • Sarlm, lme, glmmPQL
    • lmerMod, merModLmerTest, glmerMod
    • glmmTMB, gam
    library(piecewiseSEM)
    
    # Example construction
    modelList <- psem(
      lm(y1 ~ x, data),
      glm(y2 ~ x, "poisson", data),
      lm(y3 ~ y1 + y2, data),
      data
    )
  4. Summarize and resolve conflicts in a piecewiseSEM model

    main

    Use summary() to inspect the results of a psem object. If the model contains structural conflicts (e.g., paths that imply different causal directions), you can resolve them using the following arguments in summary():

    • conserve = T: Attempts to resolve conflicts by conserving the specified directions.
    • direction = c(...): Explicitly specifies the direction of the relationship to resolve a conflict (e.g., direction = c("y2 <- y1")).

    To address conflicts caused by correlated errors, use the update() function with the %~~% operator.

    # Basic summary
    summary(modelList)
    
    # Resolve conflict by conserving direction
    summary(modelList, conserve = T)
    
    # Resolve conflict by specifying direction
    summary(modelList, direction = c("y2 <- y1"))
    
    # Address correlated errors using update()
    modelList2 <- update(modelList, y2 %~~% y1)
    summary(modelList2)