Model input as binary matrix?

41 views
Skip to first unread message

MH Manuel Haqiqatkhah

unread,
Apr 16, 2019, 4:51:28 AM4/16/19
to lavaan
Hi all,

Is it possible to make the model definition as a binary matrix?

For instance, suppose we have this model:

model <- '
          visual =~ a1*x1 + a2*x2 + a3*x3
          verbal =~ a4*x4 + a5*x5
          texual =~ a6*x6 + a7*x7
          verbal ~~ visual
          verbal ~ texual + visual
         
`

And we define it by something like this:

indicators <- c("x1", "x2", "x3", "x4", "x5")
latent.v <- c("visual", "verbal", "texual")

lodings <- 
matrix(c(1, 1, 1, 0, 0, 0, 0,
                    0, 0, 0, 0, 0, 1, 1,
                    0, 0, 0, 0, 0, 1, 1),
                  nrow=2, ncol=7, byrow = TRUE)

regressions <- matrix(c(0, 0, 0,
                        0, 1, 1,
                        0, 0, 0),
                      nrow=3, ncol=3, byrow = TRUE)

model <- model.maker(
indicators , latent.v, loadings, regressions)

This would really help when one wants to do the alternative model comparison, has a lot of manifest/latent variables, or wants to compare it on different datasets in a pipeline, all of which are my problems at the moment!

Cheers,
MH Manuel.

Terrence Jorgensen

unread,
Apr 16, 2019, 5:23:05 AM4/16/19
to lavaan
Is it possible to make the model definition as a binary matrix? 
 
something like this:

indicators <- c("x1", "x2", "x3", "x4", "x5")
latent.v <- c("visual", "verbal", "texual")

lodings <- 
matrix(c(1, 1, 1, 0, 0, 0, 0,
                    0, 0, 0, 0, 0, 1, 1,
                    0, 0, 0, 0, 0, 1, 1),
                  nrow=2, ncol=7, byrow = TRUE)

regressions <- matrix(c(0, 0, 0,
                        0, 1, 1,
                        0, 0, 0),
                      nrow=3, ncol=3, byrow = TRUE)



Not in lavaan directly, but it is trivial to write a lavaan-syntax-writing function that translates your LISREL matrices into lavaan syntax.  Just loop over columns and rows in lambda, pasting the column name, "=~", and row name for any nonzero cell.  Same with rows/columns in beta, but paste "~" between the row and column names of nonzero cells.

You could also specify your matrices in the simsem package, using the analyze() function to analyze a single data set.  See the example on ?simsem::analyze (you might need to specify arbitrary population values, which will be ignored if you do not use them to generate() data).

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


MH Manuel Haqiqatkhah

unread,
Apr 17, 2019, 5:10:58 AM4/17/19
to lavaan
Thanks for your reply.

I am going to write that syntax-maker function myself then.

Also, semsim is indeed a cool package! I'm reading its wiki now, but I do not know yet whether I can parse the model specified in SimMatrix to lavaan.

Regards,
MH Manuel.

Terrence Jorgensen

unread,
Apr 24, 2019, 12:19:07 PM4/24/19
to lavaan
I do not know yet whether I can parse the model specified in SimMatrix to lavaan.

Not anymore.  Back in 2011/2012, simsem used to write lavaan syntax as I suggested you do.  Subsequently, it changed to writing parameter tables (see ?model.syntax and ?parTable), which I think turned out to be a mistake.
Reply all
Reply to author
Forward
0 new messages