lavaan icon indicating copy to clipboard operation
lavaan copied to clipboard

Support variable names with spaces or provide an informative error message

Open mronkko opened this issue 10 months ago • 2 comments

Test case:

library(lavaan)

d <- data.frame(a = rnorm(100),
                   `b b`= rnorm(100),
                check.names = F )

colnames(d)

sem("a ~ b b", data = d)

This produces an error

Error in lav_data_full(data = data, group = group, cluster = cluster,  : 
  lavaan ERROR: some (observed) variables specified in the model are not found in the dataset: bb

Instead of this error, lavaan should either just estimate the model with the variable that has a space or print an error like: "Lavaan does not support variable names with spaces. Please adjust your model and data. For example, you can convert all spaces in variable names to _ or ."

mronkko avatar Apr 19 '24 04:04 mronkko

Good point. The new parser (used by default in the latest github version) already gives a more useful warning:

a ~ b b
    ^
Error: lavaan->ldw_parse_get_modifier(): invalid modifier symbol (should be '*' or
    '?') at line 1, pos 5

In general, the parser cannot know if the user intended 'b + b', 'b*b' or 'b b'. Unless we look at the data, and check for a variable name 'b b'. When found, lavaan could perhaps produce a more informative message (saying that spaces are NOT allowed). I will investigate if this is feasible.

yrosseel avatar Apr 21 '24 17:04 yrosseel

The syntax parser on github now supports names with spaces. But we still need to check if this does not cause any issues.

yrosseel avatar Apr 22 '24 13:04 yrosseel

All seems fine. Closed issue.

yrosseel avatar Jun 02 '24 13:06 yrosseel