[R] Advice on parsing formulae

Frank E Harrell Jr f.harrell at vanderbilt.edu
Thu Dec 16 14:52:08 CET 2004


Claus Dethlefsen wrote:
> Thank you for the advice. I have now boiled my problem down to the
> following:
> 
> How do I create fm2 from fm1 ?
> 
> fm1 <-  Y ~ 1 + tvar(x:A) + tvar(z) + u + tvar(B) + tvar(poly(v,3))
> fm2 <-  Y ~ 1 + x:A + z + u + B + poly(v, 3)
> 
> Thus, how do I simply remove tvar( * ) from a formula? Do I have to write a
> function to parse the string and then re-create the formula? Is there an
> easy way of doing this?
  . . .

The Design package does manipulations of this sort to "understand" the 
model design to generate automatic tests of nonlinearity, pooled main 
effect + interaction tests, etc.  Related to your first issue, Design 
interprets a formula to find the "innermost" variable which is used on 
axes when plotting predicted values, and for other purposes.

In Function.Design you will see code like

TL <- attr(terms(object),"term.labels")
#Get inner transformations
from <- c('asis(*)','pol(*)','lsp(*)','rcs(*)','catg(*)','scored(*)',
   'strat(*)','matrx(*)','I(*)')
to   <- rep('*',9)

trans <- paste("h(",sedit(TL[ac!=9], from, to),")",sep="")
#change wrapping function to h()
h <- function(x,...) deparse(substitute(x))
for(i in (1:pm)) trans[i] <- eval(parse(text=trans[i]))

This may indirectly give you an idea, or you might see if the Design 
package does what you need in general [it doesn't support orthogonal 
polynomials, which I no longer find useful; it supports regular 
polynomials and regression splines].

-- 
Frank E Harrell Jr   Professor and Chair           School of Medicine
                      Department of Biostatistics   Vanderbilt University




More information about the R-help mailing list