Re: [R] Need expert help with model.matrix

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Wed, 18 May 2011 08:52:05 -0400

On Wed, May 18, 2011 at 7:15 AM, Axel Urbiz <axel.urbiz_at_gmail.com> wrote:
> Dear experts:
>
> Is it possible to create a new function based
> on stats:::model.matrix.default so that an alternative factor coding is used
> when the function is called instead of the default factor coding?
>
> Basically, I'd like to reproduce the results in 'mat' below, without having
> to explicitly specify my desired factor coding (identity matrices) in the
> 'contrasts.arg'.
>
> dd <- data.frame(a = gl(3,4), b = gl(4,1,12))
> ca <- contrasts(dd$a, contrasts= FALSE)  # 3 x 3 identity matrix
> cb <- contrasts(dd$b, contrasts= FALSE)  # 4 x 4 identity matrix
> mat <- model.matrix(~ a + b, dd, contrasts.arg = list(a=ca, b=cb))
>
> My approach was to modify the code in model.matrix by explicitly setting the
> contrasts argument in the contr.identity and contrasts function to FALSE.
> This is shown at the bottom of the email in the function model.matrix2:
>
> contr.identity <- contr.treatment
> formals(contr.identity)$contrasts <- FALSE
>
> contrasts <- contrasts
> formals(contrasts)$contrasts <- FALSE
>
> However, I believe this function is using contrasts = TRUE, as it doesn't
> return the identity contrasts
> mat2 <- model.matrix2(~ a + b, dd)
>
> Any help here is much appreciated.
> Axel.
>

If your objective in all this is ultimately to get lm coefficients in the original coding then see ?dummy.coef

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Wed 18 May 2011 - 12:55:37 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

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 Wed 18 May 2011 - 13:10:07 GMT.

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

list of date sections of archive