Differences in for loop and purrr::map

Hello everyone,

I'm doing some analyses using lavaan. I have a dataset that has a large amount of groups. I wanted to investigate fitting a cfa model to each group individually (yes I'm aware of multigroup CFA).

I wrote this code:

mydata %>%
group_by(country) %>%
nest() %>%
mutate(cfa = map(.x = data, .f = cfa, model = Model_4))

This returns some warnings. Which didnt strike me out of the ordinary. However, when I then tried a for loop:

cfa_list <- list()

for (i in 1:length(levels(data$country))) {
  data <- data%>% 
    filter(country == levels(data$country)[i])
  cfa_list[[i]] <- cfa(model = Model_4,
                             data  = data)

Using the loop, I get no warnings from Lavaan. Is this somehow not equivalent? I tried reproducing it but couldn't. Perhaps with a dataset that also contains a larger amount of groups (10+)?

It's really hard to say without knowing what the warnings were. (Ideally you'd be able to reproduce and make a reprex, but the warnings would help at a minimum. :slightly_smiling_face:)

1 Like

These are some the 2 warnings:

The warnings in question:

"lavaan WARNING: the optimizer (NLMINB) claimed the model converged,
but not all elements of the gradient are (near) zero;
the optimizer may not have found a local solution
use check.gradient = FALSE to skip this check."


"In lav_model_estimate(lavmodel = lavmodel, lavpartable = lavpartable, ... :
lavaan WARNING: the optimizer warns that a solution has NOT been found!"

I tried replicating the issue. I got some open data from the European Social Survey, but the issue isn't present there. It seems specific to the data I'm working on. It's kind of bizarre!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.