Re: [Rd] possible minor bug in fisher.test

From: peter dalgaard <pdalgd_at_gmail.com>
Date: Wed, 20 Apr 2011 22:22:40 +0200

On Apr 20, 2011, at 21:30 , Fay, Michael (NIH/NIAID) [E] wrote:

> Hi,
>
> I received a bug report for my exact2x2 package that seems to apply to fisher.test in the stats package also.
>
> Here is some code which creates the error:
>

>> x<-factor(c(0,1,1),levels=c(0,1))
>> y<-factor(c(1,1,1),levels=c(0,1))
>> fisher.test(x,y)

> Error in fisher.test(x, y) : 'x' and 'y' must have at least 2 levels
>> 

>
> The help says that x and y could be factors, and technically y does have 2 levels, although both are not observed. There is no problem statistically with y not having 2 observed levels, and
>
>> fisher.test(table(x,y))

>
> Produces the correct p-value of 1 and no errors.
>
> There is an easy solution. In fisher.test replace
>
> x <- factor(x[OK])
> y <- factor(y[OK])
>
>
> with
>
> x <- as.factor(x[OK])
> y <- as.factor(y[OK])
>
> (similar to how it is done in mcnemar.test).
>
>

OK. Of course, the test makes little sense statistically either way if one factor has only one level used, but I can imagine that a p value of 1 is preferable to an error, and the consistency is probably worth having too.

Fixed in r-devel.

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

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Wed 20 Apr 2011 - 20:24:46 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 21 Apr 2011 - 01:10:50 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive