jslefche / piecewiseSEM

Piecewise Structural Equation Modeling in R
155 stars 48 forks source link

Multigroup error message #250

Closed aliciagf closed 2 years ago

aliciagf commented 2 years ago

This is the model I am trying to fit:

# Fit individuals models
model1 <- lme(lnseedM ~ functional_group, random = ~ 1|sps_common/acc_number, data = data)
model2 <- lme(growthD ~ functional_group, random = ~ 1|sps_common/acc_number, data = data)
model3 <- lme(lninitialSize ~ lnseedM + growthD, random = ~ 1|sps_common/acc_number, data = data)
model4 <- lme(lnfinalSize ~ lnseedM + lninitialSize + growthD + functional_group, random = ~ 1|sps_common/acc_number, data = data)

# Fit SEM
SEModel <- psem(model1,
                   model2,
                   model3,
                   model4, data = data)
summary(SEModel, conditional = TRUE)
multigroup(SEModel, group = "dom")

And here is the result:

> summary(SEModel, conditional = TRUE)

Structural Equation Model of SEModel 

Call:
  lnseedM ~ functional_group
  growthD ~ functional_group
  lninitialSize ~ lnseedM + growthD
  lnfinalSize ~ lnseedM + lninitialSize + growthD + functional_group

    AIC      BIC
 62.020   168.191

---
Tests of directed separation:

                          Independ.Claim Test.Type  DF Crit.Value P.Value 
  lninitialSize ~ functional_group + ...     anova   2     3.6024  0.1651 
                 growthD ~ lnseedM + ...      coef 250    -1.6046  0.1098 

Global goodness-of-fit:

  Fisher's C = 8.02 with P-value = 0.091 and on 4 degrees of freedom

---
Coefficients:

       Response                 Predictor Estimate Std.Error  DF Crit.Value P.Value Std.Estimate    
        lnseedM          functional_group        -         -   2     7.8557  0.0197            -   *
        lnseedM   functional_group = Forb   1.2916    0.5755  15     2.2443  0.0403            -   *
        lnseedM functional_group = Cereal   2.9694    0.8633  17     3.4397  0.0031            -  **
        lnseedM functional_group = Legume   3.8878    0.7721  15     5.0351  0.0001            - ***
        growthD          functional_group        -         -   2     6.1666  0.0458            -   *
        growthD functional_group = Legume  36.8571    1.5483  15    23.8042  0.0000            - ***
        growthD   functional_group = Forb  41.1534    1.1541  15    35.6595  0.0000            - ***
        growthD functional_group = Cereal   41.785    1.7316  17    24.1306  0.0000            - ***
  lninitialSize                   lnseedM   0.6212    0.0419 249    14.8187  0.0000       0.9563 ***
  lninitialSize                   growthD  -0.0112     0.006 249    -1.8661  0.0632      -0.0491    
    lnfinalSize                   lnseedM   0.1339    0.0501 248     2.6724  0.0080       0.2581  **
    lnfinalSize             lninitialSize   0.1768    0.0432 248     4.0879  0.0001       0.2215 ***
    lnfinalSize                   growthD   0.0774    0.0051 248    15.0907  0.0000       0.4249 ***
    lnfinalSize          functional_group        -         -   2    17.4631  0.0002            - ***
    lnfinalSize functional_group = Legume   0.8841    0.2729  15     3.2396  0.0055            -  **
    lnfinalSize   functional_group = Forb   1.9488    0.2029  15     9.6049  0.0000            - ***
    lnfinalSize functional_group = Cereal   2.5039    0.2976  17     8.4136  0.0000            - ***

  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05

---
Individual R-squared:

       Response method Marginal Conditional
        lnseedM   none     0.29        0.98
        growthD   none     0.12        0.73
  lninitialSize   none     0.76        0.95
    lnfinalSize   none     0.58        0.90

> multigroup(SEModel, group = "dom")
Error in str2lang(x) : <text>:1:14: unexpected '*'
1: . ~  offset(-*
                 ^

I do not understand why the global SEM model fits but not the multigroup analysis. Does anyone know where the problem might lie? My database is quite complex, but I can provide a reduced example if needed.

Thank you!

seanhardison1 commented 2 years ago

The multigroup function needs to parse model formulas so that it can refit with interactions. I couldn't say for certain without a reproducible example to work with, but my guess is that the function is unable to parse the model formulas.

I've found that the function doesn't like complicated RE structures, so that might be the issue. I've gotten past this by rewriting the parts of the function that do the parsing.

jslefche commented 2 years ago

Closing this as a more robust implementation of multigroup is forthcoming

LauraR88 commented 4 months ago

Hello I am experiencing a similar issue as alicia : I do not understand why the global SEM model fits but not the multigroup analysis. Does anyone know where the problem might lie?

Below is a reproducible example

sem_ex=data.frame( AMFprop=runif(70, min = 0, max = 1), SOIL_TRT=sample(0:1, 70, replace = TRUE), N.mmol.kg=rnorm(70, mean = 2078.708, sd = 296.3191), Tot_tuber.g=rnorm(70, mean = 43, sd = 15), groupAMF=rep(c("a", "b"), 70), data=sem_ex )

pmodel <- psem( lm(Tot_tuber.g ~ N.mmol.kg, data=sem_ex), lm(N.mmol.kg ~ AMFprop+SOIL_TRT,data=sem_ex), lm(AMFprop ~ SOIL_TRT,data=sem_ex), data=sem_ex)

pmultigroup <- multigroup(pmodel, group = "groupAMF") pmultigroup

Structural Equation Model of pmodel

Groups = groupAMF [ global ]


Global goodness-of-fit:

Fisher's C = 0.982 with P-value = 0.912 and on 4 degrees of freedom


Model-wide Interactions:

 Response          Predictor Test.Stat DF P.Value 

Tot_tuber.g N.mmol.kg:groupAMF 173.2 1 0.0688 N.mmol.kg AMFprop:groupAMF 10.1 1 0.4819 N.mmol.kg groupAMF:SOIL_TRT 10.1 1 0.8229 AMFprop SOIL_TRT:groupAMF 0.0 1 0.1967

N.mmol.kg -> Tot_tuber.g constrained to the global model AMFprop -> N.mmol.kg constrained to the global model SOIL_TRT -> N.mmol.kg constrained to the global model SOIL_TRT -> AMFprop constrained to the global model


Global coefficients:

 Response Predictor Estimate Std.Error  DF Crit.Value P.Value Std.Estimate 

Tot_tuber.g N.mmol.kg 0.0042 0.0047 138 0.9052 0.3669 0.0768 N.mmol.kg AMFprop 0.1147 70.5402 137 0.0016 0.9987 0.0001 N.mmol.kg SOIL_TRT -4.2492 46.1936 137 -0.0920 0.9268 -0.0079 AMFprop SOIL_TRT -0.0443 0.0556 138 -0.7973 0.4267 -0.0677

Signif. codes: 0 '' 0.001 '' 0.01 '' 0.05 c = constrained