Unable to run SEM because of zero variance categorical dummy variables

86 views
Skip to first unread message

Kelly Lam

unread,
Mar 3, 2020, 7:50:46 AM3/3/20
to lavaan
I'm trying to use the 9th European Social Survey (ESS) to run a MIMIC model. I'm having issues using several exogenous categorical variables. The error I receive is: lavaan ERROR: some variables have no values (only missings) or no variance. The variables that I'm having trouble with are educational attainment and marriage status. They're both nominal variables. Is there a better way to recode these exogenous categorical (nominal) variables in my model?

Here's my code:

ess <- subset(mydata,cntry=="BE"|cntry=="NL"|cntry=="AT"|cntry=="FI")
ess
[,] <- lapply(ess[,], ordered)

ess$cntryNL
<- as.numeric(cntry=="NL")
ess$cntryAT
<- as.numeric(cntry=="AT")
ess$cntryFI
<- as.numeric(cntry=="FI")
ess$hincfel2
<-as.numeric(hincfel)
ess$health2
<-as.numeric(health)
ess$stfdem2
<-as.numeric(stfdem)
ess$rlgdgr2
<-as.numeric(rlgdgr)

ess$female
<-ifelse(gndr == "Female", 1, 0)
ess$agea
<-as.numeric(agea)
ess$yeskids
<-ifelse(bthcld == "Yes", 1, 0)

#dummies for marriage status - maritalb variable, reference = "None of these (NEVER married or in legally registered civil union) "
ess$married
<-as.numeric(maritalb=="Legally married")
ess$civunion
<-as.numeric(maritalb=="In a legally registered civil union")
ess$separated
<-as.numeric(maritalb=="Legally separated")
ess$divorced
<-as.numeric(maritalb=="Legally divorced/Civil union dissolved")
ess$widowed
<-as.numeric(maritalb=="Widowed/Civil partner died")
ess$NAmarsts
<-as.numeric(maritalb=="NA")

#dummies for eisced variable, reference = "ES-ISCED I , less than lower secondary "
ess$eisced2
<-as.numeric(eisced=="ES-ISCED II, lower secondary")
ess$eisced3b
<-as.numeric(eisced=="ES-ISCED IIIb, lower tier upper secondary")
ess$eisced3a
<-as.numeric(eisced=="ES-ISCED IIIa, upper tier upper secondary")
ess$eisced4
<-as.numeric(eisced=="ES-ISCED IV, advanced vocational, sub-degree")
ess$eisced51
<-as.numeric(eisced=="ES-ISCED V1, lower tertiary education, BA level")
ess$eisced52
<-as.numeric(eisced=="ES-ISCED V2, higher tertiary education, >= MA level")
ess$eiscedother
<-as.numeric(eisced=="Other")

model1
<- "
#Measurement part of model
comtrust =~ NA*ppltrst + pplfair + pplhlp


#Structural part of model
happy ~ hincfel2 + comtrust + health2 + stfdem2 + rlgdgr2 +
        female + agea + yeskids + married + civunion + separated + divorced +
        eisced2 + eisced3b + eisced3a + eisced4 + eisced51 + eisced52 + eiscedother +
        cntryNL + cntryAT + cntryFI
comtrust~~1*comtrust
"


m1fit
<-sem(model=model1, data=ess)
summary
(m1fit,fit.measures=TRUE,standardized=TRUE, modindices=TRUE)

#Checking variance
a
<-unclass(ess$eisced)
a
<-as.numeric(a)
var(a)
#[1] NA
var(as.numeric(ess$eisced))
#[1] NA
var(married)
#[1] NA
var(cntryNL)
#[1] 0.1701832


I did the same for country and it worked fine, so I'm not sure why it's giving this error for these variables...


Terrence Jorgensen

unread,
Mar 21, 2020, 10:16:19 AM3/21/20
to lavaan
Is there a better way to recode these exogenous categorical (nominal) variables in my model?

Dummy coding is the correct way (other than multigroup modeling), but it looks like you have some missing values on the exogenous variables.  Try setting missing="fiml.x" to preserve as many observations as possible.


#Checking variance
a
<-unclass(ess$eisced)
a
<-as.numeric(a)
var(a)
#[1] NA
var(as.numeric(ess$eisced))
#[1] NA
var(married)
#[1] NA
var(cntryNL)
#[1] 0.1701832

Set na.rm=TRUE to verify there is variance among non-missing values.

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