jslefche / piecewiseSEM

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

multigroup problem #307

Closed ferch811 closed 5 months ago

ferch811 commented 5 months ago

Hi! I always get the same error (Error in [.data.frame(data, , var) : undefined columns selected) with the multigroup function, in SEMs that otherwise work fine with the psem function. The error in multigroup comes up when I include the last model (LC_SAFA.b_lm1) either as linear or linear mixed-effects model, but not if that model is not included in the SEM. Could you help me figure out why this error comes up based on the code below? Could it be related with too few degrees of freedom for multigroup testing, but enough degrees of freedom for psem?

Thanks in advance,

Fernando.

Code datasets: -balg_fa_tax has one observation per site (n=48) -bmi_fa_all has one observation per taxa at each site (n=192)

Single models

ch_PCA1_lm1<-lm(ch_PCA1~Taug, data= balg_fa_tax)
Diat_lm1<-lm(logit_diat ~Taug+ch_PCA1, data= balg_fa_tax)
LC_SAFA_lm1<-lm(LC_SFA~Taug+ch_PCA1+logit_diat, data= balg_fa_tax)
**LC_SAFA.b_lm1<-lme(LC_SFA.y ~ Taug+LC_SFA+logit_diat, random= list(~ 1|site,
                                                      ~ 1|Taxon), data= bmi_fa_all)**

SEM model

terr_pmodel <- psem(
  ch_PCA1_lm1,
  Diat_lm1,
    LC_SAFA_lm1,
  LC_SAFA.b_lm1,
  data=bmi_fa_all)

Multigroup test (terr_pmultigroup <- multigroup(terr_pmodel, group = "system.x"))

Output

> summary(terr_pmodel)
Structural Equation Model of terr_pmodel 

Call:
  ch_PCA1 ~ Taug
  logit_diat ~ Taug + ch_PCA1
  LC_SFA ~ Taug + ch_PCA1 + logit_diat
  LC_SFA.y ~ Taug + LC_SFA

    AIC
 -110.408

---
Tests of directed separation:

               Independ.Claim Test.Type DF Crit.Value P.Value 
     LC_SFA.y ~ ch_PCA1 + ...      coef 40     0.4775  0.6356 
  LC_SFA.y ~ logit_diat + ...      coef 39     1.5896  0.1200 

--
Global goodness-of-fit:

Chi-Squared = NA with P-value = NA and on 2 degrees of freedom
Fisher's C = 5.147 with P-value = 0.273 and on 4 degrees of freedom

---
Coefficients:

    Response  Predictor Estimate Std.Error DF Crit.Value P.Value Std.Estimate    
     ch_PCA1       Taug   4.0642    1.0055 46     4.0419  0.0002       0.5246 ***
  logit_diat       Taug  -0.9214    3.6299 45    -0.2538  0.8008      -0.0390    
  logit_diat    ch_PCA1   1.5666    0.4572 45     3.4262  0.0013       0.5138  **
      LC_SFA       Taug   0.0403    0.3952 44     0.1020  0.9192       0.0182    
      LC_SFA    ch_PCA1   0.1459    0.0559 44     2.6113  0.0123       0.5103   *
      LC_SFA logit_diat  -0.0023    0.0162 44    -0.1396  0.8896      -0.0241    
    LC_SFA.y       Taug   0.1859    0.1302 41     1.4272  0.1611       0.1217    
    LC_SFA.y     LC_SFA  -0.0031    0.0573 41    -0.0540  0.9572      -0.0045    

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

---
Individual R-squared:

    Response method Marginal Conditional
     ch_PCA1   none     0.26          NA
  logit_diat   none     0.25          NA
      LC_SFA   none     0.21          NA
    LC_SFA.y   none     0.01         0.9
Warning message:
Check model convergence: log-likelihood estimates lead to negative Chi-squared! 

> (terr_pmultigroup <- multigroup(terr_pmodel, group = "system.x"))
Error in `[.data.frame`(data, , var) : undefined columns selected
ferch811 commented 5 months ago

It was solved a previous response in another thread using https://github.com/jslefche/piecewiseSEM/issues/284#issuecomment-1977270661