Dear all, I have the following (rather) strange problem.. For some reasons, I finally work with a variable whose name includes an R function, "a.log(z)", say. And that is a problem when I call it in a formula, for instance: > myname<-"a.log(z)" > dd<-data.frame("a.log(z)"=1:10,y=rnorm(10)) > o<-lm(y~1,data=dd) > fo<-as.formula(paste(".~.+",paste(myname, collapse = "+"))) > fo . ~ . + a.log(z) > update(o,formula=fo) Error in eval(expr, envir, enclos) : could not find function "a.log" > How can fit the model? namely how can I use "a.log(z)" in the example above? Many thanks, vito -- ===================================Vito M.R. Muggeo Dip.to Sc Statist e Matem `Vianelli' Universit? di Palermo viale delle Scienze, edificio 13 90128 Palermo - ITALY tel: 091 6626240 fax: 091 485726/485612
Names not conforming to the usual R syntax can be placed in backquotes:> `log(x)` <- 1:10 > lm(`log(x)` ~ 1)Call: lm(formula = `log(x)` ~ 1) Coefficients: (Intercept) 5.5 On Nov 28, 2007 9:52 AM, vito muggeo <vmuggeo at dssm.unipa.it> wrote:> Dear all, > I have the following (rather) strange problem.. > For some reasons, I finally work with a variable whose name includes an > R function, "a.log(z)", say. And that is a problem when I call it in a > formula, for instance: > > > myname<-"a.log(z)" > > dd<-data.frame("a.log(z)"=1:10,y=rnorm(10)) > > o<-lm(y~1,data=dd) > > fo<-as.formula(paste(".~.+",paste(myname, collapse = "+"))) > > fo > . ~ . + a.log(z) > > update(o,formula=fo) > Error in eval(expr, envir, enclos) : could not find function "a.log" > > > > How can fit the model? namely how can I use "a.log(z)" in the example above? > > Many thanks, > vito > > > -- > ===================================> Vito M.R. Muggeo > Dip.to Sc Statist e Matem `Vianelli' > Universit? di Palermo > viale delle Scienze, edificio 13 > 90128 Palermo - ITALY > tel: 091 6626240 > fax: 091 485726/485612 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
On 11/28/2007 9:52 AM, vito muggeo wrote:> Dear all, > I have the following (rather) strange problem.. > For some reasons, I finally work with a variable whose name includes an > R function, "a.log(z)", say. And that is a problem when I call it in a > formula, for instance: > > > myname<-"a.log(z)" > > dd<-data.frame("a.log(z)"=1:10,y=rnorm(10)) > > o<-lm(y~1,data=dd) > > fo<-as.formula(paste(".~.+",paste(myname, collapse = "+"))) > > fo > . ~ . + a.log(z) > > update(o,formula=fo) > Error in eval(expr, envir, enclos) : could not find function "a.log" > > > > How can fit the model? namely how can I use "a.log(z)" in the example above?You can make just about anything into a name by quoting in in backticks, e.g. `a.log(z)` should always be seen as a name. However, there is a fair bit of code in contributed packages that converts things to character vectors and then reparses it (like your example above!), and that code may well get confused by such a name. For example, > deparse(quote(`a.log(z)`)) [1] "a.log(z)" The package writer could avoid this by using > deparse(quote(`a.log(z)`), backtick=T) [1] "`a.log(z)`" You also need to worry about the fact that by default, data.frame() will convert the name of the first column to a.log.z. So you could get everything you want with this variation on your example: > myname<-"`a.log(z)`" > dd<-data.frame("a.log(z)"=1:10,y=rnorm(10), check.names=FALSE) > o<-lm(y~1,data=dd) > fo<-as.formula(paste(".~.+",paste(myname, collapse = "+"))) > fo . ~ . + `a.log(z)` > update(o,formula=fo) Call: lm(formula = y ~ `a.log(z)`, data = dd) Coefficients: (Intercept) `a.log(z)` -0.3931 0.0753 However, I'd suggest avoiding the use of such a name. Duncan Murdoch
vito muggeo wrote:> Dear all, > I have the following (rather) strange problem.. > For some reasons, I finally work with a variable whose name includes an > R function, "a.log(z)", say. And that is a problem when I call it in a > formula, for instance: > > > myname<-"a.log(z)" > > dd<-data.frame("a.log(z)"=1:10,y=rnorm(10)) > > o<-lm(y~1,data=dd) > > fo<-as.formula(paste(".~.+",paste(myname, collapse = "+"))) > > fo > . ~ . + a.log(z) > > update(o,formula=fo) > Error in eval(expr, envir, enclos) : could not find function "a.log" > > > > How can fit the model? namely how can I use "a.log(z)" in the example above? > > Many thanks, > vito > > >Use backquotes as in `a.log(z)`, I think. myname<-"a.log(z)" o<-lm(y~1,data=dd) dd<-data.frame("a.log(z)"=1:10,y=rnorm(10),check.names=F) fo<-as.formula(paste(".~.+",paste(deparse(as.name(myname), backtick=T), collapse = "+"))) update(o,formula=fo) or, actually nicer, use fo <- bquote(. ~ . + .(as.name(myname))) -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907