yrosseel / lavaan

an R package for structural equation modeling and more
http://lavaan.org
412 stars 99 forks source link

Support variable names with spaces or provide an informative error message #332

Closed mronkko closed 1 month ago

mronkko commented 2 months ago

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 ."

yrosseel commented 2 months ago

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 commented 2 months ago

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

yrosseel commented 1 month ago

All seems fine. Closed issue.