Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a function for converting lavaan parameter tables or model strings to simsem templates #22

Open
mronkko opened this issue Nov 8, 2014 · 3 comments

Comments

@mronkko
Copy link
Contributor

mronkko commented Nov 8, 2014

A user might know lavaan syntax well and prefer that over specifying simsem templates. However, some features are available only when simsem templates are used. Therefore it would be useful to have a function to covert a lavaan model string to simsem template.

Here is a code snippet demonstrating how this could be implemented.

llibrary(simsem)
library(lavaan)


MODEL <- "
A =~ .8*a1 + .7*a2 + .6*a3
a1~~(1-.8^2)*a1
a2~~(1-.7^2)*a2
a3~~(1-.6^2)*a3
A~~1*A
B =~ .8*b1 + .7*b2 + .6*b3
b1~~(1-.8^2)*b1
b2~~(1-.7^2)*b2
b3~~(1-.6^2)*b3
B ~ .3*A
B~~(1-.3^2)*B
"

parTable <- lavaanify(MODEL)
allVars <- unique(c(parTable$rhs, parTable$lhs))
latentVars <- parTable$lhs[parTable$op == "=~"]
observedVars <- setdiff(allVars,latentVars)

cvMat <- diag(length(observedVars))
colnames(cvMat) <- rownames(cvMat) <- observedVars
fit <- lavaan::lavaan(MODEL, sample.cov = cvMat, sample.nobs = 100)

simSemTemplate <- simsem::model.lavaan(fit)

# Generate some data

d1 <- sim(nRep = 1, simSemTemplate, 100, dataOnly = TRUE, sequential = TRUE,
          saveLatentVar = TRUE)

attributes(d1[[1]])

While I was implementing this, I noticed that the code produces the following error message

[1] "The regression matrix is not recursive. Simsem template does not support non-recursive matrix.

There are two issues: First, the model is recursive, so the recursiveness check produces a false positive. Second this could be more informative by giving the the matrix name (beta, I assume) as a part of the error message.

@psunthud
Copy link
Member

My current codes do not provide any problems using your codes. I did not update the github repository for a while. I will make sure you have the latest one soon.

@mronkko
Copy link
Contributor Author

mronkko commented Nov 28, 2014

I will test the error again with the latest code once I have it.

Your comment addresses the request only partly: "Provide a function for converting lavaan parameter tables or model strings to simsem templates"

I would like to have the following function to be part of SimSem:

simsemTemplateFromLavaanModel <- function(model){
   parTable <- lavaanify(model)
   allVars <- unique(c(parTable$rhs, parTable$lhs))
   latentVars <- parTable$lhs[parTable$op == "=~"]
   observedVars <- setdiff(allVars,latentVars)

   cvMat <- diag(length(observedVars))
   colnames(cvMat) <- rownames(cvMat) <- observedVars
   fit <- lavaan::lavaan(MODEL, sample.cov = cvMat, sample.nobs = 100)

   simsem::model.lavaan(fit)
}

@psunthud
Copy link
Member

I will use this code by allowing model.lavaan to take syntax or parameter tables.

@psunthud psunthud reopened this Nov 30, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants