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

From: <arnima_at_hafro.is>
Date: Tue, 19 Jan 2010 02:20:13 +0100 (CET)

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.



R-devel_at_r-project.org mailing list

https://stat.ethz.ch/mailman/listinfo/r-devel Received on Tue 19 Jan 2010 - 01:24:07 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 21 Jan 2010 - 19:50:18 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive