Error messages using lavaan

1,621 views
Skip to first unread message

Jill Rabinowitz

unread,
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? 



ORIGINAL R CODE:

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)

 

set.seed(1234)

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

unread,
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

zure...@googlemail.com

unread,
Jan 12, 2023, 7:19:03 AM1/12/23
to lavaan
I've got a similar issue. I was running a latent mediation model with categorical indicators for some of the latent variables. The model worked fine without bootstrapping and with wlsmv estimation. Now we turned to DWLS estimation for being able to get bootstrapped se for the indirect effects. DWLS alone also worked fine but adding the se = "bootstrap" command resulted in the following error message: "eigen(VarCov, symmetric = TRUE, only.values = TRUE): infinite or missing values in 'x'"
Do you have any ideas how to deal with this issue?

Yves Rosseel

unread,
Jan 12, 2023, 12:55:17 PM1/12/23
to lav...@googlegroups.com
Could you show us the full syntax, and the call to sem()?

Yves.

Laura Loy

unread,
Jan 30, 2023, 4:38:45 AM1/30/23
to lavaan
Thank you very much for immediately offering your help and please excuse our late response! This is our syntax:

gi.sd.oev.medi = "
# measurement model GI
gi.sd.t1 =~ GI_01.x + lgi2*GI_02.x + lgi3*GI_03.x + lgi4*GI_04.x + lgi5*GI_05.x
gi.sd.t2 =~ GI_01.y + lgi2*GI_02.y + lgi3*GI_03.y + lgi4*GI_04.y + lgi5*GI_05.y

GI_01.x + GI_01.y ~ 0*1
GI_02.x + GI_02.y ~ agi2*1
GI_03.x + GI_03.y ~ agi3*1
GI_04.x + GI_04.y ~ agi4*1
GI_05.x + GI_05.y ~ agi5*1
gi.sd.t1 ~ 1

IS.gi.02 =~ GI_02.x + 1*GI_02.y
IS.gi.03 =~ GI_03.x + 1*GI_03.y
IS.gi.04 =~ GI_04.x + 1*GI_04.y
IS.gi.05 =~ GI_05.x + 1*GI_05.y

IS.gi.02 + IS.gi.03 + IS.gi.04 + IS.gi.05 ~~ 0*gi.sd.t1 + 0*gi.sd.t2

# Change score modeling
diff.sd  =~ 1*gi.sd.t2
IS.gi.02 + IS.gi.03 + IS.gi.04 + IS.gi.05 ~~ 0*diff.sd
diff.sd ~ 1
gi.sd.t2  ~ 1*gi.sd.t1
gi.sd.t2 ~~ 0*gi.sd.t2

# measurement model OEV
OEV.t1 =~ OEV_01.d.x + aoev2*OEV_02.d.x + aoev3*OEV_03.d.x + aoev4*OEV_04.d.x + aoev5*OEV_05.d.x  
          + aoev6*OEV_06.d.x + aoev7*OEV_07.d.x + aoev8*OEV_08.d.x + aoev9*OEV_09.d.x + aoev10*OEV_10.d.x
          + aoev12*OEV_12.d.x + aoev13*OEV_13.d.x + aoev14*OEV_14.d.x + aoev15*OEV_15.d.x
          + aoev16*OEV_16.d.x + aoev17*OEV_17.d.x + aoev18*OEV_18.d.x + aoev20*OEV_20.d.x
          + aoev23*OEV_23.d.x + aoev24*OEV_24.d.x + aoev25*OEV_25.d.x
          + aoev26*OEV_26.d.x + aoev27*OEV_27.d.x + aoev28*OEV_28.d.x + aoev29*OEV_29.d.x + aoev30*OEV_30.d.x
OEV.t2 =~ OEV_01.d.y + aoev2*OEV_02.d.y + aoev3*OEV_03.d.y + aoev4*OEV_04.d.y + aoev5*OEV_05.d.y  
          + aoev6*OEV_06.d.y + aoev7*OEV_07.d.y + aoev8*OEV_08.d.y + aoev9*OEV_09.d.y + aoev10*OEV_10.d.y
          + aoev12*OEV_12.d.y + aoev13*OEV_13.d.y + aoev14*OEV_14.d.y + aoev15*OEV_15.d.y
          + aoev16*OEV_16.d.y + aoev17*OEV_17.d.y + aoev18*OEV_18.d.y + aoev20*OEV_20.d.y
          + aoev23*OEV_23.d.y + aoev24*OEV_24.d.y + aoev25*OEV_25.d.y
          + aoev26*OEV_26.d.y + aoev27*OEV_27.d.y + aoev28*OEV_28.d.y + aoev29*OEV_29.d.y + aoev30*OEV_30.d.y
# mean structure
OEV.t1 ~ 1

# thresholds (t1 in binary case)
OEV_01.d.x | 0*t1; OEV_01.d.y | 0*t1
OEV_02.d.x | d*t1; OEV_02.d.y | d*t1
OEV_03.d.x | e*t1; OEV_03.d.y | e*t1
OEV_04.d.x | f*t1; OEV_04.d.y | f*t1
OEV_05.d.x | g*t1; OEV_05.d.y | g*t1
OEV_06.d.x | h*t1; OEV_06.d.y | h*t1
OEV_07.d.x | i*t1; OEV_07.d.y | i*t1
OEV_08.d.x | j*t1; OEV_08.d.y | j*t1
OEV_09.d.x | k*t1; OEV_09.d.y | k*t1
OEV_10.d.x | l*t1; OEV_10.d.y | l*t1
OEV_12.d.x | m*t1; OEV_12.d.y | m*t1
OEV_13.d.x | n*t1; OEV_13.d.y | n*t1
OEV_14.d.x | o*t1; OEV_14.d.y | o*t1
OEV_15.d.x | p*t1; OEV_15.d.y | p*t1
OEV_16.d.x | q*t1; OEV_16.d.y | q*t1
OEV_17.d.x | r*t1; OEV_17.d.y | r*t1
OEV_18.d.x | s*t1; OEV_18.d.y | s*t1
OEV_20.d.x | ta*t1; OEV_20.d.y | ta*t1
OEV_23.d.x | ua*t1; OEV_23.d.y | ua*t1
OEV_24.d.x | ub*t1; OEV_24.d.y | ub*t1
OEV_25.d.x | v*t1; OEV_25.d.y | v*t1
OEV_26.d.x | w*t1; OEV_26.d.y | w*t1
OEV_27.d.x | x*t1; OEV_27.d.y | x*t1
OEV_28.d.x | y*t1; OEV_28.d.y | y*t1
OEV_29.d.x | z*t1; OEV_29.d.y | z*t1
OEV_30.d.x | b*t1; OEV_30.d.y | b*t1

# residual covariances
OEV_01.d.x ~~ OEV_01.d.y
OEV_02.d.x ~~ OEV_02.d.y
OEV_03.d.x ~~ OEV_03.d.y
OEV_04.d.x ~~ OEV_04.d.y
OEV_05.d.x ~~ OEV_05.d.y
OEV_06.d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d.x
OEV_26.d.x ~~ 1*OEV_26.d.x
OEV_27.d.x ~~ 1*OEV_27.d.x
OEV_28.d.x ~~ 1*OEV_28.d.x
OEV_29.d.x ~~ 1*OEV_29.d.x
OEV_30.d.x ~~ 1*OEV_30.d.x

OEV_01.d.y ~~ NA*OEV_01.d.y
OEV_02.d.y ~~ NA*OEV_02.d.y
OEV_03.d.y ~~ NA*OEV_03.d.y
OEV_04.d.y ~~ NA*OEV_04.d.y
OEV_05.d.y ~~ NA*OEV_05.d.y
OEV_06.d.y ~~ NA*OEV_06.d.y
OEV_07.d.y ~~ NA*OEV_07.d.y
OEV_08.d.y ~~ NA*OEV_08.d.y
OEV_09.d.y ~~ NA*OEV_09.d.y
OEV_10.d.y ~~ NA*OEV_10.d.y
OEV_12.d.y ~~ NA*OEV_12.d.y
OEV_13.d.y ~~ NA*OEV_13.d.y
OEV_14.d.y ~~ NA*OEV_14.d.y
OEV_15.d.y ~~ NA*OEV_15.d.y
OEV_16.d.y ~~ NA*OEV_16.d.y
OEV_17.d.y ~~ NA*OEV_17.d.y
OEV_18.d.y ~~ NA*OEV_18.d.y
OEV_20.d.y ~~ NA*OEV_20.d.y
OEV_23.d.y ~~ NA*OEV_23.d.y
OEV_24.d.y ~~ NA*OEV_24.d.y
OEV_25.d.y ~~ NA*OEV_25.d.y
OEV_26.d.y ~~ NA*OEV_26.d.y
OEV_27.d.y ~~ NA*OEV_27.d.y
OEV_28.d.y ~~ NA*OEV_28.d.y
OEV_29.d.y ~~ NA*OEV_29.d.y
OEV_30.d.y ~~ NA*OEV_30.d.y

# Change score modeling
diff.oev  =~ 1*OEV.t2
diff.oev ~ 1
OEV.t2  ~ 1*OEV.t1
OEV.t2 ~~ 0*OEV.t2

IS.gi.02 + IS.gi.03 + IS.gi.04 + IS.gi.05 ~~ 0*OEV.t1 + 0*diff.oev

# structural model
gi.sd.t1 ~ a1.ind*group
diff.sd ~ a3.ind*group
OEV.t1 ~ b1.ind*gi.sd.t1 + c1.ind*group
diff.oev ~ b3.ind*diff.sd + c2.ind*group

gi.sd.t1 + OEV.t1 ~~ diff.oev + diff.sd

# indirect and total effects
ind.sd1 := a1.ind*b1.ind
ind.dif.sd := a3.ind*b3.ind

total1 := a1.ind*b1.ind + c1.ind
totaldiff := a3.ind*b3.ind + c2.ind"

# '''''''' Boostrapped CIs ####
gi.sd.oev.medi.fit
# use (D)WLS for bootstrap!
gi.sd.oev.medi.fit.boot100 <- cfa(gi.sd.oev.medi, data=daten.prae.post.reduziert, missing="pairwise",
                               estimator="DWLS", parameterization="theta",
                               ordered =c("OEV_01.d.x" ,"OEV_02.d.x" ,"OEV_03.d.x" ,"OEV_04.d.x" ,"OEV_05.d.x" ,
                                          "OEV_06.d.x" ,"OEV_07.d.x" ,"OEV_08.d.x" ,"OEV_09.d.x" ,"OEV_10.d.x" ,
                                          "OEV_12.d.x" ,"OEV_13.d.x" ,"OEV_14.d.x" ,"OEV_15.d.x" ,
                                          "OEV_16.d.x" ,"OEV_17.d.x" ,"OEV_18.d.x" ,"OEV_20.d.x" ,
                                          "OEV_23.d.x" ,"OEV_24.d.x" ,"OEV_25.d.x" ,
                                          "OEV_26.d.x" ,"OEV_27.d.x" ,"OEV_28.d.x" ,"OEV_29.d.x" ,"OEV_30.d.x",
                                          "OEV_01.d.y" ,"OEV_02.d.y" ,"OEV_03.d.y" ,"OEV_04.d.y" ,"OEV_05.d.y" ,
                                          "OEV_06.d.y" ,"OEV_07.d.y" ,"OEV_08.d.y" ,"OEV_09.d.y" ,"OEV_10.d.y" ,
                                          "OEV_12.d.y" ,"OEV_13.d.y" ,"OEV_14.d.y" ,"OEV_15.d.y" ,
                                          "OEV_16.d.y" ,"OEV_17.d.y" ,"OEV_18.d.y" ,"OEV_20.d.y" ,
                                          "OEV_23.d.y" ,"OEV_24.d.y" ,"OEV_25.d.y" ,
                                          "OEV_26.d.y" ,"OEV_27.d.y" ,"OEV_28.d.y" ,"OEV_29.d.y" ,"OEV_30.d.y"),
                               se = "bootstrap", bootstrap = 100)

save(gi.sd.oev.medi.fit.boot100, file = "gi.sd.oev.medi.fit.boot100.rda")

Yves Rosseel

unread,
Jan 30, 2023, 9:53:56 AM1/30/23
to lav...@googlegroups.com
I think that there may be a bunch of bootstrap runs that fail (for some
reason). I would like to investigate why. Is it possible to send me your
dataset, together with this R script?

Yves.
> --
> You received this message because you are subscribed to the Google
> Groups "lavaan" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to lavaan+un...@googlegroups.com
> <mailto:lavaan+un...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/lavaan/e2438860-dff1-4ca6-9e3b-78986df0ae6dn%40googlegroups.com <https://groups.google.com/d/msgid/lavaan/e2438860-dff1-4ca6-9e3b-78986df0ae6dn%40googlegroups.com?utm_medium=email&utm_source=footer>.

Laura Loy

unread,
Jan 30, 2023, 4:45:17 PM1/30/23
to lavaan
Hi Yves, thank you very much, I will send it!
Reply all
Reply to author
Forward
0 new messages