Hierarchical SEM using categorical variables and random effects

295 views
Skip to first unread message

frev...@gmail.com

unread,
Apr 30, 2018, 5:07:40 PM4/30/18
to lavaan
Hello,

I am trying to set up a hierarchical SEM using multiple factors that are dependent variables and also include a random effect. I think my current issue comes down to needing to use categorical variables that can't be ordered, and how to also incorporate a random effect. Does anyone have any tips or guidance for how to proceed? 

Below is a description of my model and my code:
Description: I am interested in looking at how anthropocentric land use surrounding a site may influence the site's nutrient patterns, which in return may influence plant traits. I measured individual plant traits at several sites, but also measured site-level characteristics (see below). Because several plants were measured at a single site, site is repeated and should be used as a random effect.

My variables are: 
  • natural (factor for either low or high natural land use surrounding the site)
  • impervious (factor for either low or high amounts of impervious surface surrounding the site)
  • cv.precipitation (numeric variable for variation in precipitation at the site level)
  • n_i (factor for whether the surveyed species was native or invasive)
  • Multiple numeric nutrient measurements
  • Multiple numeric trait measures
Model: 
forb.model<-'
     landuse=~natural+impervious+cv.precipitation
     nutrients=~avg.po4+avg.n+po4.weekly.max+n.weekly.max+n_i
     traits=~avg.height+avg.culms+avg.chloro+avg.sla
     landuse~nutrients
     nutrients~traits
'

forb.fit<-growth(cv.forb.model,data=forb, ordered=c("natural","impervious"))
summary(forb.fit, fit.measures=TRUE, standardized = TRUE)

When running the above code, I get the following error:
Warning messages:
1: In muthen1984(Data = X[[g]], ov.names = ov.names[[g]], ov.types = ov.types,  :
  lavaan WARNING: trouble constructing W matrix; used generalized inverse for A11 submatrix
2: In lav_samplestats_from_data(lavdata = lavdata, missing = lavoptions$missing,  :
  lavaan WARNING: 1 bivariate tables have empty cells; to see them, use:
                  lavInspect(fit, "zero.cell.tables")
3: In lavaan(cv.forb.model, data = forb, model.type = "growth", ordered = c("nat",  :
  lavaan WARNING: model has NOT converged!


Any tips and help would be extremely appreciated! I am new to R and really struggling my way through this code. I'd also be happy to provide any addition details and clarify anything.

Thanks

Terrence Jorgensen

unread,
May 4, 2018, 6:33:43 AM5/4/18
to lavaan
I am trying to set up a hierarchical SEM

What is that?  You syntax does not specify a higher-order common factor.

a random effect.

If by hierarchical, you mean multilevel, then lavaan's limited multilevel capabilities cannot yet handle categorical data.


I think my current issue comes down to needing to use categorical variables that can't be ordered

You mean they are nominal variables?  If they are binary, you can consider them as ordinal because the distinction between an ordered vs. unordered pair of categories is only theoretical, but statistically equivalent (that is why we can use dummy codes as predictors in regression models, because a 1-unit "increase" merely means changing from one category to another).  lavaan does not support multicategory nominal outcomes.  If they are predictors, you need to make your own dummy codes to represent them.


2: In lav_samplestats_from_data(lavdata = lavdata, missing = lavoptions$missing,  :
  lavaan WARNING: 1 bivariate tables have empty cells; to see them, use:
                  lavInspect(fit, "zero.cell.tables")

Any tips and help would be extremely appreciated!

I would follow that advice and look at the tables to see if there is a "separation" issue.  For example:


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

Hayley Gains

unread,
May 24, 2020, 1:43:35 PM5/24/20
to lavaan
Hello,

Did you find out how to add a random effect to your model? 

I am running a 2 factor CFA on a set of questionnaire responses about student teacher relationships. Each teacher completed two questionnaires (one reflecting a positive relationship and another reflection a more strained relationship). So I am hoping to account for the correlation between their two responses by adding the teacher ID as a random effect. Would really appreciate you sharing any info or resources you found!

Also I would agree that lavaan can treat the variable as ordered with your ordered=c(). 

Thanks,

Hayley 

Hayley Gains

unread,
May 24, 2020, 1:46:42 PM5/24/20
to lavaan
Ah, I have actually just found the answer on the lavaan website: 

"If the data is clustered, one way to handle the clustering is to use a multilevel modeling approach. In the SEM framework, this leads to multilevel SEM. The multilevel capabilities of lavaan are still limited, but you can fit a two-level SEM with random intercepts (note: only when all data is continuous and complete; listwise deletion is currently used for cases with missing values)."

Terrence Jorgensen

unread,
May 24, 2020, 4:38:44 PM5/24/20
to lavaan
I am running a 2 factor CFA on a set of questionnaire responses about student teacher relationships. Each teacher completed two questionnaires (one reflecting a positive relationship and another reflection a more strained relationship). So I am hoping to account for the correlation between their two responses by adding the teacher ID as a random effect. 

If you only have 2 observations per teacher, I don't think you need MLSEM.  You can simply treat these 2 (sets of) measurements per teacher as repeated measures, and use the methods developed for longitudinal or dyadic data.  Store the data in wide format (positive and strained responses stored as separate variables, not separate rows), and have them each load on separate positive and strained factors.  Then the correlations you are looking for are estimated model parameter (the factor correlation, if you set std.lv=TRUE, as well as the item-specific residual/partial correlations).

Hayley Gains

unread,
May 26, 2020, 3:45:32 PM5/26/20
to lavaan
Hi Terrence,

Thanks for your response. If I save these as separate variables and load them all onto the two factors, will the model not be ran as though it is a 30 item questionnaire rather than a 15 item questionnaire? 

Terrence Jorgensen

unread,
May 27, 2020, 10:23:15 AM5/27/20
to lavaan
If I save these as separate variables and load them all onto the two factors, will the model not be ran as though it is a 30 item questionnaire rather than a 15 item questionnaire? 

You load the 15 items from one context on one factor, and the same 15 items from another context load on a different factor.  Again, you can read up on longitudinal CFA / invariance for clarification on the methods. 

Terrence D. Jorgensen
Assistant Professor, Methods and Statistics
Research Institute for Child Development and Education, the University of Amsterdam
Reply all
Reply to author
Forward
0 new messages