Change lavaan syntax dynamically and programmatically with R objects

116 views
Skip to first unread message

isaactp...@gmail.com

unread,
Mar 29, 2022, 9:59:49 AM3/29/22
to lavaan
Is it possible to change syntax for a lavaan model dynamically and programmatically with R objects? I’d like to include the contents of an R object in lavaan syntax. As an example, I’d like to fix parameters to a value specified from an R object.

Here is an example:
```
library("lavaan")

sampleSize <- 1000

set.seed(52242)
mydata <- data.frame(matrix(nrow = sampleSize, ncol = 4))
names(mydata) <- c("predictorLatentSEM","criterionLatentSEM","predictorObservedSEM","criterionObservedSEM")

latentCorrelation <- .8
reliabilityPredictor <- .9
reliabilityCriterion <- .85

mydata$predictorLatentSEM <- rnorm(sampleSize, 0 , 1)
mydata$criterionLatentSEM <- latentCorrelation * mydata$predictorLatentSEM + rnorm(sampleSize, 0, sqrt(1 - latentCorrelation ^ 2))
mydata$predictorObservedSEM <- reliabilityPredictor * mydata$predictorLatentSEM + rnorm(sampleSize, 0, sqrt(1 - reliabilityPredictor ^ 2))
mydata$criterionObservedSEM <- reliabilityCriterion * mydata$criterionLatentSEM + rnorm(sampleSize, 0, sqrt(1 - reliabilityCriterion ^ 2))

# This works
model_syntax <- '
# Factor loadings
predictorLatent =~ 1*predictorObservedSEM
criterionLatent =~ 1*criterionObservedSEM

# Factor correlation
criterionLatent ~ predictorLatent

# Residual errors
predictorObservedSEM ~~ (1 - .9^2)*predictorObservedSEM # where .9 is the value of `reliabilityPredictor`
criterionObservedSEM ~~ (1 - .85^2)*criterionObservedSEM # where .85 is the value of `reliabilityCriterion`
'

model_fit <- sem(model_syntax,
data = mydata,
missing = "ML")

# This does not work
model_syntax2 <- '
# Factor loadings
predictorLatent =~ 1*predictorObservedSEM
criterionLatent =~ 1*criterionObservedSEM

# Factor correlation
criterionLatent ~ predictorLatent

# Residual errors
predictorObservedSEM ~~ (1 - reliabilityPredictor^2)*predictorObservedSEM
criterionObservedSEM ~~ (1 - reliabilityCriterion^2)*criterionObservedSEM
'

model_fit2 <- sem(model_syntax2,
data = mydata,
missing = "ML")
```

In the example above, I’d like to fix the residual error of the `predictorObservedSEM` variable to be equal to the value of `(1 - reliabilityPredictor ^ 2)`. However, the lavaan syntax does not seem to allow fixing parameters based on the values from other R objects. In this minimal example, I’m trying to specify a fixed parameter, but It’d be nice to extend this to be able to specify fixed parameters, parameter labels, starting values, which variables load on a factor, etc. programmatically based on the contents of R objects.

Thanks in advance,
Isaac

Terrence Jorgensen

unread,
Mar 29, 2022, 10:35:53 AM3/29/22
to lavaan
lavaan's model syntax is just a character string/vector, so you can use paste() to assemble it or add quantities that are stored in other objects.

Terrence D. Jorgensen
Assistant Professor, Methods and Statistics
Research Institute for Child Development and Education, the University of Amsterdam

Christian Arnold

unread,
Mar 29, 2022, 1:59:58 PM3/29/22
to lav...@googlegroups.com
As far as I know, you have at least two choices:
(1) Create the lavaan syntax string with any dummy names. For example like this:
predictorObservedSEM ~~ (1 - %%REL%%^2)*predictorObservedSEM
Then replace the dummy with gsub.
(2) Modify the parameter table.

Best

Christian 

From: lav...@googlegroups.com <lav...@googlegroups.com> on behalf of isaactp...@gmail.com <isaactp...@gmail.com>
Sent: Tuesday, March 29, 2022 3:59:49 PM
To: lavaan <lav...@googlegroups.com>
Subject: Change lavaan syntax dynamically and programmatically with R objects
 
--
You received this message because you are subscribed to the Google Groups "lavaan" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lavaan+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lavaan/db17e692-6688-439f-ae7a-0ab55a483e2bn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages