Need expert help with model.matrix
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