From tlumley at uw.edu  Mon Oct 24 18:41:14 2011
From: tlumley at uw.edu (Thomas Lumley)
Date: Mon, 24 Oct 2011 09:41:14 -0700
Subject: [Rd] droplevels: drops contrasts as well
In-Reply-To: <F54EF8F1B477CF448729593FE421F646645E50@HQVEVE0032.nestle.com>
References: <F54EF8F1B477CF448729593FE421F646645E50@HQVEVE0032.nestle.com>
Message-ID: <CAJ55+d+41EsjF0_PsjJcsFX2xc6-BFvbZZyOkOFjLg4yEpDn1Q@mail.gmail.com>

On Fri, Oct 21, 2011 at 5:57 AM, Thaler, Thorn, LAUSANNE, Applied
Mathematics <Thorn.Thaler at rdls.nestle.com> wrote:
> Dear all,
>
> Today I figured out that there is a neat function called droplevels,
> which, well, drops unused levels in a data frame. I tried the function
> with some of my data sets and it turned out that not only the unused
> levels were dropped but also the contrasts I set via "C". I had a look
> into the code, and this behaviour arises from the fact that droplevels
> uses simply factor to drop the unused levels, which uses the default
> contrasts as set by options("contrasts").
>
> I think this behaviour is annoying, because if one does not look
> carefully enough, one looses the contrasts silently. Hence may I suggest
> to change the code of droplevels to something like the following:

This silently changes the contrasts -- eg, if the first level of the
factor is one of the empty levels, the reference level used by
contr.treatment() will change.  Also, if the contrasts are a matrix
rather than specifying a contrast function, the matrix will be invalid
for the the new factor.

I think just having a warning would be better -- in general it's not
clear what (if anything) it means to have the same contrasts on
factors with different numbers of levels.

   -thomas

-- 
Thomas Lumley
Professor of Biostatistics
University of Auckland



[Rd] droplevels: drops contrasts as well
Thomas Lumley