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

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Thu, 21 Jan 2010 19:01:11 +0000 (GMT)

A few points.

0) This seems a Wishlist item, but it does not say so (see the section on BUGS in the FAQ).

  1. A formula does not need to have an lhs, and it is an assumption that the response is the first element of 'variables' (an assumption not made a couple of lines later when 'resp' is used).
  2. I don't think this is the best way to get names. If I do

fm <- lm(cbind(a=qsec,b=log(hp),sqrt(disp))~wt, data=mtcars)

I want a and b as names, but that is not what your code gives. And if I do

> X <- with(mtcars, cbind(a = qsec, b = log(hp), c=sqrt(disp)))
> fm <- lm(X ~ wt, data=mtcars)
> model.frame(fm)[[1]]

        [,1] [,2] [,3]

You've lost the names that the current code gives.

The logic is that if you use a lhs which is a matrix with column names, then those names are used. If (as you did), you use one with empty column names, that is what you get in the model frame. This seems much more in the spirit of R than second-guessing that the author actually meant to give column names and create them, let alone renaming the columns to be different than the names supplied.

3) It looks to me as if you wanted

cbind(qsec, log(hp), sqrt(disp), deparse.level=2)

but that does not give names (despite the description). And that is I think a bug that can easily be changed. That way we can fulfil yoour wish without breaking other people's code.

On Tue, 19 Jan 2010, arnima_at_hafro.is wrote:

> 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
>
> ______________________________________________
> R-devel_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
Brian D. Ripley,                  ripley_at_stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Thu 21 Jan 2010 - 19:03:55 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 Fri 22 Jan 2010 - 01:30:15 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