Contains either a reserved word (in R) or an illegal character?

391 views
Skip to first unread message

Sierra Iwanicki

unread,
Jan 8, 2020, 9:51:30 AM1/8/20
to lavaan
Received this error. Can anyoe help?

> model <- 'executive =~ dep4 + cog9 + dep47 + dep20
+ >     language   =~ cog13 + cog23 + cog8 + cog16 + cog32
+ >     memory   =~ cog20 + cog17 + cog6 + cog22
+ >     perceptual =~ cog18 + cog21 + cog10 + cog26
+ >     social =~ lie1 + cog27 + anx3 + par15 + psy18 + psy23 + psy17 + lie8 + psy28 '
> fit <- cfa(CIS.model, data=CIS41)
Error in lavaan::lavaan(model = CIS.model, data = CIS41, model.type = "cfa",  : 
  object 'CIS.model' not found
> fit <- cfa(model, data=CIS41)
Error in lavParseModelString(model) : 
  lavaan ERROR: left hand side (lhs) of this formula:
    >language =~ cog13+cog23+cog8+cog16+cog32
    contains either a reserved word (in R) or an illegal character: “>language”
    see ?reserved for a list of reserved words in R
    please use a variable name that is not a reserved word in R
    and use only characters, digits, or the dot symbol.

Terrence Jorgensen

unread,
Jan 9, 2020, 8:03:36 AM1/9/20
to lavaan
> fit <- cfa(CIS.model, data=CIS41)
Error in lavaan::lavaan(model = CIS.model, data = CIS41, model.type = "cfa",  : 
  object 'CIS.model' not found

This happened because you assigned your model syntax to an object called "model", not "CIS.model".

 
> fit <- cfa(model, data=CIS41)
Error in lavParseModelString(model) : 
  lavaan ERROR: left hand side (lhs) of this formula:
    >language =~ cog13+cog23+cog8+cog16+cog32
    contains either a reserved word (in R) or an illegal character: “>language”
    see ?reserved for a list of reserved words in R
    please use a variable name that is not a reserved word in R
    and use only characters, digits, or the dot symbol.

This happened because it looks like you copy/pasted console output into your character string of model syntax.   Each line in "model" begins with "+ >", which should not be there.  See the example on the ?cfa help page to see how model syntax should look.

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

AvidLavaanUser

unread,
Feb 17, 2021, 9:17:59 PM2/17/21
to lavaan
lavaan community,

I received this error as well, but I don't see a reserved word or an illegal character in my syntax. The same error showed up after I changed the variable's name. Could anyone please let me know how to run this model?

ampc.w2.w1.ra <- ' 
md$ampcW2 ~ md$RespectAdults + md$ampcW1
'
fit.ampc.w2.w1.ra<-sem(ampc.w2.w1.ra,data=md,missing="fiml")
# Error in lavParseModelString(model) : 
# lavaan ERROR: left hand side (lhs) of this formula:
#   md$ampcW2 ~ md$RespectAdults+md$ampcW1
# contains either a reserved word (in R) or an illegal character: “md$ampcW2”
# See ?reserved for a list of reserved words in R
# Please use a variable name that is not a reserved word in R
# and use only characters, digits, or the dot symbol.

# contains either a reserved word (in R) or an illegal character: “md$AcademicPsychologicalControlW2”
# See ?reserved for a list of reserved words in R
# Please use a variable name that is not a reserved word in R
# and use only characters, digits, or the dot symbol.

Thanks in advance,
AvidLavaanUser

Alex Schoemann

unread,
Feb 17, 2021, 10:11:55 PM2/17/21
to lavaan
The issue is the use of $ in the model syntax. You only specify the name of variables in the model syntax, not the data frame they're in (so no need for the md$ part of each variable name.

Alex

car...@web.de

unread,
Feb 18, 2021, 5:55:37 AM2/18/21
to lav...@googlegroups.com
Remove the dollars. This is an extractor.
Am 18.02.21, 04:14 schrieb AvidLavaanUser <soyoun...@gmail.com>:
--
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/fdde4679-bc9f-4746-a8ec-d0a4fae80b24n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages