yrosseel / lavaan

an R package for structural equation modeling and more
http://lavaan.org
412 stars 99 forks source link

ERROR: parameter is not defined #267

Closed tk27182 closed 1 year ago

tk27182 commented 1 year ago

I'm trying to run a lavaan model on my dataset using a predetermined edge list, where directed edges are modeled as "A ~ B" and bidirected edges are modeled with covariance "A ~~ B". When I pass the model string to lavaan, I get the error

library(lavaan)

mdl <- lavaan(mdstr, data= Data)

Error in lav_model(lavpartable = lavpartable, lavpta = lavpta, lavoptions = lavoptions,  : 
  lavaan ERROR: parameter is not defined: A ~~ B

But both A and B are defined clearly in the string and dataset (spelling is correct and I tried renaming them to A and B). What does this "parameter is not defined" error mean?

TDJorgensen commented 1 year ago

Strange, I have never seen that error. Do you have the latest lavaan version installed?

Can you show us this output?

str(Data)

Sometimes a matrix is added to a data.frame, which is meant to be a list of vectors, rather than one column/vector at a time. So the names don't behave as you might expect. You should be able to compare your data and model names: this way:

lavNames(lavaanify(mdstr))
names(Data)

Or use intersect() to see what's in common or setdiff() tones what differs.

yrosseel commented 1 year ago

Can you show us the syntax?

This error means that a parameter does not exist in the LISREL parameterization. I am curious to see which one it is.

As an alternative, you can try using the parameterization = "RAM" argument. The RAM parameterization allows for all sorts of crazy things, like residual variances between an indicator, and the factor.

tk27182 commented 1 year ago

Thanks for your help and sorry for the delayed response.

I'm using version 0.6-14.

I can't quite show the output of str(Data) but it shows it as a data.frame with 404 observations and 25 variables. For the variables A and B, as well as 3 others, it shows them as int types while all the other variables are num types. Then it shows the values for each.

Looking at setdiff(names(Data), lavNames(lavaanify(mdstr))), it looks like there's one variable "Group" that is missing from mdstr but is in names(Data). Dropping that variable from Data doesn't fix the issue.

The parameterization = "RAM" argument in lavaan? My versions says I can only use delta or theta.

TDJorgensen commented 1 year ago

I can't quite show the output

What about the model syntax?

tk27182 commented 1 year ago

The model syntax is

"PA_M24~PA_M18\nWS_M18~WS_M12\nPA_M18~PA_M12\nQL_M06~QL_B\nPA_M06~DU_BG\nWS_M24~WS_M18\nQL_M18~QL_M12\nPA_M12~PA_B\nPA_M12~PA_M06\nQL_M18~QL_B\nQL_M24~QL_M18\nWS_M06~WS_B\nWS_M12~WS_B\nco_z_M24~co_z_M12\nQL_M12~QL_M06\nWS_M12~WS_M06\nWS_M24~WS_M06\nQL_M12~QL_B\nQL_B~~WS_B\nPA_M24~~QL_M24\nQL_M24~QL_M06\nPA_M24~PA_M06\nQL_M12~~WS_M12\nQL_M06~co_z_B\nco_z_M12~co_z_B\nQL_M18~~WS_M18\nWS_M18~WS_M06\nPA_M06~PA_B\nQL_M06~~WS_M06\nQL_B~~co_z_B\nQL_M24~QL_M12\nLF_BG~~DU_BG\nAL_BG~~DU_BG\nQL_M24~~WS_M24\nPA_M18~~QL_M18\nQL_M18~QL_M06\nAG_BG~~DU_BG\nPA_M12~~QL_M12\nQL_M24~QL_B\nPA_B~~QL_B\nPA_M24~PA_M12\nQL_M12~~co_z_M12\nA~~B\nPA_M06~~QL_M06\nco_z_B~AL_BG\nAL_BG~~A\nLF_BG~~PA_B\nAG_BG~~WS_B\n"

More cleanly, it's:

"PA_M24~PA_M18
WS_M18~WS_M12
PA_M18~PA_M12
QL_M06~QL_B
PA_M06~DU_BG
WS_M24~WS_M18
QL_M18~QL_M12
PA_M12~PA_B
PA_M12~PA_M06
QL_M18~QL_B
QL_M24~QL_M18
WS_M06~WS_B
WS_M12~WS_B
co_z_M24~co_z_M12
QL_M12~QL_M06
WS_M12~WS_M06
WS_M24~WS_M06
QL_M12~QL_B
QL_B~~WS_B
PA_M24~~QL_M24
QL_M24~QL_M06
PA_M24~PA_M06
QL_M12~~WS_M12
QL_M06~co_z_B
co_z_M12~co_z_B
QL_M18~~WS_M18
WS_M18~WS_M06
PA_M06~PA_B
QL_M06~~WS_M06
QL_B~~co_z_B
QL_M24~QL_M12
LF_BG~~DU_BG
AL_BG~~DU_BG
QL_M24~~WS_M24
PA_M18~~QL_M18
QL_M18~QL_M06
AG_BG~~DU_BG
PA_M12~~QL_M12
QL_M24~QL_B
PA_B~~QL_B
PA_M24~PA_M12
QL_M12~~co_z_M12
A~~B
PA_M06~~QL_M06
co_z_B~AL_BG
AL_BG~~A
LF_BG~~PA_B
AG_BG~~WS_B
"
TDJorgensen commented 1 year ago

By assigning that string to an object foo and running lavaanify(foo), there appear to be 24 variables in your model. It is not easy to interpret the long syntax; could you provide a path diagram? Running lavNames(lavaanify(foo), "ov.orphan") suggests there are many isolated variables in the model, so perhaps there is something odd about your specification.

yrosseel commented 1 year ago

as.data.frame(lavaan:::lav_lisrel(lavaanify(model)))

reveals that the problem is with the A ~~ B statement. But 'A' nor 'B' appear in any regression. So lavaan does not know how to assign them. As Terrence said, they are so-called orphan variables, and should be removed from the syntax.