Differences using for loops versus purrr::map

61 views
Skip to first unread message

Chiel Mues

unread,
Jan 9, 2020, 10:46:42 AM1/9/20
to lavaan
Hello everyone,

I just noticed that when I'm working in a nested dataframe, and use purrr::map to iterate over all the groups in my data, I get warnings about convergence.
However, when I use a for loop to iterate over all the groups, I get no such 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."

and 


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

Is there some issue with my code perhaps? I

purrr

map(.x = nested_dft$data,
   
.f = cfa, model = Model_2)



for loop

for (i in 1:length(levels(data$group))) {
 
  data
<- data%>%
    filter
(group== levels(data$group)[i])
 
  list
[[i]] <- cfa(model = Model_2,
                 
 data = data)

  
}

Terrence Jorgensen

unread,
Jan 17, 2020, 5:07:20 AM1/17/20
to lavaan
If you want to find out what is going on, you can fit your model to one data set at a time to see which one causes the (possible) problem.  Perhaps you have too small a sample, or the model is not empirically identified for some other reason.  You haven't provided any information about your model or data to make educated guesses, so try asking a question about a single data set and provide the syntax for that example to show what is happening.  

Terrence D. Jorgensen
Assistant Professor, Methods and Statistics
Research Institute for Child Development and Education, the University of Amsterdam

Chiel

unread,
Jan 17, 2020, 7:53:28 AM1/17/20
to lav...@googlegroups.com
Hello,

Thanks for the reply. 

I indeed didnt provide much detail, because the model seems to fit fine using the for loop. Nevertheless I did everything manually just now, and I get zero convergence issues or anything like that. It's only when using the interating functions like map and lapply! 

I'm fitting a model on 16 items about emotions/mood. I have 2 latent factors, 8 items on each factor. Sample size per groups differs between 190 and 400.



--
You received this message because you are subscribed to a topic in the Google Groups "lavaan" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/lavaan/RqdJEMvJ47Y/unsubscribe.
To unsubscribe from this group and all its topics, send an email to lavaan+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lavaan/abdd06bb-0101-498f-b0d3-0dc829be00c7%40googlegroups.com.

Jeremy Miles

unread,
Jan 17, 2020, 12:13:48 PM1/17/20
to lav...@googlegroups.com


Your original data is called data.

And then you subset data inside the loop, and call the new subset 'data'.  

That seems to be asking for trouble. :)  

list_res <- lapply(unique(data$group, function(x) {
   lavaan::cfa(model = Model_2, data = data[data$group == x, ])
}






for (i in 1:length(levels(data$group))) {
 
  data
<- data%>%
    filter
(group== levels(data$group)[i])
 
  list
[[i]] <- cfa(model = Model_2,
                 
 data = data)

  
}

--
You received this message because you are subscribed to the Google Groups "lavaan" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lavaan+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lavaan/e15c3cfe-0d85-4db7-9df2-6bf8c2e38610%40googlegroups.com.

Chiel

unread,
Jan 17, 2020, 12:28:44 PM1/17/20
to lav...@googlegroups.com
I abstracted that to make it more readable, that's not what I have in my actual code. 

Your apply code does work (also using purrr:map). Thanks!
B I have no idea why... Or rather, no idea why mine doesn't. I've tried my way using nested dataframes on different datasets and it ran without any problems.
I might be even more confused now. 

You received this message because you are subscribed to a topic in the Google Groups "lavaan" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/lavaan/RqdJEMvJ47Y/unsubscribe.
To unsubscribe from this group and all its topics, send an email to lavaan+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lavaan/CAMtGSxnWsiqs83NB4sZE5X7uHpaPm2ZnocA48-1Tvqn21kJaEA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages