caracal
caracal

Reputation: 2770

dummy.coef() for multivariate models

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():

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]]) }

Upvotes: 2

Views: 495

Answers (2)

Martin M&#228;chler
Martin M&#228;chler

Reputation: 4765

We have an official bug report about dummy.coef with a "kind of" patch at which I am looking now -- it seems good, even though changing a bit more things at once than I was expecting.

When I was googling for other problems with dummy.coef I got this old SO thread. Let me answer (as a member of the R core team) that we are interested in the extension to multivariate models if the code (and documentation) changes are not large. Please reply if you are interested in helping.

Upvotes: 1

Josh O&#39;Brien
Josh O&#39;Brien

Reputation: 162401

This only addresses your second question (the one about the if() x else y statement at lines 20-22 in the code).

To start with, try cut-and-pasting these two blocks into an R session:

test <- TRUE

# Block 1 -- Doesn't work
if(test) 
    cat("test is TRUE\n")
    else
    cat("test is FALSE\n")

# Block 2 -- Works
{
if(test) 
    cat("test is TRUE\n")
    else
    cat("test is FALSE\n")
}

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 the if() x else y block 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

Upvotes: 1

Related Questions