jslefche / piecewiseSEM

Piecewise Structural Equation Modeling in R
156 stars 49 forks source link

Model fits but summary and coefs throw errors #214

Closed leorjorge closed 2 years ago

leorjorge commented 4 years ago

Really excited to find out about this package. Being able to fit a SEM with mixed effects is quite amazing.

However, I'm getting some trouble fitting (an admittedly quite complex) model, and I'm lost on how to figure what could be the issue. I managed to get the psem function to work, running the code below:

SEM.L1 <- psem(
  glmer(A3_Endangered ~ A1_LogN.2000. + A6_PNGbible + (1|Language), data = Ind.cl, 
        family = "binomial", control = glmerControl(optimizer = "bobyqa")),

  lmer(as.numeric(B1_Urbanization) ~ as.numeric(B4_ParentJob) + (1|Language), REML = F, data = Ind.cl),

  lmer(as.numeric(B4_ParentJob) ~ B3_ParentEdu + (1|Language), REML = F, data = Ind.cl),

  lmer(as.numeric(C1_ParentMaxFluency) ~ A1_LogN.2000. + A3_Endangered + (1|Language), REML = F, data = Ind.cl),

  lmer(as.numeric(C2_HomeLang) ~ C3_SameLang + as.numeric(C1_ParentMaxFluency) + B3_ParentEdu + (1|Language), REML = F, data = Ind.cl),

  glmer(C3_SameLang ~ A1_LogN.2000. + A3_Endangered + B3_ParentEdu + (1|Language), data = Ind.cl, 
        family = "binomial", control = glmerControl(optimizer = "bobyqa")),

  lmer(D4_TradSkills ~ B3_ParentEdu + as.numeric(B1_Urbanization) + (1|Language), REML = F, data = Ind.cl),

  lmer(D5_ModSkills ~ B3_ParentEdu + as.numeric(B1_Urbanization) + (1|Language), REML = F, data = Ind.cl), #Leaving the connection from 
  #traditional skills out, just because it's very difficult to decide on a sign for that arrow, and the 
  #other arrows arriving into these probably already account for possible correlations between these?

  lmer(L1_BodyParts ~ A1_LogN.2000. + A3_Endangered + A6_PNGbible + as.numeric(B1_Urbanization) + 
         B3_ParentEdu + as.numeric(C1_ParentMaxFluency) + as.numeric(C2_HomeLang) + C3_SameLang + 
         D4_TradSkills + D5_ModSkills + (1|Language), data = Ind.cl, REML = T),

  # glmer(cbind(L1_BodyParts,24-L1_BodyParts) ~ A1_LogN.2000. + A3_Endangered + A6_PNGbible + 
  #         as.numeric(B1_Urbanization) + B3_ParentEdu + as.numeric(C1_ParentMaxFluency) + as.numeric(C2_HomeLang) + C3_SameLang + 
  #         D4_TradSkills + D5_ModSkills + (1|Language), data = Ind.cl, 
  #       family = "binomial", control = glmerControl(optimizer = "bobyqa")),
  data = Ind.cl
)

(Don't mind the commented out glmer... that was my first try at fitting the effects on the main response variable, but I think the fact it is a set of 24 binary values for each observation messes up even more with the fit and the model doesn't work at all)

When trying to run summary or coefs I get the following (different) errors. I couldn't find info on them (and they are not very clear):

> summary(SEM.L1)
  |                                                                                      |   0%Error 
in list2env(data) : first argument must be a named list
> coefs(SEM.L1)
Error in emmeans::emmeans(model, specs = v, data = data) : 
  No variable named A3_Endangered in the reference grid

However, just printing the object does show the fitted SEMs and data summary, so I have no idea what the issue actually is.

If you need a version of the data to work on, I can provide it privately somehow. Thank you in advance!

jslefche commented 4 years ago

This may because of a new change in the emmeans package. I wont have time to look at it this week but if you can do some digging, that would be appreciated


Jonathan S. Lefcheck, Ph.D. Tennenbaum Coordinating Scientist MarineGEO: https://marinegeo.si.edu/ Smithsonian Institution Phone: +1 (443) 482-2443 www.jonlefcheck.nethttp://www.jonlefcheck.net

From: Leonardo R. Jorgemailto:notifications@github.com Sent: Wednesday, September 16, 2020 11:33 AM To: jslefche/piecewiseSEMmailto:piecewiseSEM@noreply.github.com Cc: Subscribedmailto:subscribed@noreply.github.com Subject: [jslefche/piecewiseSEM] Model fits but summary and coefs throw errors (#214)

External Email - Exercise Caution

Really excited to find out about this package. Being able to fit a SEM with mixed effects is quite amazing.

However, I'm getting some trouble fitting (an admittedly quite complex) model, and I'm lost on how to figure what could be the issue. I managed to get the psem function to work, running the code below:

SEM.L1 <- psem(

glmer(A3_Endangered ~ A1_LogN.2000. + A6_PNGbible + (1|Language), data = Ind.cl,

    family = "binomial", control = glmerControl(optimizer = "bobyqa")),

lmer(as.numeric(B1_Urbanization) ~ as.numeric(B4_ParentJob) + (1|Language), REML = F, data = Ind.cl),

lmer(as.numeric(B4_ParentJob) ~ B3_ParentEdu + (1|Language), REML = F, data = Ind.cl),

lmer(as.numeric(C1_ParentMaxFluency) ~ A1_LogN.2000. + A3_Endangered + (1|Language), REML = F, data = Ind.cl),

lmer(as.numeric(C2_HomeLang) ~ C3_SameLang + as.numeric(C1_ParentMaxFluency) + B3_ParentEdu + (1|Language), REML = F, data = Ind.cl),

glmer(C3_SameLang ~ A1_LogN.2000. + A3_Endangered + B3_ParentEdu + (1|Language), data = Ind.cl,

    family = "binomial", control = glmerControl(optimizer = "bobyqa")),

lmer(D4_TradSkills ~ B3_ParentEdu + as.numeric(B1_Urbanization) + (1|Language), REML = F, data = Ind.cl),

lmer(D5_ModSkills ~ B3_ParentEdu + as.numeric(B1_Urbanization) + (1|Language), REML = F, data = Ind.cl), #Leaving the connection from

traditional skills out, just because it's very difficult to decide on a sign for that arrow, and the

other arrows arriving into these probably already account for possible correlations between these?

lmer(L1_BodyParts ~ A1_LogN.2000. + A3_Endangered + A6_PNGbible + as.numeric(B1_Urbanization) +

     B3_ParentEdu + as.numeric(C1_ParentMaxFluency) + as.numeric(C2_HomeLang) + C3_SameLang +

     D4_TradSkills + D5_ModSkills + (1|Language), data = Ind.cl, REML = T),

glmer(cbind(L1_BodyParts,24-L1_BodyParts) ~ A1_LogN.2000. + A3_Endangered + A6_PNGbible +

as.numeric(B1_Urbanization) + B3_ParentEdu + as.numeric(C1_ParentMaxFluency) + as.numeric(C2_HomeLang) + C3_SameLang +

D4_TradSkills + D5_ModSkills + (1|Language), data = Ind.cl,

family = "binomial", control = glmerControl(optimizer = "bobyqa")),

data = Ind.cl

)

(Don't mind the commented out glmer... that was my first try at fitting the effects on the main response variable, but I think the fact it is a set of 24 binary values for each observation messes up even more with the fit and the model doesn't work at all)

When trying to run summary or coefs I get the following (different) errors. I couldn't find info on them (and they are not very clear):

summary(SEM.L1)

| | 0%Error

in list2env(data) : first argument must be a named list

coefs(SEM.L1)

Error in emmeans::emmeans(model, specs = v, data = data) :

No variable named A3_Endangered in the reference grid

However, just printing the object does show the fitted SEMs and data summary, so I have no idea what the issue actually is.

If you need a version of the data to work on, I can provide it privately somehow. Thank you in advance!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fjslefche%2FpiecewiseSEM%2Fissues%2F214&data=02%7C01%7Clefcheckj%40si.edu%7C03d9f0170c4b4183534408d85a55e7d0%7C989b5e2a14e44efe93b78cdd5fc5d11c%7C0%7C0%7C637358672300126989&sdata=QQ7hFV5c%2F4n5TlJhRBrltLJfqgAjWR2QTOt6fSVCBIA%3D&reserved=0, or unsubscribehttps://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAAR4AV6CR3SUJMRL6MTBNUTSGDLFXANCNFSM4RPAPMFQ&data=02%7C01%7Clefcheckj%40si.edu%7C03d9f0170c4b4183534408d85a55e7d0%7C989b5e2a14e44efe93b78cdd5fc5d11c%7C0%7C0%7C637358672300136986&sdata=1ATwnZPAfNnMxCxIfUMGPpKVZaGLxxZDkljjUhEC22w%3D&reserved=0.

leorjorge commented 4 years ago

Ok... I entered the rabbit hole. For the error when running summary, I made it to this line in unstdCoefs https://github.com/jslefche/piecewiseSEM/blob/9bd0cdaf0d395eaf5cb6e389a1a9ce6d896a0dcd/R/coefs.R#L127 Interestingly, it gives a different error message unrelated to emmeans.

However, when I run coef, I get to the exact same line above, and I get the emmeans-related error.

What is puzzling me (and makes me think the error is not necessarily related to emmeans) is that when I enable debug(emmeans), the emmeans function is entered only when running coef, not summary.

I didn't manage to dig deeper, because the error in unstdCoefs is in a part that is quite hard to parse without knowing the code well.

I appreciate any further insight/direction whenever you get some time to have a look into this!