Skip to content

Problem with contrasts bug fix?

3 messages · Murray Efford, Sebastian Meyer

#
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
#
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
Your example function fails also in R 4.1.3 and 4.2.1:
Error in ctrfn(levels(x), contrasts = contrasts) :
   unused argument (contrasts = contrasts)

Even if we fix the missing argument:
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 :
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:
#
Thanks for your comprehensive explanation. Recent changes exposed errors in my code (and understanding!).
Murray