Learn R Programming

⚠️There's a newer version (2.3.0) of this package.Take me there.

piecewiseSEM: Piecewise Structural Equation Modeling in R

Getting Help

See our website at http://jslefche.github.io/piecewiseSEM/

Version 2.0.2

Last updated: 24 July 2018

This version is a major update to the piecewiseSEM package that usesa completely revised syntax that better reproduces the base R syntax and output. It is highly recommended that consult vignette("piecewiseSEM") even if you have used the package before as it documents the many changes.

It also incorporates new functionality in the form of coefficient standardization and updated methods for R^2 for mixed models.

Currently supported model classes: lm, glm, gls, pgls, sarlm, lme, glmmPQL, lmerMod, merModLmerTest, glmerMod

Example

# Install development branch from github
library(devtools)
install_github("jslefche/piecewiseSEM@devel", build_vignette = TRUE)

# Load library
library(piecewiseSEM)

# Read vignette
vignette("piecewiseSEM")

# Create fake data
set.seed(1)

data <- data.frame(
  x = runif(100),
  y1 = runif(100),
  y2 = rpois(100, 1),
  y3 = runif(100)
)

# Store in SEM list
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

2,735

Version

2.0.2

License

GPL-3

Maintainer

Jon Lefcheck

Last Published

July 24th, 2018

Functions in piecewiseSEM (2.0.2)

dataTrans

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

Recompute P-values using Kenward-Rogers approximation
GetVarCov

Get random effects variance-covariance from lme
all.vars_trans

Get vector of transformed variables
fisherC

Summarize tests of directed separation using Fisher's C statistic
print.psem

Print psem
print.summary.psem

Print summary
scaleFam

Properly scale standard deviations depending on the error distribution
as.psem

Convert list to psem object
Dag

Generate adjacency matrix from list of structural equations
dSep

Tests of directed separation
get.dag

get.dag
piecewiseSEM-package

Piecewise Structural Equation Modeling
GetData

Get data from model list
get.formula.list

get.formula.list
cyclic

Determine whether graph is cylic
print.attr

Do not print attributes with custom functions
psem

Fitting piecewise structural equation models
scaleGLM

Compute standard deviation or relevant range of response for GLMs
filter.exogenous

filter.exogenous
filterExisting

Remove existing paths from the basis set
specifyDir

Remove items from the basis set whose direction is a priori specified
stdCoefs

Calculate standardized regression coefficients
%~~%

Correlated error operator
removeCerror

Remove correlated errors from the basis set
AIC.psem

Generalized function for SEM AIC(c) score
BIC.psem

Generalized function for SEM BIC score
rsquared.glm

R^2 for glm objects
all.vars.merMod

Remove random effects from all.vars
rsquared.glmerMod

R^2 for glmer objects
infCrit

Information criterion values for SEM
partialCorr

Calculate partial correlations from partial residuals
cerror

Correlated errors
partialResid

Computing partial effects
sem.aic

sem.aic
scaleInt

Calculate standard deviation or relevant range for interaction terms
stripTransformations

Strip transformations
all.vars_notrans

Get vector of untransformed variables
summary.psem

Summarizing piecewise structural equation models
coefs

Extract path coefficients
basisSet

Derivation of the basis set
reverseNonLin

If intermediate endogenous variables are nonlinear, return both directions
endogenous.reverse

endogenous.reverse
rsquared

R-squared for linear regression
evaluateClasses

Evaluate model classes and stop if unsupported model class
get.model.control

get.model.control
GetOLRE

Obtain (observation-level) random effects from a generalized linear mixed model
rsquared.glmmPQL

R^2 for glmmPQL objects
get.random.formula

get.random.formula
onlyBars

Get random effects from merMod
captureTable

Captures output table
GetSingleData

Get data from one model
partial.resid

partial.resid
formatpsem

Format for psem
rsquared.gls

R^2 for gls objects
get.basis.set

get.basis.set
rsquared.lm

R^2 for lm objects
filterExogenous

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

Filter interactions from the d-sep tests
rsquared.lme

R^2 for lme objects
get.sort.dag

get.sort.dag
shipley

Data set from Shipley (2009)
sem.fisher.c

sem.fisher.c
getResidModels

Identify models with correlated errors and return modified versions
get.scaled.model

get.scaled.model
get.scaled.data

get.scaled.data
sortDag

Sort DAG based on ancestry
sem.fit

sem.fit
unstdCoefs

Get raw (undstandardized) coefficients from model
update.psem

Update psem model object with additional values.
listFormula

Get list of formula from a `sem` object
nObs

Get number of observations from a model
residuals.psem

Residual values from fit models
isSig

Assess significance
keeley

Data set from Keeley et al.
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
removeData

Remove data from the model list
resid.lme

Get residuals from innermost grouping of mixed models (replicate-level)
rsquared.merMod

R^2 for phylolm objects
sem.basis.set

sem.basis.set
sem.coefs

sem.coefs
rsquared.negbin

R^2 for negbin objects
sem.missing.paths

sem.missing.paths
sem.model.fits

sem.model.fits
dupOutput

Identify duplicate output
findbars.lme

Get random effects from lme