[R] Wilcox paired test error message

1,456 views
Skip to first unread message

ruth parker

unread,
Jun 23, 2013, 2:34:20 PM6/23/13
to r-h...@r-project.org
Hi,
I've been trying to run a wilcox paired test on some data
beforesmall aftersmall
[1,] 63.5 512.0
[2,] 54.5 237.5
[3,] 52.5 161.5
[4,] 78.0 153.5
[5,] 53.5 68.0
[6,] 50.5 65.5
[7,] 69.0 52.0
[8,] 76.0 59.0
[9,] 68.0 66.5
[10,] 75.5 66.5
[11,] 67.0 45.5
[12,] 81.0 54.5
[13,] 49.0 44.0
[14,] 51.0 42.5
[15,] 53.0 34.5

using
wilcox.test(beforesmall ~ aftersmall,paired=T)
but I get the error message:

Error in wilcox.test.formula(beforesmall ~ aftersmall, paired = T) :
grouping factor must have exactly 2 levels

I don't have any missing values, I have the same amount of data points for
each group and I've looked everywhere trying to find the answer.
Please can somebody tell me how to make it work
thanks

[[alternative HTML version deleted]]

______________________________________________
R-h...@r-project.org 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.

Sarah Goslee

unread,
Jun 23, 2013, 4:30:16 PM6/23/13
to ruth parker, r-h...@r-project.org
Hi,

If you're doing exactly as described below, then you need to add the
data argument to wilcox.test so R knows where to find beforesmall and
aftersmall. But if so, that's a rather uninformative error message.

Saraj
--
Sarah Goslee
http://www.functionaldiversity.org

arun

unread,
Jun 23, 2013, 4:31:38 PM6/23/13
to R help
HI,
May be this helps:
dat1<- read.table(text="
beforesmall aftersmall
63.5      512.0
54.5      237.5
52.5      161.5
78.0      153.5
53.5       68.0
50.5       65.5
69.0       52.0
76.0       59.0
68.0       66.5
75.5       66.5
67.0       45.5
81.0       54.5
49.0       44.0
51.0       42.5
53.0       34.5
",sep="",header=TRUE)
wilcox.test(dat1$beforesmall,dat1$aftersmall,paired=T)
#
#    Wilcoxon signed rank test with continuity correction
#
#data:  dat1$beforesmall and dat1$aftersmall
#V = 55, p-value = 0.7982
#alternative hypothesis: true location shift is not equal to 0
#or
library(reshape2)
 dat2<-melt(dat1)
wilcox.test(value~variable,data=dat2,paired=TRUE)
##
#    Wilcoxon signed rank test with continuity correction
#
#data:  value by variable
#V = 55, p-value = 0.7982
#alternative hypothesis: true location shift is not equal to 0

A.K.

David Winsemius

unread,
Jun 23, 2013, 4:51:25 PM6/23/13
to ruth parker, r-h...@r-project.org

On Jun 23, 2013, at 11:34 AM, ruth parker wrote:

> Hi,
> I've been trying to run a wilcox paired test on some data
> beforesmall aftersmall
> [1,] 63.5 512.0
> [2,] 54.5 237.5
> [3,] 52.5 161.5
> [4,] 78.0 153.5
> [5,] 53.5 68.0
> [6,] 50.5 65.5
> [7,] 69.0 52.0
> [8,] 76.0 59.0
> [9,] 68.0 66.5
> [10,] 75.5 66.5
> [11,] 67.0 45.5
> [12,] 81.0 54.5
> [13,] 49.0 44.0
> [14,] 51.0 42.5
> [15,] 53.0 34.5
>
> using
> wilcox.test(beforesmall ~ aftersmall,paired=T)
> but I get the error message:
>
> Error in wilcox.test.formula(beforesmall ~ aftersmall, paired = T) :
> grouping factor must have exactly 2 levels

In this instance, you are working with a matrix, so you need supply the two column vectors explicitly:

----------

wilcox.test( dat[ ,'beforesmall'] , dat[ , 'aftersmall'], paired=T)

------------

And if you are using attach() and haven't told us, then STOP DOING THAT. (It won't work with matrices and it creates confusion among the inexperienced people who use it most often.)
--


> I don't have any missing values, I have the same amount of data points for
> each group and I've looked everywhere trying to find the answer.
> Please can somebody tell me how to make it work
> thanks
>
> [[alternative HTML version deleted]]
--

David Winsemius
Alameda, CA, USA

peter dalgaard

unread,
Jun 24, 2013, 2:22:02 AM6/24/13
to Sarah Goslee, r-h...@r-project.org, ruth parker

On Jun 23, 2013, at 22:30 , Sarah Goslee wrote:

> Hi,
>
> If you're doing exactly as described below, then you need to add the
> data argument to wilcox.test so R knows where to find beforesmall and
> aftersmall. But if so, that's a rather uninformative error message.
>
> Saraj

Not really, if you use the formula interface, the rhs is supposed to be the grouping, as in

wilcox.test(extra ~ group, data=sleep, paired=TRUE)

so it's telling you that "aftersmall" does not describe two groups.

(It is unfortunate, though, that we don't have a formula interface to the parallel-vector data layout for paired tests. That is the common case, the sleep data set is a rather rare exception. A formula specification for paired columns has been talked about; something like cbind(before, after) ~ 1 should be workable, but nothing has materialized to date.)

-pd
--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd....@cbs.dk Priv: PDa...@gmail.com

Sarah Goslee

unread,
Jun 24, 2013, 9:21:59 AM6/24/13
to peter dalgaard, r-h...@r-project.org, ruth parker
G'morning.

On Mon, Jun 24, 2013 at 2:22 AM, peter dalgaard <pda...@gmail.com> wrote:
>
> On Jun 23, 2013, at 22:30 , Sarah Goslee wrote:
>
>> Hi,
>>
>> If you're doing exactly as described below, then you need to add the
>> data argument to wilcox.test so R knows where to find beforesmall and
>> aftersmall. But if so, that's a rather uninformative error message.
>>
>> Saraj
>
> Not really, if you use the formula interface, the rhs is supposed to be the grouping, as in
>
> wilcox.test(extra ~ group, data=sleep, paired=TRUE)
>
> so it's telling you that "aftersmall" does not describe two groups.

Yep, sorry. That was actually my first thought, but then I misread the
helpfile.

You do still need the data argument, though. :)

Sarah

peter dalgaard

unread,
Jun 24, 2013, 11:28:06 AM6/24/13
to Sarah Goslee, r-h...@r-project.org, ruth parker

On Jun 24, 2013, at 15:21 , Sarah Goslee wrote:

> G'morning.
>
> On Mon, Jun 24, 2013 at 2:22 AM, peter dalgaard <pda...@gmail.com> wrote:
>>
>> On Jun 23, 2013, at 22:30 , Sarah Goslee wrote:
>>
>>> Hi,
>>>
>>> If you're doing exactly as described below, then you need to add the
>>> data argument to wilcox.test so R knows where to find beforesmall and
>>> aftersmall. But if so, that's a rather uninformative error message.
>>>
>>> Saraj
>>
>> Not really, if you use the formula interface, the rhs is supposed to be the grouping, as in
>>
>> wilcox.test(extra ~ group, data=sleep, paired=TRUE)
>>
>> so it's telling you that "aftersmall" does not describe two groups.
>
> Yep, sorry. That was actually my first thought, but then I misread the
> helpfile.
>
> You do still need the data argument, though. :)

-- or a preceding attach(), which at least some of us still use occasionally despite its dangers. For paired tests, it doesn't actually work to use data=, you need

with(mydata, t.test(beforesmall, aftersmall, paired=TRUE))

-pd
--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd....@cbs.dk Priv: PDa...@gmail.com

Reply all
Reply to author
Forward
0 new messages