Hi all, I want to write a wrapper for an analysis of variance and I face a curious problem. Here are two different wrappers: fun.1 <- function(formula) { summary(aov(formula)) } fun.2 <- function(formula) { oneway.test(formula) } values <- c(15, 8, 17, 7, 26, 12, 8, 11, 16, 9, 16, 24, 20, 19, 9, 17, 11, 8, 15, 6, 14) group <- rep(1:3, each=7) # While the first wrapper works just fine: fun.1(values ~ group) # the second throws an error: fun.2(values ~ group) Error in substitute(formula)[[2]] : object is not subsettable ### I also tried binding the two vectors in a data.frame, with no avail. I did find a hack, creating two new vectors inside the function and creating a fresh formula, so I presume this has something to do with environments. Could anybody give me a hint on this? Thank you, Adrian -- Adrian Dusa Romanian Social Data Archive 1, Schitu Magureanu Bd 050025 Bucharest sector 5 Romania Tel./Fax: +40 21 3126618 \ +40 21 3120210 / int.101
oneway.test is using substitute on its arguments so its literally getting formula rather than the value of formula. Try these: fun.3 <- function(formula) { mc <- match.call() mc[[1]] <- as.name("oneway.test") eval.parent(mc) } fun.3(values ~ group) fun.4 <- function(formula) { do.call(oneway.test, list(formula)) } fun.4(values ~ group) On 1/9/07, Adrian Dusa <dusa.adrian at gmail.com> wrote:> > Hi all, > > I want to write a wrapper for an analysis of variance and I face a curious > problem. Here are two different wrappers: > > fun.1 <- function(formula) { > summary(aov(formula)) > } > > fun.2 <- function(formula) { > oneway.test(formula) > } > > values <- c(15, 8, 17, 7, 26, 12, 8, 11, 16, 9, 16, > 24, 20, 19, 9, 17, 11, 8, 15, 6, 14) > group <- rep(1:3, each=7) > > # While the first wrapper works just fine: > fun.1(values ~ group) > > # the second throws an error: > fun.2(values ~ group) > Error in substitute(formula)[[2]] : object is not subsettable > > ### > > I also tried binding the two vectors in a data.frame, with no avail. > I did find a hack, creating two new vectors inside the function and creating a > fresh formula, so I presume this has something to do with environments. > > Could anybody give me a hint on this? > Thank you, > Adrian > > -- > Adrian Dusa > Romanian Social Data Archive > 1, Schitu Magureanu Bd > 050025 Bucharest sector 5 > Romania > Tel./Fax: +40 21 3126618 \ > +40 21 3120210 / int.101 > > ______________________________________________ > R-help at stat.math.ethz.ch 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 Tuesday 09 January 2007 15:14, Gabor Grothendieck wrote:> oneway.test is using substitute on its arguments so its literally > getting formula rather than the value of formula.Ah-haa... I understand now. Thanks for the tips, they both work as expected. Best, Adrian> Try these: > > fun.3 <- function(formula) { > mc <- match.call() > mc[[1]] <- as.name("oneway.test") > eval.parent(mc) > } > fun.3(values ~ group) > > fun.4 <- function(formula) { > do.call(oneway.test, list(formula)) > } > fun.4(values ~ group) > > On 1/9/07, Adrian Dusa <dusa.adrian at gmail.com> wrote: > > Hi all, > > > > I want to write a wrapper for an analysis of variance and I face a > > curious problem. Here are two different wrappers: > > > > fun.1 <- function(formula) { > > summary(aov(formula)) > > } > > > > fun.2 <- function(formula) { > > oneway.test(formula) > > } > > > > values <- c(15, 8, 17, 7, 26, 12, 8, 11, 16, 9, 16, > > 24, 20, 19, 9, 17, 11, 8, 15, 6, 14) > > group <- rep(1:3, each=7) > > > > # While the first wrapper works just fine: > > fun.1(values ~ group) > > > > # the second throws an error: > > fun.2(values ~ group) > > Error in substitute(formula)[[2]] : object is not subsettable > > > > ### > > > > I also tried binding the two vectors in a data.frame, with no avail. > > I did find a hack, creating two new vectors inside the function and > > creating a fresh formula, so I presume this has something to do with > > environments. > > > > Could anybody give me a hint on this? > > Thank you, > > Adrian > > > > -- > > Adrian Dusa > > Romanian Social Data Archive > > 1, Schitu Magureanu Bd > > 050025 Bucharest sector 5 > > Romania > > Tel./Fax: +40 21 3126618 \ > > +40 21 3120210 / int.101 > > > > ______________________________________________ > > R-help at stat.math.ethz.ch 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.-- Adrian Dusa Arhiva Romana de Date Sociale Bd. Schitu Magureanu nr.1 050025 Bucuresti sectorul 5 Romania Tel./Fax: +40 21 3126618 \ +40 21 3120210 / int.101
oneway.test expects a literal formula, not a variable containing a formula. The help page says formula: a formula of the form 'lhs ~ rhs' where 'lhs' gives the sample values and 'rhs' the corresponding groups. Furthermore, if you had foo.2 <- function() oneway.test(value ~ group) it would still not work, as data: an optional matrix or data frame (or similar: see 'model.frame') containing the variables in the formula 'formula'. By default the variables are taken from 'environment(formula)'. I could show you several complicated workarounds, but why do you want to do this? On Tue, 9 Jan 2007, Adrian Dusa wrote:> > Hi all, > > I want to write a wrapper for an analysis of variance and I face a curious > problem. Here are two different wrappers: > > fun.1 <- function(formula) { > summary(aov(formula)) > } > > fun.2 <- function(formula) { > oneway.test(formula) > } > > values <- c(15, 8, 17, 7, 26, 12, 8, 11, 16, 9, 16, > 24, 20, 19, 9, 17, 11, 8, 15, 6, 14) > group <- rep(1:3, each=7) > > # While the first wrapper works just fine: > fun.1(values ~ group) > > # the second throws an error: > fun.2(values ~ group) > Error in substitute(formula)[[2]] : object is not subsettable > > ### > > I also tried binding the two vectors in a data.frame, with no avail. > I did find a hack, creating two new vectors inside the function and creating a > fresh formula, so I presume this has something to do with environments. > > Could anybody give me a hint on this? > Thank you, > Adrian > >-- 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
Looks like oneway.test has been changed for R 2.5.0. Paste the code in this file: https://svn.r-project.org/R/trunk/src/library/stats/R/oneway.test.R into your session. Then fun.2 from your post will work without the workarounds I posted: fun.2(values ~ group) On 1/9/07, Adrian Dusa <dusa.adrian at gmail.com> wrote:> On Tuesday 09 January 2007 15:14, Gabor Grothendieck wrote: > > oneway.test is using substitute on its arguments so its literally > > getting formula rather than the value of formula. > > Ah-haa... I understand now. Thanks for the tips, they both work as expected. > Best, > Adrian > > > Try these: > > > > fun.3 <- function(formula) { > > mc <- match.call() > > mc[[1]] <- as.name("oneway.test") > > eval.parent(mc) > > } > > fun.3(values ~ group) > > > > fun.4 <- function(formula) { > > do.call(oneway.test, list(formula)) > > } > > fun.4(values ~ group) > > > > On 1/9/07, Adrian Dusa <dusa.adrian at gmail.com> wrote: > > > Hi all, > > > > > > I want to write a wrapper for an analysis of variance and I face a > > > curious problem. Here are two different wrappers: > > > > > > fun.1 <- function(formula) { > > > summary(aov(formula)) > > > } > > > > > > fun.2 <- function(formula) { > > > oneway.test(formula) > > > } > > > > > > values <- c(15, 8, 17, 7, 26, 12, 8, 11, 16, 9, 16, > > > 24, 20, 19, 9, 17, 11, 8, 15, 6, 14) > > > group <- rep(1:3, each=7) > > > > > > # While the first wrapper works just fine: > > > fun.1(values ~ group) > > > > > > # the second throws an error: > > > fun.2(values ~ group) > > > Error in substitute(formula)[[2]] : object is not subsettable > > > > > > ### > > > > > > I also tried binding the two vectors in a data.frame, with no avail. > > > I did find a hack, creating two new vectors inside the function and > > > creating a fresh formula, so I presume this has something to do with > > > environments. > > > > > > Could anybody give me a hint on this? > > > Thank you, > > > Adrian > > > > > > -- > > > Adrian Dusa > > > Romanian Social Data Archive > > > 1, Schitu Magureanu Bd > > > 050025 Bucharest sector 5 > > > Romania > > > Tel./Fax: +40 21 3126618 \ > > > +40 21 3120210 / int.101 > > > > > > ______________________________________________ > > > R-help at stat.math.ethz.ch 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. > > -- > Adrian Dusa > Arhiva Romana de Date Sociale > Bd. Schitu Magureanu nr.1 > 050025 Bucuresti sectorul 5 > Romania > Tel./Fax: +40 21 3126618 \ > +40 21 3120210 / int.101 >
On Wednesday 10 January 2007 19:03, Gabor Grothendieck wrote:> Looks like oneway.test has been changed for R 2.5.0. > Paste the code in this file: > https://svn.r-project.org/R/trunk/src/library/stats/R/oneway.test.R > into your session. Then fun.2 from your post will work without > the workarounds I posted: > fun.2(values ~ group)Brilliant :) Super fast change, this is why I love R. Cheers, Adrian -- Adrian Dusa Romanian Social Data Archive 1, Schitu Magureanu Bd 050025 Bucharest sector 5 Romania Tel./Fax: +40 21 3126618 \ +40 21 3120210 / int.101