[Rd] suggested addition to model.matrix

Fox, John jfox at mcmaster.ca
Tue Oct 4 06:46:02 CEST 2016


Dear Spencer,

I don't think that the problem of "converting a data frame into a model matrix" is well-defined, because there isn't a unique mapping from one to the other. 

In your example, you build  the model matrix for the additive formula ~ a + b from the data frame matrix containing a and b, using "treatment" contrasts, but there are other possible formulas (e.g., ~ a*b) and contrasts [e.g., model.matrix(~ a + b, dd, contrasts=list(a=contr.sum, b=contr.helmert)].

So I think that the current approach is sensible -- to require both a data frame and a formula.

Best,
 John

> -----Original Message-----
> From: R-devel [mailto:r-devel-bounces at r-project.org] On Behalf Of Spencer
> Graves
> Sent: October 3, 2016 7:59 PM
> To: r-devel at r-project.org
> Subject: [Rd] suggested addition to model.matrix
> 
> Hello, All:
> 
> 
>        What's the simplest way to convert a data.frame into a model.matrix?
> 
> 
>        One way is given by the following example, modified from the examples in
> help(model.matrix):
> 
> 
> dd <- data.frame(a = gl(3,4), b = gl(4,1,12))
> ab <- model.matrix(~ a + b, dd)
> ab0 <- model.matrix(~., dd)
> all.equal(ab, ab0)
> 
> 
>        What do you think about replacing "model.matrix(~ a + b, dd)" in
> the current help(model.matrix) with this 3-line expansion?
> 
> 
>        I suggest this, because I spent a few hours today trying to
> convert a data.frame into a model.matrix before finding this.
> 
> 
>        Also, what do you think about adding something like the following
> to the stats package:
> 
> 
> model.matrix.data.frame <- function(object, ...){
>      model.matrix(~., object, ...)
> }
> 
> 
>        And then extend the above example as follows:
> 
> ab. <- model.matrix(dd)
> all.equal(ab, ab.)
> 
> 
>        Thanks,
>        Spencer Graves
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list