[Rd] Model frame when LHS is cbind (PR#14189)

arnima at hafro.is arnima at hafro.is
Tue Jan 19 02:20:13 CET 2010


The model frame shows the response and predictors in a data frame with
nicely labelled columns:

  fm <- lm(wt~qsec+log(hp)+sqrt(disp), data=mtcars)
  model.frame(fm)  # ok

When the left hand side consists of more than one response, those response
variables still look good, inside a matrix:

  fm <- lm(cbind(qsec,hp,disp)~wt, data=mtcars)
  model.frame(fm)[[1]]  # ok

A problem arises when some of the response variables are transformed:

  fm <- lm(cbind(qsec,log(hp),sqrt(disp))~wt, data=mtcars)
  model.frame(fm)[[1]]  # ugh, missing column names

The model frame is useful for many things, even more so when all column
names are legible. Therefore I propose adding two new lines to
model.frame.default() between lines 371 and 372 in
R-patched_2010-01-12/src/library/stats/R/models.R:

    varnames <- sapply(vars, deparse, width.cutoff = 500)[-1L]
    variables <- eval(predvars, data, env)

NEW if (is.matrix(variables[[1L]]))
NEW     colnames(variables[[1L]]) <- as.character(formula[[2L]])[-1L]

    if (is.null(rownames) && (resp <- attr(formula, "response")) >
        0L) {

With this fix, the above example returns legible column names:

  fm <- lm(cbind(qsec,log(hp),sqrt(disp))~wt, data=mtcars)
  model.frame(fm)[[1]]  # nice column names

I hope the R development team can either commit this fix or improve it.

Thanks,

Arni



More information about the R-devel mailing list