[R] contr.treatments query

(Ted Harding) Ted.Harding at manchester.ac.uk
Tue May 20 02:12:36 CEST 2008


Hi Folks,
I'm a bit puzzled by the following (example):

N<-factor(sample(c(1,2,3),1000,replace=TRUE))
unique(N)
# [1] 3 2 1
# Levels: 1 2 3

So far so good. Now:

contrasts(N)<-contr.treatment(3, base=1, contrasts=FALSE)
contrasts(N)
#   1 2
# 1 1 0
# 2 0 1
# 3 0 0

whereas:

contr.treatment(3, base=1, contrasts=FALSE)
#   1 2 3
# 1 1 0 0
# 2 0 1 0
# 3 0 0 1

contr.treatment(3, base=1, contrasts=TRUE)
#   2 3
# 1 0 0
# 2 1 0
# 3 0 1

I can follow the last two fine -- they are what is implied
by the code for contr.treatment().

Likewise:

contrasts(factor(Nlevs <-c(1,2,3)))
#   2 3
# 1 0 0
# 2 1 0
# 3 0 1

But why the different result when applied to N?

With thanks,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 20-May-08                                       Time: 01:12:30
------------------------------ XFMail ------------------------------



More information about the R-help mailing list