I was wondering about some things in dummy.coef() which converts the estimated parameters (contrasts) in ANOVA models to the original ones. It only works for univariate models, but the changes required to also make it work for multivariate models seem minor. In dummy.coef.lm():
- line 52
coef <- object$coefficientswould have to becoef <- as.matrix(object$coefficients)to accomodate univariate and multivariate models (coef(object)is a vector in the 1st case, and a matrix in the 2nd) - line 60
ans <- drop(mm[rn == tl[j], keep, drop = FALSE] %*% coef[keep])would have to beans <- drop(mm[rn == tl[j], keep, drop = FALSE] %*% coef[keep, ])to keep all columns incoef - line 61
names(ans) <- rnn[rn == tl[j]]could benames(ans) <- rep(rnn[rn == tl[j]], ncol(coef))to give names to the rows of all columns
The printing method would need some changes, but that seems to be it. Does anybody know why dummy.coef() was not designed to handle multivariate models?
Another thing I stumbled upon: Lines 20-22 are
for (i in vars) args[[i]] <- if (nxl[[i]] == 1)
rep.int(1, nl)
else factor(rep.int(xl[[i]][1L], nl), levels = xl[[i]])
Is that safe? I.e., if the if() clause is TRUE, wouldn’t there be an unexpected else? I would have expected something like
for (i in vars) args[[i]] <- if (nxl[[i]] == 1) {
rep.int(1, nl)
} else { factor(rep.int(xl[[i]][1L], nl), levels = xl[[i]]) }
This only addresses your second question (the one about the
if() x else ystatement at lines 20-22 in the code).To start with, try cut-and-pasting these two blocks into an R session:
What’s going on? The
{}make all the difference here. Block 1 ‘read-parse-evaluates’ the code line-by-line, causing just the problem you’d expect. Block 2, on the other hand, is read and completely parsed before any evaluation takes place. That’s part of what{}directs R to do. When it receives the block of code as a whole, the parser clearly parses theif() x else yblock as one expression.The code you quoted was taken from inside the body of a function (i.e. inside of a
{}pair). In that context, it is handled correctly (i.e. like Block 2).HTH