Hi,

I need to build a function to generate one column for each level of a factor
in the model matrix created on an arbitrary formula (instead of using the
available contrasts options such as contr.treatment, contr.SAS, etc).

My approach to this was first to use the built-in function for
contr.treatment but changing the default value of the contrasts argument to
FALSE (I named this function "contr.identity" and it shown at the bottom of
the email for reference).

So this function works fine,

> contr.identity(4)

  1 2 3 4
1 1 0 0 0
2 0 1 0 0
3 0 0 1 0
4 0 0 0 1

> contr.treatment(4)
  2 3 4
1 0 0 0
2 1 0 0
3 0 1 0
4 0 0 1

However, when I try to create a model matrix using contr.identity specified
in options(), it actually uses the contr.treatment option. Why is that? Any
hint on how can I do this?

options(contrasts = c('contr.identity', 'contr.poly'))
options("contrasts")
dd <- data.frame(a = gl(3,4), b = gl(4,1,12))
model.matrix(~ a + b, dd) #creates 2 columns for a and 3 for b instead of 3
and 4, respectively


contr.identity <-
    function(n, base = 1, contrasts = FALSE, sparse = FALSE)
{
    if(is.numeric(n) && length(n) == 1L) {
  if(n > 1L) levels <- as.character(seq_len(n))
  else stop("not enough degrees of freedom to define contrasts")
    } else {
 levels <- as.character(n)
 n <- length(n)
    }

    contr <- .Diag(levels, sparse=sparse)
    if(contrasts) {
 if(n < 2L)
     stop(gettextf("contrasts not defined for %d degrees of freedom",
                          n - 1L), domain = NA)
 if (base < 1L | base > n)
     stop("baseline group number out of range")
 contr <- contr[, -base, drop = FALSE]
    }
    contr
}

Thanks for any help,
Lars.

	[[alternative HTML version deleted]]

