Using ordered argument triggers "object not found" error

99 views
Skip to first unread message

Justine Maltais-Proulx

unread,
Oct 6, 2023, 5:04:54 PM10/6/23
to lavaan
Hello all,

I am using lavaan to test a model that uses different types of variables. There are observed categorical variables that are binary, observed continuous variables, latent continuous variables and a binary dependant variable.

Since I have a dependant variable that is binary, I was trying to use the "ordered" argument in my model fit and I constantly get this error message:
Error in lavaan::lavaan(model = dma.sem.v3.2, data = DMA, ordered = Q13.binaire, : object 'Q13.binaire' not found

Here is my script so far:
library(haven)
library(sjPlot)
library(lavaan)
library(lavaanPlot)
library(semPlot)
options(max.print = 1000000)

DMA <- haven::read_sav(file.choose())

dma.sem.v3.2 <- '
#measurement model
severite.percue =~ Q9 + Q10 + Q11
auto.eff =~ Q46 + Q47 + Q48 + Q49 + Q50 + Q51 + Q52 + Q53

#regressions
Q8 ~ SCORE_CONNAIS + QS1 + QS2.binaire + QS3A.binaire + QS5 + QS7
severite.percue ~ SCORE_CONNAIS + QS1 + QS2.binaire + QS3A.binaire + QS5 + QS7
INDEX_AV_DMA ~ SCORE_CONNAIS + QS1 + QS2.binaire + QS3A.binaire + QS5 + QS7
INDEX_DESAV_DMA ~ SCORE_CONNAIS + QS1 + QS2.binaire + QS3A.binaire + QS5 + QS7
auto.eff ~ SCORE_CONNAIS + QS1 + QS2.binaire + QS3A.binaire + QS5 + QS7
Q13.binaire ~ Q8 + severite.percue + INDEX_AV_DMA + INDEX_DESAV_DMA + auto.eff + Q55 + QS6.binaire + Q54_r1 + Q54_r2 + Q54_r4 + Q54_r5 + Q54_r8 + Q54_r9

#residual correlations
'

fit.dma.sem.v3.2 <- sem(dma.sem.v3.2, data = DMA,
                        ordered = Q13.binaire)
summary(fit.dma.sem.v3.2, standardized = TRUE, rsquare = TRUE)
fitMeasures(fit.dma.sem.v3.2, c("cfi", "nnfi", "rmsea", "srmr"))
standardizedsolution(fit.dma.sem.v3.2)


How do I solve this error message?

Thank you in advance!

Edward Rigdon

unread,
Oct 6, 2023, 5:27:49 PM10/6/23
to lav...@googlegroups.com
     I strongly suspect that you have the variable name incorrect, in some way.
     lavaan is almost certainly correct that the referenced object, "Q13.binaire," is not found BY THAT NAME within the referenced dataset, DMA. So the first thing is to check DMA, using maybe 
str(DMA)
to make sure the variable is present.
     All of your other Q**.binaire variables have the letter S after Q, as in QS2.binaire or QS3A.binaire. But this one variable does not have an S after the Q. Is that correct? 
     Second to that, your variable names use a mix of UPPER case and lower case letters, which make me worry about a capitalization difference in the variable names in DMA.

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/lavaan/bd4cad46-21af-4e3e-8337-51584e9150a5n%40googlegroups.com.

Shu Fai Cheung (張樹輝)

unread,
Oct 6, 2023, 5:56:35 PM10/6/23
to lavaan
I believe by  Q13.binaire you mean the column name. The value for  ordered should be a character vector of the column name(s) (or a logical, TRUE/FALSE)

You can try this, changing  Q13.binaire to " Q13.binaire":

fit.dma.sem.v3.2 <- sem(dma.sem.v3.2, data = DMA,
                        ordered = "Q13.binaire")

Hope this helps.

-- Shu Fai

Justine Maltais-Proulx

unread,
Oct 6, 2023, 5:59:46 PM10/6/23
to lavaan
The variable name is indeed correct, although I understand how it would appear as though it is not.
The script works as intended when the argument "ordered = Q13.binaire" is absent. It is only when I introduce this particular argument that I run into this problem.
I am very novice so it's possible I don't understand my problem very well, but it's like R (or lavaan) cannot refer to a single variable within my data frame for this specific "ordered" argument. I tried different ways of formatting my data, but I still have no luck in figuring out this problem.

Justine Maltais-Proulx

unread,
Oct 6, 2023, 6:07:35 PM10/6/23
to lavaan
I tried this and had a different error message:
Error in lav_samplestats_step1(Y = Data, wt = wt, ov.names = ov.names, : lavaan ERROR: unknown ov.types:haven_labelled

But I then repeated the same process by importing my data as a .csv file instead of using the haven package for a .sav file, and it worked!

Thank you very much! Your help is very much appreciated.

Shu Fai Cheung (張樹輝)

unread,
Oct 6, 2023, 6:09:20 PM10/6/23
to lavaan
For some arguments in some functions, if you use Q13.binaire, a function will try to find the object named Q13.binaire. It does not exist, and hence the error "object 'Q13.binaire' not found". 

If you typed "Q13.binaire", it means a string (a length-one character vector, actually). As far as I know, the argument ordered expects a character vector or TRUE/FALSE.

-- Shu Fai

P.S.: I wrote "For some arguments in some functions" because for some functions, they can accept column names without the quotes. E.g., ggplot2 functions.

Justine Maltais-Proulx

unread,
Oct 6, 2023, 7:03:17 PM10/6/23
to lavaan
I just double-checked my variable with class() and it reads as this:
> class("Q13.binaire") [1] "character"

It is a variable that is coded as 0 or 1. Should I expect the "ordered" argument in sem() to work properly in this case?

Thank you very much!

Shu Fai Cheung (張樹輝)

unread,
Oct 6, 2023, 8:21:48 PM10/6/23
to lavaan
Sorry for the confusion caused.

With ordered = "Q13.binaire",  lavaan will treat the column named Q13.binaire in the data frame as an ordinal variable, even if it actually contains, say, 0 and 1 (or 1 and 2). If you want to check the content of this column in the data frame, you type this:

DMA$Q13.binaire

I would suggest using head(DMA) to check the content of the data frame first.

By the way, you converted the file to a CSV file. You may want to check whether the conversion of variables with labels, which may be numeric variables representing categorical variables (common in SPSS), is done in the intended way.

Instead of using a CSV file, you can use  haven::read_sav()as you did, and use haven::as_factor() to convert labelled columns to factors, which sem() should support:


This may allow you to remove the error lavaan ERROR: unknown ov.types:haven_labelled .

The conversion for (SPSS) labelled variables to factors is quite complicated (to me) and so you should check the content of the converted dataset before using it in sem().

Hope this helps.

-- Shu Fai

Reply all
Reply to author
Forward
0 new messages