Warning:unordered factors detected in data

5,890 views
Skip to first unread message

Katerina Bohle Carbonell

unread,
Oct 3, 2012, 6:51:03 AM10/3/12
to lav...@googlegroups.com
Hello

i'm new to R and Lavaan, so please bear with me :-)

I think I have a basic problem in my dataset which due to my lack of understanding of R I can't solve. My dataset contains numeric values from a Likert-type survey (1 to 5). Here is the code

study1<-read.table("F:/TULIP_RESEARCH/Study 1/study1_forinputR.csv", sep = ";")
is.na(study1)   
study1[study1==8888888]<-NA 
is.na(study1)   

install.packages("lavaan", dependencies = TRUE)
library(lavaan)

AE.model <- '
            dkw =~V6 + V7 +V8 +V9 +V10
             mc =~V11 + V12 +V13 +V14
             c  =~V15 + V16 + V17+V18+V19+V20+V21+V22
            '
       
fit<-cfa(AE.model, data=study1)

and this is the warning message I receive:

Warning messages:
1: In if (f.names %in% unlist(ov.names)) warning(paste("lavaan WARNING: unordered factor(s) detected in data:",  :
  the condition has length > 1 and only the first element will be used
2: In getDataFull(data = data, group = group, group.label = group.label,  :
  lavaan WARNING: unordered factor(s) detected in data: V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20 V21 V22

I'm a bit confused. Do I need to order variable v6 to v22 or do I need to order the levels (1 to 5) within variable v6 etc?

Thank you for your help.

Regards,
Katerina

yrosseel

unread,
Oct 5, 2012, 3:01:14 AM10/5/12
to lav...@googlegroups.com
> I think I have a basic problem in my dataset which due to my lack of
> understanding of R I can't solve. My dataset contains numeric values
> from a Likert-type survey (1 to 5). Here is the code
>
> study1<-read.table("F:/TULIP_RESEARCH/Study 1/study1_forinputR.csv", sep
> = ";")
> is.na(study1)
> study1[study1==8888888]<-NA
> is.na(study1)
>
> install.packages("lavaan", dependencies = TRUE)
> library(lavaan)
>
> AE.model <- '
> dkw =~V6 + V7 +V8 +V9 +V10
> mc =~V11 + V12 +V13 +V14
> c =~V15 + V16 + V17+V18+V19+V20+V21+V22
> '
>
> fit<-cfa(AE.model, data=study1)
>
> and this is the warning message I receive:
>
> Warning messages:
> 1: In if (f.names %in% unlist(ov.names)) warning(paste("lavaan WARNING:
> unordered factor(s) detected in data:", :
> the condition has length > 1 and only the first element will be used
> 2: In getDataFull(data = data, group = group, group.label = group.label, :
> lavaan WARNING: unordered factor(s) detected in data: V6 V7 V8 V9 V10
> V11 V12 V13 V14 V15 V16 V17 V18 V19 V20 V21 V22

Your dataset contains unordered factors (categorical variables). [which
is weird, if they are just numeric values 1,2,3,4,5; I would check the
input file; perhaps all the numbers are quoted? In that case, remove the
quotes; OR, did you forget 'header=TRUE' in the call to read.table to
get the variable names?]

To treat them as ordinal variables, you need to declare them as
'ordered'. You can do this before you call lavaan as follows:

Data[,] <- lapply(Data[,], ordered)

(here I assume *all* variables are ordered; if only a selection is
ordinal, while others are continuous, use something like:

Data[,c("item1","item2","item3","item4")] <-
lapply(Data[,c("item1","item2","item3","item4")], ordered)

If you now call the cfa() function, lavaan will treat the 'ordered'
variables as ordinal.

Another approach is to leave the data as it is, and to declare variables
as ordered usin the 'ordered' argument in the cfa() call:

fit<-cfa(AE.model, data=study1, ordered=c("V1","V2","V3",....) )

The latter approach has the advantage that you can switch between a
continuous approach and an ordinal approach to deal with likert scale items.

Hope this helps,

Yves.


Katerina Bohle Carbonell

unread,
Oct 8, 2012, 8:56:00 AM10/8/12
to lav...@googlegroups.com
Thanks Yves. Indeed I forgot to call the argument header=true. That fixed it for me.

Daniel Revillini

unread,
Oct 10, 2013, 4:44:13 PM10/10/13
to lav...@googlegroups.com
Hi Katerina, 

For future reference you can simply use the function read.csv if you save your excel files in .csv format. It just takes away the need to write ...header = TRUE, sep = ",")

D

Al

unread,
May 23, 2016, 5:24:27 PM5/23/16
to lavaan
Hi, also new to lavaan so please be kind. I ran into the same problem but used the "read.spss" command instead of "read.table", where do I include (and is this still the appropriate fix) "header = TRUE" to fix this?

Thanks!

Terrence Jorgensen

unread,
May 26, 2016, 4:57:35 AM5/26/16
to lavaan
Hi, also new to lavaan so please be kind. I ran into the same problem but used the "read.spss" command instead of "read.table", where do I include (and is this still the appropriate fix) "header = TRUE" to fix this?

I'm not sure how it could be the same problem because read.spss() doesn't need a header argument; SPSS data files don't put their variables in the first row, like text or Excel files do.  Are you talking about the having unordered factors that should be numeric (like scales whose points were labeled in the SPSS file)?  In that case, you set use.value.labels = FALSE and use the factor() or ordered() function to assign levels to nominal/ordinal factors if you have any.   I also usually have to override the default by setting to.data.frame = TRUE.

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

Zulkhairi Azizi Zainal Abidin

unread,
Oct 5, 2017, 5:40:37 AM10/5/17
to lavaan
Hi Terrence,

I encountered the same problem and I found this post. 
Thanks for your input. Unfortunately, I still got the same warning after I inserted to.data.frame = TRUE 
May you help me with this and thank you for your valuable time. 

Warning message:
In lav_data_full(data = data, group = group, cluster = cluster,  :
  lavaan WARNING: unordered factor(s) with more than 2 levels detected in data: Q1.1 Q1.2 Q1.3 Q1.4 Q1.5 Q1.6 Q1.7 Q1.10 Q1.8 Q1.9 Q1.15 Q1.16 Q1.17 Q1.18 Q1.19 Q1.20 Q1.21 Q1.22 Q1.23)

Kind regards, Zul

Zulkhairi Azizi Zainal Abidin

unread,
Oct 6, 2017, 5:08:29 AM10/6/17
to lavaan
It is solved now. I did type syntax as suggested: 

SPSSdata3<-read.spss(file.choose(), use.value.labels=FALSE, to.data.frame=TRUE)
SPSSdata3

Thank you for your help Terrence. 

Elena Murray

unread,
Mar 31, 2019, 4:14:20 PM3/31/19
to lavaan
Thanks so much! Worked for me too

F. Fereidooni

unread,
Apr 6, 2020, 7:38:52 AM4/6/20
to lavaan

It did not help me unfortunately. I got this warning: lavaan 0.6-5 did NOT end normally after 438 iterations
** WARNING ** Estimates below are most likely unreliable
Reply all
Reply to author
Forward
0 new messages