Issues Defining SEM Model - a non-responsive RStudio console

151 views
Skip to first unread message

Maejda

unread,
May 12, 2023, 3:45:10 AM5/12/23
to lavaan
Hello everyone,

I'm a newcomer to using R and conducting SEM analysis, and I'm encountering an issue with defining my model in R Studio.

When I attempt to define my model, R Studio doesn't seem to respond to the request. If I try to stop the run, it doesn't halt as expected, and I find myself having to stop R Studio entirely. Interestingly, there are times when it works without a hitch, but when I attempt to run the code a second time, it fails again. Other times, the process is significantly delayed, but it eventually manages to define my model.

In an attempt to troubleshoot, I've added R and R Studio to my Windows Defender Exclusions list (Windows 10), but this has not resolved the issue. I've also checked for package updates and tried different versions of R, to no avail. Unfortunately, the problem persists.

Packages : lavaan, tidyvers, dplyr and semhelpinghands

Any advice or suggestions would be greatly appreciated. I'm eager to learn and resolve this issue. Thank you in advance for your assistance.

Best regards,
Maejda

Shu Fai Cheung (張樹輝)

unread,
May 12, 2023, 4:19:02 AM5/12/23
to lavaan
Dear Maejda,

It is not easy to know what happened without having more information. For example, what steps have you taken, at which step RStudio stopped responding? It is also not clear what you meant by defining the model. I guess you meant fitting the model, e.g., when running sem() or cfa(). Did you request something that can take some time, sometimes a long time, to run? For example, bootstrapping, bootstrapping with missing data handled by FIML, a large data file, or a big model with many parameters. I encountered cases in which lavaan could take over 20 minutes to run (big dataset with missing data and bootstrapping). Therefore, what you encountered "may be" normal.

Did you get any warnings or error messages when it "fails"?

If you asked for something like bootstrapping, then I would suggest you to set the iseed argument such that the resampling process can be reproduced. The randomness of the problem may be due to the random sampling process, if you requested them.

If you do think that it may be related to RStudio, one thing you can try is to check whether it is an R issue or an RStudio issue (sometimes both). I assume that you have the code in a file, as an .R file, an .Rmd file, or a .qmd file. Start the R GUI (not RStudio), which should be an icon with something like "R 4.x.x" in Windows. Copy and paste the code there to run them, preferably chunk by chunk or line by line. Set the working directory ("File -> Change dir") if you script does not set the working directory. Check whether the code can run there. If yes, then it is something about RStudio. If not, then it is something about the code, model, data, packages, etc..

Regards,
Shu Fai

Maejda

unread,
May 17, 2023, 7:12:56 AM5/17/23
to lavaan

Dear Shu Fai,

Thank you for your prompt response and insightful suggestions.

To provide more context, I've appended my code at the end of this message, and I've highlighted the specific part where the execution halts. The issue doesn't appear to be related to missing data or bootstrapping, as those aren't factors in my current project.

Interestingly, I don't receive any warnings or error messages in R Studio when the process "fails". Other programs don't freeze or slow down during this time, and sometimes, I'm still able to access other parts of R Studio just fine, while at other times, I have to interrupt R. I've noticed that the stop icon at the top right of the console is generally illuminated when this occurs.

I've also tried running my code through the standalone R GUI, as you suggested. However, the issue remains the same - the process stalls when I attempt to run the chunk to "#define the model". I've included one of my scripts below for reference, but I don't believe the issue is stemming from my code. I'm importing my data from an Excel file.

I've also attempted to resolve this by trying different versions (and updates) of my packages and R itself, but to no avail.


Your continued assistance would be greatly appreciated.

 

Best regards,

Maejda


library("lavaan")

library("tidyverse")

library("semhelpinghands")

library("dplyr")

 

# Importer le fichier Excel

data_Atti_t2 <- Questionnaire_full

 

# retrieve data file from excel

data_res_mod_Atti2<-select(data_Atti_t2,

                          APP1,

                          APP2,

                          APP3,

                          APP4,

                          APP5,

                          ANP1,

                          ANP2,

                          ANP3,

                          ANP4,

                          ANP5,

                          APS1,

                          APS2,

                          APS3,

                          APS4,

                          APS5,

                          ANS1,

                          ANS2,

                          ANS3,

                          ANS4,

                          ANS5,

                          So2_it1,

                          So2_it2,

                          So2_it3,

                          So2_it4,

                          An2_it1,

                          An2_it2,

                          An2_it3,

                          An2_it4,

                          Ag2_it1,

                          Ag2_it2,

                          Ag2_it3,

                          Ag2_it4,

                          Dis2_it1,

                          Dis2_it2,

                          Dis2_it3,

                          Dis2_it4,

)

 

fix(data_res_mod_Atti2)

 

#Définir le modèle

model_Atti_t2 <-'

# variables latentes

 

# Pour les attitudes

APP =~ APP5 + APP2 + APP3 + APP4 + APP1

ANP =~ ANP3 + ANP1+ ANP4 + ANP5 + ANP2

 

# Pour HRIES 2

Sociability_2=~ So2_it3 + So2_it1 + So2_it2 + So2_it4

Animacy_2 =~ An2_it1 + An2_it2 + An2_it3 + An2_it4

Agency_2=~ Ag2_it3 + Ag2_it1 + Ag2_it2 + Ag2_it4

Disturbance_2 =~ Dis2_it3 + Dis2_it1 + Dis2_it2 + Dis2_it4


#regs

 

Sociability_2~0*ANP

Sociability_2 ~APP

Animacy_2 ~0*ANP

Animacy_2 ~ APP

Agency_2 ~ ANP + APP

Disturbance_2 ~ ANP + APP

 

#cov

ANP ~~ APP

Sociability_2 ~~ Animacy_2

Sociability_2 ~~ Agency_2

Sociability_2 ~~ Disturbance_2

Animacy_2 ~~ Agency_2

Animacy_2 ~~0*Disturbance_2

Agency_2 ~~0*Disturbance_2

'

#resultats

#Pour forcer modèle

fit_model_Atti_t2 <- sem(model_Atti_t2,

                         data = data_res_mod_Atti2,

                         meanstructure=TRUE, optim.method = "BFGS")

 

# Obtenir les indices d'ajustement, y compris le RMSEA

summary(fit_model_Atti_t2, fit.measures=TRUE, standardized=TRUE, rsquare=F)

 

#bootstrap

fit_model_Atti_t2 <- sem(model_Atti_t2,

                         data = data_res_mod_Atti2,

                         se = "bootstrap",

                         bootstrap = 5000,

                         parallel ="snow", ncpus = 8)

 

#results bootstrap (percentile method)

ci_boot1 <- standardizedSolution_boot_ci(fit_model_Atti_t2)

ci_boot1

 

# Afficher un résumé des résultats du modèle

summary(fit_model_Atti_t2)

Shu Fai Cheung (張樹輝)

unread,
May 18, 2023, 12:12:36 AM5/18/23
to lavaan
Dear Maejda,

This is strange. The lines to define the model are just lines to create a string variable.

How about removing this line of comment, given that RStudio/R stop responding there?

#Définir le modèle


-- Shu Fai
Reply all
Reply to author
Forward
0 new messages