Ben Bolker
2019-Feb-21 00:14 UTC
[Rd] model.matrix.default() silently ignores bad contrasts.arg
An lme4 user pointed out <https://github.com/lme4/lme4/issues/491> that passing contrasts as a string or symbol to [g]lmer (which would work if we were using `contrasts<-` to set contrasts on a factor variable) is *silently ignored*. This goes back to model.matrix(), and seems bad (this is a very easy mistake to make, because of the multitude of ways to specify contrasts for factors in R - e.g. options(contrasts=...); setting contrasts on the specific factors; passing contrasts as a list to the model function ... ) The relevant code is here: https://github.com/wch/r-source/blob/trunk/src/library/stats/R/models.R#L578-L603 The following code shows the problem: a plain-vanilla model.matrix() call with no contrasts argument, followed by two wrong contrasts arguments, followed by a correct contrasts argument. data(cbpp, package="lme4") mf1 <- model.matrix(~period, data=cbpp) mf2 <- model.matrix(~period, contrasts.arg="contr.sum", data=cbpp) all.equal(mf1,mf2) ## TRUE mf3 <- model.matrix(~period, contrasts.arg=contr.sum, data=cbpp) all.equal(mf1,mf3) ## TRUE mf4 <- model.matrix(~period, contrasts.arg=list(period=contr.sum), data=cbpp) isTRUE(all.equal(mf1,mf4)) ## FALSE I've attached a potential patch for this, which is IMO the mildest possible case (if contrasts.arg is non-NULL and not a list, it produces a warning). I haven't been able to test it because of some mysterious issues I'm having with re-making R properly ... Thoughts? Should I submit this as a bug report/patch? cheers Ben Bolker -------------- next part -------------- A non-text attachment was scrubbed... Name: models.R.diff Type: text/x-patch Size: 2138 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-devel/attachments/20190220/7469d872/attachment.bin>
Possibly Parallel Threads
- model.matrix.default() silently ignores bad contrasts.arg
- model.matrix.default() silently ignores bad contrasts.arg
- model.matrix.default() silently ignores bad contrasts.arg
- model.matrix.default() silently ignores bad contrasts.arg
- model.matrix.default() silently ignores bad contrasts.arg