Creating lavaan syntax from a parameter table?

137 views
Skip to first unread message

Shu Fai Cheung

unread,
Nov 6, 2021, 10:17:02 PM11/6/21
to lavaan
Just curious. Is there a function that can create a model specification in lavaan syntax from the output of parameterTable()?

We can always figure out the model specification from the parameter table and the results of summary(), and I know parameter tables can be used directly to specify a model if we want to fit a model. I just wonder whether a parameter table can be converted back to the model syntax for the ease of readability.

-- Shu Fai

Terrence Jorgensen

unread,
Nov 8, 2021, 7:37:24 AM11/8/21
to lavaan
Is there a function that can create a model specification in lavaan syntax from the output of parameterTable()?

Not that I know of.
 
I just wonder whether a parameter table can be converted back to the model syntax for the ease of readability.

Certainly, with some clever, rather easy use of paste() along with ifelse() to check for a user-defined $label or fixed value per parameter. 
You should know that you can specify each parameter on its own line (without using the + operator like so many examples), and that lavaan accepts a character vector that it collapses into a single string, so you would not have to paste(..., collapse="") it yourself.  You could certainly collect all righthand-side variables that share a lefthand side and operator, but if your goal is readability, it wouldn't always be prudent in models with many variables.
 
Terrence D. Jorgensen
Assistant Professor, Methods and Statistics
Research Institute for Child Development and Education, the University of Amsterdam

Shu Fai Cheung

unread,
Nov 8, 2021, 8:35:14 PM11/8/21
to lavaan
Thanks. I thought about that idea because it may be a useful tool for teaching and writing R markdown files. The lavaan syntax is more than a syntax. It is now a "language" to communicate a model, at least between my friends and me.

The update() method can be used to modify a model. There are other functions that will generate models. However, if I want to describe the updated/generated model in lavaan syntax/language in the same R markdown file, then I need to manually write the syntax in the same document. It would be great to have a function to read the parameter table and then generate the corresponding lavaan syntax, to reduce the chance for human error.

Maybe I will write this function myself .... (if I have time).

-- Shu Fai

Shu Fai Cheung (張樹輝)

unread,
Sep 25, 2023, 10:01:27 AM9/25/23
to lavaan
This thread (I started) is nearly two-year old ... and I finally wrote a function, ptable_to_syntax(), to do this in the package semhelpinghands: converting a parameter table back to a model syntax (not because I "have time" ... but because I need to do this in another project):


As Terrence showed, it is not difficult if we just need to have a long list of lhs-op-rhs strings. I wrote this function to make the syntax more readable, hopefully, as this is what I need in that project.

As a safeguard, I also wrote a function to compare two parameter tables, to check whether the syntax can produce the same parameter table.

I shared it here, in case there are other lavaan users who would like to do this (very) unusual thing.

It is only in the GitHub version of the package for now as I won't submit a new version to CRAN until probably late October.

There are models that the function does not yet support, due to the complexity that is difficult to foresee. But what it supports now is enough for me.

I tried to be conservative and it should refuse to work on models I know it does not support. Nevertheless, if you encountered any bugs, I would love to know. Thanks.

-- Shu Fai

Shu Fai Cheung (張樹輝)

unread,
Sep 25, 2023, 10:23:43 AM9/25/23
to lavaan
Oh ... just found a "bug", though in the help file, not in the function. The description of an argument is wrong.

The function requires a lavaan-object, not a parameter table. The table is extracted from the lavaan-object. I did that because I make use of some lavaan functions, to minimize the need to write new functions. This is OK for my need as the parameter table is not created manually but from a fitted model created by modifying an existing model.

I will make it works directly on a parameter table soon.

-- Shu Fai
Reply all
Reply to author
Forward
0 new messages