Re: Categorical variables (have been converted to dummy variables) as a covariate

86 views
Skip to first unread message

Jeremy Miles

unread,
Mar 14, 2024, 5:37:30 PMMar 14
to lav...@googlegroups.com
You need to use the new variable name(s) not Category, in your model syntax.  (I'm not familiar with the model.matrix() function, so I'm not sure what it would do.)

Jeremy



On Wed, 13 Mar 2024 at 21:17, Serena <wsere...@gmail.com> wrote:
Hi there,

I am using a categorical variable (3 levels) as a covariate (independent/exogenous variable) in my sem model.

Since I know,  this variable is needed to be recorded as a dummy (K-1) variable.

I did this by model.matrix() function. I checked the results, and I got the two (k-1) new dummy variables.
res <- model.matrix(~Category, data = data)
head(res)
head(res[, -1])



But when I run my sem model:



It still shows 'lavaan ERROR: unordered factor(s) with more than 2 levels detected as exogenous covariate(s): Category


--
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/4582877a-1fb7-4339-a82a-75fdd6fd0952n%40googlegroups.com.

Serena

unread,
Mar 14, 2024, 7:52:05 PMMar 14
to lavaan
Hi Jeremy,

I used a different name in my syntax not 'Category', which I just used to show here.

Serena

Shu Fai Cheung (張樹輝)

unread,
Mar 14, 2024, 8:17:42 PMMar 14
to lavaan
In this post, you have this line, "v1 + v2 ~ Category"", in the model syntax:


To my understanding, 'Category' is the original categorical variable.

Have you tried using the dummy variables careated by model.matrix()? This is an illustration. The first model produce the error you encountered, and the second model use the dummy variables in the model syntax:

``` r
set.seed(1234)
n <- 10
dat <- data.frame(x = rnorm(10),
                  gp = sample(c("a", "b", "c"), 10, replace = TRUE))
dat$gp <- as.factor(dat$gp)
head(dat)
#>            x gp
#> 1 -1.2070657  b
#> 2  0.2774292  b
#> 3  1.0844412  b
#> 4 -2.3456977  a
#> 5  0.4291247  c
#> 6  0.5060559  c
res <- model.matrix(~ gp, data = dat)
res
#>    (Intercept) gpb gpc
#> 1            1   1   0
#> 2            1   1   0
#> 3            1   1   0
#> 4            1   0   0
#> 5            1   0   1
#> 6            1   0   1
#> 7            1   0   1
#> 8            1   0   0
#> 9            1   1   0
#> 10           1   0   0
#> attr(,"assign")
#> [1] 0 1 1
#> attr(,"contrasts")
#> attr(,"contrasts")$gp
#> [1] "contr.treatment"
head(res[, -1])
#>   gpb gpc
#> 1   1   0
#> 2   1   0
#> 3   1   0
#> 4   0   0
#> 5   0   1
#> 6   0   1
dat_new <- cbind(dat, res[, -1])
head(dat_new)
#>            x gp gpb gpc
#> 1 -1.2070657  b   1   0
#> 2  0.2774292  b   1   0
#> 3  1.0844412  b   1   0
#> 4 -2.3456977  a   0   0
#> 5  0.4291247  c   0   1
#> 6  0.5060559  c   0   1

library(lavaan)
#> This is lavaan 0.6-17
#> lavaan is FREE software! Please report any bugs.

mod1 <-
"
x ~ gp
"
fit1 <- sem(mod1, dat_new)
#> Error in lav_data_full(data = data, group = group, cluster = cluster, : lavaan ERROR: unordered factor(s) with more than 2 levels detected as exogenous covariate(s): gp

mod2 <-
"
x ~ gpb + gpc
"
fit2 <- sem(mod2, dat_new)
parameterEstimates(fit2)
#>   lhs op rhs    est    se     z pvalue ci.lower ci.upper
#> 1   x  ~ gpb  1.158 0.568 2.038  0.042    0.044    2.272
#> 2   x  ~ gpc  1.381 0.608 2.273  0.023    0.190    2.572
#> 3   x ~~   x  0.554 0.248 2.236  0.025    0.068    1.039
#> 4 gpb ~~ gpb  0.240 0.000    NA     NA    0.240    0.240
#> 5 gpb ~~ gpc -0.120 0.000    NA     NA   -0.120   -0.120
#> 6 gpc ~~ gpc  0.210 0.000    NA     NA    0.210    0.210
```

<sup>Created on 2024-03-15 with [reprex v2.1.0](https://reprex.tidyverse.org)</sup>

My two cents.

-- Shu Fai

Serena

unread,
Mar 14, 2024, 9:21:54 PMMar 14
to lavaan
Hi Shu Fai,

It works!

Thank you,
Serena
Reply all
Reply to author
Forward
0 new messages