cjvanlissa / tidySEM

55 stars 7 forks source link

Update mx_mixture.R: fix Error in FUN(X[[i]], ...) : object 'isfac' not found #72

Closed Gootjes closed 1 year ago

Gootjes commented 1 year ago

A line of code was missing in the function mixture_starts which leads to: Error in FUN(X[[i]], ...) : object 'isfac' not found

The error occurs when using the splits argument in a call to mx_mixture or mx_growth_mixture.

The line of code that needs to be added can look like:

isfac <- sapply(data_split, inherits, what = "factor")

The line can also be inserted at line 540 (outside the loop) since data columns are constant.

A sidenote, per the documentation of mixture_starts, it is possible to create a model and in step 2 set the start values. However, the initial model creation via mx_mixture also tries to set the start values. If this fails, because of missing data or other reasons, there seems to be no way of setting the starting values manually with the splits argument to mx_mixture.

Gootjes commented 1 year ago

Maybe also a comment more in general: what is nclasses is a range (2:6), how should the splits argument be dealt with? Should it be a matrix with the same width as length(nclasses)?

cjvanlissa commented 1 year ago

Dear @Gootjes , thank you for pointing this out! I solved it in a slightly different way, by moving the isfac line outside of the conditional, as it's used regardless of whether the user provides splits or not. That's why I closed the PR!

Maybe also a comment more in general: what is nclasses is a range (2:6), how should the splits argument be dealt with? Should it be a matrix with the same width as length(nclasses)?

By design, mixture_starts() accepts only one model, not a list of models. Only the wrapper functions deal with mixture models in batch. Mixture_starts is an internal function that is exposed so users can troubleshoot/override defaults.

I don't love the idea of making matrices of splits; if a user is comfortable binding things into matrices, they're probably also comfortable using lapply() across their model list instead. But for users who are not comfortable with such operations, I think it would be confusing if the function accepts different objects: vectors and matrices.

A sidenote, per the documentation of mixture_starts, it is possible to create a model and in step 2 set the start values. However, the initial model creation via mx_mixture also tries to set the start values. If this fails, because of missing data or other reasons, there seems to be no way of setting the starting values manually with the splits argument to mx_mixture.

Can you give an example of where setting starting values fails? It should work with missing data. Have you tried setting run = FALSE?