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.1

Last updated: 25 June 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.1

License

GPL-3

Maintainer

Jon Lefcheck

Last Published

July 3rd, 2018

Functions in piecewiseSEM (2.0.1)

%~~%

Correlated error operator
print.psem

Print psem
print.summary.psem

Print summary
infCrit

Information criterion values for SEM
as.psem

Convert list to psem object
rsquared.glm

R^2 for glm objects
nObs

Get number of observations from a model
get.formula.list

get.formula.list
filterExogenous

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

Strip transformations
listFormula

Get list of formula from a `sem` object
dupOutput

Identify duplicate output
summary.psem

Summarizing piecewise structural equation models
sem.coefs

sem.coefs
sem.basis.set

sem.basis.set
get.dag

get.dag
filterInteractions

Filter interactions from the d-sep tests
rsquared.glmerMod

R^2 for glmer objects
specifyDir

Remove items from the basis set whose direction is a priori specified
endogenous.reverse

endogenous.reverse
evaluateClasses

Evaluate model classes and stop if unsupported model class
fisherC

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

Format for psem
dSep

Tests of directed separation
findbars.lme

Get random effects from lme
get.sort.dag

get.sort.dag
partialResid

Computing partial effects
getResidModels

Identify models with correlated errors and return modified versions
partialCorr

Calculate partial correlations from partial residuals
get.model.control

get.model.control
piecewiseSEM-package

Piecewise Structural Equation Modeling
get.basis.set

get.basis.set
stdCoefs

Calculate standardized regression coefficients
residuals.psem

Residual values from fit models
print.attr

Do not print attributes with custom functions
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
get.random.formula

get.random.formula
keeley

Data set from Keeley et al.
isSig

Assess significance
filter.exogenous

filter.exogenous
scaleFam

Properly scale standard deviations depending on the error distribution
removeData

Remove data from the model list
sem.aic

sem.aic
resid.lme

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

Remove existing paths from the basis set
scaleInt

Calculate standard deviation or relevant range for interaction terms
scaleGLM

Compute standard deviation or relevant range of response for GLMs
rsquared.lme

R^2 for lme objects
rsquared.lm

R^2 for lm objects
psem

Fitting piecewise structural equation models
get.scaled.data

get.scaled.data
removeCerror

Remove correlated errors from the basis set
rsquared.negbin

R^2 for negbin objects
rsquared.merMod

R^2 for phylolm objects
sem.missing.paths

sem.missing.paths
sem.model.fits

sem.model.fits
get.scaled.model

get.scaled.model
unstdCoefs

Get raw (undstandardized) coefficients from model
update.psem

Update psem model object with additional values.
onlyBars

Get random effects from merMod
partial.resid

partial.resid
sem.fisher.c

sem.fisher.c
reverseNonLin

If intermediate endogenous variables are nonlinear, return both directions
sem.fit

sem.fit
rsquared

R-squared for linear regression
rsquared.glmmPQL

R^2 for glmmPQL objects
rsquared.gls

R^2 for gls objects
shipley

Data set from Shipley (2009)
sortDag

Sort DAG based on ancestry
Dag

Generate adjacency matrix from list of structural equations
all.vars.merMod

Remove random effects from all.vars
all.vars_notrans

Get vector of untransformed variables
GetData

Get data from model list
basisSet

Derivation of the basis set
captureTable

Captures output table
all.vars_trans

Get vector of transformed variables
GetVarCov

Get random effects variance-covariance from lme
KRp

Recompute P-values using Kenward-Rogers approximation
GetOLRE

Obtain (observation-level) random effects from a generalized linear mixed model
cerror

Correlated errors
GetSingleData

Get data from one model
cyclic

Determine whether graph is cylic
coefs

Extract path coefficients
AIC.psem

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

Generalized function for SEM BIC score
dataTrans

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