# Regression analysis with interaction effects ----------------------------
# A silly dataset:
X <- rnorm(100)
Y <- rnorm(100)
Z <- rnorm(1)*X + rnorm(1)*Y + rnorm(1)*X*Y
DF <- data.frame(X,Y,Z)
# Regression including interaction:
res <- lm(Z ~ X*Y, data = DF)
# Path diagram:
semPaths(res, intAtSide=TRUE)
# Standardized estimates:
semPaths(res,"std","hide", intAtSide=TRUE)
# MIMIC model ----------------------------------------------------
## Lavaan
library("lavaan")
# Example 5.8 from mplus user guide:
Data <- read.table("http://www.statmodel.com/usersguide/chap5/ex5.8.dat")
names(Data) <- c(paste("y", 1:6, sep=""),
paste("x", 1:3, sep=""))
# Model:
model.Lavaan <- 'f1 =~ y1 + y2 + y3
f2 =~ y4 + y5 + y6
f1 + f2 ~ x1 + x2 + x3 '
# Run Lavaan:
library("lavaan")
fit <- lavaan:::cfa(model.Lavaan, data=Data, std.lv=TRUE)
# Plot path diagram:
semPaths(fit,title=FALSE, curvePivot = TRUE)
# Omit exogenous covariances:
semPaths(fit,title=FALSE, curvePivot = TRUE, exoVar = FALSE, exoCov = FALSE)
# Standardized parameters:
semPaths(fit,"std",edge.label.cex=0.5, curvePivot = TRUE, exoVar = FALSE,
exoCov = FALSE)
## Mplus
# Same model, now using mplus output:
outfile <- tempfile(fileext=".out")
download.file("http://www.statmodel.com/usersguide/chap5/ex5.8.out",outfile)
# Plot model:
semPaths(outfile,intercepts=FALSE)
# Note that mplus did not report the fixed variances of the exogenous variables.
# Thresholds -----------------------------------------------------
## Lavaan
# Example 5.8 from mplus user guide:
Data <- read.table("http://www.statmodel.com/usersguide/chap5/ex5.2.dat")
names(Data) <- c("u1","u2","u3","u4","u5","u6")
Data <- as.data.frame(lapply(Data, ordered))
# Lavaan model:
model <- ' f1 =~ u1 + u2 + u3; f2 =~ u4 + u5 + u6 '
# Run Lavaan:
fit <- lavaan::cfa(model, data=Data)
# Plot path diagram:
semPaths(fit,intercepts=FALSE)
## Mplus
# Same model, now using mplus output:
outfile <- tempfile(fileext=".out")
download.file("http://www.statmodel.com/usersguide/chap5/ex5.2.out",outfile)
# Plot model:
semPaths(outfile)
# OpenMx ----------------------------------------------------------
# To isntall OpenMx see:
# http://openmx.psyc.virginia.edu/
library("OpenMx")
# Example from mxRun help page:
# Create and run the 1-factor CFA on the openmx.psyc.virginia.edu front page
data(demoOneFactor) # load the demoOneFactor dataframe
manifests <- names(demoOneFactor) # set the manifest to the 5 demo variables
latents <- c("G") # define 1 latent variable
model <- mxModel("One Factor", type="RAM",
manifestVars = manifests,
latentVars = latents,
mxPath(from=latents , to=manifests),
mxPath(from=manifests, arrows=2),
mxPath(from=latents , arrows=2, free=FALSE, values=1.0),
mxData(cov(demoOneFactor), type="cov", numObs=500)
)
model <- mxRun(model) #run model, returning the result
# Plot with colors from OpenMx front page:
semPaths(model, color = list(
lat = rgb(245, 253, 118, maxColorValue = 255),
man = rgb(155, 253, 175, maxColorValue = 255)),
mar = c(10, 5, 10, 5))
## Factor Analysis:
source("http://openmx.psyc.virginia.edu/svn/trunk/demo/TwoFactorModel_PathCov.R")
semPaths(twoFactorFit, layout = "tree2")
# Multi-group analysis -------------------------------------------
## LISREL:
# Download measurment invariance example:
modFile <- tempfile(fileext=".OUT")
download.file("http://sachaepskamp.com/files/mi1.OUT",modFile)
layout(t(1:2))
semPaths(modFile,"eq",ask=FALSE, intAtSide = TRUE, mar = c(8, 1, 5, 1))
# Color indicates equality constraints.Run the code above in your browser using DataLab