[R] Error in gam?

Prof Brian Ripley ripley at stats.ox.ac.uk
Sun Apr 18 23:23:13 CEST 2004


On Mon, 19 Apr 2004, Hadley Wickham wrote:

> I'm having some problems using variable names containing spaces (using 
> backticks) with gam (mgcv 0.9-6, R 1.8.1).  Some toy code to reproduce 
> my problem is below.  Am I doing something wrong, or should I pass this 
> bug on to Simon Wood? (Or do I need to rename my variables to get rid of 
> the spaces?)

Well, that's not the current mgcv but the latter gives a similar error.
Yes, it needs Simon's attention.  The problem is in interpret.gam, where
lines like

ff1 <- paste(smooth.spec[[i]]$term[1:nt], collapse = "+")
fake.formula <- paste(fake.formula, "+", ff1)

expect syntactically valid names, and so the deparse calls in s() (and
probably elsewhere) need backtick=TRUE.  With that change, your example 
works.

It's probably optimistic to expect most of the code available for R to 
work with backtick-quoted names.

> 
> Thanks,
> 
> Hadley
> 
> 
> library(mgcv)
> 
> test <- data.frame(a = c(1:10), `a b` = c(1:10)^2, c=2:11, check.names=F)
> test$`a b`
> 
> lm(a ~ `a b`, data=test) #works
> gam(a ~ s(c), data=test) #works
> gam(a ~ s(`a b`), data=test) #error below
> 
> Error in parse(file, n, text, prompt) : parse error
> 
> traceback()
>   9: parse(text = x)
>   8: eval(parse(text = x)[[1]])
>   7: formula(eval(parse(text = x)[[1]]))
>   6: switch(mode(x), "NULL" = structure(NULL, class = "formula"),
>         character = formula(eval(parse(text = x)[[1]])), call = eval(x),
>         stop("invalid formula"))
> 5: formula.default(object, env = NULL)
> 4: formula(object, env = NULL)
> 3: as.formula(rf)
> 2: gam.parser(formula)
> 1: gam(a ~ s(`a b`), data = test)
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> 
> 

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list