[Rd] Should mcnemar.test use as.factor instead of factor

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Apr 28 11:54:43 CEST 2010


On Tue, 27 Apr 2010, Greg Snow wrote:

> I am working with the mcnemar.test function and the help does not show a maintainer/author, but it is part of the stats package.
>
> My issue is that I want to use the test on 2 variables with possible 
> values of 0:3, in one of the tests one of the variables does not 
> have any 3's, so to make sure that the matrix is square I do:
>
>> x <- factor(x, levels=0:3)
>> y <- factor(y, levels=0:3)
>
> If I run mcnemar.test on the table of x and y then everything works fine, but if I pass in x and y without running table first then I get an error about both variables needing to have the same number of levels (which they did when I passed them in).  The problem occurs because the function when handed the raw data does its own conversion to factor using the factor function which drops the unused level in the one variable.  A simple fix should be to replace the call to factor with a call to as.factor (which will not change anything for variables that are already factors and therefore not drop levels).  I cannot imagine any code that would break from this change, but that could just be a lack of imagination on my part.
>
> So, can anyone think of a reason not to change factor to as.factor?

Given the description

   If \code{x} is a matrix, it is taken as a two-dimensional contingency
   table, and hence its entries should be nonnegative integers.
   Otherwise, both \code{x} and \code{y} must be vectors of the same
   length.  Incomplete cases are removed, the vectors are coerced into
   factor objects, and the contingency table is computed from these.

it should 'coerce'.

> Is this worth a bug report/enhancement request?

Not necessary, I'll change this in R-patched.  Thanks for pointing it 
out.

>
>
>
>> sessionInfo()
> R version 2.11.0 (2010-04-22)
> i386-pc-mingw32
>
> locale:
> [1] LC_COLLATE=English_United States.1252
> [2] LC_CTYPE=English_United States.1252
> [3] LC_MONETARY=English_United States.1252
> [4] LC_NUMERIC=C
> [5] LC_TIME=English_United States.1252
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> loaded via a namespace (and not attached):
> [1] tools_2.11.0
>
> -- 
> Gregory (Greg) L. Snow Ph.D.
> Statistical Data Center
> Intermountain Healthcare
> greg.snow at imail.org
> 801.408.8111
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-devel mailing list