[R] Mann-Whitney by group

R. Michael Weylandt michael.weylandt at gmail.com
Tue Jul 17 05:13:57 CEST 2012


On Mon, Jul 16, 2012 at 3:39 PM, Oxenstierna <david.chertudi at gmail.com> wrote:
> lapply(thing, function(x) x[['p.value']]) --works very well, thank you.
>
> Not to be a chore, but I'm interested in comparing the results of
> wilcox.test--and the methodology we've employed so far--with the results and
> methodology of wilcox_test (library("coin")).  So, I'd like to compare
> groups 5 and 6 across A through H using wilcox_test, and then I'd like to
> extract the p-values.  Going through the same methodology as above, but
> replacing wilcox.test with wilcox_test has failed, and so has the p.value
> extraction method:  lapply(thing, function(x) x[['p.value']]) .
>
> I believe the latter failure has to do with the fact that the coin package
> has a built-in class and built-in extraction method (pvalue() to extract and
> class "IndependenceTest"), but I don't know how to work around it.  For
> example, for a single comparison:  wilcox_test(A~Group, Dtb) works fine, and
> pvalue(wilcox.test(A~Group, Dtb)) extracts the p-value.
>
> So, any ideas about how to compare groups 5 and 6 across A through H using
> wilcox_test?

I think there are a few things at play here.

1) coin uses so-called S4 objects, so `[[` style subsetting isn't
going to work. The "right way" is, as you have found to use the
pvalue() function.

2) It looks like you need to use the formula intervace for
wilcox_test. Unfortunately, this makes things a little more
complicated as you'll need to construct the formula programmatically.

A one liner looks something like this.

lapply(LETTERS[1:8], function(x)
pvalue(wilcox_test(as.formula(paste(x, "~ Group ")), Dtb)))

Where lapply loops over the letters A,B, etc. and makes the string `A
~ Group`, converts it to a formula, passes that to wilcox_test, then
gets the pvalue and returns it.

In two lines you could do:

thing <- lapply(LETTERS[1:8], function(x)
wilcox_test(as.formula(paste(x, "~ Group")), Dtb))
thing2 <- lapply(thing, pvalue)

Where thing has all the test result objects, and thing2 collects the pvalues.

Hope this helps,
Michael



More information about the R-help mailing list