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). Mike ****************************************************************** Michael P. Fay, PhD Mathematical Statistician National Institute of Allergy and Infectious Diseases Tel: 301-451-5124 Fax:301-480-0912 (U.S. postal mail address) 6700B Rockledge Drive MSC 7609 Bethesda, MD 20892-7609 (Overnight mail address) 6700-A Rockledge Drive, Room 5133 Bethesda, MD 20817 http://www3.niaid.nih.gov/about/organization/dcr/BRB/staff/michael.htm ********************************************************************** Disclaimer: The information in this e-mail and any of its attachment...{{dropped:11}}
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