simsem / semTools

Useful tools for structural equation modeling
74 stars 36 forks source link

Error when calling the permuteMeasEq() function #100

Closed sebastianmarinc closed 3 years ago

sebastianmarinc commented 3 years ago

I'm trying to run the following code:

fitmeasures <- c("chisq","df","cfi","tli","aic","bic","rmsea","srmr")
fit_null <- cfa(mod, data = df, std.lv = T, group = "lang")
invar <- measurementInvariance(model = mod, data = df, std.lv = T, group = "lang")
fit_config <- invar[["fit.configural"]]
fit_metric <- invar[["fit.loadings"]]
fit_scalar <- invar[["fit.intercepts"]]

permute_config <- permuteMeasEq(
  nPermute = 20, 
  con = fit_config, 
  AFIs = fitmeasures,
  null = fit_null, 
  #parallelType = "snow", 
  iseed = 3141593
)

However, I get the following error in the console: ...

Error in exists(as.character(call$model)) : first argument has length > 1
Error in exists(as.character(call$model)) : first argument has length > 1
Error in exists(as.character(call$model)) : first argument has length > 1
Error in permuteOnce.mgcfa(con = new("lavaan", version = "0.6.9", call = lavaan::lavaan(model = list( : 
  object 'out0' not found

If you have any insight on the problem, please lmk when you can. Thanks!!

TDJorgensen commented 3 years ago

Damn, another reason to completely gut this function and redesign it to use lavaanList().

The problem is that it expects the model= argument to be the name of an object (in your example, mod). But when you submit it to measurementInvariance(), the models are defined for you as parameter tables that are passed to cfa(). Thus, when permuteMeasEq() looks for the object in fit_config@call, it does not find the name mod, but rather a data.frame resembling what is returned by parTable(fit_config). That is why the help-page example did not work (I just updated it to not use the deprecated measurementInvariance() function). 8ffbf69

For it to work, you just have to fit each of your models by calling cfa() with the first argument as a named object.

sebastianmarinc commented 3 years ago

Thanks for this! That makes sense. Also, whenever I try to parallelize (with snow), I get the following error message Error in get(name, envir = envir) : object 'permuteOnce.mgcfa' not found

TDJorgensen commented 3 years ago

yup, that's the other reason (#95) I need to scrap this and start it from the ground up. I just need to find the time... But the idea is to simply generate all the permuted data sets first (rather than generate and analyze at each step). Then I can pass the list of data sets to lavaanList(), which has the whole parallel situation worked out much better than I could in 2016. This approach has worked out quite well for my revamping of the runMI() functionality.

But if you are using a unix-like OS (Mac or Linux), then the multicore options work just fine because they are capable of forking, which means each node can share a common workspace.

sebastianmarinc commented 2 years ago

Just to confirm, this essentially means to randomly order the grouping variable in the data set. Each time I randomly order the grouping variable, that would be the same number of times I'd "permutate" the data?

TDJorgensen commented 2 years ago

Yes, the grouping variable is what is permuted. The remainder of the data set remains intact each time a random permutation of the grouping variable is drawn via sample().

sebastianmarinc commented 2 years ago

Awesome! And after I fit the models, how do I extract the fit measures to compare to the original data (i.e., "Make an inference about the observed fit measure by comparing it with the vector of permuted fit measures")?

sebastianmarinc commented 2 years ago

As a follow up, I'm not sure how to compare the original fit statistics to all of the permuted ones.

TDJorgensen commented 2 years ago

The steps of the procedure are described on p. 712 of the source article: https://pure.uva.nl/ws/files/32815315/00060744_201812000_00008.pdf