[Rd] Problem with contrasts bug fix?

Sebastian Meyer @eb@meyer @end|ng |rom |@u@de
Thu Jun 30 10:19:52 CEST 2022


For reference: this is about 
<https://bugs.r-project.org/show_bug.cgi?id=17616>.

The problem with your 'contr.none' example is that it is not a proper 
contrasts function (and has never been). ?contrasts says

>      Suitable
>      functions have a first argument which is the character vector of
>      levels, a named argument 'contrasts' (always called with
>      'contrasts = TRUE') and optionally a logical argument 'sparse'.

Your example function fails also in R 4.1.3 and 4.2.1:

> contr.none <- function(n) contrasts(factor(1:n), contrasts = FALSE)
> contrasts(CO2$Treatment) <- "contr.none"
> contrasts(CO2$Treatment)

Error in ctrfn(levels(x), contrasts = contrasts) :
   unused argument (contrasts = contrasts)

Even if we fix the missing argument:

> contr.none <- function(n, ...) contrasts(factor(1:n), contrasts = FALSE)
> contrasts(CO2$Treatment) <- "contr.none"
> contrasts(CO2$Treatment)

Error in 1:n : NA/NaN argument
In addition: Warning messages:
1: In 1:n : numerical expression has 2 elements: only the first used
2: In factor(1:n) : NAs introduced by coercion

What *did* work (inconsistently) was :

> contrasts(CO2$Treatment) <- contr.none
> contrasts(CO2$Treatment)

            1
nonchilled 1
chilled    0

It no longer does in R-devel as 'contr.none' is now called with the 
factor *levels* (not nlevels) also in this case as documented.

Note that the argument name 'n' in the default contrast functions such 
as contr.treatment() is a bit delicate as these support passing either 
levels or nlevels.

Hope this helps.
Best regards,

	Sebastian Meyer


Am 30.06.22 um 00:27 schrieb Murray Efford:
> This worked previously but gives an error in R-devel (Windows today), triggering a warning to a package maintainer:
> 
> contr.none <- function(n) contrasts(factor(1:n), contrasts = FALSE)
> lm(uptake~Treatment, CO2, contrasts=list(Treatment=contr.none))
> 
> Error in 1:n : NA/NaN argument
> In addition: Warning messages:
> 1: In 1:n : numerical expression has 2 elements: only the first used
> 2: In factor(1:n) : NAs introduced by coercion
> 
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list