Re: [Rd] model.matrix.default chokes on backquote (PR#7202)

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Sat 28 Aug 2004 - 18:40:59 EST

ggrothendieck@myway.com writes:

> The following gives an error:
>
>
> `a(b)` <- 1:4
>
> `c(d)` <- (1:4)^2
>
> lm(`a(b)` ~ `c(d)`)
> Error in model.matrix.default(mt, mf, contrasts) :

> model frame and formula mismatch in model.matrix()
>

> To fix it replace this line in model.matrix.default:
>

> reorder <- match(attr(t, "variables")[-1], names(data))
>

>
with these two lines:
>

> strip.backquote <- function(x) gsub("^`(.*)`", "\\1", x)

> reorder <- match(strip.backquote(attr(t, "variables"))[-1],

> strip.backquote(names(data)))

Hmm.. Yes, there's a bug (and it's likely not the only one we have relating to odd variable names in model formulas), but I suspect that ink1">the fix is wrong.

The backquotes are not part of the variable names, but get added by deparsing -- sometimes! Other times they do not: Try for instance as.character(quote(`a(b)`)). (Which is as it should be. Other pieces of logic relating to nonsyntactical names represent some rather awkward compromises.)

When backquotes have found their way into names(data) or the "variables" attribute, I would rather suspect that they were created by the wrong tool and fix that, not cure the symptom by stripping them off at a later stage.

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907

______________________________________________
R-devel@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Sat Aug 28 18:49:35 2004

This archive was generated by hypermail 2.1.8 : Wed 03 Nov 2004 - 22:45:10 EST