s-news
[Top] [All Lists]

[S] contr.treatment modification

To: "'S-NEWS'" <s-news@wubios.wustl.edu>
Subject: [S] contr.treatment modification
From: Mark Bravington FSMG CEFAS <M.V.BRAVINGTON@cefas.co.uk>
Date: Wed, 27 May 1998 14:54:04 +0100
Alternate-recipient: Allowed
Sender: owner-s-news@wubios.wustl.edu
X400-content-type: P2-1984 (2)
X400-mts-identifier: [/PRMD=MAFF400/ADMD=ATTmail/C=GB/;LOWNTG-980527135404Z-7162]
X400-originator: M.V.BRAVINGTON@cefas.co.uk
X400-received: by mta parker in /PRMD=MAFF400/ADMD=ATTmail/C=GB/; Relayed; Wed, 27 May 1998 13:38:12 +0100
X400-received: by mta scott in /PRMD=MAFF400/ADMD=ATTmail/C=GB/; Relayed; Wed, 27 May 1998 13:35:47 +0100
X400-received: by mta lowntg in /PRMD=MAFF400/ADMD=ATTmail/C=GB/; Relayed; Wed, 27 May 1998 14:54:04 +0100
X400-recipients: non-disclosure:;
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

<Prev in Thread] Current Thread [Next in Thread>
  • [S] contr.treatment modification, Mark Bravington FSMG CEFAS <=