how to use categorical random variable as a covariate

2,412 views
Skip to first unread message

Willa Song

unread,
Sep 14, 2018, 12:44:51 AM9/14/18
to lavaan
Hello,
I'm a first-year PhD student stumbling around with data analysis. Could someone please help with my lavaan R code?

Here's my simplified model:

my.model <- '
#random effect?

#covariates
group ~ age + sex 
cg ~ age + sex  + cell + slide
ctq ~ age + sex 

# direct effect
group ~ c*ctq
# mediator
cg ~ a*ctq
group ~ b*cg

# indirect effect (a*b)
ab := a*b
# total effect
total := c + (a*b)
'
fit <- cfa(my.model, data=tbTest, ordered = c("group","slide"))

slide is a categorical random variable I use as a covariate. It's NOT an ordinal variable. But if I don't make it ordered, I got an error "lavaan ERROR: unordered factor(s) with more than 2 levels detected as exogenous covariate(s): slide".
If I make it ordered I receive a warning: "lavaan WARNING: exogenous variable(s) declared as ordered in data: slide"

I have three questions:
1. how to indicate slide as a random variable? (I read the tutorial but didn't understand) 
2. will it affect the result to define the variable as ordered but it's actually not?
3. Did I define my covariates with the right lavaan grammar?

Thanks in advance!

Terrence Jorgensen

unread,
Sep 14, 2018, 11:44:14 AM9/14/18
to lavaan
slide is a categorical random variable I use as a covariate. It's NOT an ordinal variable. But if I don't make it ordered, I got an error "lavaan ERROR: unordered factor(s) with more than 2 levels detected as exogenous covariate(s): slide".

The ordered= argument only applies to endogenous variables.  For exogenous predictors, create dummy codes.


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

Willa Song

unread,
Sep 14, 2018, 5:19:44 PM9/14/18
to lavaan
Thank you Terrence. However the reason of using slide a random factor is to avoid greatly decreasing degree of freedom (it has many levels,and it is random), dummy coding will defeat the purpose.
With glmer I can use (1|slide) to indicate it as a random variable. What can I do with lavaan to do the same?
Thanks,
Willa

Terrence Jorgensen

unread,
Sep 16, 2018, 10:26:35 AM9/16/18
to lavaan
What can I do with lavaan to do the same?

Oh, you want to run a multilevel model in lavaan?  Then use the argument cluster = "slide"

Willa Song

unread,
Sep 20, 2018, 11:25:06 PM9/20/18
to lavaan
Thank you, Terrence, it works! just to clarify: did you mean a mixed model where  "slide" modelled as a random factor, be the same as a multi-level model where clustering by slide?

Thank,
Willa

Terrence Jorgensen

unread,
Sep 24, 2018, 6:00:57 AM9/24/18
to lavaan
 did you mean a mixed model where  "slide" modelled as a random factor, be the same as a multi-level model where clustering by slide?

Absolutely not.  I was only responding to the fact that you were using "slide" as the clustering variable in the glmer() syntax you sent.  Clustering variables are presumed nominal, so no ordinal information would be preserved.  If you intended to use slide as a substantive predictor, but you want to treat it as continuous, you can use it to define a single-indicator construct. 

model <- ' ...
SLIDE =~ 1*slide
slide ~~ 0*slide
SLIDE ~~ NA*SLIDE
...
'

SLIDE then represents the latent item-response (assumed normally distributed) underlying the discrete observed item-response "slide".  Then you can use SLIDE as your predictor.


Reply all
Reply to author
Forward
0 new messages