[S] contr.treatment modification

Mark Bravington FSMG CEFAS (M.V.BRAVINGTON@cefas.co.uk)
Wed, 27 May 1998 14:54:04 +0100


Victor--

[NB I couldn't reply directly because your email address isn't in the message, and my email system strips the necessary information from the header.]

The function "contr.relative" below does what you require, i.e. a version of contr.treatment but without always using the first factor level as the reference. Examples:

> f1_ factor( c( 'dog', 'cat', 'dog', 'armadilllo'))
> contr.relative( f1, 'cat')
armadillo/cat dog/cat
armadillo 1 0
cat 0 0
dog 0 1
>

The second parameter to "contr.relative" is the factor level to be used as a reference. The 3rd & 4th parameters determine the labelling of the contrasts. The default is illustrated in the example. You can control the separator via the 3rd parameter, and control whether the reference level is named explicitly using the 4th parameter. E.G.

> contr.relative( f1, 'cat', ' vs. ')
armadillo vs. cat dog vs. cat
armadillo 1 0
cat 0 0
dog 0 1

> contr.relative( f1, 'cat',,F)
armadillo dog
armadillo 1 0
cat 0 0
dog 0 1

Here's the definition-- hope this helps

Mark Bravington
m.v.bravington@cefas.co.uk

contr.relative <-
function(f, level, sep = "/", second = T)
{
if(class(f) != "factor")
stop(substitute(f) %&% " is not a factor!")
if(length(level) != 1 || all(levels(f) != level))
stop(substitute(level) %&%
" is not a level in factor " %&%
substitute(f))
mm <- diag(1, length(levels(f)))
colnames <- levels(f) %&% ifelse(second, sep %&% level,
"")
dimnames(mm) <- list(levels(f), colnames)
mm[, levels(f) != level]
}
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news