Learn R Programming

piecewiseSEM: Piecewise Structural Equation Modeling in R

Version 2.3.0

Last updated: 03 March 2023

To install

Run the following code to install the latest version from CRAN:

install.packages("piecewiseSEM")

Run the following code to install the development version:

devtools::install_github("jslefche/piecewiseSEM@devel")

Note: the development version may be unstable and lead to unanticipated bugs. Contact the package developer with any bugs or issues.

Getting Help

See our website at piecewiseSEM

There is an online resource available for SEM, including piecewiseSEM and lavaan, available https://jslefche.github.io/sem_book/

Version 2 is a major update to the piecewiseSEM package that uses a completely revised syntax that better reproduces the base R syntax and output. It is highly recommended that consult the resource above even if you have used the package before as it documents the many changes.

Currently supported model classes: lm, glm, gls, Sarlm, lme, glmmPQL, lmerMod, merModLmerTest, glmerMod. glmmTMB, gam

Example

# 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)

Copy Link

Version

Install

install.packages('piecewiseSEM')

Monthly Downloads

1,940

Version

2.3.0

License

GPL-3

Maintainer

Last Published

March 4th, 2023

Functions in piecewiseSEM (2.3.0)

captureTable

Captures output table
checkData

Check to see whether supplied data.frame matches model-extracted data
evaluateClasses

Evaluate model classes and stop if unsupported model class
dataTrans

Transform variables based on model formula and store in new data frame
fisherC

Summarize tests of directed separation using Fisher's C statistic
fixCatDir

Flip independence claims so categorical variables are not the response
getDAG

Generate adjacency matrix from list of structural equations
as.psem

Convert list to psem object
anova.psem

ANOVA and chi-squared difference test for model comparison
filterInteractions

Filter interactions from the d-sep tests
filterExogenous

Filter relationships among exogenous variables from the basis set (ignoring add.vars)
getCoefficients

Get coefficients from linear regression
anovaLRT

Chi-square difference test
getLHS

Get Left-hand side of formulae
getRHS

Get Right-hand side of formulae
getResidModels

Identify models with correlated errors and return modified versions
checkTransformations

Check to see whether variables exist as transformed and untransformed
getSortedPsem

Get a sorted psem object in DAG order
filterExisting

Remove existing paths from the basis set
isSig

Assess significance
get_response

Get Response Name as a Character
keeley

Data set from Grace & Keeley (2006)
import

Functions to import from dependencies
handleCategoricalCoefs

Handles putting categorical variables into coefficient tables for easy use in path analysis
dupOutput

Identify duplicate output
print.anova.psem

Print anova
plot.psem

Plotting of Piecewise Structural Equation Models
print.psem

Print psem
print.multigroup.psem

Print multigroup
meadows

Data set from Grace & Jutila (1999)
removeCerror

Remove correlated errors from the basis set
multigroup

Multigroup Analysis for Piecewise SEM
rsquared

R-squared for linear regression
listFormula

Recompute P-values using Kenward-Rogers approximation
findbars.lme

Get random effects from lme
rsquared.gam

R^2 for gam objects
rsquared.lme

R^2 for lme objects
nObs

Get number of observations from a model
rsquared.glm

R^2 for glm objects
filterSmoothed

First, remove claims where linear and non-linear terms appear in the same claim
partialResid

Computing partial effects
print.summary.psem

Print summary
psem

Fitting piecewise structural equation models
rsquared.merMod

R^2 for phylolm objects
rsquared.lm

R^2 for lm objects
piecewiseSEM-package

Piecewise Structural Equation Modeling
rsquared.gls

R^2 for gls objects
removeData

Remove data from the model list
getSatModels

Get saturated model by reinserting all excluded paths
print.attr

Do not print attributes with custom functions
specifyDir

Remove duplicate items from the basis set whose direction is not a priori specified
sortDag

Sort DAG based on ancestry
rsquared.Sarlm

R^2 for Sarlm objects
rsquared.glmerMod

R^2 for glmer objects
update.psem

Update psem model object with additional values.
dSep

Tests of directed separation
getAnova

Get ANOVA results from `merMod`
coefs

Extract path coefficients
formatpsem

Format for psem
rsquared.glmmPQL

R^2 for glmmPQL objects
%~~%

Correlated error operator
%not_in%

Operator for non-overlap in sets
partialCorr

Calculate partial correlations from partial residuals
resid.lme

Get residuals from innermost grouping of mixed models (replicate-level)
onlyBars

Get random effects from merMod
print.basisSet

Print basis set
rsquared.negbin

R^2 for negbin objects
scaleInt

Calculate standard deviation or relevant range for interaction terms
scaleGLM

Compute standard deviation or relevant range of response for GLMs
shipley

Data set from Shipley (2006)
residuals.psem

Residual values from fit models
reverseAddVars

Replace transformations in the basis set by cycling through neighbors and applying transformations in order of how variables are treated in the child nearest to current node
summary.psem

Summarizing piecewise structural equation models
stripTransformations

Strip transformations
reverseNonLin

If intermediate endogenous variables are nonlinear, return both directions
testBasisSetElements

Evaluate conditional independence claim from the basis set
stdCoefs

Calculate standardized regression coefficients
stop_psem

Stop function for unsupported methods
unstdCoefs

Get raw (undstandardized) coefficients from model
GetOLRE

Obtain (observation-level) random effects from a generalized linear mixed model
AIC.psem

Generic function for SEM AIC(c) score
LLchisq

Generalized chi-squared for piecewise SEM
AIC_psem

Information criterion values for SEM
cerror

Correlated errors
cbind_fill

Bind data.frames of differing dimensions
GetData

Get data from model list
GetSDy

Properly scale standard deviations depending on the error distribution
GetVarCov

Get random effects variance-covariance from lme
anovaTable

Single anova
all.vars_notrans

Get vector of untransformed variables
all.vars.merMod

Remove random effects from all.vars
GetSDx

Get standard deviation of predictor variables
basisSet

Derivation of the basis set
GetSingleData

Get data from one model
all.vars_trans

Get vector of transformed variables