[R] formula parsing, using parts ...

Uwe Ligges ligges at statistik.uni-dortmund.de
Tue Oct 28 11:27:08 CET 2003


Russell Senior wrote:

>>>>>>"Uwe" == Uwe Ligges <ligges at statistik.uni-dortmund.de> writes:
> 
> 
> Russell> I am writing a little abstraction for a series of tests. For
> Russell> example, I am running an anova and kruskal.test on a
> Russell> one-factor model.  That isn't a particular problem, I have an
> Russell> interface like: my.function <- function(model,data) {
> Russell> print(deparse(substitute(data))) a <- anova(lm(formula,data))
> Russell> print(a) if(a$"Pr(>F)"[1] < 0.05) { pairwise.t.test(???)  } b
> Russell> <- kruskal.test(formula,data) print(b) if ...  } I want to
> Russell> run each test, then depending on the resulting p-value, run
> Russell> pairwise tests.  I am getting into trouble where I put the
> Russell> ??? above.  The pairwise.t.test has a different interface,
> Russell> that seems to want me to dismember the formula into
> Russell> constituent parts to feed in.  The other alternative is to
> Russell> give my.function the constituent parts and let it build the
> Russell> model.  I haven't figured out how to do either one.  Can
> Russell> someone give me some pointers?
> 
> Uwe> See ?formula and its "See Also" Section on how to do formula
> Uwe> manipulation. There's also an example on how to construct a
> Uwe> formula.
> 
> Russell> In order to use the 'as.formula(paste(response," ~
> Russell> ",factor))' approach, response and factor seem to need to be
> Russell> strings (at least they seem to if response is "log(x)" or the
> Russell> like).  Whereas, for pairwise.t.test they need to be names.
> Russell> What is the proper way to do that?
> 
> 
> Uwe> In order to run pairwise.t.test() you can simply get() the values
> Uwe> from objects:
> 
> Uwe> Let's change the example in ?pairwise.t.test:
> 
> Uwe>   data(airquality) 
> Uwe>   attach(airquality) 
> Uwe>   Month <- factor(Month, labels = month.abb[5:9]) 
> Uwe>   x <- "Ozone" 
> Uwe>   y <- "Month"
> Uwe>   pairwise.t.test(get(x), get(y))
> 
> Suppose I want x to be "log(Ozone)"?  The get() function doesn't help
> me there.
> 


  eval(parse(text=x))

Uwe Ligges




More information about the R-help mailing list