Error messages using lavaan

Skip to first unread message

Jill Rabinowitz

Oct 28, 2018, 12:40:50 PM10/28/18
to lavaan
HI there,

I am new to R so please bare with me. I am running a mediation model controlling for gender and I am running into some R messages. I have attached my original R code below and error messages that I received above.

Error in lav_data_full(data = data, group = group, cluster = cluster,  : 
  lavaan ERROR: unordered factor(s) detected; make them numeric or ordered: EducAge20Recoded gender

Based on my reading of R forums, I added this text, although I don't know if this is correct because EducAge20Recoded and gender are categorical variables, not ordinal.

subsetcoh3nNM[,c("gender","EducAge20Recoded","ncred39f","prs_ea2_p05_2018_r")] <-

 lapply(subsetcoh3nNM[,c("gender","EducAge20Recoded","ncred39f","prs_ea2_p05_2018_r")], ordered)

After adding this text, I get this error message:

Error in lav_options_set(opt) : 

  lavaan ERROR: use (D)WLS estimator for bootstrap

Based on my reading of R forums, I added this text:

fsem1 <- sem(mediationCONCOV, data=subsetcoh3nNM, se="bootstrap", test="scaled.shifted", 

           estimator="DWLS", verbose=TRUE) 

I get the following R error message:
Error in eigen(VarCov, symmetric = TRUE, only.values = TRUE) : 
  infinite or missing values in 'x'

Can someone please help me? 


data <-read.csv(file.choose("c1-3EAall_10.20.18.2"), header=TRUE)


data$gender =as.factor(data$gender)

data$intyorn =as.factor(data$intyorn)

data$EducAge20Recoded =as.factor(data$EducAge20Recoded)

data$cohort =as.numeric(data$cohort)


subsetcoh3 <- subset(data, cohort > 2)


subsetcoh3n <- subsetcoh3[c(2,5,10,21,37,38,52)]


subsetcoh3n[subsetcoh3n ==999] <-NA


subsetcoh3nNM <- na.omit(subsetcoh3n)



prs_ea2_p05_2018_r <- rnorm(400)

ncred39f <- 0.5*prs_ea2_p05_2018_r + rnorm(400)

EducAge20Recoded <- 0.7*ncred39f + rnorm(400)

meddata <- data.frame(EducAge20Recoded, ncred39f, prs_ea2_p05_2018_r)


mediationCONCOV <- 'EducAge20Recoded ~ b1 *ncred39f + c1 * prs_ea2_p05_2018_r + c3 * gender

ncred39f ~ a1 * prs_ea2_p05_2018_r + a5 * gender

indirect := a1 * b1

total1 := c1 + (a1 * b1)

total3 := c3 + (a5 * b1)'


fsem1 <- sem(mediationCONCOV, data = subsetcoh3nNM, se = "bootstrap", bootstrap = 1000) 

Terrence Jorgensen

Nov 2, 2018, 7:49:37 AM11/2/18
to lavaan
I am running a mediation model controlling for gender

Exogenous categorical variables should not be factors. They should be represented with numerical codes (e.g., dummy codes) and those codes should be used as predictors in the model.

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

Reply all
Reply to author
0 new messages