Load required libraries
library(lavaan)
library(semPlot)
# Exercise and weight loss
exercise_lower <- '
1.0
.16 1.0,
.24, -.43, 1.0,
.31, .05, .07, 1.0 '
# Sample size
N1 <- 767
exercise_cor <- getCov(exercise_lower,
names = c("exercise",
"food",
"wt_loss",
"attitude"))
exercise_model <- '
# regressions
exercise ~ attitude
food ~ exercise
wt_loss ~ exercise + food '
exercise_model_fit <- sem(exercise_model,
sample.cov = exercise_cor,
sample.nobs = N1)
summary(exercise_model_fit,
standardized = TRUE)
lavaan (0.5-21) converged normally after 14 iterations
Number of observations 767
Estimator ML
Minimum Function Test Statistic 0.021
Degrees of freedom 2
P-value (Chi-square) 0.990
Parameter Estimates:
Information Expected
Standard Errors Standard
Regressions:
Estimate Std.Err z-value P(>|z|) Std.lv Std.all
exercise ~
attitude 0.310 0.034 9.030 0.000 0.310 0.310
food ~
exercise 0.160 0.036 4.489 0.000 0.160 0.160
wt_loss ~
exercise 0.317 0.031 10.230 0.000 0.317 0.317
food -0.481 0.031 -15.517 0.000 -0.481 -0.481
Variances:
Estimate Std.Err z-value P(>|z|) Std.lv Std.all
.exercise 0.903 0.046 19.583 0.000 0.903 0.904
.food 0.973 0.050 19.583 0.000 0.973 0.974
.wt_loss 0.716 0.037 19.583 0.000 0.716 0.717
6.Plot the path diagram
semPaths(exercise_model_fit,
"par",
style = "ram",
layout = "spring")
# Arbuckle's analysis of Felson & Bornstedt's data
Arbuckle_lower <- '
1
.43 1
.50 .48 1
.49 .22 .32 1
.10 -.04 -.03 .18 1
.04 .02 -.16 -.10 .34 1
.09 .14 .43 .15 -.16 -.27 1 '
# Sample size
N2 <- 209
Arbuckle_cor <- getCov(Arbuckle_lower,
names = c("academic",
"athletic",
"attract",
"GPA",
"height",
"weight",
"rating"))
## replace the "**" with the model specification
Arbuckle_model <- '
# regressions
** ~ **
** ~ **
# correlations
** ~~ ** '
# replace the "**" with the correct parameters
Arbuckle_model_fit <- sem( **,
sample.cov = **,
sample.nobs = **)
summary(Arbuckle_model_fit,
standardized = TRUE)
lavaan (0.5-21) converged normally after 19 iterations
Number of observations 209
Estimator ML
Minimum Function Test Statistic 2.774
Degrees of freedom 2
P-value (Chi-square) 0.250
Parameter Estimates:
Information Expected
Standard Errors Standard
Regressions:
Estimate Std.Err z-value P(>|z|) Std.lv Std.all
academic ~
attract -0.006 0.157 -0.039 0.969 -0.006 -0.006
GPA 0.492 0.079 6.256 0.000 0.492 0.492
attract ~
academic 0.525 0.114 4.610 0.000 0.525 0.525
height 0.003 0.059 0.050 0.960 0.003 0.003
weight -0.078 0.059 -1.324 0.185 -0.078 -0.078
rating 0.363 0.056 6.460 0.000 0.363 0.363
Covariances:
Estimate Std.Err z-value P(>|z|) Std.lv Std.all
.academic ~~
.attract -0.051 0.133 -0.383 0.702 -0.051 -0.076
Variances:
Estimate Std.Err z-value P(>|z|) Std.lv Std.all
.academic 0.760 0.132 5.761 0.000 0.760 0.764
.attract 0.595 0.059 9.998 0.000 0.595 0.598
We can also output a list of model matrices
lavInspect(Arbuckle_model_fit, "est")
$lambda
acadmc attrct GPA height weight rating
academic 1 0 0 0 0 0
attract 0 1 0 0 0 0
GPA 0 0 1 0 0 0
height 0 0 0 1 0 0
weight 0 0 0 0 1 0
rating 0 0 0 0 0 1
$theta
acadmc attrct GPA height weight rating
academic 0
attract 0 0
GPA 0 0 0
height 0 0 0 0
weight 0 0 0 0 0
rating 0 0 0 0 0 0
$psi
acadmc attrct GPA height weight rating
academic 0.760
attract -0.051 0.595
GPA 0.000 0.000 0.995
height 0.000 0.000 0.179 0.995
weight 0.000 0.000 -0.100 0.338 0.995
rating 0.000 0.000 0.149 -0.159 -0.269 0.995
$beta
acadmc attrct GPA height weight rating
academic 0.000 -0.006 0.492 0.000 0.000 0.000
attract 0.525 0.000 0.000 0.003 -0.078 0.363
GPA 0.000 0.000 0.000 0.000 0.000 0.000
height 0.000 0.000 0.000 0.000 0.000 0.000
weight 0.000 0.000 0.000 0.000 0.000 0.000
rating 0.000 0.000 0.000 0.000 0.000 0.000
Note that the psi matrix corresponds to the following compound matrix in the notes
\[ \begin{bmatrix} \boldsymbol \Psi & \mathbf 0\\ \mathbf 0 & \boldsymbol \Phi \end{bmatrix} \]
and the beta matrix to
\[ \begin{bmatrix} \mathbf B & \boldsymbol \Gamma\\ \mathbf 0 & \mathbf 0 \end{bmatrix} \]
semPaths(**,
"par",
style = "ram",
layout = "spring")
# SES, IQ and Need Achievement data
SES_lower <- '
1
.30 1
.41 .16 1
.33 .57 .50 1'
# Sample size
N3 <- 306
SES_cor <- getCov(SES_lower,
names = c("SES",
"IQ",
"nAch",
"GPA"))
SES_model <- '
# regressions
** ~ **
** ~ **
# correlations
** ~~ ** '
SES_model_fit <- sem(**,
sample.cov = **,
sample.nobs = **)
summary(**,
standardized = TRUE)
We can also output a list of model matrices
lavInspect(**, **)
semPaths(**,
"par",
style = "ram",
layout = "spring")
# Social status, actual and perceived income data
income_lower <- '
1.0000
0.3670 1.0000
0.5103 0.4310 1.0000
0.3633 0.1840 0.2772 1.0000
0.1574 0.2819 0.2311 0.2921 1.0000'
# Sample size
N4 <- 432
income_cor <- getCov(income_lower,
names = c("subj_income",
"subj_prestige",
"subj_status",
"actual_income",
"actual_prestige"))
income_model <- '
# regressions
** ~ **
# correlations
** ~~ ** '
income_model_fit <- sem(**,
sample.cov = **,
sample.nobs = **)
summary(**,
standardized = TRUE)
We can also output a list of model matrices
lavInspect(**, **)
semPaths(**,
"par",
style = "ram",
layout = "spring")