[Rd] possible minor bug in fisher.test

peter dalgaard pdalgd at gmail.com
Wed Apr 20 22:22:40 CEST 2011


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



More information about the R-devel mailing list