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?)
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)
Hadley Wickham <h.wickham at auckland.ac.nz> writes:> 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?)It's a problem of a sort that we've seen with several kinds of modeling code. Basically, the code appears to be using a deparse-paste-parse technique and gets the backtick bit wrong. Probably, we should start an effort to get things done right within base R first and only then pester Simon and others. In particular, I suspect we should supply a replacement for the parse(paste("~", paste(terms, collapse="+"))) construction that doesn't have to go via the textual representation. For now, I think you might want to rename variables and begone with it... -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
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